Frogs(容斥+数论)

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

曾经天真地以为容斥就是二进制枚举啥的(?)(弱鸡←)。直到看了这题大神的操作才知道还有这种操作。
这个青蛙跳的石头编号是k*gcd(a[i],m),k是正整数。
gcd(a[i],m)一定是m的因子,然后就是先预处理出m的因子并排序。在读入时若fac[j]%gcd==0标记vis[j]=1表示第j个因子所代表的石头可以被跳到
num[i]代表的是实际走到该位置的次数并把它实时更新就可以了,一开始num[i]=0;先从小的因子开始,找在此次跳跃中跳到的位置,并且更新num[i],下次决定加上或减去的次数(即vis[i]-num[i])就可以了;

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#define maxn 10010
using namespace std;
typedef long long ll;
int n,m;
int gcd(int a,int b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}
int vis[maxn];
ll fac[maxn];
int num[maxn];
int cnt=0;
void init()
{
    cnt=0;
    for(int i=1;i*i<=m;i++)
    {
        if(m%i==0)
        {
            fac[cnt++]=i;
            if(i*i!=m)
                fac[cnt++]=m/i;
        }
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
}
int main()
{
    int t;
    int ca=1;
    scanf("%d",&t);
    ll a;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        int tmp;
        sort(fac,fac+cnt);
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a);
            tmp=gcd(a,m);
            for(int j=0;j<cnt;j++)
                if(fac[j]%tmp==0)
                    vis[j]=1;
        }
        vis[cnt-1]=0;
        ll ans=0;
        ll sans;
        for(int i=0;i<cnt;i++)
        {
            if(num[i]!=vis[i])
            {
                tmp=vis[i]-num[i];
                sans=m/fac[i];
                ans+=(ll)(sans-1)*sans/2*fac[i]*tmp;
                for(int j=i;j<cnt;j++)
                {
                    if(fac[j]%fac[i]==0)
                        num[j]+=tmp;
                }
            }
        }
        printf("Case #%d: %lld\n",ca++,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值