Sawtooth
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 979 accepted submission (s): 375
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
Question: n m-type instances (two instances must be parallel). How many parts can be divided into the entire space. Idea: first, consider the simple situation: that is, the maximum number of parts of space that can be divided into n straight lines. L (n) is used to represent L (0) = 1, L (1) = 2, L (2) = 4, L (3) = 7 ........ it is easy to think that when I calculate L (n), this line has at most n-1 intersections with n-1 lines, and N regions are added (N existing regions are divided) so the recursive formula is: L (n) = L (n-1) + n. We can get L (n) = (n + 1) * n/2 + 1. Let's look at M: if we think of a M-type as four straight lines (two sides are parallel), we can get the answer directly through the formula just now! However, it is a ray! If you try to extend the line segment infinitely, we will find that you can regard this M-type as four straight lines to divide the space, but some space is wasted (these wasted space is at that three heads ), in addition, we can calculate that the loss space is 9, but it is easy to think that the maximum space you want to divide is to expose the three heads of each M-type to the outside! (Greedy) in this case, the three heads of each M-type are exposed, so we can obtain the formula m (n) = L (4 * n) -9 * n = 8n ^ 2-7n + 1. If the two sides are not parallel, the result is the same! Code details optimization is meaningless...
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; # define maxn 9999 # define maxsize 1010 # define dlen 4 class bignum {PRIVATE: int A [500]; // The number of digits that can control a large number int Len; public: bignum () {Len = 1; // constructor memset (A, 0, sizeof (a);} bignum (const long); // convert an int type variable to a big number bignum (const char *); // convert a variable of the string type to the bignum (const bignum &); // copy the constructor bignum & operator = (const bignum &); // overload the value assignment operator, large number Value assignment between them: Friend istream & operator> (istream &, bignum &); // overload the input operator friend ostream & operator <(ostream &, bignum &); // overload output operator bignum operator + (const bignum &) const; // overload addition operator, bignum operator-(const bignum &) const calculation between two large numbers; // The overload subtraction operator. subtraction between two large numbers: bignum operator * (const bignum &) const; // The overload multiplication operator, the multiplication operation between two large numbers is bignum operator/(const Int &) const; // the heavy-duty division operator. A large number is used to divide an integer by bignum operator ^ (const Int &) const; // Nth power operation int operator % (const Int &) const; // Nth Power Operation bool operator> (const bignum & T) const; // compare the size of a large number with that of another large number bool operator> (const Int & T) const; // compare the size of a large number with that of an int Type Variable Void print (); // output large number}; bignum: bignum (const long B) // convert an int type variable to a large number {long c, d = B; Len = 0; memset (A, 0, sizeof (a); While (D> maxn) {c = D-(D/(maxn + 1) * (maxn + 1 ); D = D/(maxn + 1); A [Len ++] = C;} A [Len ++] = D;} bignum: bignum (con St char * s) // convert a variable of the string type to a large number {int T, K, index, l, I; memset (A, 0, sizeof ()); L = strlen (s); Len = L/dlen; If (L % dlen) Len ++; Index = 0; for (I = L-1; I> = 0; i-= dlen) {T = 0; k = I-dlen + 1; if (k <0) k = 0; For (Int J = K; j <= I; j ++) t = T * 10 + s [J]-'0'; A [index ++] = T ;}} bignum: bignum (const bignum & T): Len (T. len) // copy the constructor {int I; memset (A, 0, sizeof (a); for (I = 0; I <Len; I ++) A [I] = T. A [I];} bignum & bignum: Operator = (const bignum & N) // Overload the value assignment operator. The value assignment operation is {int I; Len = n. len; memset (A, 0, sizeof (a); for (I = 0; I <Len; I ++) A [I] = n. A [I]; return * This;} istream & operator> (istream & in, bignum & B) {char ch [maxsize * 4]; int I =-1; in> CH; int L = strlen (CH); int COUNT = 0, sum = 0; for (I = L-1; I> = 0;) {sum = 0; int T = 1; for (Int J = 0; j <4 & I> = 0; j ++, I --, T * = 10) {sum + = (CH [I]-'0') * t;} B. A [count] = sum; count ++;} B. len = count ++; Return in;} ostream & operator <( Ostream & out, bignum & B) // reload the output operator {int I; cout <B. A [B. len-1]; for (I = B. len-2; I> = 0; I --) {printf ("% 04d", B. A [I]);} return out;} bignum: Operator + (const bignum & T) const // The addition operation between two large numbers {bignum T (* This ); int I, big; big = T. len> Len? T. len: Len; for (I = 0; I <big; I ++) {T. A [I] + = T. A [I]; If (T. A [I]> maxn) {T. A [I + 1] ++; T. A [I]-= maxn + 1 ;}} if (T. A [Big]! = 0) T. len = big + 1; else t. len = big; return t;} bignum: Operator-(const bignum & T) const // subtraction between two large numbers {int I, j, big; bool flag; bignum T1, T2; If (* This> T) {T1 = * This; t2 = T; flag = 0;} else {T1 = T; t2 = * this; flag = 1 ;}big = t1.len; for (I = 0; I <big; I ++) {If (t1.a [I] <t2.a [I]) {J = I + 1; while (t1.a [J] = 0) J ++; t1.a [j --] --; while (j> I) t1.a [j --] + = maxn; t1.a [I] + = maxn + 1-t2.a [I];} else t1.a [I]-= t2.a [I];} t1.le N = big; while (t1.a [len-1] = 0 & t1.len> 1) {t1.len --; big --;} If (FLAG) t1.a [Big-1] = 0-t1.a [Big-1]; return T1;} bignum: Operator * (const bignum & T) const // multiply two large numbers {bignum ret; int I, j, up; int temp, temp1; for (I = 0; I <Len; I ++) {up = 0; For (j = 0; j <t. len; j ++) {temp = A [I] * t. A [J] + ret. A [I + J] + up; If (temp> maxn) {temp1 = temp-temp/(maxn + 1) * (maxn + 1 ); up = temp/(maxn + 1); ret. A [I + J] = temp1;} else {up = 0; RET. A [I + J] = temp;} If (up! = 0) ret. A [I + J] = up;} ret. len = I + J; while (Ret. A [ret. len-1] = 0 & ret. len> 1) ret. len --; return ret;} bignum: Operator/(const Int & B) const // perform the division operation on an integer {bignum ret; int I, down = 0; for (I = len-1; I> = 0; I --) {ret. A [I] = (a [I] + down * (maxn + 1)/B; down = A [I] + down * (maxn + 1)-ret. A [I] * B;} ret. len = Len; while (Ret. A [ret. len-1] = 0 & ret. len> 1) ret. len --; return ret;} int bignum: Operator % (const Int & B) const // perform modulo {int I, D = 0 for an int type variable; for (I = len-1; I> = 0; I --) d = (D * (maxn + 1) % B + A [I]) % B; return D;} bool bignum: Operator> (const bignum & T) const // compare the size of a large number with that of another one {int ln; If (LEN> T. len) return true; else if (LEN = T. len) {Ln = len-1; while (A [Ln] = T. A [Ln] & ln> = 0) ln --; If (LN> = 0 & A [Ln]> T. A [Ln]) return true; else return false;} bool bignum: Operator> (const Int & T) const // compare the size of a large number and an int type variable {bignum B (t); return * This> B;} void bignum: Print () // output large number {int I; printf ("% d", a [len-1]); for (I = len-2; I> = 0; I --) printf ("% 04d", a [I]); printf ("\ n");} int main () {int ncase, T = 1; CIN> ncase; while (ncase --) {long T; scanf ("% i64d", & T); If (T <= 1e9) {printf ("case # % d: % i64d \ n ", t ++, 8 * T * T-7 * t + 1);} else {bignum TMP = bignum (t); printf (" case # % d: ", t ++); TMP = (TMP * 8-tmp * 7 + 1); TMP. print () ;}} return 0 ;}
HDU 5047 sawtooth (interesting thinking questions + proof)