hdu 5514 容斥

hdu 5514
题意:有n个青蛙编号1~n,m块石头编号0~m-1,石头围成一圈,每只青蛙会从第 i 块跳到第(i + ai) mod m块,青蛙会跳无数次,问被青蛙踩过的石头编号的和是多少。
思路:对所有ai求gcd(ai, m)得bi,对于每个bi,编号为bi的倍数的石头一定也被踩过,但是会被重复踩。
求m的所有因子f,这些bi必定在f中,f中的数有青蛙踩过一次的,也有踩过多次的,我们只希望它们被踩一次。
所以设定f中的数要恰好被踩一次,将f中的数排序然后依次覆盖它的倍数,f中不是素因子的数就会被它的因子重复覆盖,实际上这就是一个容斥的过程。

/****************************************************
  >Created Date: 2015-11-04-12.38.37
  >My Soul, Your Beats!
****************************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef long long LL;

const int INF = 1 << 30;
const long long LINF = 1LL << 50;
const int MAXM = 1e5 + 5;
const int MAXN = 1e5 + 5;
const double PI = acos(-1.0);
const double eps = 1e-6;
int n, m;
int gcd(int a, int b){
    return a % b == 0 ? b : gcd(b, a % b);
}
int cnt[MAXM], factors[MAXM];
bool vis[MAXM];
int main(){
    #ifndef ONLINE_JUDGE
    //freopen("in.in", "r", stdin);
    #endif
    int t;
    scanf("%d", &t);
    for(int cas = 1; cas <= t; cas++){
        scanf("%d %d", &n, &m);
        LL tmp = sqrt(m), k = 0;
        for(int i = 1; i <= tmp; i++) {
            if(m % i == 0){
                factors[k++] = i;
                if(i * i != m) factors[k++] = m / i;
            }
        }
        sort(factors, factors + k);
        k--;
        memset(cnt, 0, sizeof cnt);
        memset(vis, 0, sizeof vis);
        for(int i = 0; i < n; i++){
            LL a;
            scanf("%I64d", &a);
            tmp = gcd(a, m);
            for(int j = 0; j < k; j++)
                if(factors[j] % tmp == 0)
                    vis[j] = true;
        }
        LL ans = 0;
        for(int i = 0; i < k; i++){
            if(vis[i] != cnt[i]){
                tmp = m / factors[i];
                ans += tmp * (tmp - 1) / 2 * factors[i] * (vis[i] - cnt[i]);
                tmp = vis[i] - cnt[i];
                for(int j = i; j < k; j++){
                    if(factors[j] % factors[i] == 0)
                        cnt[j] += tmp;
                }
            }
        }
        printf("Case #%d: %I64d\n", cas, ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值