概述
Description
求有多少
n
位十进制数是
Solution
考虑DP。
设
dpi,j,k
为考虑前
i
位,膜
那么就有这样的转移
dpi,j,k→dpi+1,(j+10x)modp,k+x
考虑倍增,从
dp⌊i2⌋
转移到
dpi
。
那么就有
dp⌊i2⌋,j,xdp⌊i2⌋,k,y→dpi,(j+10Bk)modp,x+y
这是个卷积的形式。倍增NTT。
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int M = 80010;
const int P = 55;
const int G = 3;
typedef long long ll;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
inline void read(int &x) {
static char c; x = 0;
for (c = get(); c < '0' || c > '9'; c = get());
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
int dp[P][M], dp1[P][M];
int n, in, m, p, L;
int w[2][M];
int Rev[M];
int g, invg, num, ans;
inline int Pow(int a, int b) {
int c = 1;
while (b) {
if (b & 1) c = (ll)c * a % MOD;
b >>= 1; a = (ll)a * a % MOD;
}
return c;
}
inline int Inv(int x) {
return Pow(x, MOD - 2);
}
inline int Mod(int x) {
while (x >= MOD) x -= MOD; return x;
}
inline void Add(int &x, int a) {
x += a; while (x >= MOD) x -= MOD;
}
void Prep(int n) {
g = Pow(G, (MOD - 1) / n);
invg = Inv(g); num = n;
w[0][0] = w[1][0] = 1;
for (int i = 1; i <= n; i++) {
w[0][i] = (ll)w[0][i - 1] * invg % MOD;
w[1][i] = (ll)w[1][i - 1] * g % MOD;
}
}
inline void FFT(int *a, int n, int r) {
int x, y;
for (int i = 0; i < n; i++)
if (Rev[i] > i) swap(a[i], a[Rev[i]]);
for (int i = 1; i < n; i <<= 1)
for (int j = 0; j < n; j += i << 1)
for (int k = 0; k < i; k++) {
x = a[j + k]; y = (ll)a[j + k + i] * w[r][num / (i << 1) * k] % MOD;
a[j + k] = Mod(x + y); a[j + k + i] = Mod(x - y + MOD);
}
if (!r) {
int INV = Inv(n);
for (int i = 0; i < n; i++)
a[i] = (ll)a[i] * INV % MOD;
}
}
inline int DP(int i) {
if (!i) return 1;
int B = DP(i >> 1);
for (int j = 0; j < p; j++) FFT(dp1[j], n, 1);
for (int j = 0; j < p; j++)
for (int k = 0; k < p; k++)
for (int x = 0; x < n; x++)
Add(dp[(j + k * B) % p][x], (ll)dp1[j][x] * dp1[k][x] % MOD);
for (int j = 0; j < p; j++)
for (int x = 0; x < n; x++) dp1[j][x] = 0;
for (int j = 0; j < p; j++) {
FFT(dp[j], n, 0);
for (int x = 0; x < m; x++) dp1[j][x] = dp[j][x];
for (int x = 0; x < n; x++) dp[j][x] = 0;
}
B = B * B % p;
if (i & 1) {
for (int j = 0; j < p; j++)
for (int k = 0; k < 10; k++)
for (int x = 0; x + k < m; x++)
Add(dp[(j + k * B) % p][k + x], dp1[j][x]);
for (int j = 0; j < p; j++)
for (int x = 0; x < m; x++) {
dp1[j][x] = dp[j][x];
dp[j][x] = 0;
}
B = B * 10 % p;
}
return B;
}
int a[M], b[M];
int main(void) {
read(in); read(p); read(m);
dp1[0][0] = 1; ++m;
for (n = 1; n <= m; n <<= 1) ++L;
n <<= 1;
for (int i = 1; i < n; i++)
Rev[i] = (Rev[i >> 1] >> 1) | ((i & 1) << L);
Prep(n << 1); DP(in);
for (int i = 0; i < m; i++) {
Add(ans, dp1[0][i]);
printf("%d ", ans);
}
putchar('n');
return 0;
}
最后
以上就是漂亮奇异果为你收集整理的[倍增NTT][DP] LOJ#6059. 「2017 山东一轮集训 Day1」Sum的全部内容,希望文章能够帮你解决[倍增NTT][DP] LOJ#6059. 「2017 山东一轮集训 Day1」Sum所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复