题解:P7360 「JZOI-1」红包 / ARIS0_0 - 51
SentencedFate · · 题解
Min-Max 容斥是什么。我怎么什么都不懂。
设
为了避免过多的东西跑到指数上去看着难受,设
推式子:
现在问题转化到了:对于给定的
先考虑:对于给定的
如果每组询问直接枚举
注意到,
一个比较有趣的事实是,笔者做过的整除分块和莫反后整除分块题,
写代码的时候发现不对劲了。如果直接整除分块的话,差分这个东西怎么处理呢。对于
然后发现是我饭堂了。对于
#include <cstdio>
#include <vector>
namespace FastIn
{
const int S = 4e6 + 7;
char *p1, *p2, buf[S];
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, S, stdin), p1 == p2) ? EOF : *p1++)
inline void read(int &x)
{
x = 0;
char c = gc();
while (c < '0' || c > '9')
c = gc();
for (; c >= '0' && c <= '9'; c = gc())
x = (x << 3) + (x << 1) + (c & 15);
}
inline void readstr(char *s)
{
int l = 0; char c = gc();
while (c < '0' || c > '9')
c = gc();
for (; c >= '0' && c <= '9'; c = gc())
s[++l] = c;
s[++l] = 0;
}
}
using FastIn::read;
using FastIn::readstr;
const int MAXN = 1e6 + 7, mod = 998244353, phimod = 998244352, phiphimod = 402653184;
inline int qpow(int a, int b, const int &p)
{
int res = 1;
for (; b; a = (long long)(a) * a % p, b >>= 1)
(b & 1) && (res = (long long)(res) * a % p);
return res;
}
int T, n = 1000000, ans, mnp[MAXN], c[MAXN], invc[MAXN];
long long K;
char strK[107];
std::vector<int> p;
inline void sieve()
{
for (int i = 2, ij; i <= n; ++i)
{
!mnp[i] && (p.push_back(mnp[i] = i), true);
for (const int &j : p)
if ((ij = i * j) > n)
break;
else if (j == mnp[i])
{
mnp[ij] = j;
break;
}
else
mnp[ij] = j;
}
}
inline void prework()
{
sieve(), c[0] = invc[0] = 1;
for (int i = 1; i <= n; ++i)
c[i] = mnp[i] == i ? mnp[i] : 1, c[i] = (long long)(c[i - 1]) * c[i] % mod;
invc[n] = qpow(c[n], mod - 2, mod);
for (int i = n - 1; i; --i)
invc[i] = (long long)(invc[i + 1]) * (mnp[i + 1] == i + 1 ? mnp[i + 1] : 1) % mod;
}
inline void solve()
{
read(n), readstr(strK), K = 0, ans = 1;
for (int i = 1; strK[i]; ++i)
K = (K << 3) + (K << 1) + (strK[i] & 15), K >= phiphimod && (K = K % phiphimod + phiphimod);
int pi = 0;
for (const int &pr : p)
{
if (pr * pr > n)
break;
++pi; int k = 1, exps = 0, lst = 0, cur = 0;
for (int prk = pr; prk <= n; ++k, prk *= pr, lst = cur)
cur = qpow(n - n / prk, K, phimod),
exps = (exps + (long long)(cur - lst) * (k - 1)) % phimod + phimod,
exps >= phimod && (exps -= phimod);
lst = cur, cur = qpow(n, K, phimod),
exps = (exps + (long long)(cur - lst) * (k - 1)) % phimod + phimod,
exps >= phimod && (exps -= phimod),
ans = (long long)(ans) * qpow(pr, exps, mod) % mod;
}
int nK = qpow(n, K, phimod);
for (int l = p[pi], r = p[pi]; l <= n && r <= n; l = r + 1)
r = (n / (n / l)),
ans = (long long)(ans) * qpow(
(long long)(c[r]) * invc[l - 1] % mod,
(nK + phimod - qpow(n - n / l, K, phimod)) % phimod,
mod
) % mod;
printf("%d\n", ans);
}
int main()
{
for (prework(), read(T); T; --T)
solve();
return 0;
}
// 指针偏过半百的回目,定命滑向残篇的序章
// 51