Submission #43699

# Submission time Handle Problem Language Result Execution time Memory
43699 2018-03-20T05:51:39 Z szawinis Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
6 ms 876 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll a, b, dp[20][11][11][11];
vector<ll> get_digits(ll k) {
	vector<ll> ret;
	ll len = log10(k) + 1;
	for(int i = 0; i < len; i++) {
		ret.push_back(k % 10);
		k /= 10;
	}
	reverse(ret.begin(), ret.end());
	return ret;
}
ll pf_leq(ll k) {
	if(k <= 9) return k+1;
	ll len = log10(k)+1;
	vector<ll> digits = get_digits(k);

	memset(dp, 0, sizeof(dp));
	for(int p0 = 0; p0 <= 9; p0++) dp[len-1][p0][10][10] = 1;
	for(int i = len-2; i >= 0; i--) {
		for(int p0 = 0; p0 <= 9; p0++) {
			for(int p1 = 0; p1 <= 9; p1++) if(p0 != p1) {
				if(i+2 < len) {
					for(int p2 = 0; p2 <= 9; p2++) if(p2 != p1 && p2 != p0) {
						if(i+3 < len) for(int p3 = 0; p3 <= 9; p3++) {
							dp[i][p0][p1][p2] += dp[i+1][p1][p2][p3];
						} else {
							dp[i][p0][p1][p2] += dp[i+1][p1][p2][10];
						}
						// cout << i << ' ' << p0 << ' ' << p1 << ' ' << p2 << ' ' << dp[i][p0][p1][p2] << endl;
					}
				} else {
					dp[i][p0][p1][10] += dp[i+1][p1][10][10];
				}
			}
		}
	}

	ll ret = 0;
	// cout << digits[0] << endl;
	for(int i = 0; i < len; i++) {
		if((i >= 2 && digits[i-1] == digits[i-2]) || (i >= 3 && digits[i-1] == digits[i-3])) break;
		for(int p0 = (i == 0); p0 <= (i == len-1 ? digits[i] : digits[i]-1); p0++) if((i == 0 || p0 != digits[i-1]) && (i <= 1 || p0 != digits[i-2])) {
			for(int p1 = 0; p1 <= 10; p1++) if(p1 != p0 && (i == 0 || p1 != digits[i-1])) {
				for(int p2 = 0; p2 <= 10; p2++) if((i == len-1 || p2 != p1) && p2 != p0) {
					ret += dp[i][p0][p1][p2];
					// cout << i << ' ' << p0 << ' ' << p1 << ' ' << p2 << ' ' << dp[i][p0][p1][p2] << endl;
				}
			}
		}
	}
	return ret;
}
int main() {
	cin >> a >> b;
	ll pa = log10(a-1), pb = log10(b);
	ll curra = 1, currb = 1;
	ll resa = 0, resb = 0;
	for(int i = 1; i <= pa; i++) {
		curra *= 10;
		resa += pf_leq(curra-1);
	}
	for(int i = 1; i <= pb; i++) {
		currb *= 10;
		resb += pf_leq(currb-1);
	}
	resa += pf_leq(a-1);
	resb += pf_leq(b);
	cout << resb - resa;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 4 ms 664 KB Output is correct
4 Correct 2 ms 664 KB Output is correct
5 Correct 2 ms 664 KB Output is correct
6 Correct 2 ms 664 KB Output is correct
7 Correct 2 ms 712 KB Output is correct
8 Correct 2 ms 732 KB Output is correct
9 Correct 2 ms 732 KB Output is correct
10 Correct 2 ms 732 KB Output is correct
11 Correct 2 ms 748 KB Output is correct
12 Correct 2 ms 804 KB Output is correct
13 Correct 2 ms 804 KB Output is correct
14 Correct 2 ms 804 KB Output is correct
15 Correct 2 ms 804 KB Output is correct
16 Correct 2 ms 804 KB Output is correct
17 Correct 2 ms 804 KB Output is correct
18 Correct 1 ms 804 KB Output is correct
19 Correct 5 ms 804 KB Output is correct
20 Correct 2 ms 804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 804 KB Output is correct
2 Correct 5 ms 804 KB Output is correct
3 Correct 5 ms 804 KB Output is correct
4 Correct 6 ms 804 KB Output is correct
5 Correct 2 ms 804 KB Output is correct
6 Correct 2 ms 804 KB Output is correct
7 Correct 2 ms 804 KB Output is correct
8 Correct 2 ms 804 KB Output is correct
9 Correct 2 ms 804 KB Output is correct
10 Correct 2 ms 804 KB Output is correct
11 Correct 3 ms 804 KB Output is correct
12 Correct 2 ms 804 KB Output is correct
13 Correct 2 ms 804 KB Output is correct
14 Correct 2 ms 804 KB Output is correct
15 Correct 2 ms 804 KB Output is correct
16 Correct 4 ms 804 KB Output is correct
17 Correct 4 ms 804 KB Output is correct
18 Correct 4 ms 812 KB Output is correct
19 Correct 4 ms 812 KB Output is correct
20 Correct 5 ms 812 KB Output is correct
21 Correct 5 ms 812 KB Output is correct
22 Correct 4 ms 812 KB Output is correct
23 Correct 4 ms 812 KB Output is correct
24 Correct 4 ms 812 KB Output is correct
25 Correct 4 ms 812 KB Output is correct
26 Correct 5 ms 812 KB Output is correct
27 Correct 5 ms 812 KB Output is correct
28 Correct 5 ms 812 KB Output is correct
29 Correct 5 ms 812 KB Output is correct
30 Correct 4 ms 812 KB Output is correct
31 Correct 4 ms 812 KB Output is correct
32 Correct 4 ms 812 KB Output is correct
33 Correct 4 ms 876 KB Output is correct
34 Correct 4 ms 876 KB Output is correct
35 Correct 4 ms 876 KB Output is correct
36 Correct 5 ms 876 KB Output is correct
37 Correct 4 ms 876 KB Output is correct
38 Correct 6 ms 876 KB Output is correct
39 Correct 6 ms 876 KB Output is correct
40 Correct 6 ms 876 KB Output is correct
41 Correct 4 ms 876 KB Output is correct
42 Correct 5 ms 876 KB Output is correct
43 Correct 4 ms 876 KB Output is correct
44 Correct 4 ms 876 KB Output is correct
45 Correct 5 ms 876 KB Output is correct