「SCOI2009」windy数

传送门
Luogu

解题思路

数位 \(\text{DP}\)
设状态 \(dp[now][las][0/1][0/1]\) 表示当前 \(\text{DP}\) 到第 \(i\) 位,前一个数是 \(las\),有没有顶到上界,有没有前导零的答案。
转移十分显然。

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <vector>
#define rg register
using namespace std;

template < typename T > inline void read(T& s) {
	s = 0; int f = 0; char c = getchar();
	while (!isdigit(c)) f |= c == '-', c = getchar();
	while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
	s = f ? -s : s;
}

const int _ = 11;

int a[_], dp[_][_];

inline int dfs(int now, int las, int lim, int zero) {
	if (now == 0) return 1;
	if (!lim && !zero && dp[now][las] != -1) return dp[now][las];
	int res = 0, tp = lim ? a[now] : 9;
	for (rg int j = 0; j <= tp; ++j)
		if (abs(j - las) >= 2) {
			int _lim = lim && j == tp;
			int _zero = zero && j == 0;
			int _las = _zero ? -2 : j;
			int _now = now - 1;
			res += dfs(_now, _las, _lim, _zero);
		}
	if (!lim && !zero) dp[now][las] = res;
	return res;
}

inline int solve(int x) {
	int n = 0;
	for (rg int i = x; i; i /= 10) a[++n] = i % 10;
	memset(dp, -1, sizeof dp);
	return dfs(n, -2, 1, 1);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	int l, r;
	read(l), read(r);
	printf("%d\n", solve(r) - solve(l - 1));
	return 0;
}

完结撒花 \(qwq\)

posted @ 2019-10-27 07:44  Sangber  阅读(126)  评论(0编辑  收藏  举报