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;

}

  

转载于:https://www.cnblogs.com/liulangye/archive/2013/05/30/3107461.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值