hdu5514 Frogs(容斥)

本文探讨了一群青蛙在一圈石子上跳跃并标记所经石子的问题,通过枚举约数与容斥原理求解被标记石子编号之和。介绍了问题背景、解题思路及C++实现代码。

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

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
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 个石子围成一圈,编号为 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);
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值