HDU Frogs 5514 容斥

There are  m m stones lying on a circle, and  n n frogs are jumping over them. 
The stones are numbered from  0 0 to  m1 m−1 and the frogs are numbered from  1 1 to  n n. The  i i-th frog can jump over exactly  ai ai stones in a single step, which means from stone  j mod m j 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  0 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 20), and the first line contains an integer  t t
meaning the total number of test cases. 

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

The second line contains  n n integers  a1,a2,,an a1,a2,⋯,an, where  ai ai denotes step length of the  i i-th frog  (1ai109) (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


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
#define maxn 10005
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
///每个青蛙,可以跳到gcd(m,a[i])*k的位置
int ppp[maxn];///存因子
int num[maxn];///存访问几次
int vis[maxn];///存是否访问
int main()
{
    int tt;
    scanf("%d",&tt);
    for(int cas=1;cas<=tt;cas++)
    {
        int n,m;
        int cnt = 0;
        memset(vis,0,sizeof(vis));
        memset(num,0,sizeof(num));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=sqrt(m);i++)///把因子全部筛出来
        {
            if(m%i==0)
            {
                ppp[cnt++]=i;
                if(i*i!=m)
                    ppp[cnt++]=m/i;
            }
        }
        sort(ppp,ppp+cnt);
        for(int i=0;i<n;i++)
        {
            int x;
            scanf("%d",&x);
            int kk = gcd(x,m);
            for(int j=0;j<cnt;j++)
                if(ppp[j]%kk==0)///只要这个因子是GCD的倍数,都是可以被跳到的位置
                    vis[j]=1;
        }
        vis[cnt-1]=0;///最大那个因子是m, m是不可能被跳到的
        long long ans = 0;
        for(int i = 0; i < cnt; i++)
        {
            if(vis[i] != num[i])
            {
                int t = (m-1)/ppp[i];
                ans += (long long)t*(t+1)/2 * ppp[i] * (vis[i]-num[i]);
                //容斥一波
                //一开始vis[i] - num[i] = 1的
                //对于每个因数,如果重复计算了,在之后,减去就好了
                    int temp=vis[i] - num[i];
                for(int j = i; j < cnt; j++)
                    if(ppp[j]%ppp[i] == 0)
                        num[j] +=temp ;
            }
        }
        printf("Case #%d: %lld\n",cas,ans);
    }
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值