HDU 5047 Sawtooth (Java large number Class)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5047


Surface:

SawtoothTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1636 Accepted Submission (s): 637


Problem Descriptionthink about a plane:

One straight line can divide a plane into the regions.
The lines can divide a plane into at the most four regions.
Three lines can divide a plane into at the most seven regions.
And so on ...

Now we had some figure constructed with a parallel rays in the same direction, joined by both straight segments. It looks like a character "M". You are given N such "M" s. What's the maximum number of regions that these "M" s can divide a plane?


Inputthe first line of the input was 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≤1012)
Outputfor, print a line ' case #t: ' (without quotes, t means the index of the test case) at the beginning. Then a 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 Problem Solving:

Each m four edge, each edge at most with the original edge intersection, forming a n-1 intersection, is not related to Euler theorem?? Then the team-mates magically moved out F (n) =f (n-1) +4*4* (n-1) +1; then came out 8*n^2-7*n+1 ...

Well, the point is that he used the formula directly with the C + + large number template T. So, I used the Java large number of classes handed a pitch, 1900ms, have to say time card really tight. On the Internet a check, the original Java read output can be optimized, see this blog. I made a large number of my own 800ms, but it seems that can also be optimized on a large number of times.


Code (JAVA):

Import Java.io.*;import java.util.*;import java.math.*;; public class Main {public static void Main (String args[]) {Scanner cin = new Scanner (system.in);        int T=cin.nextint ();        for (int i=1;i<=t;i=i+1)        {        BigInteger ans,tmp;        BigInteger N=cin.nextbiginteger ();        Ans=n.multiply (n);        Ans=ans.multiply (biginteger.valueof (8));        Tmp=n.multiply (biginteger.valueof (7));        Ans=ans.subtract (TMP);        Ans=ans.add (biginteger.one);        System.out.println ("Case #" +i+ ":" +ans);}}}        

Code (Java optimized Edition):

Import Java.io.*;import java.util.*;import java.math.*;; public class Main {public static void main (String args[]) {Scanner sc = new Scanner (new Bufferedinputstream (system.in)); PrintWriter cout=new PrintWriter (system.out);        int T=sc.nextint ();        for (int i=1;i<=t;i=i+1)        {        BigInteger ans,tmp;        BigInteger N=sc.nextbiginteger ();        Ans=n.multiply (n);        Ans=ans.multiply (biginteger.valueof (8));        Tmp=n.multiply (biginteger.valueof (7));        Ans=ans.subtract (TMP);        Ans=ans.add (biginteger.one);        Cout.println ("Case #" +i+ ":" +ans);        }        Cout.flush ();}}


Code (c + + large number template):

#include <iostream> #include <string> #include <iomanip> #include <cstring> #include < Algorithm> using namespace std;    #define MAXN 9999#define MAXSIZE 10#define dlen 4class bignum{private:int a[500];       You can control the number of digits of the large number int len;   Large number of Lengths Public:bignum () {len = 1;memset (A,0,sizeof (a));}       constructor Bignum (const int);     Converts a variable of type int to a large number bignum (const char*);  Converts a variable of a string type to a large number bignum (const bignum &);   Copy constructor Bignum &operator= (const bignum &);   Overloaded assignment operators, assigning values between large numbers friend istream& operator>> (istream&, bignum&);   Overloaded input operator friend ostream& operator<< (ostream&, bignum&);   Overloaded output operator Bignum operator+ (const bignum &) const;   Overloaded addition operator, the addition of two large numbers bignum operator-(const bignum &) const;   The overloaded subtraction operator, the subtraction operation between two large numbers bignum operator* (const bignum &) const;    Overloaded multiplication operator, the multiplication operation between two large numbers bignum operator/(const int &) const;    Overloaded division operator, large number divides an integer by bignum operator^ (const int &) const; Large number of n-th-square operations int operator% (const int &) const;   Large number modulo a variable of an int type bool operator> (const BIGNUM & T) const;      Large number and another large number of sizes compare bool Operator> (const INT & T) const;       The size of a variable of large number and an int type is compared with void print (); Output large number}; Bignum::bignum (const int B)//Converts a variable of type int 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)//Converts a variable of a string type to a large number {int T,k,index,l,i;memset (a,0,sizeof (a)); 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 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)//overloaded assignment operator, assignment operation between large numbers {int i;len = N.len;memset (A,0,sizeof (a)); for (i = 0; i < len; i++) a[i] = N.a[i]; return *this; }istream&amp Operator>> (IStream & In, Bignum & B)//Overloaded input operator {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)//overloaded output operator {int i; cout << b.a[b.len-1]; for (i = b.len-2; I >= 0; i--) {cout.width (Dlen); Cout.fill (' 0 '), cout << b.a[i];} return out;}      Bignum bignum::operator+ (const BIGNUM & T) const//Two large number of addition operations {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;   Elset.len = big; return t;} Bignum bignum::operator-(const BIGNUM & T) const//Two large number subtraction operation {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]; } Elset1.a[i]-= T2.a[i];} T1.len = Big;while (t1.a[t1.len-1] = = 0 && t1.len > 1) {t1.len--; big--;} if (flag) T1.a[big-1]=0-t1.a[big-1];return T1;   } bignum bignum::operator* (const BIGNUM & T) const//Two large number multiplication operation {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; r Et.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 bignum::operator/(const int & B) const//large number divides 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//large number The modulo operation of a variable of type int {int i,d=0;for (i = len-1; i>=0; i--) {d =  ((d * (maxn+1))% B + a[i])% B; }return D;} Bignum bignum::operator^ (const int & N) const//large number of n-th square operations {bignum T,ret (1); int i;if (n<0) exit ( -1); if (n==0) return 1; if (n==1) return *this;int m=n;while (m>1) {t=*this;for (i=1;i<<1<=m;i<<=1) {t=t*t;} M-=i;ret=ret*t;if (m==1) ret=ret* (*this);} return ret;} BOOL Bignum::operator> (const BIGNUM & T) const//large number and another large number size comparison {int ln; if (len > T.len) return true; else if (le n = = t.len) {ln = len-1; while (a[ln] = t.a[ln] && ln >= 0) ln--; if (ln >= 0 && A[ln] > T.a[ln]) return true; Elsereturn false; } Elsereturn false; }bool bignum::operator > (const int & T) const//large number and an int type variable size comparison {Bignum B (t);return *this>b;}   void Bignum::p rint ()//output large number {int i; cout << a[len-1]; for (i = len-2; I >= 0; i--) {cout.width (Dlen); Cout.fill (' 0 '); cout << a[i];} cout << Endl;} int main (void) {int t;          Bignum N,ans; cin>>t;for (int i=1;i<=t;i++) {cin>>n;cout<< "Case #" <<i<< ":"; Ans=bignum (8) *n* N-bignum (7) *n+bignum (1); Cout<<ans<<endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5047 Sawtooth (Java large number Class)

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.