Submission #497316

# Submission time Handle Problem Language Result Execution time Memory
497316 2021-12-23T02:53:49 Z zhougz Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
1 ms 424 KB
/**
 *    author: zhougz
 *    created: 23/12/2021 10:31:38
**/
#include <bits/stdc++.h>

using namespace std;

long long memo[21][10][10][2];
int d[21];

long long dp(int len, int first, int second, int bounded) {
	if (len <= 2) {
		return 1;
	}
	long long &res = memo[len][first][second][bounded];
	if (res != -1) {
		return res;
	}
	res = 0;
	if (!bounded) {
		for (int i = 0; i <= 9; i++) {
			if (i == first || i == second) {
				continue;
			}
			res += dp(len - 1, second, i, false);
		}
		return res;
	}
	for (int i = 0; i <= d[len - 2]; i++) {
		if (i == first || i == second) {
			continue;
		}
		res += dp(len - 1, second, i, i == d[len - 2]);
	}
	return res;
}

long long slv(long long x) {
	memset(memo, -1, sizeof memo);
	memset(d, 0, sizeof d);
	int len = 0;
	do { // In case x == 0
		d[++len] = x % 10;
		x /= 10;
	} while (x != 0);
	long long res = 1;
	for (int i = 2; i <= len; i++) {
		for (int j = 1; j <= 9; j++) {
			res += dp(i, j, j, false);
		}
	}
	for (int j = 1; j <= d[len]; j++) {
		res += dp(len + 1, j, j, j == d[len]);
	}
	return res;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	long long a, b;
	cin >> a >> b;
	cout << slv(b) - (a != 0 ? slv(a - 1) : 0) << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 0 ms 312 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 0 ms 292 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 316 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 424 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 1 ms 316 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 0 ms 332 KB Output is correct
37 Correct 1 ms 316 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 0 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 320 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 0 ms 332 KB Output is correct