HDU-5514

Frogs

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


 

Problem Description

There are m stones lying on a circle, and n frogs are jumping over them.
The stones are numbered from 0 to m−1 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 (1≤n≤104, 1≤m≤109) .

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

 

 

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

 

3

2 12

9 10

3 60

22 33 66

9 96

81 40 48 32 64 16 96 42 72

 

 

Sample Output

 

Case #1: 42

Case #2: 1170

Case #3: 1872

题意:n个青蛙在一个有m个节点的圆上跳,m个节点的标号为0-m-1,每只青蛙每次跳的节点数给出,让求n只青蛙所跳位置标号之和。分析:经过打表可以知道,青蛙在圆上跳的时候,节点的标号是gcd(a[i],m)的倍数,例如第一个样例:9:0,3,6,9;(3的倍数)10:0,2,4,6,8,10;(2的倍数)想到这又会出现一个问题:有些位置会重复计算,例如上面的6,所以这个时候还要加上容斥来解决。容斥过程如下:由于青蛙所走的位置是gcd(a[i],m)的倍数,并且还是一个等差数列,所以我们可以提前找出m的因子。用vis数组来标记m的每个因子所使用的次数(次数可以为正,也可为负,为正说明结果加上该因子所产生的贡献,为负说明结果要减去该因子所产生的贡献)。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>
using namespace std;
const int maxn=1e6+10;
typedef long long ll;
ll a[maxn];
ll G[maxn];
ll vis[maxn];
ll gcd(ll a,ll b)
{
    if(b==0)
        return a;
    else
        gcd(b,a%b);
}
int main()
{
    ll t;
    scanf("%lld",&t);
    for(ll cnt=1; cnt<=t; cnt++)
    {
        memset(vis,0,sizeof(vis));
        ll n,m;
        scanf("%lld%lld",&n,&m);
        ll idex=0;
        for(ll i=1; i*i<=m; i++)
        {
            if(m%i==0)
            {
                if(i*i!=m)
                    G[++idex]=i;
                G[++idex]=m/i;
            }
        }
        sort(G+1,G+1+idex);
        for(ll i=1; i<=n; i++)
        {
            scanf("%lld",&a[i]);
            ll res=gcd(a[i],m);
            for(ll j=1; j<=idex; j++)
            {
                if(G[j]%res==0)
                    vis[j]=1;     ///先在因子中标记参与产生贡献的因子
            }
        }
        ll ans=0;
        for(ll i=1; i<=idex; i++)
        {
            if(vis[i])
            {
                ll num=m/G[i];
                ans+=(0+(num-1)*G[i])*num/2*vis[i];
                for(ll j=i+1; j<=idex; j++)
                {
                    if(G[j]%G[i]==0)
                        vis[j]-=vis[i];///由于第i个因子产生的贡献已经算出,那么后面能够整除第i个因子的数要进行相应的变化,因为第i个因子所产生的贡献包含了后面可以整除该因子的数所产生的贡献。
                }
            }
        }
        printf("Case #%lld: %lld\n",cnt,ans);

    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值