【莫比乌斯】 SPOJ VLATTICE

通道

题意:求gcd(a,b,c) = 1    a,b,c <=N 的对数

代码:

#include <cstdio>

typedef long long ll;

template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if(c = getchar() , c == EOF) return false;
    while(c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while(c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return true;
}

const int MAX_N = 1000007;

int mu[MAX_N], cnt[MAX_N];
bool vis[MAX_N];

void Mobius(int n) {
    vis[1] = mu[1] = 1;
    for(int i = 1;i <= n; i++)  mu[i] = 1;
    for(int i = 2;i <= n; i++) {
        if(!vis[i]) {
            for(int j = i;j <= n;j += i) {
                vis[j] = 1;
                mu[j] *= -1;
                if((j/i) % i == 0)  mu[j] = 0;
                int t = j;
                while(t % i == 0) t /= i, cnt[j]++;
            }
        }
    }
}
int n;
int main() {  
    Mobius(1000001);
    int T; rd(T);
    while (T-- > 0) {
        rd(n);
        ll ans = 3;
        for (int i = 1; i <= n; ++i) 
            ans += (ll)mu[i] * (n / i) * (n / i) * (n / i + 3);
        printf("%lld\n", ans);
    }
    return 0;
}
View Code

 

posted @ 2015-08-17 21:38  mithrilhan  阅读(203)  评论(0编辑  收藏  举报