Submission #255604

# Submission time Handle Problem Language Result Execution time Memory
255604 2020-08-01T12:16:08 Z amoo_safar Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 ms 384 KB
// 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

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 time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 0 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 0 ms 384 KB Output is correct
28 Correct 0 ms 384 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 1 ms 384 KB Output is correct
37 Correct 0 ms 384 KB Output is correct
38 Correct 0 ms 384 KB Output is correct
39 Correct 0 ms 384 KB Output is correct
40 Correct 0 ms 384 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 0 ms 384 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 0 ms 384 KB Output is correct