当前位置: 代码迷 >> 综合 >> HDOJ 4342 History repeat itself
  详细解决方案

HDOJ 4342 History repeat itself

热度:13   发布时间:2023-12-06 03:23:55.0

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4342

一道简单的数论题……其实说这道题是数论有点牵强,因为这道题并没有用到特别费脑的数学规律。

题意是让我们求第a个非平方数,以及1~a的所有的开根的和。

先说求第a个非平方数。设离a最近的那个平方数为n^2,那么显然,在n^2之前有n^2-n个非平方数,首先,我们把这个n解出来,也就是解出n*n-n < a这个不等式成立的最大的n,解得n < sqrt(a+1/4)+1/2,得到满足的最大的n之后我们再来求解第二个问,这个很好思考,在n^2~(n+1)^2之间的数,开根得到的都是n,所以我们能够很容易的得到答案。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int main()
{int T;scanf("%d", &T);while(T--){LL a;cin >> a;double tmp = ceil((1+sqrt(1+4*a))/2)-1;LL n = (LL)tmp;LL res1 = a+n;LL res2 = n*(n+1)*(2*n+1)/3 - 3*(n+1)*n/2 + n + (n+a-n*n+1)*n;printf("%I64d %I64d\n", res1, res2);}return 0;
}


  相关解决方案