답안 #779241

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
779241 2023-07-11T09:26:34 Z vjudge1 Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
1 ms 396 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define mp make_pair

const int NM = 19;

int n, f[NM+5][2][2][11][11], ans = 0;
string a, b;

pii trans(string &a, string &b, int i, int j, int k, int d){
	if (j == 1 && k == 1) return mp(1, 1);
	if (j == 0 && k == 1){
		if (d < a[i+1]) return mp(-1, -1);
		if (d == a[i+1]) return mp(0, 1);
		return mp(1, 1);
	}
	if (j == 1 && k == 0){
		if (d > b[i+1]) return mp(-1, -1);
		if (d == b[i+1]) return mp(1, 0);
		return mp(1, 1);
	}
	if (d < a[i+1] || d > b[i+1]) return mp(-1, -1);
	if (d == a[i+1] && d == b[i+1]) return mp(0, 0);
	if (d == a[i+1]) return mp(0, 1);
	if (d == b[i+1]) return mp(1, 0);
	return mp(1, 1);
}

int solve(string a, string b){
	n = a.size();
	for (int i = 0; i < n; i++)
		a[i] -= '0', b[i] -= '0';
	memset(f, 0, sizeof(f));
	if (a[0] == b[0]){
		f[0][0][0][10][a[0]] = 1;
	}
	else{
		f[0][0][1][10][a[0]] = 1;
		f[0][1][0][10][b[0]] = 1;
		for (int i = a[0]+1; i < b[0]; i++)
			f[0][1][1][10][i]++;
	}
	for (int i = 0; i < n-1; i++)
		for (int j = 0; j < 2; j++)
			for (int k = 0; k < 2; k++)
				for (int d1 = 0; d1 <= 10; d1++)
					for (int d2 = 0; d2 <= 10; d2++){
						if (f[i][j][k][d1][d2] == 0) continue;
						for (int d = 0; d <= 9; d++){
							if (d == d1 || d == d2) continue;
							pii P = trans(a, b, i, j, k, d);
							if (P.fi == -1) continue;
							f[i+1][P.fi][P.se][d2][d] += f[i][j][k][d1][d2];
						}
					}
	int res = 0;
	for (int j = 0; j < 2; j++)
		for (int k = 0; k < 2; k++)
			for (int d1 = 0; d1 <= 10; d1++)
				for (int d2 = 0; d2 <= 10; d2++)
					res += f[n-1][j][k][d1][d2];
	return res;
}

signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> a >> b;
	if (a == "0" && b == "0"){
		cout << 1;
		return 0;
	}
	if (a == "0"){
		ans++;
		a = "1";
	}
	if (a.size() == b.size()){
		cout << solve(a, b);
		return 0;
	}
	string s = "";
	for (int i = 0; i < a.size(); i++) s.push_back('9');
	ans += solve(a, s);
	s = "1";
	for (int i = 1; i < b.size(); i++) s.push_back('0');
	ans += solve(s, b);
	for (int i = a.size()+1; i < b.size(); i++){
		string s1 = "1", s2 = "";
		for (int j = 0; j < i; j++){
			if (j > 0) s1.push_back('0');
			s2.push_back('9');
		}
		ans += solve(s1, s2);
	}
	cout << ans;
	return 0;
}

Compilation message

numbers.cpp: In function 'long long int solve(std::string, std::string)':
numbers.cpp:40:22: warning: array subscript has type 'char' [-Wchar-subscripts]
   40 |   f[0][0][0][10][a[0]] = 1;
      |                      ^
numbers.cpp:43:22: warning: array subscript has type 'char' [-Wchar-subscripts]
   43 |   f[0][0][1][10][a[0]] = 1;
      |                      ^
numbers.cpp:44:22: warning: array subscript has type 'char' [-Wchar-subscripts]
   44 |   f[0][1][0][10][b[0]] = 1;
      |                      ^
numbers.cpp: In function 'int main()':
numbers.cpp:88:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |  for (int i = 0; i < a.size(); i++) s.push_back('9');
      |                  ~~^~~~~~~~~~
numbers.cpp:91:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |  for (int i = 1; i < b.size(); i++) s.push_back('0');
      |                  ~~^~~~~~~~~~
numbers.cpp:93:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |  for (int i = a.size()+1; i < b.size(); i++){
      |                           ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 316 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 396 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 324 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 324 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 324 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 320 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct