[洛谷P4345][SHOI2015]超能粒子炮·改
题目大意:给你$n,k$,求:
$$
\sum\limits_{i=0}^k\binom n i\pmod{2333}
$$
题解:令$p=2333,f(n,k)\equiv\sum\limits_{i=0}^k\binom n i\pmod p$
$$
\begin{align*}
f(n,k)\equiv&\sum\limits_{i=0}^k\binom n i\pmod p\\ \equiv&\sum\limits_{i=0}^k\binom{\big\lfloor\frac np\big\rfloor}{\big\lfloor\frac ip\big\rfloor}\binom{n\bmod p}{i\bmod p}\pmod p\\
\end{align*}\\
令s=\left\lfloor\dfrac k p\right\rfloor
$$
$$
\begin{align*}
f(n,k)\equiv&[\sum\limits_{i=0}^{p-1}\binom{n\bmod p}{i}][\sum\limits_{i=0}^{s-1}\binom{\big\lfloor\frac n p\big\rfloor}{i}]\\
&+\binom{\left\lfloor\frac np\right\rfloor}{s}\sum\limits_{i=sp}^k\binom{n\bmod p}{i\bmod p}\pmod p\\
\equiv&[\sum\limits_{i=0}^{p-1}\binom{n\bmod p}{i}][\sum\limits_{i=0}^{s-1}\binom{\big\lfloor\frac n p\big\rfloor}{i}]\\
&+\binom{\left\lfloor\frac np\right\rfloor}{s}\sum\limits_{i=0}^{k\bmod p}\binom{n\bmod p}{i}\pmod p\\
\equiv&f(n\bmod p, p-1)f(\left\lfloor\dfrac np\right\rfloor,s-1)\\
&+\binom{\big\lfloor\frac np\big\rfloor}{s}f(n\bmod p,k\bmod p)\pmod p\\
\end{align*}
$$
卡点:未注意$n,k\leqslant10^{18}$
C++ Code:
#include <cstdio> const int mod = 2333; #define maxn mod inline void reduce(int &x) { x += x >> 31 & mod; } int Tim; long long n, k; int com[maxn][maxn], pre[maxn][maxn]; int C(long long a, long long b) { if (a < b) return 0; if (a < mod) return com[a][b]; return com[a % mod][b % mod] * C(a / mod, b / mod) % mod; } int solve(long long n, long long k) { if (k < 0) return 0; if (n < mod && k < mod) return pre[n][k]; const long long s = k / mod; return (pre[n % mod][mod - 1] * solve(n / mod, s - 1) + pre[n % mod][k % mod] * C(n / mod, s)) % mod; } int main() { scanf("%d", &Tim); for (int i = 0; i < mod; ++i) { *com[i] = *pre[i] = 1; for (int j = 1; j <= i; ++j) { reduce(com[i][j] = com[i - 1][j] + com[i - 1][j - 1] - mod); reduce(pre[i][j] = pre[i][j - 1] + com[i][j] - mod); } for (int j = i + 1; j < mod; ++j) reduce(pre[i][j] = pre[i][j - 1] + com[i][j] - mod); } while (Tim --> 0) { scanf("%lld%lld", &n, &k); printf("%d\n", solve(n, k)); } return 0; }