Hdu5047sawtooth (Java large number)
Topic links
The main idea: in a rectangular drawing of n "M", asked how to draw the rectangle can be divided into the most areas. Give the number of this area.
Solution thinking: The best way is that each new picture of the "M" line and the original line intersect, so you can find AI = ai-1 + (i-1)? 4? 4 + 1. You can see that each new edge and the original edge intersect each other 4 more areas. Finally, there will be a small corner area to come out more. Last formula: Ai = a0 + 8Ii-7 * i;a0 = 1, to use large numbers, and the large number of input and output to use faster (with buffered), otherwise it will tle.
Code:
Import Java.util.*;import Java.io.*;import java.math.*; Public classMain { Public Static voidMain (String args[]) {ScannerCin=NewScanner (NewBufferedinputstream (system.in)); PrintWritercout=NewPrintWriter (NewBufferedoutputstream (System.out));//Read in the output speed is relatively fast intT T =Cin. Nextint (); BigInteger N; for(inti =1; I <= T; i++) {n =Cin. Nextbiginteger (); BigInteger A = biginteger.valueof (1); BigInteger ans = biginteger.valueof (8). Multiply (n). Multiply (n). Subtract (Biginteger.valueof (7). Multiply (n)). Add (a);cout.printf("Case #%d:", i);cout. println (ANS); }Cin. Close ();cout. Close (); }}
Hdu5047sawtooth (Java large number)