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

分析:1~m中至少能被列表里其中一个数整除的数的个数;

   容斥即可,注意列表中的数不一定互质,所以要求lcm;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+10;
using namespace std;
inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int n,m,k,t,fac[10];
int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
        rep(i,0,n-1)scanf("%d",&fac[i]);
        int ret=0;
        rep(i,1,(1<<n)-1)
        {
            int now=1,cnt=0;
            rep(j,0,n-1)
            {
                if(i&(1<<j))
                {
                    cnt++;
                    now=now*fac[j]/gcd(now,fac[j]);
                }
            }
            if(cnt&1)ret+=m/now;
            else ret-=m/now;
        }
        printf("%d\n",ret);
    }
    return 0;
}
时间: 2024-08-07 00:12:27

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

Number Puzzle Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 283664-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 fi

[容斥原理] 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

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 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

[全排列]--A Number Puzzle

Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间. 这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写某个数字),然后,他叫同学随便写两个数字X和K.Lele要做的事情就是重新拼这些纸牌,组成数字 T ,并且 T + X 是 K 的正整数倍. 有时候,当纸片很多的时候,Lele经常不能在一节课之内拼出来,但是他又想知道答案,所以,他想请你帮忙写一个程序来计算答案. Input 本题目包含多组测试数据,请处理到文件结束. 每组数据第一行包含两个整数

E - Puzzle( UVA-227)

 Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, t

Puzzle UVA - 227

A children's puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, the frame

UVa 227 / UVALive 5166 Puzzle 谜题 (结构体)

Puzzle Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description   A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet