题意:求从1开始的第n个非平方数 以及图中公式
History repeat itself
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 875 Accepted Submission(s): 313
Problem Description
Tom took the Discrete Mathematics course in the 2011,but his bad attendance angered Professor Lee who is in charge of the course. Therefore, Professor Lee decided to let Tom face a hard probability problem, and announced that if he fail to slove the problem there would 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 Professor 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!!
Input
There 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.
Output
For each test case, print the N-th non square number and the result of the formula.
SampleInput
4
1
3
6
10
SampleOutput
2 2
5 7
8 13
13 28
/*假如让求第n个非平方数的话,看n前面有多少个平方数,假设有x个,则第n个非平方数就是n+x
则有 sqrt(n+x)=x x>根号n 之后暴力
对于第二个式子 由于下取整,所以每两个平方数之间的数的sqrt(i),都等于前面的那个平方数,
这样就很好计算了,一段一段的算
*/
#include <stdio.h>
#include <math.h>
int main()
{
__int64 i,m,n,tt;
__int64 x;
__int64 s;
while(scanf("%I64d",&m)!=EOF)
{
while(m--)
{
scanf("%I64d",&n);
i=(__int64)sqrt(n*1.0);
while((__int64)sqrt((n+i)*1.0)!=i)//这里必须要乘1.0 否则编译错误
{
i++;
}
x=n+i;
n=n+i;
tt=(__int64)(sqrt(x*1.0));
s=0;
for(i=2;i<=tt;i++)
{
s=s+(i*i-(i-1)*(i-1))*(i-1);//从i*i到 (i-1)*(i-1)的数 开方后向下取整 均为i-1
}
s=s+(x-tt*tt+1)*tt;
printf("%I64d %I64d\n",n,s);
}
}
return 0;
}