Harmonic Number (II) LightOJ - 1245

版权声明: https://blog.csdn.net/weixin_40959045/article/details/88381900

类似因数分解

对于所有下于sqrt(N)值i,有(N/(i+1), N/(i)]的所有数贡献的权值为i,而大于sqrt(N)的值仅有一次可以得到

    #include<bits/stdc++.h>
    using namespace std;
    #define fst first
    #define sec second
    #define sci(num) scanf("%d",&num)
    #define scl(num) scanf("%lld",&num)
    #define mem(a,b) memset(a,b,sizeof a)
    #define cpy(a,b) memcopy(a,b,sizeof b)
    typedef long long LL;
    typedef pair<int,int> P;
    const int MAX_N = 510;
    const int MAX_M = 10000;
    LL s(LL N) {
        LL tp = sqrt(N);
        LL sum = 0;
        for (LL i = 1;i <= tp;i++) {
            sum = sum + (N / i - (N / (i + 1))) * i + N / i;
        }
        if (N/ tp == tp) {
            sum -= tp;
        }
        return sum;
    }
    int main() {
        int T;
        sci(T);
        for (int cs = 1;cs <= T;cs++) {
            LL N;
            scl(N);
            printf("Case %d: %lld\n",cs,s(N));
        }
        return 0;
    }

猜你喜欢

转载自blog.csdn.net/weixin_40959045/article/details/88381900
今日推荐