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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【ZOJ - 2836 】Number Puzzle (容斥原理)

發(fā)布時(shí)間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【ZOJ - 2836 】Number Puzzle (容斥原理) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers that are not greater than M and dividable by any integer from the given list.

?

Input

?

?

The input contains several test cases.

For each test case, there are two lines. The first line contains N (1 <= N <= 10) and M (1 <= M <= 200000000), and the second line contains A1, A2, ..., An(1 <= Ai?<= 10, for i = 1, 2, ..., N).

?

Output

?

For each test case in the input, output the result in a single line.

?

Sample Input

?

3 2
2 3 7
3 6
2 3 7
?

?

Sample Output

?

1
4

題目大意:

給N個(gè)整數(shù),和一個(gè)整數(shù)M。求小于等于M的非負(fù)整數(shù)(1~M)中能被這N個(gè)數(shù)中任意一個(gè)整除的數(shù)的個(gè)數(shù)。

解題報(bào)告:

? ?裸的容斥原理啊不解釋了。。。

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 ans,m,n; ll a[105]; ll LCM(ll a,ll b) {return (a*b)/__gcd(a,b); } void dfs(ll cur,ll lcm,ll id) {if(cur == n) {if(id == 0) return ;if(id&1) ans += ((m)/lcm);else ans-=((m)/lcm);return ;}dfs(cur+1,lcm,id);dfs(cur+1,LCM(a[cur+1],lcm),id+1); } int main() {while(~scanf("%lld%lld",&n,&m)) {for(int i = 1; i<=n; i++) scanf("%lld",a+i);ans=0;dfs(1,a[1],1);//沒選第一個(gè) dfs(1,1,0);//選了第一個(gè) printf("%lld\n",ans);}return 0 ;}

?

總結(jié)

以上是生活随笔為你收集整理的【ZOJ - 2836 】Number Puzzle (容斥原理)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。