CO-PRIME(初探 莫比乌斯)NYOJ1066(经典)gcd(a,b)=1
CO-PRIME
- 描写叙述
-
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个数中有多少对互素的数。
分析: fr=aladdin" target="_blank" style="text-decoration:none; color:rgb(12,137,207)">莫比乌斯反演。
此题中,设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的更多相关文章
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA 1642 Magical GCD(经典gcd)
题意:给你n(n<=100000)个正整数,求一个连续子序列使序列的所有元素的最大公约数与个数乘积最大 题解:我们知道一个原理就是对于n+1个数与n个数的最大公约数要么相等,要么减小并且减小至少 ...
- 数学:莫比乌斯反演-GCD计数
Luogu3455:莫比乌斯反演进行GCD计数 莫比乌斯反演就是用来解决这一类问题的,通常f函数是要求的那个,F函数是显然的 这样利用F的结果就可以推出来f的结果 在计算结果的时候整除分快儿一下就可以 ...
- 牛客小白月赛13-J小A的数学题 (莫比乌斯反演)
链接:https://ac.nowcoder.com/acm/contest/549/J来源:牛客网 题目描述 小A最近开始研究数论题了,这一次他随手写出来一个式子,∑ni=1∑mj=1gcd(i,j ...
- ZOJ 3435 Ideal Puzzle Bobble 莫比乌斯反演
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4119 依然是三维空间内求(1,1,1)~(a,b,c)能看到的整点数,平移一下 ...
- Algorithm: Prime & Euler Function & Productive Function
素数筛 朴素算法 一般来说,可以用试除法判断某一个数是不是素数: bool isPrime(int n) { if(n < 2) return false; for(int i = 2; i & ...
- (转载)有关反演和gcd
tips : 积性函数 F (n) = Π F (piai ) 若F (n), G (n)是积性函数则 F (n) * G (n) Σd | n F (n) 是积性函数 n = Σd | n φ ( ...
- hdu-3071 Gcd & Lcm game---质因数分解+状态压缩+线段树
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3071 题目大意: 给定一个长度为n的序列m次操作,操作的种类一共有三种 查询 L :查询一个区间的所 ...
- 51Nod 1439:互质对(用莫比乌斯来容斥)
有n个数字,a11,a22,…,ann.有一个集合,刚开始集合为空.然后有一种操作每次向集合中加入一个数字或者删除一个数字.每次操作给出一个下标x(1 ≤ x ≤ n),如果axx已经在集合中,那么就 ...
随机推荐
- angular7升级到angular8
1.首先我们对:angular的命令的安装 ng install -g @angular/cli的安装则会升级到最新的版本,并且再次创建项目的时候,我们就能够使用ng version查看到已经是最新的 ...
- [Debug]SpaceVim中neomake报错 Error while trying to load a compilation database
回家装上archlinux,突发奇想装个SpaceVim写题 安装配置一路可以说是没有太大问题 最后在写题时出现如下问题 Error while trying to load a compilatio ...
- luogu P1622 释放囚犯
题目描述 Caima王国中有一个奇怪的监狱,这个监狱一共有P个牢房,这些牢房一字排开,第i个紧挨着第i+1个(最后一个除外).现在正好牢房是满的. 上级下发了一个释放名单,要求每天释放名单上的一个人. ...
- VMware exsi 虚拟化嵌套
默认情况下exsi 虚拟化嵌套是没开启的 需要我们连接exsi主机,从后台找到对应的虚拟机修改配置文件开启虚拟化功能 1.连接exsi主机,开启ssh功能 2.ssh到exsi主机,修改配置文件 查找 ...
- ZOJ 2601 Warehouse Keeper
Warehouse Keeper Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Origin ...
- Memcached 集群环境Java客户端
Memcached 集群环境Java客户端 学习了: http://blog.csdn.net/zhouzhiwengang/article/details/53154112 http://guazi ...
- java匿名内部类的使用注意事项
1.首先匿名内部类要继承自抽象基类或者实现基类接口 like this abstract class Seed{ int cnt; public Seed(int x){ cnt=x; } abstr ...
- Linux线程相互排斥量--进程共享属性
多线程中.在相互排斥量和 读写锁的 属性中.都有一个叫 进程共享属性 . 对于相互排斥量,查询和设置这个属性的方法为: pthread_mutexattr_getpshared pthread_mut ...
- 设置Webdriver启动chrome为默认用户的配置信息
Webdriver 启动Chrome浏览器时,默认是打开一个新用户,而非默认用户.即新用户没有我们安装扩展程序.但在实际应用中,我们会须要 默认用户安装的一些扩展程序,比方对于某些js或者css样式. ...
- spark 卡在spark context,运行出现spark Exception encountered while connecting to the server : javax.security.sasl.SaslException
原因: 使用root用户运行spark代码 解决方法:使用非管理员账户运行spark即可 [userone@localhost bin]$ ./add-user.sh What type of use ...