标签:莫比乌斯反演
5 1 3 4 2 4
64Hintgcd(x,y) means the greatest common divisor of x and y.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 1e4; typedef long long ll; const int mod = 10007; bool check[MAXN + 10]; ll prime[MAXN + 10],cnt[MAXN + 10]; ll mu[MAXN + 10],F[MAXN + 10]; void Moblus() { memset(check,false,sizeof(check)); mu[1] = 1; int tot = 0; for(int i = 2; i <= MAXN; i++) { if(!check[i]) { prime[tot++] = i; mu[i] = -1; } for(int j = 0; i * prime[j] <= MAXN; j++) { check[i * prime[j]] = true; if(i % prime[j] == 0) { mu[i * prime[j]] = 0; break; } mu[i * prime[j]] = -mu[i]; } } } int main() { //freopen("in","r",stdin); Moblus(); int n,x; while(~scanf("%d",&n)) { memset(cnt,0,sizeof(cnt)); for(int i = 0; i < n; i++) { scanf("%d",&x); cnt[x]++; } for(int i = 1; i <= MAXN; i++) for(int j = i * 2; j <= MAXN; j += i) cnt[i] += cnt[j]; for(int i = 1; i <= MAXN; i++) F[i] = cnt[i] * cnt[i]; ll res = 0; for(int i = 1; i <= MAXN; i++) { ll tp = 0; for(int j = i; j <= MAXN; j += i) { tp += mu[j / i] * F[j]; if(tp >= mod) tp %= mod; } res += tp * i % mod * (i - 1); if(res >= mod) res %= mod; } printf("%I64d\n",res); } return 0; }
Code( BestCoder Round #39 ($) C) (莫比乌斯反演)
标签:莫比乌斯反演
原文地址:http://blog.csdn.net/zsgg_acm/article/details/45319475