HDU 5047 SAWTOOTH--2014ACM Shanghai Tournament (with Java template)

Source: Internet
Author: User

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

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


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
Recommendhujie | We have carefully selected several similar problems for you:5052 5051 5049 5048 5046
Statistic | Submit | Discuss | Note


A regular problem.

The formula is like this 8*n^2-7*n + 1

But the question person seems to intentionally card Java to increase the difficulty of the problem ...

as an ACM weak slag ... Ben to today contributed 2 times after the tle only know the original Java inside Bufferreader than scanner faster ... The reason is because the former is read into the buffer, and the latter is directly read into the hard disk ...


Sacnner : A text scanner based on regular expressions that resolves primitive type values and string values from files, input streams, and strings.

< Span style= "Background-color:rgb (255,255,255)" >bufferedreader : is a string, wrapper class in the Javaio stream that must be built on the basis of another character stream, but system.in is a byte stream that needs to be wrapped in InputStreamReader to a character stream.

To use Bufferreader to enter data of a type other than some characters, it is relatively troublesome, need to pass some xxxx.parsexxx (), to convert the corresponding data type, although, some trouble, but by comparing on some OJ systems and scanner, Bufferreader efficiency is one times higher than scanner, this gap can be imagined, read more data, the effect is more obvious.


AC Program

Import Java.io.*;import java.math.*;p ublic class main{public  static BufferedReader cin=new BufferedReader (new InputStreamReader (system.in));  public static BufferedWriter cout=new BufferedWriter (New OutputStreamWriter (System.out));  public static void Main (String []args) throws exception{    int t=integer.parseint (Cin.readline ());    for (int nkase=1;nkase<=t;nkase++) {      cout.write ("Case #" +nkase+ ":");      BigInteger n=new BigInteger (Cin.readline ());      BigInteger ans=n.multiply (N). Multiply (biginteger.valueof (8)). Subtract (N.multiply (biginteger.valueof (7))). Add ( Biginteger.valueof (1));      Cout.write (Ans.tostring ());      Cout.newline ();    }    Cout.flush ();    Cout.close ();  }}



TLE Program

Package Sawtooth;import Java.util.*;import java.math.*;p ublic class Sawtooth {public static void main (String []args) thro WS exception{        BigInteger one=new BigInteger ("1");        BigInteger zero=new BigInteger ("0");        BigInteger two= New BigInteger ("2");        BigInteger four= New BigInteger ("4");        BigInteger six = new BigInteger ("6");        BigInteger eight=new  BigInteger ("8");        BigInteger seven=new BigInteger ("7"); Scanner cin=new Scanner (system.in); int t=cin.nextint (); BigInteger n,ans;for (int nkase=1;nkase<=t;nkase++) {String str;str=cin.next (); n=new BigInteger (str); ans= ( N.multiply (n)). Multiply (eight); BigInteger Tmp=n.multiply (seven); Ans=ans.subtract (TMP); Ans=ans.add (one); System.out.print ("Case #" +nkase+ ":"); System.out.println (Ans.tostring ());}}}


Finally, I enclose the input and output template of a cattle school.

Import Java.math.biginteger;import Java.util.*;import java.io.*;class inputreader {private InputStream stream;    Private byte[] buf = new byte[1000];    private int Curchar;    private int numChars;    Public Inputreader (InputStream stream) {this.stream = stream;        } private int Read () {if (NumChars = =-1) throw new Unknownerror ();            if (Curchar >= numChars) {Curchar = 0;            try {numChars = Stream.read (BUF);            } catch (IOException e) {throw new Unknownerror ();        } if (numChars <= 0) return-1;    } return buf[curchar++];        } public int readInt () {int c = read ();        while (Isspacechar (c)) C = Read ();        int sgn = 1;            if (c = = '-') {sgn =-1;        c = Read ();        } int res = 0;        do {if (C < ' 0 ' | | c > ' 9 ') throw new Inputmismatchexception ();    Res *= 10;            Res + = C-' 0 ';        c = Read ();        } while (!isspacechar (c));    return res * SGN;        } public int[] Readintarray (int length) {int[] res = new Int[length];        for (int i = 0; i < length; i + +) res[i] = ReadInt ();    return res;        } public int[][] Readintarray (int n, int m) {int[][] res = new INT[N][M];        for (int i = 0; i < n; i + +) for (int j = 0; J < m; j + +) Res[i][j] = ReadInt ();    return res;        } public Long Readlong () {int c = read ();        while (Isspacechar (c)) C = Read ();        int sgn = 1;            if (c = = '-') {sgn =-1;        c = Read ();        } Long res = 0;            do {if (C < ' 0 ' | | c > ' 9 ') throw new Inputmismatchexception ();            Res *= 10;            Res + = C-' 0 ';        c = Read ();        } while (!isspacechar (c));    return res * SGN; } public long[] Readlongarray (int length) {long[] res = new Long[length];        for (int i = 0; i < length; i + +) res[i] = Readlong ();    return res;        } public long[][] Readlongarray (int n, int m) {long[][] res = new LONG[N][M];        for (int i = 0; i < n; i + +) for (int j = 0; J < m; j + +) Res[i][j] = Readlong ();    return res;        } public String readString () {int c = read ();        while (Isspacechar (c)) C = Read ();        StringBuffer res = new StringBuffer ();            do {res.appendcodepoint (c);        c = Read ();        } while (!isspacechar (c));    return res.tostring ();        } public string[] Readstringarray (int length) {string[] res = new String[length];        for (int i = 0; i < length; i + +) res[i] = readString ();    return res;    } public String Next () {return readString ();        } private String ReadLine0 () {StringBuffer buf = new StringBuffer (); int c =Read ();            while (c! = ' \ n ' && c! =-1) {buf.appendcodepoint (c);        c = Read ();    } return buf.tostring ();        Public String ReadLine () {string s = readLine0 ();        while (S.trim (). Length () = = 0) s = readLine0 ();    return s;        Public String ReadLine (Boolean ignoreemptylines) {if (ignoreemptylines) return ReadLine ();    else return readLine0 ();        } public BigInteger Readbiginteger () {try {return new BigInteger (readString ());        } catch (NumberFormatException e) {throw new inputmismatchexception ();        }} public char readcharacter () {int c = read ();        while (Isspacechar (c)) C = Read ();    return (char) C;        } public char[] Readchararray (int length) {char[] res = new Char[length];        for (int i = 0; i < length; i + +) res[i] = Readcharacter ();    return res;    }Public char[][] Readchararray (int n, int m) {char[][] res = new CHAR[N][M];        for (int i = 0; i < n; i + +) for (int j = 0; J < m; j + +) Res[i][j] = Readcharacter ();    return res;        } public double readdouble () {int c = read ();        while (Isspacechar (c)) C = Read ();        int sgn = 1;            if (c = = '-') {sgn =-1;        c = Read ();        } double res = 0; while (!isspacechar (c) && c! = '. ')            {if (C < ' 0 ' | | c > ' 9 ') throw new Inputmismatchexception ();            Res *= 10;            Res + = C-' 0 ';        c = Read (); } if (c = = '. ')            {c = read ();            Double m = 1; while (!isspacechar (c)) {if (C < ' 0 ' | | c > ' 9 ') throw new inputmismatchexception                ();                M/= 10;                Res + = (C-' 0 ') * m;            c = Read ();     }   } return res * SGN;        } public double[] Readdoublearray (int length) {double[] res = new Double[length];        for (int i = 0; i < length; i + +) res[i] = readdouble ();    return res;        } public double[][] Readdoublearray (int n, int m) {double[][] res = new DOUBLE[N][M];        for (int i = 0; i < n; i + +) for (int j = 0; J < m; j + +) Res[i][j] = readdouble ();    return res;    } private Boolean Isspacechar (int c) {return c = = ' | | c = = ' \ n ' | | c = = ' \ r ' | | c = = ' \ t ' | | c = =-1;    }}class Outputwriter {private final printwriter writer; Public Outputwriter (OutputStream outputstream) {writer = new PrintWriter (New BufferedWriter    (OutputStream)));    Public Outputwriter (writer writer) {this.writer = new PrintWriter (writer);         public void print (object...objects) {for (int i = 0; i < objects.length; i++) {if (I! = 0)       Writer.print (");        Writer.print (Objects[i]);    }} public void Printdouble (double x, int precision) {writer.printf ("%." + precision + "F", X);        } public void Printlinedouble (double x, int precision) {printdouble (x, precision);    PrintLine ();        } public void PrintLine (object...objects) {print (objects);    Writer.println (); } public void Printintarray (int[] data) {for (int i = 0; i < data.length; i + +) if (I < DATA.L            Ength-1) {print (Data[i] + "");            } else {print (data[i]);  }} public void Printlongarray (long[] data) {for (int i = 0; i < data.length; i + +) if (I <            Data.length-1) {print (Data[i] + "");            } else {print (data[i]);    }} public void Close () {writer.close ();    }}public class Main {public static void Main (string[] args) {    Inputreader s = new Inputreader (system.in);        Outputwriter out = new Outputwriter (System.out);        int T = S.readint ();            for (int caseno = 0; Caseno < T; Caseno + +) {BigInteger n = s.readbiginteger ();            BigInteger ans = biginteger.valueof (8). Multiply (n). Multiply (N.subtract (biginteger.one));            Ans = Ans.add (n);            Ans = ans.add (biginteger.one);        Out.printline ("Case #" + (Caseno + 1) + ":" + ans);    } out.close (); }}















HDU 5047 SAWTOOTH--2014ACM Shanghai Tournament (with Java template)

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.