2017年10月19日提高组 整除
Description
给出n个数a1,a2……an,求区间[L,R]中有多少个整数不能被其中任何一个数整除。
Input
第一行三个正整数,n,L,R。
第二行n个正整数a1,a2……an
Output
一个数,即区间[L,R]中有多少个整数不能被其中任何一个数整除。
分析:容斥原理。
代码
#include <cstdio>
#define maxn 20
#define ll long long
using namespace std;
int a[maxn],n,l,r;
ll ans;
ll gcd(ll x,ll y)
{
return y==0?x:gcd(y,x%y);
}
ll lcm(ll x,ll y)
{
return (ll)x*(ll)y/(ll)gcd(x,y);
}
void dfs(int dep,int x,ll lc)
{
if (dep>n)
{
if (x%2==1) ans-=(ll)r/lc-(ll)(l-1)/lc;
else ans+=(ll)r/lc-(ll)(l-1)/lc;
return;
}
dfs(dep+1,x+1,lcm(lc,a[dep]));
dfs(dep+1,x,lc);
}
int main()
{
scanf("%d%d%d",&n,&l,&r);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
dfs(1,0,1);
printf("%lld",ans);
}