History repeat itself
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1387 Accepted Submission(s): 625
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.
Sample Input
4
1
3
6
10
Sample Output
2 2
5 7
8 13
13 28
Author
HIT
Source
2012 Multi-University Training Contest 5
题意:要求找到第n个除了平方数的数m,然后计算个1-m的根号和。
一个比较简单的数学题,sqr里保存的是到达这个平方数的时候有多少个不是平方数的数,就是i*i-i,然后就是数学题了。。。。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll __int64
using namespace std;
const int MAXN=100010;
ll sqr[MAXN],sum[MAXN];
int main()
{
ll i;
int n,t;
sqr[0]=sum[0]=0;
for(i=1;i<=100000;i++)
{
sqr[i]=i*i-i;
sum[i]=sum[i-1]+((sqr[i]+i)-(sqr[i-1]+i-1)-1)*(i-1)+i;
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=100000;i++)
if(sqr[i]>=n)
break;
ll ans1=sqr[i]+i-(sqr[i]-n+1);
ll ans2=sum[i-1]+(ans1-(sqr[i-1]+i-1))*(i-1);
printf("%I64d %I64d\n",ans1,ans2);
}
return 0;
}