您的位置:首页 > 大数据 > 人工智能

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

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: