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!!
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.
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 2 31 and more then 0.
To simplified the problem , The N will be within 2 31 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
题目要求第n个正整数的非平方数x,并算出sqrt(1)+sqrt(2)+....+sqrt(x)。由于n<2^31,可以预处理出x^2<n的平方数,并求前缀和。
判断第n个非平方数的方法,一开始看n以内有多少个平方数,设为偏移量offset,如果[n,n+offset]之内没有平方数,则n+offset就是第n个非平方数;否则,令n=n+offset,迭代这个过程。求和的时候,找出比第n个非平方数x小的最大的平方数y,利用之前求得前缀和+(x-y+1)*sqrt(x),得出答案。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<bitset>
using namespace std;
#define LL long long
#define N 46350
#define NN 46400
LL a[NN];
LL pre[NN];
LL n;
int main()
{
LL ans;
a[0]=0;
pre[0]=0;
for (LL i=1;i<=N;i++){
a[i]=i*i;
pre[i]=pre[i-1]+(a[i]-a[i-1])*(i-1);
}
int T;
scanf("%d",&T);
while (T--){
scanf("%I64d",&n);
LL ans=0;
int i=1,del=0;
LL nn=n+i;
while (del!=i){
del=i;
for (i=1;i<=N;i++){
if (a[i]>nn){
i--;
break;
}
}
nn=n+i;
}
ans=pre[i];
ans+=(nn-a[i]+1)*i;
printf("%I64d %I64d\n",nn,ans);
}
return 0;
}