Submission #444934

# Submission time Handle Problem Language Result Execution time Memory
444934 2021-07-15T21:49:13 Z aryan12 Palindrome-Free Numbers (BOI13_numbers) C++17
71.25 / 100
2 ms 332 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

int dp[20][12][12][2];

vector<int> Convert(int x) {
	vector<int> digits;
	while(x != 0) {
		digits.push_back(x % 10);
		x /= 10;
	}
	reverse(digits.begin(), digits.end());
	/*for(int i = 0; i < digits.size(); i++) {
		cout << digits[i] << " ";
	}
	cout << "\n";*/
	return digits;
}

void Ini() {
	for(int i = 0; i < 20; i++) {
		for(int j = 0; j < 12; j++) {
			for(int k = 0; k < 12; k++) {
				for(int l = 0; l < 2; l++) {
					dp[i][j][k][l] = -1;
				}
			}
		}
	}
}

int DigitDP(vector<int> s, int pos, int second_last, int last, bool edge) {
	//cout << "pos = " << pos << ", second_last = " << second_last << ", last = " << last << ", edge = " << edge << "\n";
	if(pos == s.size()) {
		return 1;
	}
	if(dp[pos][second_last][last][(edge) ? 1 : 0] != -1) {
		return dp[pos][second_last][last][(edge) ? 1 : 0];
	}
	int ans = 0;
	if(edge) {
		for(int i = 0; i < s[pos]; i++) {
			if(i != second_last && i != last) {
				ans += DigitDP(s, pos + 1, last, i, false);
			}
		}
		if(s[pos] != second_last && s[pos] != last) {
			ans += DigitDP(s, pos + 1, last, s[pos], true);
		}
	}
	else {
		for(int i = 0; i < 10; i++) {
			if(i != second_last && i != last) {
				ans += DigitDP(s, pos + 1, last, i, false);
			}
		}
	}
	return dp[pos][second_last][last][(edge) ? 1 : 0] = ans;
}

void Solve() {
	int left, right;
	cin >> left >> right;
	vector<int> l = Convert(left - 1), r = Convert(right);
	Ini();
	int ans = DigitDP(r, 0, 10, 10, true);
	Ini();
	//cout << "ans = " << ans << "\n";
	ans -= DigitDP(l, 0, 10, 10, true);
	cout << ans << "\n";
}

int32_t main() {
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}

Compilation message

numbers.cpp: In function 'long long int DigitDP(std::vector<long long int>, long long int, long long int, long long int, bool)':
numbers.cpp:37:9: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |  if(pos == s.size()) {
      |     ~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 320 KB Output isn't correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Correct 1 ms 332 KB Output is correct
4 Incorrect 1 ms 332 KB Output isn't correct
5 Incorrect 1 ms 332 KB Output isn't correct
6 Incorrect 1 ms 332 KB Output isn't correct
7 Incorrect 1 ms 332 KB Output isn't correct
8 Incorrect 1 ms 316 KB Output isn't correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 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 1 ms 332 KB Output is correct
14 Incorrect 1 ms 332 KB Output isn't correct
15 Incorrect 1 ms 332 KB Output isn't correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Incorrect 1 ms 332 KB Output isn't correct
19 Correct 2 ms 332 KB Output is correct
20 Incorrect 1 ms 332 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Incorrect 1 ms 332 KB Output isn't correct
6 Correct 1 ms 332 KB Output is correct
7 Incorrect 1 ms 332 KB Output isn't correct
8 Incorrect 1 ms 332 KB Output isn't correct
9 Incorrect 1 ms 332 KB Output isn't correct
10 Incorrect 1 ms 332 KB Output isn't correct
11 Correct 1 ms 332 KB Output is correct
12 Incorrect 1 ms 332 KB Output isn't correct
13 Incorrect 1 ms 332 KB Output isn't correct
14 Incorrect 1 ms 332 KB Output isn't correct
15 Incorrect 1 ms 332 KB Output isn't correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 2 ms 316 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 2 ms 332 KB Output is correct
40 Correct 2 ms 332 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 2 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 2 ms 324 KB Output is correct
45 Correct 2 ms 332 KB Output is correct