HDU4944-FSF’s game(递推)

题目链接

nj=1nf(n,j)f(i,j)=ijgcd(i/k,j/k)(ki,j)

ans[n]=ans[n1]+j=1nf(n,j),
j=1nf(n,j)O(n)
j=1nf(n,j)aj(1<=j<=n)gcd(n/k,j/k)
j=1nf(n,j)=(n1/a1+n2/a2++nn/an)=n(1/a1+2/a2++n/an),ajn
nm1m,2m...najm
sum(m) = (1m / m) + (2m / m) + ... + (n / m) = (1 + n / m) * (n / m) / 2
所以
j=1n=mnnsum(m)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef unsigned __int64 ull;
const int MAXN = 500000;
const ull MOD = (ull)1 << 32;

ull  ans[MAXN + 5], sum[MAXN + 5];
int n;

void init() {
    memset(ans, 0, sizeof(ans));
    memset(sum , 0, sizeof(sum));
    for (ull i = 1; i <= MAXN; i++)
        for (ull j = i; j <= MAXN; j += i)
            sum[j] += (j / i + 1) * (j / i) / 2;
    for (ull i = 1; i <= MAXN; i++) {
        ans[i] = ans[i - 1] + sum[i] * i;  
        ans[i] %= MOD; 
    }
}

int main() {
    int t = 1, cas;
    init();
    scanf("%d", &cas); 
    while (cas--) {
        scanf("%d", &n);
        printf("Case #%d: %I64u\n", t++, ans[n]);  
    }  
    return 0;
}


你可能感兴趣的:(HDU4944-FSF’s game(递推))