Submission #476309

#TimeUsernameProblemLanguageResultExecution timeMemory
476309tht2005Palindrome-Free Numbers (BOI13_numbers)C++14
100 / 100
1 ms376 KiB
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ll;

ll memo[19][11][11][2][2];

ll dp(const string& s, int i, int p1, int p2, int tight, int lz)
{
	if(i == s.size()) return 1;
	ll& res = memo[i][p1][p2][tight][lz];
	if(~res) return res;
	res = 0;
	if(tight) {
		if(lz) {
			res += dp(s, i + 1, 10, 10, 1, 1);
			for(int c = 1; c < 10; ++c) res += dp(s, i + 1, 10, c, 1, 0);
		}
		else {
			for(int c = 0; c < 10; ++c) {
				if(c ^ p1 && c ^ p2) {
					res += dp(s, i + 1, p2, c, 1, 0);
				}
			}
		}
	}
	else {
		if(lz) {
			res += dp(s, i + 1, 10, 10, s[i] > '0', 1);
			for(int c = 1; c + 48 < s[i]; ++c) res += dp(s, i + 1, p2, c, 1, 0);
			if(s[i] > '0' && s[i] ^ (p1 + 48) && s[i] ^ (p2 + 48)) res += dp(s, i + 1, p2, s[i] - 48, 0, 0);
		}
		else {
			for(int c = 0; c + 48 < s[i]; ++c) {
				if(c ^ p1 && c ^ p2) {
					res += dp(s, i + 1, p2, c, 1, 0);
				}
			}
			if(s[i] ^ (p1 + 48) && s[i] ^ (p2 + 48)) res += dp(s, i + 1, p2, s[i] - 48, 0, 0);
		}
	}
	return res;
}

ll calc(ll i)
{
	string s = to_string(i);
	memset(memo, -1, sizeof(memo));
	return dp(s, 0, 10, 10, 0, 1); 
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(NULL);
	ll l, r; cin >> l >> r;
	cout << calc(r) - (l ? calc(l - 1) : 0);
	return 0;
}

Compilation message (stderr)

numbers.cpp: In function 'll dp(const string&, int, int, int, int, int)':
numbers.cpp:10:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |  if(i == s.size()) return 1;
      |     ~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...