hdu 5514
题意:有n个青蛙编号1~n,m块石头编号0~m-1,石头围成一圈,每只青蛙会从第
i
块跳到第
思路:对所有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;
}