Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5047
Problem descriptionthink about a plane:
● One straight line can divide a plane into two regions.
● Two lines can divide a plane into at most four regions.
● Three lines can divide a plane into at most seven regions.
● And so on...
Now we have some figure constructed with two parallel rays in the same direction, joined by two straight segments. it looks like a character "M ". you are given n such "M" S. what is the maximum number of regions that these "M" s can divide a plane?
Inputthe first line of the input is t (1 ≤ T ≤ 100000), which stands for the number of test cases you need to solve.
Each case contains one single non-negative integer, indicating number of "M" S. (0 ≤ n ≤0 ≤ 1012)
Outputfor each test case, print a line "Case # T:" (without quotes, t means the index of the test case) at the beginning. then an integer that is the maximum number of regions n the "M" figures can divide.
Sample Input
212
Sample output
Case #1: 2Case #2: 19
Source2014 ACM/ICPC Asia Regional Shanghai Online
PS:
Formula: 8 * n ^ 2-7 * n + 1. Just set the template! But many large number template this question will t, but this template: http://blog.csdn.net/u012860063/article/details/39612037 thanks to the teammate God template 2333333!
The Code is as follows:
# Include <cstdio> # include <cstring> # include <malloc. h>/* Addition of large numbers */void add (char * a, char * B, char * c) {int I, J, K, Max, Min, N, temp; char * s, * Pmax, * pmin; max = strlen (a); min = strlen (B); If (max <min) {temp = max; max = min; min = temp; Pmax = B; pmin = A;} else {Pmax = A; pmin = B;} s = (char *) malloc (sizeof (char) * (MAX + 1); s [0] = '0'; for (I = min-1, j = max-1, K = max; I> = 0; I --, J --, k --) s [k] = pmin [I]-'0' + Pmax [J]; for (; j> = 0; j --, K --) S [k] = Pmax [J]; for (I = max; I> = 0; I --) if (s [I]> '9 ') {s [I]-= 10; s [I-1] ++;} If (s [0] = '0') {for (I = 0; I <= max; I ++) C [I-1] = s [I]; C [I-1] = '\ 0';} else {for (I = 0; I <= max; I ++) C [I] = s [I]; C [I] = '\ 0';} Free (s );} /* subtraction of large numbers */void subtract (char * a, char * B, char * c) {int I, j, CA, CB; CA = strlen (); CB = strlen (B); If (Ca> CB | (CA = CB & strcmp (a, B)> = 0) {for (I = ca-1, j = CB-1; j> = 0; I --, j --) A [I]-= (B [J]-'0'); for (I = Ca-1; I> = 0; I --) if (a [I] <'0') {A [I] + = 10; A [I-1] --;} I = 0; while (A [I] = '0') I ++; if (a [I] = '\ 0 ') {c [0] = '0'; C [1] = '\ 0';} else {for (j = 0; A [I]! = '\ 0'; I ++, J ++) C [J] = A [I]; C [J] =' \ 0 ';}} else {for (I = ca-1, j = CB-1; I> = 0; I --, j --) B [J]-= (a [I]-'0'); For (j = CB-1; j> = 0; j --) if (B [J] <'0') {B [J] + = 10; B [J-1] --;} J = 0; while (B [J] = '0') J ++; I = 1; C [0] = '-'; For (; B [J]! = '\ 0'; I ++, J ++) C [I] = B [J]; C [I] =' \ 0 ';}} /* multiplication of large numbers */void multiply (char * a, char * B, char * c) {int I, j, CA, CB, * s; CA = strlen (a); Cb = strlen (B); s = (int *) malloc (sizeof (INT) * (Ca + CB); for (I = 0; I <Ca + CB; I ++) s [I] = 0; for (I = 0; I <CA; I ++) for (j = 0; j <CB; j ++) s [I + J + 1] + = (a [I]-'0') * (B [J]-'0 '); for (I = Ca + CB-1; I> = 0; I --) if (s [I]> = 10) {s [I-1] + = s [I]/10; s [I] % = 10;} I = 0; while (s [I] = 0) I ++; For (j = 0; I <Ca + CB; I ++, J ++) C [J] = s [I] + '0'; C [J] = '\ 0'; free (s) ;}/ * Division of large numbers, returns the remainder */INT dividor (char * a, int B, char * c) {int I, j, temp = 0, N; char * s; N = strlen (a); s = (char *) malloc (sizeof (char) * (n + 1); for (I = 0; A [I]! = 0; I ++) {temp = temp * 10 + A [I]-'0'; s [I] = temp/B + '0 '; temp % = B;} s [I] = '\ 0'; for (I = 0; s [I] = '0' & S [I]! = '\ 0'; I ++); If (s [I] =' \ 0') {C [0] = '0 '; c [1] = '\ 0';} else {for (j = 0; s [I]! = '\ 0'; I ++, J ++) C [J] = s [I]; C [J] =' \ 0';} Free (s ); return temp;} const int maxn = 1017; char s [maxn], T1 [maxn], T2 [maxn], t3 [maxn]; char a [17], B [17], C [17]; char ans [maxn]; int main () {A [0] = '8'; A [1] = '\ 0 '; B [0] = '7'; B [1] = '\ 0'; C [0] = '1'; C [1] =' \ 0 '; int t; int CAS = 0; scanf ("% d", & T); getchar (); While (t --) {memset (ANS, '\ 0 ', sizeof (ANS); gets (s); multiply (S, S, T1); // n ^ 2 multiply (T1, A, T2 ); // 8 * n ^ 2 multiply (B, S, T3); // 7 * n subtract (T2, T3, ANS ); // 8 * n ^ 2-7 * n add (ANS, C, ANS ); // 8 * n ^ 2-7 * n + 1 printf ("case # % d: % s \ n", ++ cas, ANS);} return 0 ;} // 8 * n ^ 2-7 * n + 1
HDU 5047 sawtooth (mathematical formula)