CO-PRIME
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
This problem is so easy! Can you solve it?
You are given a sequence which contains n integers a1,a2……an, your task is to find how many pair(ai, aj)(i < j) that ai and aj is co-prime.
-
输入
-
There are multiple test cases.
Each test case conatains two line,the first line contains a single integer n,the second line contains n integers.
All the integer is not greater than 10^5.
输出
- For each test case, you should output one line that contains the answer. 样例输入
-
3 1 2 3
样例输出
-
3
上传者
-
TC_李远航
/* 经典好题,,好几天一直搞不明白怎么回事儿,,,一问qi神,结果说这题是他队友出的比赛题,多亏qi神指点,,当qi神送给的新年礼物了。。 Time:2014-12-31 18:51 */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int INF=0x3f3f3f3f; const int MAX=100000+10; typedef long long LL; int prime[MAX+10],mu[MAX+10]; bool vis[MAX+10]; void mobius(){//eular筛法求mu memset(vis,0,sizeof(vis)); memset(prime,0,sizeof(prime)); mu[1]=1;vis[1]=true;int cnt=0; for(int i=2;i<=MAX;i++){ if(!vis[i]){ prime[cnt++]=i; mu[i]=-1; } for(int j=0;j<cnt;j++){ if(i*prime[j]>=MAX)break; vis[i*prime[j]]=true; if(i%prime[j]==0){ mu[i*prime[j]]=0; break; } mu[i*prime[j]]=-mu[i]; } } } int num[MAX+10],a[MAX+10];//num表示的i是倍数的个数 LL Get(LL x){//get函数求的是gcd>=n的对数 return x*(x-1)/2; } int main(){ int n; int maxV,x; mobius(); while(scanf("%d",&n)!=EOF){ maxV=0; memset(num,0,sizeof(num)); memset(a,0,sizeof(a)); for(int i=1;i<=n;i++){ scanf("%d",&x); a[x]++; maxV=max(maxV,x); } for(int i=1;i<=maxV;i++){ for(int j=i;j<=maxV;j+=i){ num[i]+=a[j]; } } LL cnt=0; for(int i=1;i<=maxV;i++){ cnt+=(LL)Get(num[i])*mu[i];//为了造福初学的后来人,在这儿特地多加点注释,直接拿i本身的的所有倍数个数与mu[i]相乘和每一个n的约数x*mu[x]最后相加得到的结果是一样的,,,而后者就是莫比乌斯反演,貌似前者是另一种莫比乌斯反演的形式,这个跟1---n与1--m互质个数求mu[i]*(n/i)*(m/i)是一样的道理 } printf("%lld\n",cnt); } return 0; }
-
There are multiple test cases.