日韩性视频-久久久蜜桃-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)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 久久金品 | 亚洲精品乱码久久久久久麻豆不卡 | 波多野结衣av一区二区全免费观看 | 自拍偷拍99 | 五月中文字幕 | 国产又粗又猛又黄又爽无遮挡 | 香港台湾日本三级大全 | 色哟哟在线观看 | 亚洲国产aⅴ精品一区二区 日韩黄色在线视频 | 视频在线一区 | 三日本三级少妇三级99 | 亚洲中文一区二区 | 日韩av一 | 在线观看91av | 国产福利小视频在线 | 插插插插综合 | 男男受被啪到高潮自述 | 在线免费观看视频网站 | 国产成人啪免费观看软件 | 亚洲第一视频在线 | 日鲁鲁| 国产嫩草在线观看 | 国产亚洲天堂网 | 免费香蕉视频 | 91精品国产91久久久久久黑人 | 亚洲精品20p | 免费久久精品 | 亚洲一二三四五 | 神马电影久久 | 中文字幕精品在线观看 | 扒开jk护士狂揉免费 | 视频在线91 | 五月天男人天堂 | 中文字幕在线观看播放 | 亚洲欲| 日韩女优在线观看 | 天天干天天噜 | 日韩视频在线观看免费 | 台湾佬在线 | 一本色道久久加勒比精品 | 蜜臀少妇久久久久久久高潮 | 免费国产区 | 中文字幕校园春色 | 一区二区传媒有限公司 | 婷婷久久精品 | 成人尹人 | 天天干 夜夜操 | 谁有免费黄色网址 | av小次郎收藏 | 精品在线免费播放 | 综合第一页 | 精品裸体舞一区二区三区 | 欧洲天堂网| 亚洲精品乱码久久久久久麻豆不卡 | 青青草婷婷 | 国产中文字幕一区 | 久久精品国产亚洲av麻豆蜜芽 | 四虎成人网 | www.五月婷婷.com | 欧美成本人视频 | 人妻丝袜一区二区三区 | 日韩中文一区二区三区 | 97久久精品视频 | 国产精品日韩欧美一区二区三区 | 亚洲精品美女在线观看 | 国产精品久久久国产盗摄 | 在线免费观看网站入口在哪 | 懂色av蜜臀av粉嫩av分享吧最新章节 | 日韩高清免费观看 | 无码视频在线观看 | 亚洲欧美色图 | 黄色片在线免费 | 亚洲欧美精品一区二区三区 | 激情欧美在线 | 亚洲国产精品激情在线观看 | 亚洲视频在线看 | 国产精品日韩欧美 | 亚洲欧美日韩在线一区二区 | 久久6精品| 新av在线 | 日韩少妇内射免费播放 | 在线视频免费观看你懂的 | 亚洲成人h | 欧美特一级 | 国产精品外围 | 在线v| 午夜天堂影院 | 精品成人 | 变态另类一区 | 欧美日本韩国在线 | 日韩一级在线 | 激情文学88 | 欧美日韩精品一区二区三区视频播放 | 伊人久久大香线蕉综合75 | 悟空影视大全免费高清观看在线 | 香蕉黄视频 | 日本男女网站 | 18禁肉肉无遮挡无码网站 | 精国产品一区二区三区a片 国产精品第一 |