日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【 HDU - 1796】How many integers can you find (容斥原理,二进制枚举或者dfs)

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【 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 3

Sample 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。