HDOJ 4342 History repeat itself

http://acm.hdu.edu.cn/showproblem.php?pid=4342

題意:給定一個數N,求第N個素數M以及公式的結果。

思路:1、先打表將部分公式值求出。2、第N的非平方數即N+N之前的平方數個數:add(計算add時加了一個偏移量:sqrt((double)n),否則會出錯)。3、計算M的公式結果時先求出M開方取證的結果:tmp,tmp*tmp-1的公式結果已存在表中,直接調用即可,tmp*tmp到M的結果直接計算就可以~

#include<stdio.h>
#include<math.h>

#define maxn 111111

__int64	num[maxn];

int main()
{
	__int64 i,t,n,add,sum,tmp;
	for(i=2;i<=maxn;i++)
    {
        num[i]=num[i-1]+(i*i-(i-1)*(i-1))*(i-1);
    }//打表,num[i]儲存M=(i*i-1)的公式值
	while(scanf("%I64d",&t)==1)
	{
		while(t--)
		{
			scanf("%I64d",&n);
			add=(__int64)sqrt((double)n+(sqrt((double)n)));
			printf("%I64d ",n+add);
			sum=0;
			tmp=(__int64)sqrt((double)n+add);
			sum+=num[tmp];
			sum+=(n+add-tmp*tmp+1)*tmp;
			printf("%I64d\n",sum);
		}
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章