Number Puzzle
Time Limit: 2 Seconds Memory Limit: 65536 KB
Given a list of integers (A, A, ..., A), 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 A, A, ..., A(1 <= A <= 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+;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}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=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,fac[];
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
rep(i,,n-)scanf("%d",&fac[i]);
int ret=;
rep(i,,(<<n)-)
{
int now=,cnt=;
rep(j,,n-)
{
if(i&(<<j))
{
cnt++;
now=now*fac[j]/gcd(now,fac[j]);
}
}
if(cnt&)ret+=m/now;
else ret-=m/now;
}
printf("%d\n",ret);
}
return ;
}