POJ1091 跳蚤 素因子分解+容斥原理
来源:互联网 发布:网络贷款 申请流程 编辑:程序博客网 时间:2024/04/30 13:18
题目链接:http://poj.org/problem?id=1091
分析:我们假设卡片上的标号分别是a1,a2,...,an,M,跳蚤跳对应标号的卡片的次数分别为x1,x2,...,xn,xn+1,那么要满足已知条件只需满足方程a1*x1+a2*x2+...+an*xn+M*xn+1=1有解,即gcd(a1,a2,...,an,m)=1,接下来对M进行质因子分解,然后排除公共因子非1的情况即可。我们可以分别找出公因子数为1到num内的所有符合条件的数组(a1,a2,...,an)的数目,然后用容斥原理解决。
实现代码如下:
#include <cstdio>#include <iostream>using namespace std;typedef long long LL;const int N=64;int prime[N],num;void divide(int m){//分解质因子 num=0; for(int i=2;i*i<=m;i++) { if(m%i==0) { prime[++num]=i; m/=i; while(m%i==0) m/=i; } } if(m>1) prime[++num]=m;}LL quick_mul(LL a,LL b){//计算a^b LL ans=1; while(b) { if(b&1) ans*=a; a*=a; b>>=1; } return ans;}LL temp,ans;int p[N],m,n;void dfs(int b,int cnt,int c){//搜索含有c个公共因子数组的个数 //b记录当前质因子的位置,cnt记录当前公共因子的个数 if(cnt==c) { int x=m; for(int i=1;i<=c;i++) x/=p[i]; temp+=quick_mul(x,n); return ; } for(int i=b+1;i<=num;i++) { p[cnt+1]=prime[i]; dfs(i,cnt+1,c); }}int main(){ while(scanf("%d%d",&n,&m)!=-1) { ans=0; divide(m); for(int i=1;i<=num;i++) { temp=0; dfs(0,0,i); if(i&1) ans+=temp; else ans-=temp; } ans=quick_mul(m,n)-ans; printf("%lld\n",ans); } return 0;}
0 0
- POJ1091 跳蚤 素因子分解+容斥原理
- poj1091(容斥原理+质因子分解)跳蚤
- poj1091跳蚤---------容斥
- POJ1091 跳蚤 【容斥】
- POJ 2773 happy2006 (素因子分解+容斥原理+二分)
- HDU 2841 Visible Trees (素因子分解,容斥原理)
- HNOI2002跳蚤--容斥原理
- poj 1091 跳蚤(分解质因数+容斥)
- POJ 1091 跳蚤 【容斥】【质因数分解】
- HDU 4135&HDU 4407--容斥原理--质因子分解
- hdu GCD 【欧拉函数,素因子分解,筛选法,容斥原理】
- HDU 1695 GCD(素因子分解+容斥原理+欧拉函数)
- poj2773-容斥原理(dfs+二分+素数表+素因子分解)
- hdu 1695 GCD (欧拉函数+容斥原理+素因子分解)
- HDU 2841 Visible Trees (容斥原理+素因子分解)
- PKU 1091 跳蚤 数论 容斥原理
- poj 1091 跳蚤 (数论,容斥原理)
- 跳蚤 - POJ 1091 容斥原理
- CRC16校验程序
- DNS劫持与DNS污染
- 常用正则表达式
- Android开发:Android设备传感器开发总结
- 试题推荐系统
- POJ1091 跳蚤 素因子分解+容斥原理
- github入门和GitHub for Windows教程整理
- GoldenGate的ADD SCHEMATRANDATA命令研究
- Spring学习笔记(8)----属性注入的方式
- 226Invert Binary Tree
- 机器学习实战之决策树(1)---ID3算法与信息熵,基尼不纯度
- Android Activity管理类,管理Activity和退出应用
- linux下关于后台任务和前台任务之间的且切换命令小结
- 一个任务调度问题