HDU 1796 How many integers can you find (容斥原理)
来源:互联网 发布:软件项目管理流程图 编辑:程序博客网 时间:2024/05/29 13:37
https://vjudge.net/contest/177343#problem/C
参考http://blog.csdn.net/sr_19930829/article/details/44938217
大概感觉容斥有两种
二进制:
e(~sf("%lld%lld",&n,&m)){ cnt=0;ans=0; rep(i,1,m){ int x; sf("%d",&x);if(x>0&&x<n)num[cnt++]=x; } int g=1<<cnt; for(int i=1;i<g;++i){ int k=0,tmp=i;LL lcm=1; for(int j=0;j<cnt;++j){ if(tmp&1)k++,lcm=LCM(lcm,num[j]); tmp>>=1; } if(k&1)ans+=(n-1)/lcm; else ans-=(n-1)/lcm; } cout<<ans<<'\n' ; }
dfs的枚举:(貌似会快些)
LL gcd(int x,int y){ return y==0?x:gcd(y,x%y);}LL LCM(int x,int y){ return (LL)x/gcd(x,y)*y;}int cnt;void dfs(int th,LL now,int step){ if(step>cnt)return; LL lcm=LCM(num[th],now); if(step&1)ans+=(n-1)/lcm; else ans-=(n-1)/lcm; for(int p=th+1;p<cnt;++p) dfs(p,lcm,step+1);}int main(){ while(~sf("%d%d",&n,&m)){ ans=0; cnt=0; rep(i,1,m){ int x; sf("%d",&x);if(x>0&&x<n)num[cnt++]=x; } for(int i=0;i<cnt;++i)dfs(i,num[i],1); cout<<ans<<'\n'; }}
阅读全文
0 0
- HDU 1796 How many integers can you find(容斥原理)
- hdu 1796 How many integers can you find(容斥原理)
- hdu 1796 How many integers can you find(容斥原理)
- HDU 1796 How many integers can you find(组合数学-容斥原理)
- HDU 1796 How many integers can you find(组合数学-容斥原理)
- hdu 1796 How many integers can you find(容斥原理)
- HDU 1796 - How many integers can you find(容斥原理)
- HDU 1796 How many integers can you find(简单容斥原理)
- HDU 1796 How many integers can you find (容斥原理)
- [ACM] HDU 1796 How many integers can you find (容斥原理)
- hdu-1796 How many integers can you find(容斥原理)
- HDU 1796 How many integers can you find(容斥原理+二进制/DFS)
- hdu 1796 How many integers can you find(容斥原理)
- 【HDU】1796 - How many integers can you find(容斥原理,GCD)
- HDU 1796:How many integers can you find(容斥原理)
- HDU 1796 How many integers can you find (容斥原理)
- HDU 1796 How many integers can you find(容斥原理)
- hdu-1796-How many integers can you find(容斥)
- [环境搭建] VS-Visual Studio-IIS Express 支持局域网访问
- 题目:扑克牌的顺子 从扑克牌中随机抽出5张牌,判断是不是一个顺子,即这五张牌是不是连续的。2——10为数字本身,A为1,J为11,Q为12,K为13,而大小王为任意数字。
- vuex
- Wechall刷题(二) Training: MySQL I 总结
- java数据结构与算法-高级排序-基数排序
- HDU 1796 How many integers can you find (容斥原理)
- hessian ClassCastException
- 【Android破解笔记】《兔豆冒险》内购破解
- 奔小康赚大钱 HDU
- 常用adb指令
- java跳出外循环
- Java泛型:泛型类、泛型接口和泛型方法
- hdu-1878-欧拉回路(并查集||dfs)&&欧拉回路
- IntelliJ IDEA 自动导入包 快捷方式