ZOJ 2836 Number Puzzle

Number Puzzle

Time Limit: 2000ms

Memory Limit: 65536KB

This problem will be judged on ZJU. Original ID: 2836
64-bit integer IO format: %lld      Java class name: Main

Given a list of integers ($A_1, A_2, ..., A_n$), 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 \leq N \leq 10$) and $M (1 \leq M \leq 200000000)$, and the second line contains $A_1, A_2, ..., A_n(1 \leq A_i \leq 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

Source

Zhejiang University Local Contest 2007

Author

MAO, Yiqiang

解题:容斥,求是集合中某些数的LCM的倍数且不超过m的数的数量

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 20;
 5 int a[maxn],n,m;
 6 LL gcd(LL a,LL b) {
 7     return b?gcd(b,a%b):a;
 8 }
 9 LL LCM(LL a,LL b) {
10     return a/gcd(a,b)*b;
11 }
12 int main() {
13     while(~scanf("%d%d",&n,&m)) {
14         for(int i = 0; i < n; ++i)
15             scanf("%d",a+i);
16         LL ret = 0;
17         for(int i = 1; i < (1<<n); ++i) {
18             int cnt = 0;
19             LL x = 1;
20             for(int j = 0; j < n; ++j) {
21                 if((i>>j)&1) {
22                     x = LCM(x,a[j]);
23                     cnt++;
24                 }
25             }
26             if(cnt&1) ret += m/x;
27             else ret -= m/x;
28         }
29         printf("%lld\n",ret);
30     }
31     return 0;
32 }

时间: 2024-12-20 13:19:07

ZOJ 2836 Number Puzzle的相关文章

ZOJ 2836 Number Puzzle ( 容斥原理 )

ZOJ 2836 Number Puzzle( 容斥原理 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define CLR( a, b ) memset( a, b, sizeof(a) ) int m, n, A[11]; LL gcd( LL a, LL b ) { return b == 0 ? a :

[容斥原理] zoj 2836 Number Puzzle

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836 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 th

ZOJ 2836 Number Puzzle(容斥原理啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836 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 g

ZOJ 3814 Sawtooth Puzzle(牡丹江网络赛F题)

ZOJ 3814 Sawtooth Puzzle 题目链接 记录状态广搜,把9个拼图都压缩成一个状态,然后去搜索,就是模拟的过程比较麻烦 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include <set> using namespace std; typedef unsigned long long ll; int t; int

ACM HDU 1755 -- A Number Puzzle

A Number Puzzle Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 938    Accepted Submission(s): 276 Problem Description Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间.这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写

zoj 3814 Sawtooth Puzzle(隐式图搜索)

题目链接:zoj 3814 Sawtooth Puzzle 题目大意:给定一个9宫拼图,每次可以挑选一个位置顺时针旋转,和普通拼图不一样的是每块拼图周围可能有齿转动一个可能导致全部拼图转变. 解题思路:隐式图搜索,9块拼图最多49个状态,对于每个状态枚举转动的位置,考虑转动的状态.一开始转移是用bfs写的,但是由于频繁申请队列,然后时间爆了 #include <cstdio> #include <cstring> #include <queue> #include &l

ZOJ 3435 Ideal Puzzle Bobble

ZOJ Problem Set - 3435 Ideal Puzzle Bobble Time Limit: 2 Seconds      Memory Limit: 65536 KB Have you ever played Puzzle Bobble, a very famous PC game? In this game, as a very cute bobble dragon, you must keep shooting powerful bubbles to crush all t

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 Th

ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F

Number Game Time Limit: 2 Seconds      Memory Limit: 65536 KB The bored Bob is playing a number game. In the beginning, there are n numbers. For each turn, Bob will take out two numbers from the remaining numbers, and get the product of them. There i