Submission #255611

# Submission time Handle Problem Language Result Execution time Memory
255611 2020-08-01T12:35:46 Z Saboon Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 ms 384 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
const int maxn = 1e5 + 10;

ll dp[20][12][12];

ll get(string s){
	if (s.size() == 1)
		return stoi(s) + 1;
	int n = s.size();
	ll ret = 0;
	for (int i = 1; i <= n-1; i++)
		ret += 9ll * dp[i][10][1];
	ret ++;
	for (int i = 0; i < n; i++){
		for (char c = '0'; c < s[i]; c++){
			if (i == 0 and c == '0')
				continue;
			if (i > 0 and c == s[i-1])
				continue;
			if (i > 1 and c == s[i-2])
				continue;
			if (i == 0)
				ret += dp[n-i][10][(int)(c-'0')];
			else
				ret += dp[n-i+1][(int)(s[i-1]-'0')][(int)(c-'0')];
		}
		
		if (i > 0 and s[i] == s[i-1])
			break;
		if (i > 1 and s[i] == s[i-2])
			break;
		if (i == n-1)
			ret ++;
	}
	return ret;
}

bool check(string s){
	for (int i = 1; i < s.size(); i++)
		if (s[i] == s[i-1])
			return false;
	for (int i = 2; i < s.size(); i++)
		if (s[i] == s[i-2])
			return false;
	return true;
}

int main(){
	ios_base::sync_with_stdio(false);
	for (int i = 0; i <= 9; i++)
		dp[1][10][i] = 1;
	for (int a = 0; a < 10; a++)
		for (int b = 0; b < 10; b++)
			if (a != b)
				dp[2][a][b] = 1;
	for (int len = 2; len <= 19; len++){
		for (int a = 0; a < 10; a++){
			for (int b = 0; b < 10; b++){
				if (a == b)
					continue;
				for (int c = 0; c < 10; c++){
					if (a == b or b == c)
						continue;
					dp[len][a][b] += dp[len-1][c][a];
				}
			}
		}
		for (int b = 0; b < 10; b++){
			for (int c = 0; c < 10; c++){
				if (b == c)
					continue;
				dp[len][10][b] += dp[len][b][c];
			}
		}
	}
	ll l, r;
	cin >> l >> r;
	string a = to_string(r), b = to_string(l-1);
	if (l == 0)
		return cout << get(a) << endl, 0;
	cout << get(a) - get(b) << endl;
}

Compilation message

numbers.cpp: In function 'bool check(std::__cxx11::string)':
numbers.cpp:44:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 1; i < s.size(); i++)
                  ~~^~~~~~~~~~
numbers.cpp:47:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 2; i < s.size(); i++)
                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 0 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 0 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 0 ms 384 KB Output is correct
37 Correct 0 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 0 ms 384 KB Output is correct
40 Correct 1 ms 384 KB Output is correct
41 Correct 0 ms 384 KB Output is correct
42 Correct 0 ms 384 KB Output is correct
43 Correct 0 ms 384 KB Output is correct
44 Correct 0 ms 384 KB Output is correct
45 Correct 0 ms 384 KB Output is correct