[HDU 1796]How many integers can you find:容斥原理
来源:互联网 发布:雪梨淘宝店名叫什么 编辑:程序博客网 时间:2024/06/11 00:58
点击这里查看原题
典型的容斥原理,枚举所有取数情况求最小公倍数
有几个坑需要注意:
- 输入的数可能有0
- 所有数的最小公倍数可能会爆int
/*User:SmallLanguage:C++Problem No.:1796*/#include<bits/stdc++.h>#define ll long long#define inf 999999999using namespace std;int n,m,p[15];ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b);}ll lcm(ll a,ll b){ return a/gcd(a,b)*b;}int main(){ freopen("data.in","r",stdin);// while(cin>>n>>m){ int tot=0; for(int i=1;i<=m;i++) cin>>p[i]; for(int i=1;i<=m;i++) if(p[i]) p[++tot]=p[i]; m=tot; int ans=0; for(int i=1;i<(1<<m);i++){ ll u=1,cnt=0; for(int j=0;j<m;j++){ if((i>>j)&1){ cnt++; u=lcm(u,p[j+1]); } } ans+=(n-1)/u*(cnt%2?1:-1); } cout<<ans<<endl; } return 0;}
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 (容斥原理)
- 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(容斥原理)
- 我的java学习之路-tomcat与jsp
- (转载)2017第八届蓝桥杯C/C++ B组省赛题解
- 01_链表的翻转(C语言)
- 多线程
- 插件与主题
- [HDU 1796]How many integers can you find:容斥原理
- oralce监听配置允许访问本地数据库
- 拉格朗日插值方法示例
- 蓝桥杯JAVA语言B组_饮料换购
- golang 一行一行读文件
- iOS应用支持IPV6,就那点事儿
- HDU 2594 Simpsons’ Hidden Talents KMP
- React Native之TextInput自定义
- 个人记录-LeetCode 96. Unique Binary Search Trees