CO-PRIME(初探 莫比乌斯)NYOJ1066(经典)gcd(a,b)=1
来源:互联网 发布:传淘宝代销是什么意思 编辑:程序博客网 时间:2024/05/15 21:20
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.
- 样例输入
31 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;}
0 0
- CO-PRIME(初探 莫比乌斯)NYOJ1066(经典)gcd(a,b)=1
- NYOJ1066 CO-PRIME(莫比乌斯反演)
- Visible Lattice Points(spoj7001+初探莫比乌斯)gcd(a,b,c)=1 经典
- CodeForces 632B Co-prime Array 【GCD(互质)】
- bzoj 2818 Gcd(莫比乌斯+gcd(a,b)=d) 经典
- CodeForces 660A Co-prime Array(GCD)
- 莫比乌斯反演1-x1-y,中的质数GCD(a,b)
- Codeforces--660A--Co-prime Array(数学水题)
- CodeForces - 660A Co-prime Array (模拟)
- 【CodeForces】660A - Co-prime Array(水)
- Codeforces 660A:Co-prime Array(水题+思维)
- CodeForces 660A Co-prime Array(互质数列)
- 【Codeforces】-660A-Co-prime Array(数学)
- *gcd(a,b)==a^b
- 莫比乌斯反演之初探(1)
- NOJ 2079 Prime (莫比乌斯反演)
- NYOJ 1066 CO-PRIME(数论)
- HDU 4135 Co-prime (容斥)
- c++连接mysql编程(mysq-connector/c++)
- 【JZOJ】3169 生产汽车
- N! SDUT 3138
- spark standalone zookeeper HA部署方式
- openstack 管理 三十七 - 创建 vm 并 指定IP 地址
- CO-PRIME(初探 莫比乌斯)NYOJ1066(经典)gcd(a,b)=1
- 通过xcodebulid 归档 xcarchive 导出ipa
- wxString类型转化
- Python——makefile函数
- poi导出excel表格
- LINGO出错时错误代码含义:
- Getting started with Javacc
- Java基本数据类型总结
- mysql分组取每组前几条记录