hdu5514 Frogs(容斥)

来源:互联网 发布:网上荣誉室源码 编辑:程序博客网 时间:2024/04/30 12:21

Frogs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1463    Accepted Submission(s): 488


Problem Description
There are m stones lying on a circle, and n frogs are jumping over them.
The stones are numbered from 0 to m1 and the frogs are numbered from 1 to n. The i-th frog can jump over exactly ai stones in a single step, which means from stone j mod m to stone (j+ai) mod m (since all stones lie on a circle).

All frogs start their jump at stone 0, then each of them can jump as many steps as he wants. A frog will occupy a stone when he reach it, and he will keep jumping to occupy as much stones as possible. A stone is still considered ``occupied" after a frog jumped away.
They would like to know which stones can be occupied by at least one of them. Since there may be too many stones, the frogs only want to know the sum of those stones' identifiers.
 

Input
There are multiple test cases (no more than 20), and the first line contains an integer t,
meaning the total number of test cases.

For each test case, the first line contains two positive integer n and m - the number of frogs and stones respectively (1n104, 1m109).

The second line contains n integers a1,a2,,an, where ai denotes step length of the i-th frog (1ai109).
 

Output
For each test case, you should print first the identifier of the test case and then the sum of all occupied stones' identifiers.
 

Sample Input
32 129 103 6022 33 669 9681 40 48 32 64 16 96 42 72
 

Sample Output
Case #1: 42Case #2: 1170Case #3: 1872


题目大意:

m 个石子围成一圈,编号为 1m 。又有 n 只青蛙,每只青蛙的跳跃距离为 ai ,所有青蛙从 1 号石子起跳,路过的石子都打上标记(每个青蛙的标记无区别),问最后被打上标记的石子的标号和是多少。

思路:

枚举约数+容斥原理


代码如下:

#include<iostream>#include<cmath>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<bitset>#include<map>typedef long long LL;using namespace std;const int maxn=10005;LL fac[maxn],gc[maxn],num[maxn];map<LL,bool>vis;LL gcd(LL a,LL b){    return b==0?a:gcd(b,a%b);}LL getfac(LL n){    int cnt=0;    for(int i=1;i*i<=n;i++)    {    if(n%i==0)    {        fac[cnt++]=i;        if(i*i!=n)fac[cnt++]=n/i;    }    }    return cnt;//返回约数个数}LL getsum(int a1,int an,int cnt){    LL ans=(LL)(a1+an)*cnt/2;    return ans;}int main(){    int T,n,l,x,cas=1;    scanf("%d",&T);    while(T--)    {    scanf("%d%d",&n,&l);    vis.clear();    memset(num,0,sizeof(num));    int cnt=getfac(l);    int index=0;    for(int i=0;i<n;i++)    {        scanf("%d",&x);        int gcdxl=gcd(x,l);        if(vis[gcdxl])continue;        else  vis[gcdxl]=true,gc[index++]=gcdxl;    }    sort(fac,fac+cnt);    LL ans=0;    for(int i=0;i<index;i++)for(int j=0;j<cnt;j++)if(fac[j]%gc[i]==0)num[j]=1;    for(int i=0;i<cnt;i++)    {        if(num[i]==0)continue;               int step=(l-1)/fac[i];        LL k=getsum(fac[i],fac[i]+(step-1)*fac[i],step);        ans+=num[i]*k;//k为一个等差数列的前n项和        for(int j=i+1;j<cnt;j++)            {        if(fac[j]%fac[i]==0)        {            num[j]-=num[i];        }        }    }    printf("Case #%d: %lld\n",cas++,ans);    }}


0 0