HDU5514Frogs 【容斥定理】

本文针对一组青蛙在圆圈上的石头间跳跃的问题进行了解析。通过数学推导和算法设计,探讨了哪些石头能够被青蛙占据,并计算这些石头编号之和。采用容斥原理并结合预处理因子的方法来优化解决方案。

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

Frogs

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


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


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

明显青蛙i可以跳到位置pos满足:pos%gcd(a[i],m)==0
青蛙i条过的下标总和 可以直接用等差数列求出

显然 当
gcd(a[i],m)=x
gcd(a[j],m)=y
lcm(x,y)的倍数位置都被重复走了2次
到这里已经很明显要容斥了

然而二进制压缩跑一遍容斥,各种TLE,WA
赛后发现状态数比较多 ,1e9的因子就有100个了 long long的状态会被爆,就算换dfs也会T

如果预处理出m的所有因子factor,因为gcd(a[i],m)必然是m的因子
定义
used[i]=1 | m的第i个因子的倍数能被青蛙到达
used[i]=0 | 不能被青蛙到达

ans 就加上 used[i]*factor[i]
如果used[i]!=0
所有factor[j]%factor[i]==0的因子的都不需要再被累计
所以 used[j]-=used[i] | factor[j]%factor[i]==0


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include <string.h>
#include<math.h>

using namespace std;
#define ll long long
#define pii pair<int,int>

const int inf=1e9+7;
const int N = 1e4+5;

int a[N];
vector<int>factor;
int used[500];

int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}

ll f(ll a1,ll an,ll n){
    return (a1+an)*n/2;
}

ll slove(int m){
    ll ans=0;
    for(int i=0;i<factor.size();++i){
        int a=factor[i];
        if(used[i]){
            int num=m/a;
            ans+=used[i]*f(a,a*(num-1),num-1);
            for(int j=i+1;j<factor.size();++j){
                int b=factor[j];
                if(b%a==0){
                    used[j]-=used[i];
                }
            }
        }
    }
    return ans;
}

void init(int m){
    factor.clear();
    for(int i=1,end=sqrt(m+1);i<=end;++i){
        if(m%i==0){
            factor.push_back(i);
            if(i*i!=m){
                factor.push_back(m/i);
            }
        }
    }
    sort(factor.begin(),factor.end());
    fill(used,used+factor.size()+1,0);
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;++t){
        int n,m;
        scanf("%d%d",&n,&m);
        init(m);
        for(int i=1;i<=n;++i){
            scanf("%d",a+i);
            a[i]=gcd(a[i]%m,m);
            for(int j=0;j<factor.size();++j){
                if(factor[j]%a[i]==0){
                    used[j]=1;
                }
            }
        }
        ll ans=slove(m);
        printf("Case #%d: %lld\n",t,ans);

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值