1. 程式人生 > >How many integers can you find HDU

How many integers can you find HDU

Code:

#include<cstdio>
using namespace std;
typedef long long ll;
const int R=13;
ll a[R];
ll n,ans;
int m,cnt=0;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
void dfs(int cur,ll lcm,int id){
    if(cur>cnt)return;
    lcm=a[cur]/gcd(a[cur],lcm)*lcm;

    if(id)ans+=(n-1)/lcm;
    else ans-=(n-
1)/lcm; for(int i=cur+1;i<=cnt;++i) dfs(i,lcm,!id); } int main(){ while(scanf("%lld%d",&n,&m)!=EOF) { ans=cnt=0; for(int i=1;i<=m;++i){ ll k; scanf("%lld",&k); if(k)a[++cnt]=k; } for(int i=1;i<=
cnt;++i) dfs(i,a[i],1); printf("%lld\n",ans); } return 0; }