HDU 5514 Frogs (容斥定理)

题意:有n个青蛙在由m个石头组成的圆圈上跳。告诉你每个青蛙每次跳的步长,计算所有被青蛙跳过的石头的编号和。
解法:http://www.cnblogs.com/qscqesze/p/4933949.html


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define X first
#define Y second
#define cl(a,b) memset(a,b,sizeof(a))

typedef pair<int,int> P;
const int maxn=100005;
const LL inf=1<<27;
int f[maxn],g[maxn];
int p[maxn];
LL gcd(LL a,LL b){
    return b==0?a:gcd(b,a%b);
}
int main(){
     int T,cas=1;
     scanf("%d",&T);
     while(T--){
        LL n,m;
        scanf("%lld%lld",&n,&m);
        int cnt=0;
        for(int i=1;i*i<=m;i++){
            if(m%i==0){
                p[cnt++]=i;
                if(m/i!=i)p[cnt++]=m/i;
            }
        }
        sort(p,p+cnt);
        cl(f,0);
        cl(g,0);
        for(int i=0;i<n;i++){
            LL x;scanf("%lld",&x);
            LL t=gcd(x,m);
            for(int j=0;j<cnt;j++)if(p[j]%t==0){
                f[j]=1;
            }
        }
        f[cnt-1]=0;
        LL ans=0;
        for(int i=0;i<cnt;i++){
            if(f[i]!=g[i]){
                LL t=(m-1)/p[i];
                ans+=t*(t+1)/2*p[i]*(f[i]-g[i]);
                t=f[i]-g[i];
                for(int j=i;j<cnt;j++){
                    if(p[j]%p[i]==0){
                        g[j]+=t;
                    }
                }
            }
        }
        printf("Case #%d: %lld\n",cas++,ans);
     }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值