History repeat itself
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1709 Accepted Submission(s): 805
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!!
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 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
Author
HIT
Source
记录平方数的位置就行,找找规律吧
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
const double EPS=1e-8;
const int MAXN =1000000;
const long long INF =0x3f3f3f3f3f3f3f;
typedef long long LL;
long long cnt[MAXN];
long long num[MAXN];
int main(){
cnt[1]=0;
num[1]=0;
for(long long i=2;i<MAXN;i++){
cnt[i]=cnt[i-1]+2*i-2;
num[i]=num[i-1]+(2*i-1)*(i-1);
}
//printf("%lld\n",num[MAXN-1]);
int t;
scanf("%d",&t);
while(t--){
long long n;
scanf("%lld",&n);
long long temp=lower_bound(cnt,cnt+MAXN,n)-cnt-1;
long long x=temp*temp;
// printf("%lld\n",num[temp]);
printf("%lld ",n-cnt[temp]+x);
long long res=0;
res+=num[temp];
res+=(n-cnt[temp]+1)*temp;
printf("%lld\n",res);
}
}