跳到正文

题解

题解 P3281 【[SCOI2013]数数 】

ViXbob约 9 分钟
原文
文章信息与版本

文章信息

发布
2018年12月21日
收录
2026年8月14日
更新
2026年11月13日
篇幅
约 9 分钟
获赞
31
题目
P3281

版本

  1. v1当前

博客推广

大家好像写的都是递推, 太强辣吧, 然而我很菜只会打记忆化

所以来贡献一个记忆化的打法

首先我们考虑只有一个数的时候怎么算贡献, 记 a[i]a[i] 表示这个数字 从高到低 的第 ii 位是什么, LL 表示这个数的长度, 那么这个数的贡献可以记为

S[n]=∑i=1nBi−1 S[n] = \sum _ {i = 1} ^ n B ^ {i - 1} W=∑i=1Li⋅a[i]⋅S[L−i+1] W = \sum _ {i = 1} ^ L i \cdot a[i] \cdot S[L - i + 1]

然后我们再考虑往这个数的最高位前面加一位数 kk 之后的贡献可以被表示为

W′=k⋅S[L+1]+W+∑i=1La[i]⋅S[L−i+1] W' = k \cdot S[L + 1] + W + \sum _ {i = 1} ^ L a[i] \cdot S[L - i + 1]

我们再设

V=∑i=1La[i]⋅S[L−i+1]V = \sum _ {i = 1} ^ L a[i] \cdot S[L - i + 1]

再考虑 dpdp, 我们想要求 [1,x][1, x] 中所有数的 WW 和, 所以对于一个 xx 我们设 f[i]f[i] 表示 [1,BL−i+1)[1, B ^ {L - i + 1}) 中所有的数的 W,VW, V 的和

转移就直接搞一搞就好了

但是注意到对一一个状态如果有前导零和没有前导零, 算出来的 WW 值是不一样的, 所以将状态改成 f[i][0/1]f[i][0/1] 就好了, 第二维表示有没有前导零

naivenaive 的我以为做到这里就做完了, 记搜的打法还是和以前的套路一样枚举这一位的数是什么, 但注意到 B,N≤105B , N\le 10^5 如果强行枚举选什么数的话, 最终的复杂度会退化到 O(BN)O(BN) 的, 一开始我还以为这是O(n), 我太naive了

考虑一个事实能影响你 dpdp 值的只有三种情况

  • 你前面选出来的数都是卡着上限的
  • 你选的数有前导零
  • otherwiseotherwise

如果对于当前位你可选的数在 [0,Lim][0, Lim] 范围内, 那么当前如果选 LimLim 可能 会满足第一种情况, 选 00 可能 会满足第二种情况, 否则都是第三种情况

那我们其实可以直接分开讨论一下对于三种不同的状态都快速转移一下, 就可以不用枚举这一位选的具体是什么数了, 复杂度成功降至了 O(N)O(N)

其实大部分的数位 dpdp 的记忆化打法都可以这样优化吧, 只是当进制是 1010 进制时, 优化的幅度并没有那么大, 因为就算不优化也只是多了个 1010 的常数, 但当进制数非常大时, 这个优化还是肥肠优秀的

代码

C++
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int P = 20130427;
const int maxn = 2e5 + 5;

int B, n, m, l[maxn], r[maxn], sum[maxn], a[maxn], len, sa[maxn], mi[maxn];
pair<int, int> f[maxn][2], em;

inline int read() {
	char ch = getchar(); int u = 0, f = 1;
	while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
	while(isdigit(ch))  {u = u * 10 + ch - 48; ch = getchar();} return u * f;
}

inline int pls(int x, int y) {x += y; return x >= P ? x - P : (x < 0 ? x + P : x);}
inline int mul(int x, int y) {LL rnt = 1ll * x * y; return rnt >= P ? rnt % P : rnt;}
inline int mul(int a, int b, int c) {return mul(a, mul(b, c));}
inline int pls(int a, int b, int c) {return pls(a, pls(b, c));}

inline int check(int *s, int n, int rnt = 0) {
	for(register int i = 1; i <= n; i++)
		rnt = pls(rnt, mul(i, s[i], sum[n - i + 1]));
	return rnt;
}

inline pair<int, int> dfs(int pos, bool lim, bool lead) {
	if(pos > len) return em;
	pair<int, int> &rnt = f[pos][lead], tmp;
	int Lim = lim ? a[pos] : (B - 1), S, l, r, i;
	bool is;
	if(!lim && rnt.first != -1) return rnt;
	rnt.first = rnt.second = 0;
	/*-------------------- i = 0 -----------------------*/
	i = 0;
	is = !lead || i != 0;
	S = (lim && (i == Lim)) ? (sa[pos + 1] + 1) : mi[len - pos + 1];
	tmp = dfs(pos + 1, lim && (i == Lim), lead && (i == 0));
	rnt.first = pls(rnt.first, tmp.first, is * tmp.second);
	rnt.first = pls(rnt.first, mul(i, sum[len - pos + 1], S));
	rnt.second = pls(rnt.second, tmp.second, mul(i, sum[len - pos + 1], S));
	/*-------------------- i > 0 -----------------------*/
	if(Lim > 1) {
		i = 1;
		is = !lead || i != 0;
		S = (lim && (i == Lim)) ? (sa[pos + 1] + 1) : mi[len - pos + 1];
		tmp = dfs(pos + 1, lim && (i == Lim), lead && (i == 0));
		l = i; r = Lim - 1;
		LL Len = r - l + 1;
		rnt.first = pls(rnt.first, mul(pls(tmp.first, tmp.second * is), Len));
		rnt.second = pls(rnt.second, mul(tmp.second, Len));
		Len = (l + r) * Len / 2;
		Len %= P; Len = mul(Len, S, sum[len - pos + 1]);
		rnt.first = pls(rnt.first, Len);
		rnt.second = pls(rnt.second, Len);
	}
	/*------------------- i = Lim ----------------------*/
	if(Lim > 0) {
		i = Lim;
		is = !lead || i != 0;
		S = (lim && (i == Lim)) ? (sa[pos + 1] + 1) : mi[len - pos + 1];
		tmp = dfs(pos + 1, lim && (i == Lim), lead && (i == 0));
		rnt.first = pls(rnt.first, tmp.first, is * tmp.second);
		rnt.first = pls(rnt.first, mul(i, sum[len - pos + 1], S));
		rnt.second = pls(rnt.second, tmp.second, mul(i, sum[len - pos + 1], S));
	} return rnt;
}

inline pair<int, int> calc(int *s, int n) {
	memset(f, 255, sizeof(f));
	memset(sa, 0, sizeof(sa));
	len = n;
	for(register int i = 1; i <= n; i++) a[i] = s[i];
	for(register int i = n; i >= 1; i--)
		sa[i] = pls(sa[i + 1], mul(mi[n - i + 1], a[i]));
	return dfs(1, 1, 1);
}

int main() {
	B = read(); n = read();
	for(register int i = 1; i <= n; i++) l[i] = read();
	m = read();
	for(register int i = 1; i <= m; i++) r[i] = read();
	//if(cmp(l, r, n, m) > 0) {cout << 0; return 0;}
	mi[1] = sum[1] = 1;
	for(register int i = 2; i < maxn; i++) mi[i] = mul(mi[i - 1], B);
	for(register int i = 2; i < maxn; i++) sum[i] = pls(mi[i], sum[i - 1]);
	cout << pls(calc(r, m).first, check(l, n) - calc(l, n).first);
	return 0;
}
/*
100801110

d[maxn]

L, pos, a[pos]
d[L - pos + 2] -= a[pos] * pos
d[1] += a[pos] * pos

ans

103
1 0 0 -1
1 0 0 -1
10 -9 0 -1
10 1 1
1 10 100 1000
1 11 111 1111
O(n)

1 10 100 1000
1103
1111 + 222 + 0 + 12
0000 + 111 + 0 + 09
-> 各个位上数的和
1103


*/
原文
存档评论18 条抓取于