POJ 3904 Sky Code(素因子分解+容斥)
来源:互联网 发布:刀哥swift3.0源码 编辑:程序博客网 时间:2024/05/30 04:32
Description
给出n个数,先从中选取四个数,使得这四个数最大公因子为1,问有多少种选取方法
Input
多组输入,每组用例第一行为一整数n(n<=10000),第二行n个整数ai(ai<=10000)
Output
对于每种用例,输出满足条件的选取方案数
Sample Input
4
2 3 4 5
4
2 4 6 8
7
2 3 4 5 7 6 8
Sample Output
1
0
34
Solution
显然直接从这n个数中找四个最大公因子为1的数是不可能的,那么正难则反,首先从n个数中选取4个数的方案数是C(n,4),然后我们轻易知道只有这4个数具有至少一个共同因子时才不满足最大公因子为1的条件,设这n个数中以i为因子的数有num[i]个,那么从n个数中选取四个具有共同公因子的选取方案数sum就可以通过容斥定理得到,对于某个i,设cnt[i]为i的素因子数,那么当cnt[i]为奇,sum+=C(num[i],4),当cnt[i]为偶,sum-=C(num[i],4),最后令C(n,4)-sum即为答案
Code
#include<stdio.h>#include<string.h>#define maxn 10001typedef long long ll;int n,prime[20],num[maxn],cnt[maxn];ll C[maxn];void init(){ C[0]=C[1]=C[2]=C[3]=0; for(ll i=4;i<maxn;i++) C[i]=i*(i-1)*(i-2)*(i-3)/24;}void deal(int x){ //素分解 int res=0;//记录x的素因子个数 for(int i=2;i*i<=x;i++) if(x%i==0) { prime[res++]=i; while(x%i==0) x/=i; } if(x>1)prime[res++]=x; for(int i=0;i<(1<<res);i++)//2^res枚举得到x的所有因子 { int temp=0,sum=1;//sum记录该因子,temp记录该因子的素因子个数 for(int j=0;j<res;j++) if(i&(1<<j)) temp++,sum*=prime[j]; num[sum]++;//以sum为因子的数个数加一 cnt[sum]=temp;//sum的素因子数为temp }}int main(){ init();//预处理出C(i,4) while(~scanf("%d",&n)) { memset(num,0,sizeof(num)); memset(cnt,0,sizeof(cnt)); int d; for(int i=0;i<n;i++) { scanf("%d",&d); deal(d);//对d素分解 } ll ans=C[n]; for(int i=2;i<maxn;i++)//容斥 if(cnt[i]&1)ans-=C[num[i]]; else ans+=C[num[i]]; printf("%lld\n",ans); } return 0;}
0 0
- POJ 3904 Sky Code(素因子分解+容斥)
- POJ 3904 Sky Code(容斥)
- POJ 3904 Sky Code(容斥)
- POJ 3904 Sky Code 容斥原理
- poj 3904 sky code 容斥原理
- POJ 3904 Sky Code 容斥原理
- poj 3904 Sky Code【容斥原理】
- 容斥原理:poj 3904 Sky Code
- Sky Code - POJ 3904 容斥原理
- Sky Code - POJ 3904 容斥原理
- POJ 3904 Sky Code (容斥原理)
- POJ 3904 Sky Code (容斥原理)
- POJ 3904 Sky Code (容斥原理)
- poj 3904 Sky Code(容斥原理)
- POJ 3904 Sky Code 解题报告(容斥原理)
- POJ 3904 Sky Code (容斥原理)
- POJ 3904:Sky Code _容斥原理
- [容斥原理] poj 3094 Sky Code
- 怎样找到一个view所在的控制器(viewController)
- 数据库
- GCD 线程
- 【五校联考四】总结
- 使用python来调试串口
- POJ 3904 Sky Code(素因子分解+容斥)
- c++ primer 学习之路 指针的引用
- information retrieval
- 剑指offer—字符串的排列
- A015-布局之LinearLayout
- 关于系统运维监控规范的几点建议
- hdoj 2012 素数判定
- 天地图专题二:在天地图上循环显示标注点以及悬停显示信息窗口
- linux 正则表达式 sed