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;
}
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;
}
相关文章推荐
- url模块转换完整URL字符串
- 协程与线程和进程的关系理解
- java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
- android notification 详解
- POJ 4014
- Linux防火墙iptables学习笔记
- 编译、链接、加载
- swift 进阶资料
- 为什么使用spring Shiro去做安全认证
- 二维树状数组模板题 poj 1195 Mobile phones
- WPF 多语言支持
- 【C++】打印1到最大的n位数
- Metasploit(一)--Meterpreter的命令速查表
- WM_QUIT,WM_CLOSE,WM_DESTROY
- 鼠标的几种响应
- Android:EditText 多行显示及所有属性
- 理解 Android Build 系统
- cxf生成webservice客户端代码
- oracle 12c rac gi 安装检测有Node Connectivity Failed报错
- LeetCode 214. Shortest Palindrome