How Many Pieces of Land?
Input: Standard Input
Output: Standard Output
Time Limit: 3 seconds
You are given an elliptical shaped land and you are asked to choose n arbitrary points on its boundary. Then you connect all these points with one another with straight lines (that’s n*(n-1)/2 connections for n points). What is the maximum number of pieces of land you will get by choosing the points on the boundary carefully?
Fig: When the value of n is 6.
Input
The first line of the input file contains one integer S (0 < S < 3500), which indicates how many sets of input are there. The next Slines contain S sets of input. Each input contains one integer N (0<=N<2^31).
Output
For each set of input you should output in a single line the maximum number pieces of land possible to get for the value of N.
Sample Input:
41
2
3
4
Sample Output:
12
4
8
Shahriar Manzoor
公式!別人的。 1 + (n, 2) + (n, 4)
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#define LEN 100
void Mult (int *a, int *b) {
int c[LEN];
memset (c, 0, sizeof (c));
for (int i=0; i<LEN; ++i) {
for (int k=0; i+k<LEN; ++k) {
c[i + k] += a[i] * b[k];
}
}
int car = 0;
for (int i=0; i<LEN; ++i) {
int t = car + c[i];
c[i] = t % 10;
car = t / 10;
}
assert (car == 0);
memcpy (a, c, sizeof (c));
}
void Add (int *a, int *b) {
int car = 0;
for (int i=0; i<LEN; ++i) {
int t = a[i] + b[i] + car;
a[i] = t % 10;
car = t / 10;
}
assert (car == 0);
}
void Put (int *a) {
int i = LEN;
while (--i > 0 && !a[i]) {}
while (i >= 0) {
printf ("%d", a[i--]);
}
printf ("\n");
}
void Dev (int *a, int d) {
assert (0 < d && d < 10);
int r = 0;
for (int i=LEN-1; i>=0; --i) {
int k = 10;
while (--k > 0 && k * d > (r * 10 + a[i])) {}
r = r * 10 + a[i] - k * d;
assert (r < 10);
a[i] = k;
}
#ifdef _DEBUG
if (r) {
printf ("Warnning, not devisable!\n");
}
#endif
}
void Sub (int *a, int *b) {
int bor = 0; // borrow
for (int i=0; i<LEN; ++i) {
int t = a[i] - b[i] - bor;
a[i] = (t + 10) % 10;
bor = 1 - (t + 10) / 10;
}
assert (bor == 0);
}
void Get (int *a) {
char s[100];
if (scanf ("%s", s) == EOF) {
exit (0);
}
memset (a, 0, sizeof (*a) * LEN);
char *p = s + strlen (s);
do {
*a++ = *--p - '0';
} while (p > s);
}
bool IsOne (int *a) {
int one[LEN];
memset (one, 0, sizeof (one));
*one = 1;
return memcmp (one, a, sizeof (one)) == 0;
}
bool IsTwo (int *a) {
int two[LEN];
memset (two, 0, sizeof (two));
*two = 2;
return memcmp (two, a, sizeof (two)) == 0;
}
bool IsZero (int *a) {
int zero[LEN];
memset (zero, 0, sizeof (zero));
return memcmp (zero, a, sizeof (zero)) == 0;
}
int main () {
int cs;
scanf ("%d", &cs);
while (cs--) {
int n[LEN];
Get (n);
if (IsOne (n) || IsZero (n)) {
printf ("1\n");
continue;
} else if (IsTwo (n)) {
printf ("2\n");
continue;
}
int a[LEN];
memset (a, 0, sizeof (a));
*a = 1;
Mult (a, n);
int b[LEN];
memset (b, 0, sizeof (b));
*b = 1;
Mult (b, n);
int c[LEN];
memset (c, 0, sizeof (c));
*c = 1;
Sub (n, c);
Mult (a, n);
Mult (b, n);
Dev (b, 2);
Sub (n, c);
Mult (a, n);
Sub (n, c);
Mult (a, n);
Dev (a, 4);
Dev (a, 6);
Add (c, a);
Add (c, b);
Put (c);
}
return 0;
}