【容斥好题】HDU - 5514 F - Frogs

本文探讨了HDU-5514青蛙跳石子问题,详细解释了如何计算所有被青蛙至少踩过一次的石子序号之和。通过分析青蛙跳跃规律,利用数学方法求解,并提供了一段C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F - Frogs  HDU - 5514

There are mm stones lying on a circle, and nn frogs are jumping over them. 
The stones are numbered from 00 to m−1m−1 and the frogs are numbered from 11 to nn. The ii-th frog can jump over exactly aiai stones in a single step, which means from stone j mod mj mod m to stone (j+ai) mod m(j+ai) mod m (since all stones lie on a circle). 

All frogs start their jump at stone 00, 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 2020), and the first line contains an integer tt, 
meaning the total number of test cases. 

For each test case, the first line contains two positive integer nn and mm - the number of frogs and stones respectively (1≤n≤104, 1≤m≤109)(1≤n≤104, 1≤m≤109). 

The second line contains nn integers a1,a2,⋯,ana1,a2,⋯,an, where aiai denotes step length of the ii-th frog (1≤ai≤109)(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

题意:有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过xi个石子。

           问所有被至少踩过一次的石子的序号之和。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
ll fac[maxn],vis[maxn],num[maxn];

int main()
{
    int T,cas=0;
    scanf("%d",&T);
    while(T--)
    {
        ll ans=0,m,x;
        int n,tot=0;
        scanf("%d%lld",&n,&m);
        memset(vis,0,sizeof(vis)); //m的因数%a[i]==0
        memset(num,0,sizeof(num));
        for(ll i=1;i*i<=m;i++)
        {
            if(m%i==0)
            {
                fac[++tot]=i;
                if(i*i!=m)
                    fac[++tot]=m/i;
            }
        }
        sort(fac+1,fac+tot+1);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&x);
            ll p=__gcd(x,m);
            for(int j=1;j<tot;j++)
            {
                if(fac[j]%p==0)
                    vis[j]=1;
            }
        }

        for(int i=1;i<tot;i++)
        {
            if(num[i]!=vis[i])
            {
                ll t=(m-1)/fac[i];
                ans+=t*(t+1)/2*fac[i]*(vis[i]-num[i]); //重复加减掉
                for(int j=i+1;j<tot;j++)
                {
                    if(fac[j]%fac[i]==0)
                    {
                        num[j]+=vis[i]-num[i];  //哪些因数被重复加了几次
                    }
                }
            }
        }
        printf("Case #%d: %lld\n",++cas,ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值