Submission #255604

#TimeUsernameProblemLanguageResultExecution timeMemory
255604amoo_safarPalindrome-Free Numbers (BOI13_numbers)C++14
100 / 100
1 ms384 KiB
// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 20;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

ll pw[N];

ll Solve(ll x){
	if(x < 0) return 0;
	if(x == 0) return 1;

	vector<int> C;
	while(x){
		C.pb(x % 10);
		x /= 10;
	}
	ll res = 1;
	for(int i = 1; i <= C.size() - 1; i++){
		if(i == 1) res += 9;
		else res += 81 * pw[i - 2];
	}
	reverse(all(C));
	for(int i = 0; i <= C.size(); i++){
		if(i == C.size()){
			res ++;
			break;
		}
		if(i == 0){
			if(C.size() == 1) res += C[0] - 1;
			else res += (C[0] - 1) * 9ll * pw[C.size() - 2];
		} else {
			for(int j = 0; j < C[i]; j++){
				if(j == C[i - 1]) continue;
				if((i >= 2) && (j == C[i - 2])) continue;
				res += pw[C.size() - i - 1];
			}
		}

		if((i >= 1) && (C[i] == C[i - 1])) break;
		if((i >= 2) && (C[i] == C[i - 2])) break;
	}


	return res;
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	pw[0] = 1;
	for(int i = 1; i < N; i++) pw[i] = 8 * pw[i - 1];  
	
	ll a, b;
	cin >> a >> b;
	cout << Solve(b) - Solve(a - 1) << '\n';
	return 0;
}

/*
1 1000000000000000000

*/

Compilation message (stderr)

numbers.cpp: In function 'll Solve(ll)':
numbers.cpp:34:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i <= C.size() - 1; i++){
                 ~~^~~~~~~~~~~~~~~
numbers.cpp:39:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i <= C.size(); i++){
                 ~~^~~~~~~~~~~
numbers.cpp:40:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(i == C.size()){
      ~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...