# 九校联考（DL24凉心模拟） 整除（中国剩余定理＋原根性质）

## 题意简述

• 给定 $$c$$ 个不超过 $$t$$ 的质数 $$p_i(i \in [1, c])$$，有 $$n = \prod_\limits{i = 1}^c p_i$$

## 题解

$\left(\sum_{x = 1}^p[x ^ m \equiv x{\ \rm mod\ }p]\right) = {\rm gcd}(m - 1, p - 1) + 1$

$g^{my} \equiv g^y\ ({\rm mod}\ p)$

$my \equiv y\ ({\rm mod}\ p - 1) \Rightarrow (m - 1)y \equiv 0\ ({\rm mod}\ p - 1)$

$$k = \gcd(m - 1, p - 1)$$，两边同时除以 $$k$$，得：

$\frac{m - 1}{k}y \equiv 0\ ({\rm mod}\ \frac{p - 1}{k})$

## 代码

#include<bits/stdc++.h>

using namespace std;

#define rg register

const int e = 998244353, N = 1e4 + 10;

int mod, c, m;

inline void add(rg int& x, rg int y) {
x += y, x -= x >= mod ? mod : 0;
}

inline void mul(rg int& x, rg int y) {
x = 1ull * x * y % mod;
}

inline int qpow(rg int v, rg int p) {
rg int res = 1;
for (; p; p >>= 1, mul(v, v)) {
if (p & 1) {
mul(res, v);
}
}
return res;
}

inline int solve(rg int n) {
rg int p[N], f[N], pri[N], t;
mod = n, t = 0;
fill(p + 1, p + 1 + n, 1);
rg int res = 2;
for (rg int i = 2; i < n; ++i) {
if (p[i]) {
pri[++t] = i, f[i] = qpow(i, m);
}
res += (i == f[i]);
for (rg int j = 1, d; j <= t && (d = i * pri[j]) <= n; ++j) {
p[d] = 0;
f[d] = 1ull * f[i] * f[pri[j]] % mod;
if (i % pri[j] == 0) {
break;
}
}
}
return res;
}

int main() {
int T; scanf("%*d%d", &T);
for (rg int kase = 1; kase <= T; ++kase) {
scanf("%d%d", &c, &m);
rg int ans = 1;
for (rg int i = 1; i <= c; ++i) {
rg int x; scanf("%d", &x);
rg int v = solve(x);
mod = e, mul(ans, v);
}
printf("%d\n", ans);
}
return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define rg register

const int mod = 998244353;

inline void mul(int& x, int y) {
x = 1ll * x * y % mod;
}

int main() {
int T; scanf("%*d%d", &T);
for (rg int kase = 1; kase <= T; ++kase) {
int n, m; scanf("%d%d", &n, &m);
int ans = 1;
for (rg int i = 1; i <= n; ++i) {
int x; scanf("%d", &x);
mul(ans, __gcd(m - 1, x - 1) + 1);
}
printf("%d\n", ans);
}
return 0;
}

posted @ 2018-10-31 08:05  ImagineC  阅读(935)  评论(1编辑  收藏  举报