CodeForces839D[莫比乌斯反演] Codeforces Round #428 (Div. 2)

/*CodeForces839D[莫比乌斯反演]*/
#include <bits/stdc++.h>
typedef long long LL;
const LL MOD = 1000000007LL;
using namespace std;
int n, maxa = 0, mina = 0x3f3f3f3f;
LL m[200005], F[1000005], f[1000005];
int sum[1000005], a[200005];
int  prime[1000005], vis[1000005], mu[1000005];
void mo_init(int N) {
    memset(vis, 0, sizeof(vis));
    mu[1] = 1;
    int cnt = 0;
    for (int i = 2; i < N; i++)
    {
        if (!vis[i])
        {
            prime[cnt++] = i;
            mu[i] = -1;
        }
        for (int j = 0; j < cnt && i * prime[j] < N; j++)
        {
            vis[i * prime[j]] = 1;
            if (i % prime[j]) mu[i * prime[j]] = -mu[i];
            else
            {
                mu[i * prime[j]] = 0;
                break;
            }
        }
    }
}
void solve() {
    for (int i = 0; i < n; i++) {
        for (int j = 1; j * j <= a[i]; j++) {
            if (a[i] % j == 0) {
                if (j * j == a[i]) {
                    sum[j]++;
                }
                else {
                    sum[j]++, sum[a[i] / j]++;
                }
            }
        }
    }
    LL ans = 0;
    for (int i = 2; i <= maxa; i++) {
        F[i] = sum[i] * m[sum[i] - 1] % MOD;
    }
    for (int i = 2; i <= maxa; i++) {
        for (int j = i; j <= maxa; j += i) {
            f[i] = (f[i] + mu[j / i] * F[j] % MOD) % MOD;
        }
        ans = (ans + f[i] * i % MOD) % MOD;
    }
    printf("%lld\n", ans);
}
void init() {
    mo_init(1000002);
    m[0] = 1LL;
    for (int i = 1; i <= n; i++) {
        m[i] = m[i - 1] * 2LL % MOD;
    }
}
int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        maxa = max(maxa, a[i]);
        mina = min(mina, a[i]);
    } init(), solve();
    return 0;
}

 

posted @ 2017-08-14 14:41 UnderSilence 阅读(...) 评论(...) 编辑 收藏