zoj 2836 Number Puzzle(容斥原理)
来源:互联网 发布:云和山的彼端 mac 编辑:程序博客网 时间:2024/05/22 10:57
题目链接:传送门
Number Puzzle
Time Limit: 2 Seconds Memory Limit: 65536 KB
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个数,问m中有多少个数(这个数是,能被n个数中任意一个数整除)。
思路:容斥原理,最简单的,适合新手。
#include<stdio.h>#include<string.h>int n,m,a[15];int sum;int gcd(int x,int y){ if(y) return gcd(y,x%y); return x;}int lcm(int x,int y){ return x*y/gcd(x,y);}void dfs(int x,int k,int f){ sum+=k*m/f; for(int i=x;i<n;i++) { dfs(i+1,-k,lcm(a[i],f)); }}int main(){ while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;i++) scanf("%d",&a[i]); sum=m; dfs(0,-1,1); printf("%d\n",sum); } return 0;}
阅读全文
0 0
- [容斥原理] zoj 2836 Number Puzzle
- 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 容斥、lcm
- ZOJ 2836 Number Puzzle (容斥)
- ZOJ2836---Number Puzzle(容斥原理)
- ZOJ2836 Number Puzzle【容斥原理】
- ZOJ 2836 Number Puzzle
- ZOJ 3233 Lucky Number(容斥原理)
- 【ZOJ】3233【容斥原理】【Lucky Number】
- zoj 3233 Lucky Number(容斥原理)
- zoj 2836 Number Puzzle(容斥,求1到m有多少个数能被数组里任意一个整除)
- ZOJ 2836 容斥原理
- ZOJ 3233 Lucky Number(数论,容斥原理)
- 利用外键关系实现多表关联更新
- 30岁还一事无成,你该怎么办?
- yii excle导出 项目实战
- 全面的freemarker教程
- 【HTCVR】VRTK插件案例分析之001~010
- zoj 2836 Number Puzzle(容斥原理)
- SQL常见死锁例子及分析
- HDU 6140 Hybrid Crystals(简单题 17多校第八场)
- 数据结构实验之排序五:归并求逆序数
- SPOJ CIRU(The area of the union of circles-圆的面积并)
- 电脑网页打不开怎么回事但qq能用
- MySQL常见的错误类型
- java内存区域-方法区
- 桌面笔记工具KeepNote