hoj Simple Computing

Simple Computing

    (Edit)
 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 
这题是我做过的题,今天无意看这个题,竟然没有思路,看来有必要贴出来, 有时间看看
用到容斥原理,
View Code
#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;
}


转载于:https://www.cnblogs.com/one--world--one--dream/archive/2012/03/31/2426544.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值