【 HDU - 1796】How many integers can you find (容斥原理,二进制枚举或者dfs)
題干:
??Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.
Input
??There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.
Output
??For each case, output the number.
Sample Input
12 2 2 3Sample Output
7題目大意:
給定n和一個大小為m的集合,集合元素為非負整數。為1...n內能被集合里任意一個數整除的數字個數。n<=2^31,m<=10
輸入n和m,接下來m個數。多組輸入數據。
解題報告:
? ? 就是個簡單的容斥啦~直接暴力、。但是用二進制的話時間挺緊湊的吧感覺。。。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; ll a[MAX]; ll LCM(ll a,ll b) {return (a*b)/__gcd(a,b); } int main() {ll n,m,tmp,tot,ans;while(~scanf("%lld%lld",&n,&m)) {tot=ans=0;for(int i = 1; i<=m; i++) {scanf("%lld",&tmp);if(tmp != 0) a[++tot] = tmp; }for(int i = 1; i<=(1<<tot)-1; i++) {ll k = 0,lcm = 1;for(int j = 0; j<=tot-1; j++) {if(i & (1<<j)) {k++;lcm =LCM(lcm,a[j+1]);}}if(k & 1) ans += (n-1) / lcm;else ans -= (n-1) / lcm; }printf("%lld\n",ans);}return 0 ;}總結:
? 注意判0啊,不然就會出現Runtime Error(INTEGER_DIVIDE_BY_ZERO)、、、其實想想也是嘛,因為gcd沒事,但是lcm的時候有問題,比如lcm(0,100) return?(0*100)/100
這題還可以用dfs寫:(234ms)
//234ms #include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; int n,m,cnt; ll ans,a[30]; ll LCM(ll a,ll b) {return (a*b)/__gcd(a,b); } void dfs(int cur,ll lcm,int id) {lcm=LCM(a[cur],lcm); //遞歸找兩個數的最小公倍數(其中一個數還是集合里面上一個兩個數的最小公倍數)所以lcm表示的真實意義是很多個數的最小公倍數if(id&1) ans+=(n-1)/lcm; //因為這題并不包含n本身,所以用n-1else ans-=(n-1)/lcm;for(int i=cur+1; i<cnt; i++)dfs(i,lcm,id+1); //id+1是表示如果上一次是奇數個數的倍數那么這次就是找的是偶數個數的倍數 }int main() {while(~scanf("%d%d",&n,&m)) {cnt=0;int x;while(m--) {scanf("%d",&x);if(x!=0)a[cnt++]=x;}ans=0;for(int i=0; i<cnt; i++)dfs(i,a[i],1);printf("%lld\n",ans);}return 0; }另一種dfs:(218ms)
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; int n,m,cnt; ll ans,a[30]; ll LCM(ll a,ll b) {return (a*b)/__gcd(a,b); }void dfs(int cur,ll lcm,int id) {if(cur == cnt) {if(id == 0) return ;if(id&1) ans += (n-1)/lcm;else ans-=(n-1)/lcm;return ;}dfs(cur+1,lcm,id);dfs(cur+1,LCM(lcm,a[cur+1]),id+1); }int main() {while(~scanf("%d%d",&n,&m)) {cnt=0;int x;while(m--) {scanf("%d",&x);if(x!=0)a[++cnt]=x;}ans=0;dfs(1,a[1],1);dfs(1,1,0);printf("%lld\n",ans);}return 0; }附:
一種新奇的dfs的思路:(31msAC)
總結
以上是生活随笔為你收集整理的【 HDU - 1796】How many integers can you find (容斥原理,二进制枚举或者dfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光大信用卡逾期怎么办 千万不要急于注销卡
- 下一篇: 物价下跌只是假象?12月CPI回正,过