History repeat itself
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 460 accepted submission (s): 223
Problem descriptiontom took the discrete mathematics course in the 2011, but his bad attendance angered pointer sor Lee who is in charge of the course. therefore, login sor Lee decided to let Tom face a hard probability problem, and announced that if he fail to slove the problem there wocould be no way for Tom to pass the final exam.
As a result, Tom passed.
History repeat itself. You, the bad boy, also angered the cipher sor Lee When September ends. You have to faced the problem too.
The problem comes that you must find the n-th positive non-square number M and printed it. and that's for normal bad student, such as Tom. but the real bad student has to calculate the formula below.
So, that you can really understand what a bad student you are !!
Inputthere is a number (t) in the first line, tell you the number of test cases below. for the next t lines, there is just one number on the each line which tell you the N of the case.
To simplified the problem, the N will be within 231 and more then 0.
Outputfor each test case, print the n-th non square number and the result of the formula.
Sample input4 1 3 6 10
Sample output2 2 5 7 8 13 13 28
Source2012 multi-university training contest 5
Recommendzhuyuanchen520 indicates the number of non-partitions required for the parameter? Assume that the nth non-vertex number is X and there are n integers before X, then n x n <x <(n + 1) * (n + 1 ); the number of non-integers before N * n is N * n-n. First, n is obtained based on a, and N is the maximum positive integer that satisfies the N * n-n <. The solution to the inequality is: (1 + SQRT (1 + 4 * A)/2; the number must be rounded up, and then minus one is N. Then the number of non-decimal places in a is N * n + (a-n * n + n) = a + N, and then the value of another formula is obtained. First, 1 ~ is obtained ~ N * n-1 summation: (2 * N-1) * (n-1) Sums N from 2 to n to get n * (n + 1) * (2 * n + 1) /3-3 * n * (n-1)/2 + N; then add (n + A-N * n + 1) * N;
# Include <stdio. h> # Include <Math. h> # Include <Algorithm> Using Namespace STD; Int Main (){ Int T; scanf ( " % D " ,& T ); Long Long A; While (T -- ) {Scanf ( " % I64d " ,& A ); Double Temp = Ceil (( 1 + SQRT ( 1 + 4 * ))/ 2 )- 1 ; Long Long N = ( Long Long ) Temp; Long Long RES1 = N + A; Long Long RES2 = N * (N + 1 )*( 2 * N + 1 )/ 3 - 3 * (N + 1 ) * N/ 2 + N + (n + A-N * n + 1 )* N; printf ( " % I64d % i64d \ n " , RES1, RES2 );} Return 0 ;}
At the beginning, I thought that I would use high precision, so I wrote a high precision in Java.CodeRight:
Import Java. util .* ; Import Java. Math .* ; Import Java. Io .* ; Public Class Main { Public Static Void Main (string [] ARGs ){ Int T; CIN = New Partition ( New Bufferedinputstream (system. In); t = Cin. nextint (); Double A; For ( Int I = 0; I <t; I ++) { = Cin. nextdouble (); Double N = math. Ceil (1 + math. SQRT (4 * A + 1)/2)-1 ; Int RES1 = ( Int ) (N + A); biginteger RES2 = Biginteger. valueof (0 ); Biginteger T1 = Biginteger. valueof (1 ); T1 = T1.multiply (biginteger. valueof (( Int ) N); T1 = T1.multiply (biginteger. valueof (( Int ) (N + 1 ); T1 = T1.multiply (biginteger. valueof (( Int ) (2 * n + 1 ); T1 = T1.divide (biginteger. valueof (3 ); Biginteger T2 = Biginteger. valueof (1 ); T2 = T2.multiply (biginteger. valueof (( Int ) N); T2 = T2.multiply (biginteger. valueof (( Int ) (N + 1 ); T2 = T2.multiply (biginteger. valueof (3 ); T2 = T2.divide (biginteger. valueof (2 ); Biginteger T3 = Biginteger. valueof (0 ); T3 = T3.add (biginteger. valueof (( Int ) (N + A-N * n + 1 ); T3 = T3.multiply (biginteger. valueof (( Int ) N); T3 = T3.add (biginteger. valueof (( Int ) N); RES2 = Res2.add (T1); RES2 = Res2.subtract (T2); RES2 = Res2.add (T3 ); // System. Out. println (t1 + "" + T2 + "" + T3 ); System. Out. println (RES1 + "" + RES2 );}}}