SPOJ 6285. Another Game With Numbers

http://www.spoj.com/problems/NGM2/

容斥原理 第四题  水!  

难题各种各样,水题都一个样,无语。

代码:

#include<iostream>

#include<cstring>

#include<cstdio>

#include<string>

#include<set>

#include<map>

#include<cmath>

#include<algorithm>

#include<vector>

#include<cmath>

#include<queue>

#include<stack>



//#define ull unsigned long long

#define ll long long



using namespace std;



const int INF=0x3f3f3f3f;

const int MOD=1000000007;

const ll LMOD=1000000007;

const double eps=1e-6;

const int N=1050;

ll a[N];

ll gcd(ll x,ll y)

{

    if(x%y==0)

    return y;

    return gcd(y,x%y);

}

ll lcm(ll x,ll y)

{

    return x*y/gcd(x,y);

}

ll solve(ll n,ll *a,int m)

{

    ll sum=0;

    for(int k=1;k<(1<<m);++k)

    {

        int num=0;

        ll LCM=1;

        for(int i=0;i<m;++i)

        {

            if((k&(1<<i))>0)

            {

                ++num;

                LCM=lcm(LCM,(ll)a[i]);

                if(LCM>n)

                break;

            }

        }

        if(LCM>n)

        continue;



        if((num&1)>0)

        {

            sum+=(n/LCM);

        }else

        {

            sum-=(n/LCM);

        }

    }

    return sum;

}

int main()

{

    ll n;

    int k;

    while(cin>>n>>k)

    {

        for(int i=0;i<k;++i)

        cin>>a[i];

        cout<<(n-solve(n,a,k))<<endl;

    }

    return 0;



}

  

你可能感兴趣的:(number)