2012 Multi-University Training Contest 5:History repeat itself
2012-08-08 19:45
344 查看
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 ann
ounced 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 Septem
ber 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
SampleOutput
//题意:给出一个n,求第n个非平方数m,并求出 1到m每个数的根号(取下界)之和。
//分析:1、假设不考虑是否为平方数 ,直接询问第n个数,那么就是n, 而n前面有sqrt(n)个平方数,那么再考虑平方数的情况下在 n前面就少计
算了sqrt(n)个数。 所以第n个平方数就是n+sqrt(n),但我们会发现 n到n+sqrt(n)之间可能也有平方数,那么仍旧计算小了。 写几组数据其实就
可以发现,在计算sqrt(n)取整的时候需要取较近的那个整数点。 所以第n个非平方数为n+[sqrt(n)];
2、找到m,则只需要计算1到m每个数的根号(取下界)之和。 从1一直到m每个数开根号会发现出现了3个1 , 5个2 ,7个3 ,9个4,式子的
第二个数肯定是一直递增1,而第一个数递增2。那么到m的时候肯定是(2*k-1)*sqrt(m)的形式。 所以只要循环到sqrt(m)利用这个规律则可以累加
值,需要注意的是最后的sqrt(m)不一定恰好就是(2*k-1)个。 则需先处理到sqrt(m)-1。 而另t=(int)sqrt(m)剩余的便是(m-t*t+1)个sqrt(m)了。 时间
复杂度为sqrt(n)。
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 ann
ounced 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 Septem
ber 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个非平方数m,并求出 1到m每个数的根号(取下界)之和。
//分析:1、假设不考虑是否为平方数 ,直接询问第n个数,那么就是n, 而n前面有sqrt(n)个平方数,那么再考虑平方数的情况下在 n前面就少计
算了sqrt(n)个数。 所以第n个平方数就是n+sqrt(n),但我们会发现 n到n+sqrt(n)之间可能也有平方数,那么仍旧计算小了。 写几组数据其实就
可以发现,在计算sqrt(n)取整的时候需要取较近的那个整数点。 所以第n个非平方数为n+[sqrt(n)];
2、找到m,则只需要计算1到m每个数的根号(取下界)之和。 从1一直到m每个数开根号会发现出现了3个1 , 5个2 ,7个3 ,9个4,式子的
第二个数肯定是一直递增1,而第一个数递增2。那么到m的时候肯定是(2*k-1)*sqrt(m)的形式。 所以只要循环到sqrt(m)利用这个规律则可以累加
值,需要注意的是最后的sqrt(m)不一定恰好就是(2*k-1)个。 则需先处理到sqrt(m)-1。 而另t=(int)sqrt(m)剩余的便是(m-t*t+1)个sqrt(m)了。 时间
复杂度为sqrt(n)。
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<queue> #include<stack> #include<map> #include<vector> #include<algorithm> using namespace std; #define LL __int64 int main() { int t,i; scanf("%d",&t); while(t--) { LL n,m; scanf("%I64d",&n); double c=sqrt(1.0*n),tmp1=floor(c),tmp2=ceil(c); m=n+((c-tmp1)>(tmp2-c)?((int)tmp2):((int)tmp1)); LL k=(int)sqrt(1.0*m); LL sum=0,cnt=3; for(i=1;i<=k-1;i++) { sum+=cnt*i; cnt+=2; } sum+=(m-k*k+1)*k; printf("%I64d %I64d\n",m,sum); } return 0; }
相关文章推荐
- HDU 4342 History repeat itself 2012 Multi-University Training Contest 5
- 2012 Multi-University Training Contest 4[hdu4331~4339]
- 2012 Multi-University Training Contest 7-1001 hdu4360 As long as Binbin loves Sangsang
- 2012 Multi-University Training Contest 8[hdu4370~4379]
- 2012 Multi-University Training Contest 8
- 2012 Multi-University Training Contest 6
- Matrix&&2012 Multi-University Training Contest 2
- 2012 Multi-University Training Contest 3[hdu4320~4330]
- 2012 Multi-University Training Contest 5:Xiao Ming's Hope
- 2012 2012 Multi-University Training Contest 7 Dragon Ball
- 2012 Multi-University Training Contest 7-1003 hdu4362 Dragon Ball
- 2012 Multi-University Training Contest 8
- 2012 Multi-University Training Contest 6-1009 hdu4358 Boring counting
- hdu 4393 Throw nails (2012 Multi-University Training Contest 10 )
- HDU 4340 Capturing a country 2012 Multi-University Training Contest 5
- 2012 Multi-University Training Contest 4
- 2012 Multi-University Training Contest 5:Mark the Rope
- hdu 4355 Party All the Time (2012 Multi-University Training Contest 6 ) 三分搜索
- 2012 Multi-University Training Contest 7-1005 hdu4364 Matrix operation
- 2012 Multi-University Training Contest 9__Farmer Greedy和Quadrilateral