26/7/2 模拟赛 题解
\textit{\textbf{string}}
给定一个长度为
n 的字符串S ,你可以选择k 个位置,将这k 个位置替换成你想要的任何字符。你需要最大化替换之后,新的串
S 中最少出现两次的连续子串的长度(两次出现可以重合)。*同 [Baekjoon-14085](https://vjudge.net/problem/Baekjoon-14085#author=translator:1281309:zh)。*
下文的“匹配”都指可以通过合法的替换完美匹配。容易发现,对于可以匹配的两个子串
\textit{\textbf{alliance}}
同 UVA11998。
\textit{\textbf{clockin}}
由于某种原因,小 A 每天都需要在家打卡。规定的打卡时间有
n 个,从1 到n 编号。由于小 A 很咕,所以他只会选择其中m 个时间打卡。为了不被人怀疑,他需要保证所有打了卡的时间的编号之和模n 余k 。小 A 每天都想换一种姿势打卡,因此他想求出有多少种不同的打卡方案对
998244353 取模的结果。为了避免必将惨败,小 A 找到了你,你能帮帮他吗?
并非好题,但是从头到尾都很自然。
看到题目容易想到生成函数与单位根反演:
注意到对于任意
容易看出
适当预处理后直接计算即可,注意组合数计算需要适当打表。时空复杂度玄学,但是跑得飞快。
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define ll long long
#define ull unsigned long long
#define mod 998244353LL
#define decode(x) ((pfact[x * 5 - 5] - 32) * 81450625 + (pfact[x * 5 - 4] - 32) * 857375 + (pfact[x * 5 - 3] - 32) * 9025 + (pfact[x * 5 - 2] - 32) * 95 + pfact[x * 5 - 1] - 32)
using namespace std; using __gnu_pbds::gp_hash_table;
const string pfact = "(@\":#^o*hT\\\"]J9L'...FkUb% 3!h 5:to\"9 {Y!C:lm"; // 中间的 ... 表省略
int n, m, p, tp; ll ans; int cmus[100009];
vector <int> pfs, lfs; gp_hash_table <int, int> cid, tfact;
auto fastpow = [](ll a, ll b) -> ll {
ll ans = 1 % mod; a %= mod;
for(; b; b >>= 1, a = (a * a) % mod) if(b & 1) ans = (ans * a) % mod;
return ans;
};
inline int fact(int x) {
if(tfact.find(x) != tfact.end()) return tfact[x];
tfact[x] = (x / 100000) ? decode(x / 100000) : 1;
for(int i = (x / 100000) * 100000 + 1; i <= x; i++) { tfact[x] = ((ll)tfact[x] * i) % mod; }
return tfact[x];
}
inline int binom(int n, int m) { return (m == 0 || m == n) ? 1 : (fastpow((ll)fact(n - m) * fact(m), mod - 2) * fact(n)) % mod; }
ll fmta[1000009];
inline void fmt(int* a, int sz) { for(int i = 0; i < (1 << sz); i++) fmta[i] = a[i]; for(int i = 0; i < sz; i++) for(int j = 0; j < (1 << sz - i - 1); j++) for(int k = 0; k < (1 << i); k++) fmta[(j << i + 1) | k | (1 << i)] += fmta[(j << i + 1) | k]; for(int i = 0; i < (1 << sz); i++) a[i] = (fmta[i] % mod + mod) % mod; return; }
int main() {
freopen("clockin.in", "r", stdin);
freopen("clockin.out", "w", stdout);
scanf("%d %d %d", &n, &m, &p);
// n = 998244352, m = 499122176, p = 0;
ll limit = __gcd(n, m); tp = p = __gcd(limit, (ll)p); for(int i = 2; i * i <= tp; i++) while(tp % i == 0) { if(!pfs.size() || pfs[pfs.size() - 1] != i) pfs.push_back(i); tp /= i; }
if(tp != 1) pfs.push_back(tp);
cid[1] = 0, cmus[0] = 1; for(int i = 2, cnt = 0; i * i <= p; i++, cnt = 0) if(p % i == 0) for(int pf : pfs) { if(i % pf == 0) { cid[i] = (1 << cnt) | cid[i / pf]; if(cid[i] != cid[i / pf]) cmus[cid[i]] = fastpow(i, mod - 2); break; } cnt++; }
for(int i = floor(sqrt(p)), cnt = 0; i >= 1; i--, cnt = 0) if(p % i == 0 && i != p / i) for(int pf : pfs) { if((p / i) % pf == 0) { cid[p / i] = (1 << cnt) | cid[(p / i) / pf]; if(cid[p / i] != cid[(p / i) / pf]) cmus[cid[p / i]] = fastpow(p / i, mod - 2); break; } cnt++; }
for(int i = 0; i < (1 << pfs.size()); i++) cmus[i] = ((ll)cmus[i] * (__builtin_parity(i) ? mod - 1 : 1)) % mod; fmt(cmus, pfs.size());
ll tlimit = limit; for(int i = 2; i * i <= tlimit; i++) while(tlimit % i == 0) { if(!lfs.size() || lfs[lfs.size() - 1] != i) lfs.push_back(i); tlimit /= i; }
if(tlimit != 1) lfs.push_back(tlimit);
ans = (((ll)binom(n, m) * cmus[cid[1]]) % mod * (m & 1 ? mod - 1 : 1)) % mod;
int ptr = 2; for(int i = 2; i * i <= limit; i++) if(limit % i == 0) {
int tmp = __gcd(p, i), ttmp = tmp, mu = 1;
for(int j : lfs) if((i / tmp) % j == 0) { if((i / tmp) % ((ll)j * j) == 0) { mu = 0; break; } mu = mod - mu; while(ttmp % j == 0) ttmp /= j; }
if(mu) ans = (((((ll)binom(n / i, m / i) * cmus[cid[ttmp]]) % mod * (m / i & 1 ? mod - tmp : tmp)) % mod * mu) % mod + ans) % mod; ptr += 3;
if(i != limit / i) {
tmp = __gcd((ll)p, limit / i), ttmp = tmp, mu = 1;
for(int j : lfs) if(((limit / i) / tmp) % j == 0) { if(((limit / i) / tmp) % ((ll)j * j) == 0) { mu = 0; break; } mu = mod - mu; while(ttmp % j == 0) ttmp /= j; }
if(mu) ans = (((((ll)binom(n / (limit / i), m / (limit / i)) * cmus[cid[ttmp]]) % mod * (m / (limit / i) & 1 ? mod - tmp : tmp)) % mod * mu) % mod + ans) % mod; ptr += 3;
}
}
if(limit != 1) {
int tmp = p, mu = 1;
for(int j : lfs) if((limit / p) % j == 0) { if((limit / p) % ((ll)j * j) == 0) { mu = 0; break; } mu = mod - mu; while(tmp % j == 0) tmp /= j; }
if(mu) ans = (((((ll)binom(n / limit, m / limit) * cmus[cid[tmp]]) % mod * (m / limit & 1 ? mod - p : p)) % mod * mu) % mod + ans) % mod; ptr += 3;
}
ans = ((fastpow(n, mod - 2) * ans) % mod * (m & 1 ? mod - 1 : 1)) % mod;
printf("%lld\n", ans);
return 0;
}