UESTC-621 吴神的大脑

https://vjudge.net/problem/UESTC-621 吴神的大脑
gcd为x的个数为phi[1]+phi[2]+…+phi[n/x],用前缀和预处理。
剩下的无可奉告。
代码:

#include 
using namespace std;
typedef long long LL;
const int N = 1e6;
LL sphi[N+5];
void pre () {
    for (int i = 1; i <= N; ++ i) sphi[i] = i;
    for (int i = 2; i <= N; ++ i) if (sphi[i] == i)
        for (int j = i; j <= N; j += i)
            sphi[j] = sphi[j]/i*(i-1);
    for (int i = 1; i <= N; ++ i) sphi[i] += sphi[i-1];
}
int main () {
    int t; scanf ("%d", &t); pre ();
    while (t--) {
        int n; LL ans = 0; scanf ("%d", &n); 
        for (int i = 1; i <= n; ++ i)
            ans += i*(sphi[n/i]-1);
        printf ("%lld\n", ans);
    }
    return 0;
}

你可能感兴趣的:(UESTC-621 吴神的大脑)