zoj 2836 Number Puzzle(容斥,求1到m有多少个数能被数组里任意一个整除)
来源:互联网 发布:gta5淘宝刷钱会封号吗 编辑:程序博客网 时间:2024/05/07 11:33
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
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836
#include<iostream>#include<algorithm>#include<string>#include<map>#include<vector>#include<cmath>#include<string.h>#include<stdlib.h>#include<cstdio>#define ll long longusing namespace std;ll n,m,s;int x[15];ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}void dfs(ll aim,ll step,ll k,ll per,ll u){//k:从第k个开始 per:当前数的最小公倍数 if(aim==step){if(u==1)s+=m/per; //m除所有数的最小公倍数 elses-=m/per;return;}elsefor(int i=k;i<n;++i) //这个K真是妙极了!(保证了每一种组合的存在) dfs(aim,step+1,i+1,(per*x[i])/gcd(per,x[i]),u);}int main(){while(cin>>n>>m){s=0;for(int i=0;i<n;++i)cin>>x[i];for(int i=1;i<=n;++i){if(i%2==1)dfs(i,0,0,1,1);//1个数的时候加,2个减,3个加,4个减... elsedfs(i,0,0,1,0);}cout<<s<<endl;}return 0;}
0 0
- zoj 2836 Number Puzzle(容斥,求1到m有多少个数能被数组里任意一个整除)
- 求一个区间有多少个数能整除k
- [容斥原理] zoj 2836 Number Puzzle
- ZOJ 2836 Number Puzzle 容斥、lcm
- ZOJ 2836 Number Puzzle(容斥原理)
- ZOJ 2836Number Puzzle(容斥原理)
- ZOJ 2836 Number Puzzle (容斥)
- zoj 2836 Number Puzzle(容斥原理)
- ZOJ 2836 Number Puzzle(容斥原理啊)
- ZOJ 2836 Number Puzzle (容斥原理)
- zoj 2836 Number Puzzle(容斥原理)
- 容斥原理 —— 求1~n有多少个数与k互质(二进制算法详细解释&模板)
- 容斥原理 —— 求1~n有多少个数与k互质(二进制算法详细解释&模板)
- 容斥原理应用(求1~r中有多少个数与n互素)
- 容斥原理应用(求1~r中有多少个数与n互素)
- 容斥原理求1~r中有多少个数与n互素
- 【容斥原理】求1~n有多少个数与k互质
- HDU 1796 How many integers can you find(求1到n-1之间能被一个集合A内元素整除的数的个数)
- Qt Creator 库设置
- Xilinx FPGA 学习笔记——时钟资源
- 第七周项目一——图形用户界面(GUI)应用程序开发初体验
- Lesson: Terminate a telnet session
- LeetCode 203 Remove Linked List Elements
- zoj 2836 Number Puzzle(容斥,求1到m有多少个数能被数组里任意一个整除)
- Android Obb文件
- CentOS创建免密码SSH(密钥)
- HDU 4546 比赛难度
- 使用python编写opencv程序(安装及载入图片)
- 第八周——重载运算符——项目一(1)实现复数的+-*/
- JSP九大隐含对象之request和response
- 学经济学五年有感
- java.net.BindException: bind failed: EADDRINUSE (Address already in use)