1245

http://lightoj.com/volume_showproblem.php?problem=1245

题目大意:一个数n除以1到n之和

分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的每个数的个数,然后再求n除以1~sqrt(n)之间的数的和

这样算下来就只有2*sqrt(n)的复杂度

最后还要排除多加的,、

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>

using namespace std;
typedef long long int LL;
#define N 10001000
#define ESP 1e-8
#define INF 0x3f3f3f3f
#define memset(a,b) memset(a,b,sizeof(a))

int main()
{
    int T, t=1;
    scanf("%d", &T);
    while(T --)
    {
        LL n;
        scanf("%lld", &n);

        LL sum =0;
        for(int i=1; i<=sqrt(n); i++)
        {
            sum += n/i;
        }

        for(int i=1; i<=sqrt(n); i++)
        {
            sum += (n/i - n/(i+1)) * i;
        }

        if(n/(int)sqrt(n) == (int)sqrt(n))
            sum -= (n/sqrt(n) - n/(sqrt(n)+1)) * sqrt(n);

        printf("Case %d: %lld
", t++, sum);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/linliu/p/5547543.html