码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 2836 Number Puzzle

时间:2015-09-18 20:18:31      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:

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

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 }
View Code

 

ZOJ 2836 Number Puzzle

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4820218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!