Submission #779878

#TimeUsernameProblemLanguageResultExecution timeMemory
779878vjudge1Palindrome-Free Numbers (BOI13_numbers)C++17
71.25 / 100
1 ms468 KiB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 1e5 + 5, mod = 1e9 + 7, inf = 1e18;
ll dp[20][12][12][2];
ll bachtrach(string &lim, int ith, int last1, int last2, bool smaller)
{
	if(ith == lim.size()) return 1;

	if(dp[ith][last1+1][last2+1][smaller] != -1)
	{
		return dp[ith][last1+1][last2+1][smaller];
	}
	int curdig = lim[ith]-'0';
	ll ans = 0;

	if(smaller)
	{
		for(int i = 0; i <= 9; ++i)
		{
			if(i != last1 && i != last2) ans += bachtrach(lim, ith + 1, i, last1, 1);
		}
	}
	else
	{
		for(int i = 0; i < curdig; ++i)
		{
			if(i != last1 && i != last2) ans += bachtrach(lim, ith + 1, i, last1, 1);
		}
		if(curdig != last1 && curdig != last2)
		ans += bachtrach(lim, ith + 1, curdig, last1, 0);
	}
	return dp[ith][last1+1][last2+1][smaller] = ans;
}
ll slove(ll x)
{
	string lim = to_string(x);
	memset(dp, -1, sizeof(dp));
	return bachtrach(lim, 0, -1, -1, 0);
}
int main()
{
	// freopen(".INP", "r", stdin);
	// freopen(".OUT", "w", stdout);
	cin.tie(0)->sync_with_stdio(0);
	ll a, b;
	cin >> a >> b;
	cout << slove(b) - slove(a - 1);		    
}

Compilation message (stderr)

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