题解 P1822 【魔法指纹】
不明白为什么这题会被扔到“分块”里。
说一下我的做法(顺带一提,目前AC代码中除了打表的之外只有两个,另一个既看不明白有编译不过)。
首先写个暴力,可以发现
我们考虑如果有一个图,每个顶点有一个数,然后从
广搜每次拓展时我们需要从
不过要注意一点:由于求
代码:
#include <algorithm>
#include <cstdio>
typedef long long LL;
bool mm[10000050];
int p[10], A, B;
int queue[40000], num, head, tail;
void dfs(int x, LL y, int p10) {
if (y > B) return;
if (x == 0) {
int last = y / (p10 / 10);
if (!last) return;
dfs(x, y + (LL)last * p10, p10 * 10);
if (y >= A && y <= B) ++num;
if (p10 < B) queue[tail++] = y;
return;
}
int last = y / (p10 / 10), nxt = x % 10;
x /= 10;
if (last - nxt >= 0) dfs(x, y + p10 * (last - nxt), p10 * 10);
if (nxt && last + nxt < 10) dfs(x, y + p10 * (last + nxt), p10 * 10);
}
int main() {
scanf("%d%d", &A, &B);
head = tail = num = 0;
queue[tail++] = 7;
if (A <= 7 && B >= 7) ++num;
do
for (int i = 0; i < 10; ++i)
dfs(queue[head], i, 10);
while (++head < tail);
printf("%d\n", num);
return 0;
}