Simple Computing
Source : ACMGroup | |||
Time limit : 1 sec | Memory limit : 32 M |
Submitted : 364, Accepted : 114
Given n integers x1 x2 ... xn, you should count how many intergers from 1 to m that can be divided by at least one of them.
Input
The first line is an integer c which shows the number of cases. Each case contains 2 lines. The first line has 2 integers n and m. The second line contains n integers. (0<n<11, 0<m<2^31, 0<xi<100)
Output
For each case print the answer in a single line.
Sample Input
2 1 10 2 2 20 3 4
Sample Output
5 1
这题是我做过的题,今天无意看这个题,竟然没有思路,看来有必要贴出来, 有时间看看
用到容斥原理,


#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int f[15],n;
ll gcd(ll a,ll b)
{
ll r,mu=a*b;
while(a%b)
{
r=a;
a=b;
b=r%b;
}
return mu/b;
}
int main()
{
int ca;
ll m;
cin>>ca;
while(ca--)
{
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>f[i];
sort(f,f+n);
int cnt=1;
for(int i=1;i<n;i++)
if(f[i]!=f[i-1])
f[cnt++]=f[i];
n=cnt;
ll dd=1<<n;
ll sum=0;
for(ll i=1;i<dd;i++)
{
ll mult=1,bits=0;
for(int j=0;j<n;j++)
if(i&(1<<j))
{
mult=gcd(f[j],mult);bits++;
}
if(bits%2)
sum+=m/mult;
else
sum-=m/mult;
}
cout<< sum <<endl;
}
return 0;
}