链接:
hdu 1796 How many integers can you find
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 112;
ll a[maxn],num;
ll GCD(ll x,ll y)
{
return x%y==0?y:GCD(y,x%y);
}
ll LCM(ll x,ll y)
{
return x*y/GCD(x,y);
}
ll rongchi(ll n)
{
ll ans=0;
for(ll i=1;i<(1<<num);i++)
{
ll cnt=0,lcm=-1;
for(ll j=0;j<num;j++)
{
if(i&(1<<j))
{
cnt++;
if(lcm==-1) lcm=a[j];
else lcm=LCM(lcm,a[j]);
}
}
if(lcm==-1) continue;
if(cnt&1) ans+=(n-1)/lcm;
else ans-=(n-1)/lcm;
}
return ans;
}
int main()
{
ll n,m;