CO-PRIME(初探 莫比烏斯)NYOJ1066(經典)gcd(a,b)=1
阿新 • • 發佈:2017-06-10
put size 兩個 test hat ott == clas otto
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
參考學長博客 >>芷水<<
題意:給出n個正整數。求這n個數中有多少對互素的數。
分析:莫比烏斯反演。
此題中,設F(d)表示n個數中gcd為d的倍數的數有多少對,f(d)表示n個數中gcd恰好為d的數有多少對。
則F(d)=∑f(n) (n % d == 0)
f(d)=∑mu[n / d] * F(n) (n %d == 0)
上面兩個式子是莫比烏斯反演中的式子。
所以要求互素的數有多少對,就是求f(1)。
而依據上面的式子能夠得出f(1)=∑mu[n] * F(n)。
所以把mu[]求出來。枚舉n即可了,當中mu[i]為i的莫比烏斯函數。
初探莫比烏斯。還有非常多不是非常懂。跟進中。
。
。
轉載請註明出處:尋找&星空の孩子
題目鏈接:http://acm.nyist.net/JudgeOnline/problem.php?pid=1066
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN = 1e5+10; typedef long long LL; LL F[MAXN],f[MAXN]; int pri[MAXN],pri_num; int mu[MAXN];//莫比烏斯函數值 int vis[MAXN],a[MAXN]; void mobius(int N) //篩法求莫比烏斯函數 { pri_num = 0;//素數個數 memset(vis, 0, sizeof(vis)); vis[1] = mu[1] = 1; for(int i = 2; i <=N; i++) { if(!vis[i]) { pri[pri_num++] = i; mu[i] = -1; } for(int j=0; j<pri_num && i*pri[j]<N ;j++) { vis[i*pri[j]]=1;//標記非素數 //eg:i=3,i%2,mu[3*2]=-mu[3]=1;----;i=6,i%5,mu[6*5]=-mu[6]=-1; if(i%pri[j])mu[i*pri[j]] = -mu[i]; else { mu[i*pri[j]] = 0; break; } } } } inline LL get(int x) { return (LL)((x*(x-1))/2); } int main() { mobius(100005); int n; while(~scanf("%d",&n)) { memset(F,0,sizeof(F)); memset(f,0,sizeof(f)); int mmax = -1; for(int i = 1; i <= n; i++) { scanf("%d",&a[i]); f[a[i]]++; mmax = max(mmax, a[i]); } //求F[N] for(int i=1;i<=mmax;i++) { for(int j=i;j<=mmax;j+= i) { F[i]+=f[j];//個數 } F[i]=get(F[i]);//C(N,2),表示對數;保證了gcd(a,b);(a<b) } LL ans = 0; for(int i=1; i<=mmax; i++) ans+=F[i]*mu[i]; printf("%lld\n", ans); } return 0; }
CO-PRIME(初探 莫比烏斯)NYOJ1066(經典)gcd(a,b)=1