HDU5514 Frogs (gcd + 容斥原理)

来源:互联网 发布:网络通信安全员证书 编辑:程序博客网 时间:2024/05/17 08:22

Frogs

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


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
 

Source
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
 

题意:有n个青蛙,m个石头,其中这m个石头围成一个圆,求所有青蛙走过的编号之和(相同的编号不能重复计算求和)

思路:对于第i个青蛙来说,这个青蛙一定会经过k*gcd(a[i],m) < m的所有点,证明如下:设该青蛙的位置为c,走了x圈,跳了y次,则可得一个方程:ay+c=mx;

=> mx - ay = c ,则可得c的最小取值为gcd(a,m),则gcd(a,m) | c, 设c=k * gcd(a,m) => mx - ay  = k * gcd(a,m) => ay % m = k * gcd (a,m) % m ; 所以可证第i个青蛙经过的点 为gcd(a[i],m)的所有倍数。又因为m较大,所以应先求出m的所有因子,第i个青蛙经过的石头一定是m的因子,最后通过容斥去掉重复即可,详细代码如下:



#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#define LL long longusing namespace std;const int maxn = 1e4 + 10;int t,n,m,cas=0,k;int vis[maxn],num[maxn],p[maxn];int gcd(int a, int b){    return b ? gcd(b,a%b) : a;}int main(){    scanf("%d",&t);    while(t --){        scanf("%d%d",&n,&m);        memset(vis,0,sizeof(vis));        memset(num,0,sizeof(num));        k = 0;        for(int i = 1; i <= sqrt(m); i ++){            if(m % i == 0){//计算出m的所有因子                p[k ++] = i;                if(i != m/i) p[k ++] = m/i;              }        }        int x;        sort(p,p+k);//把m的因子按照从小到大排序,最后一个元素一定是m        for(int i = 1; i <= n; i ++){            scanf("%d",&x);            int r = gcd(x,m);//求x和m的最大公约数            for(int j = 0; j < k; j ++){                if(p[j] % r == 0) vis[j] = 1;//如果m的因子对r取模为0,则证明该点可以被走到            }        }        LL ans = 0;        vis[k - 1] = 0;//m点不会被走到,应该点的编号是从0~m-1        for(int i = 0; i < k; i ++){            if(vis[i] != num[i]){//初始时vis[] - num[] = 1                int t = (m-1) / p[i];//计算有多少个p[i]的倍数                ans += (LL) t * (t+1) / 2 * p[i] * (vis[i] - num[i]);//t*(t+1)/2等差数列求和,后面再去掉重复的                t = vis[i] - num[i];//表示相应点的元素多算了几个                for(int j = i; j < k; j ++){                    if(p[j] % p[i] == 0) num[j] += t;//后面减掉多计算的数                }            }        }        printf("Case #%d: %lld\n",++cas,ans);    }    return 0;}


原创粉丝点击