NYOJ 1066 CO-PRIME(数论)
来源:互联网 发布:vf表单关于淘宝设计 编辑:程序博客网 时间:2024/06/04 08:23
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的莫比乌斯函数。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 1e5 + 10;typedef long long LL;int cnt[MAXN], pri[MAXN], num[MAXN], pri_num, mu[MAXN], 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; j++) { if(i * pri[j] > n) break; vis[i*pri[j]] = 1; if(i % pri[j] == 0) { mu[i*pri[j]] = 0; break; } mu[i*pri[j]] = -mu[i]; } }}LL get(int x){ return (LL)x * (x-1) / 2;}int main(){ mobius(100005); int n; while(~scanf("%d",&n)) { int mmax = 0; for(int i = 1; i <= n; i++) { scanf("%d",&a[i]); mmax = max(mmax, a[i]); } memset(cnt, 0, sizeof(cnt)); memset(num, 0, sizeof(num)); for(int i = 1; i <= n; i++) num[a[i]]++; for(int i = 1; i <= mmax; i++) for(int j = i; j <= mmax; j += i) cnt[i] += num[j]; LL ans = 0; for(int i = 1; i <= mmax; i++) ans += get(cnt[i]) * mu[i]; printf("%lld\n", ans); } return 0;}
1 0
- NYOJ 1066 CO-PRIME(数论)
- HDU 4135 Co-prime(容斥原理 + 基础数论)
- HDU 4135 Co-prime 数论-求素数对
- HDU4135-Co-prime-数论(容斥原理-模板)
- HDU4135——Co-prime(数论,容斥原理)
- HDU 4135 Co-prime [容斥定理]【数论】
- Co-prime
- Co-prime
- Co-prime
- Co-prime
- HDU 4135 Co-prime (容斥)
- HDU4135 Co-prime(容斥原理)
- hdu4135(容斥原理)Co-prime
- HDOJ4135 Co-prime (容斥原理)
- csu 1756 prime(数论)
- hdu4135 Co-prime
- hdu 4135 co-prime
- HDU 4135 Co-prime
- vmware:ubuntu piix4_smbus: Host SMBus controller not enabled
- linux安装jdk1.4.2
- Gayway层 --- 概要
- Introduction to Machine Learning
- CentOS Linux解决Device eth0 does not seem to be present
- NYOJ 1066 CO-PRIME(数论)
- 查找素数
- 我对虚函数的理解
- jdk动态代理机制1
- android studio 如何删除一个module
- POJ 2516 基础费用流
- 【HTTP】Cookie/Session机制详解
- nyoj 1038 纸牌游戏
- Codeforces 455C Civilization 树的直径+并查集