首页 > ACM题库 > HDU-杭电 > HDU 4342-History repeat itself-数论-[解题报告]HOJ
2015
05-23

HDU 4342-History repeat itself-数论-[解题报告]HOJ

History repeat itself

问题描述 :

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.
Gold miner

So, that you can really understand WHAT A BAD STUDENT YOU ARE!!

输入:

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.

输出:

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.

样例输入:

4
1
3
6
10

样例输出:

2 2
5 7
8 13
13 28

题意:求第n个非完全 平方数m,并求
求和sqrt(i)(1<=i<=m)

分析:m-sqrt(m)<=n,,,解得:m<=2*n+1+sqrt(4*n+1);当m为完全平方数时,m–

求和过程:1+sqrt(2)+sqrt(3)+sqrt(4)+sqrt(5)+sqrt(6)+sqrt(7)+sqrt(8)+sqrt(9)+sqrt(10)+……+sqrt(m),

代码:

#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
int main()
{
	int T;
	__int64 n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%I64d",&n);
		__int64 m=(2*n+1+sqrt(4*n+1.0))/2;
		if(  (__int64(sqrt(m*1.0)))*(__int64(sqrt(m*1.0)))  ==m)
			m--;
		__int64 ans=0;
		__int64 temp=(__int64)sqrt(m*1.0);
		for(__int64 i=1;i<temp;i++)  //i也要用__int64,不然一直WA
		{
           ans+=i*((i+1)*(i+1)-i*i);
		}
		for(__int64 i=temp*temp;i<=m;i++)
			ans+=temp;
		printf("%I64d %I64d\n",m,ans);
	}
	//system("pause");
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/wconvey/article/details/7841728