[ZOJ 2836] Number Puzzle

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

入门容斥原理,见代码

#include <iostream>
#include <cstdio>
using namespace std;
#define ll long long
#define N 10

int n,m;
int a[N];
int gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
int lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
////写法1/////
int solve()
{
    int res=0;
    for(int i=1;i<(1<<n);i++)
    {
        int cnt=0;
        ll LCM=1;
        for(int j=0;j<n;j++)
        {
            if(i&(1<<j)) cnt++,LCM=lcm(LCM,a[j]);
        }
        if(cnt&1) res+=m/LCM;
        else res-=m/LCM;
    }
    return res;
}
////写法2/////
int ans;
void dfs(int pos,int LCM,int cnt)
{
    if(cnt)
    {
        if(cnt&1) ans+=m/LCM;
        else ans-=m/LCM;
    }
    for(int i=pos+1;i<n;i++)
        dfs(i,lcm(LCM,a[i]),cnt+1);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        //printf("%d\n",solve());
        ans=0;
        dfs(-1,1,0);
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2015-05-06 18:55  哈特13  阅读(188)  评论(0编辑  收藏  举报