2014 ACM/ICPC Asia Regional Shanghai Online 1006 Sawtooth, icpcsawtooth

Source: Internet
Author: User

2014 ACM/ICPC Asia Regional Shanghai Online 1006 Sawtooth, icpcsawtooth

<Pre name = "code" class = "cpp"> # include <iostream> # include <string> # include <iomanip> # include <algorithm> # include <cstring> # include <cstdio> using namespace std; # define MAXN 9999 # define MAXSIZE 10 # define DLEN 4 class BigNum {private: int a [500]; // The number of digits that can be controlled for a large number int len; // the length of a large number is public: bigNum () {len = 1; memset (a, 0, sizeof (a);} // constructor BigNum (const int ); // convert an int type variable to a big number BigNum (const char *); // convert a string type Convert a variable to a big number BigNum (const BigNum &); // copy the constructor BigNum & operator = (const BigNum &); // overload the value assignment operator, assign values between large numbers: friend void cinn (BigNum &); // overload input operator friend void coutt (BigNum &); // overload output operator BigNum operator + (const BigNum &) const; // overload addition operator, BigNum operator-(const BigNum &) const between two large numbers; // overload subtraction operator, subtraction between two large numbers BigNum operator * (const BigNum &) const; // overload multiplication operator, multiplication between two large numbers BigNum operator/(const int &) const; // reload Division Algorithm operator. A large number is used to divide an integer by bool operator> (const BigNum & T) const; // compare the size of a large number with that of another big number void print (); // output large number}; BigNum: BigNum (const int B) // convert an int type variable to a large number {int 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 (const char * s) // convert a variable of the string type to a large number {int t, k, index, l, I; memset (a, 0, sizeof (a); l = strle N (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 to assign values between large numbers {int I; len = n. len; memset (a, 0, sizeof ()); For (I = 0; I <len; I ++) a [I] = n. a [I]; return * this;} void cinn (BigNum & B) // overload input operator {char ch [MAXSIZE * 4]; int I =-1; scanf ("% s", 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 ++;} void coutt (BigNum & B) // reload the output operator {int I; printf ("% d", B. a [B. len-1]); for (I = B. Len-2; I> = 0; I --) {printf ("% 04d", B. a [I]) ;}} BigNum: operator + (const BigNum & T) const // sum operation between two large numbers {BigNum t (* this); int I, big; // number of digits 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.len = 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 // multiplication between 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 = te Mp-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;} 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;} int main () {int t, I; BigNum n; BigNum tmp = 1, t1 = 8, t2 = 7; cin> t; for (I = 0; I <t; I ++) {cinn (n); printf ("Case # % d :", I + 1); if (tmp> n) cout <1 <endl; else {n = t1 * n * n-t2 * n + 1; coutt (n ); printf ("\ n") ;}} return 0 ;}


 

What is ACM/ICPC Asia Regional?

International. The finals are world-class. Asia belongs to the intercontinental and international level!
 
2012 how many teams are ACM/ICPC Asia Regional Chengdu Online?

130 teams
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.