Submission #116269

# Submission time Handle Problem Language Result Execution time Memory
116269 2019-06-11T22:01:08 Z ZwariowanyMarcin Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
3 ms 512 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>


#define pb push_back
#define ld long double
#define fi first
#define se second
#define ll long long
#define ss(x) (int) x.size()
#define mp make_pair
#define FOR(i, n) for(int i = 1; n >= i; ++i)

using namespace std;
using namespace __gnu_pbds;

const int nax = 1e5 + 5, pod = (1 << 19), mod = 998244353;
const ll inf = 1e18;

ll dp[20][10][10][2];

ll daj(string x, int ile) {
	if(ile == 1) 
		return x[0] - '0';
	for(int i = 1; i <= ile; ++i)
		for(int j = 0; j <= 9; ++j)
			for(int k = 0; k <= 9; ++k)
				dp[i][j][k][0] = dp[i][j][k][1] = 0;
	for(int i = 1; i <= 9; ++i) {
		for(int j = 0; j <= 9; ++j) {
			if(i == j)
				continue;
			int fir = x[0] - '0';
			int sec = x[1] - '0';
			if(fir > i || (fir == i && sec > j))
				dp[2][i][j][0] = 1;
			else if(fir == i && sec == j)
				dp[2][i][j][1] = 1;
			
		}
	}
	
	for(int i = 2; ile > i; ++i) {
		for(int j = 0; j <= 9; ++j)
			for(int k = 0; k <= 9; ++k) {
				for(int nowa = 0; nowa <= 9; ++nowa) {
					if(nowa != j && nowa != k)
						dp[i + 1][k][nowa][0] += dp[i][j][k][0];
				}
				for(int nowa = 0; nowa <= x[i] - '0'; ++nowa) {
					if(nowa != j && nowa != k)
						dp[i + 1][k][nowa][(nowa == x[i] - '0')] += dp[i][j][k][1];
				}
			}
	}
	ll ans = 0;
	for(int i = 0; i <= 9; ++i)
		for(int j = 0; j <= 9; ++j)
			ans += dp[ile][i][j][0] + dp[ile][i][j][1];
	return ans;
}
				
			
			
		

ll f(string x) {
	string y = "9";
	int r = 1;
	ll res = 0;
	while(ss(x) > ss(y)) {
		res += daj(y, r);
		y.pb('9');
		r++;
	}
	res += daj(x, r);
	return res;
}

int check(string x) {
	if(ss(x) == 1)
		return 1;
	for(int i = 1; ss(x) > i; ++i) {
		if(x[i] == x[i - 1])
			return 0;
		if(i > 1 && x[i] == x[i - 2])
			return 0;
	}
	return 1;
}

int main() {
	
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	
	string a, b;
	cin >> a >> b;
	cout << f(b) - f(a) + check(a) << endl;
	
		
	
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 3 ms 424 KB Output is correct
28 Correct 3 ms 384 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 3 ms 384 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 3 ms 384 KB Output is correct
35 Correct 3 ms 384 KB Output is correct
36 Correct 3 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 3 ms 384 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 3 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 3 ms 384 KB Output is correct
44 Correct 3 ms 384 KB Output is correct
45 Correct 3 ms 384 KB Output is correct