您的位置:首页 > 其它

HDU 4342 History repeat itself 简单公式

2015-08-27 17:11 323 查看
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!!

这个题目题意就是求M 第N个非完全平方数,对M用公式求和

求第N个非完全平方数,就先对N与一个完全平方数减去之前出现的完全平方数个数进行比较,容易判断出当第一次出现N小的情况时就可以确实M

对已经求的M 如果M大于 I 的平方 则  I * I 到 (i - 1)*(i - 1) 之间的所有数都要加到总和中 如果M小 则对M到 (i
- 1)*(i - 1) 之间求和

又因为对于该函数
两个平方数之间的数的函数值是一样的,所以求和可以简化为乘法;

#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 30100;
long long n;
long long p[200000];
int main()
{
int t;
scanf("%d",&t);
// freopen("b.txt","w",stdout);
while(t--)
{
scanf("%I64d",&n);
// printf("%I64d\n",n);
long long m;
for(int i = 0; i <= (2<<16); i++) p[i] = (long long)(i*i);
// printf("%I64d\n",n);
for(int i = 1; i <= (2<<16); i++)
{
if( n <= (p[i] - i))
{
m = n + (i-1) ;
break;
}
}
long long ans = 0;
for(int i = 1; i <= (2<<16); i++ )
{
if(m >= p[i])
{
ans += (p[i] - p[i-1])*(long long)(i-1);
}
if( m < p[i])
{
ans+= (m - p[i-1]+1) * (long long)(i-1);
break;
}
}
printf("%I64d %I64d\n",m,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: