Submission #383231

# Submission time Handle Problem Language Result Execution time Memory
383231 2021-03-29T08:59:23 Z negar_a Palindrome-Free Numbers (BOI13_numbers) C++14
20 / 100
2 ms 512 KB
//!yrt tsuj uoy srettam gnihton no emoc
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define pii pair <int, int>
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define F first
#define S second

ll a, b, ans = 0;
ll x, y;
string A, B;

ll pw(ll x, ll y){
//	cout << x << " | " << y << endl;
	ll res = 1;
	for(ll i = 0; i < y; i ++){
		res *= x;
	}
	return res;
}

void solve_s(ll st){
	for(ll i = st - 1; i >= 0; i --){
		ll t = 9 - (A[i] - '0') - (i < x - 1 && A[i + 1] > A[i]) - (i < x - 2 && A[i + 2] > A[i] && A[i + 2] != A[i + 1]);
		ans += 1LL * t * (i == x - 1 ? 9 * pw(8, i - 1) : pw(8, i));
		if(i < x - 1 && A[i] == A[i + 1]){
			return;
		}
		if(i < x - 2 && A[i] == A[i + 2]){
			return;
		}
//		cout << "ani = " << i << " : " << ans << endl;
	}
	if(0 < x - 1 && A[0] == A[1]){
		return;
	}
	if(0 < x - 2 && A[0] == A[2]){
		return;
	}
	ans ++;
	return;
}

void solve_b(ll st){
	for(ll i = st - 1; i >= 0; i --){
		ll t = (B[i] - '0' - (i == y - 1)) - (i < y - 1 && B[i + 1] < B[i]) - (i < y - 2 && B[i + 2] < B[i] && B[i + 2] != B[i + 1]);
		ans += 1LL * t * (i == y - 1 ? 9 * pw(8, i - 1) : pw(8, i));
		if(i < y - 1 && B[i] == B[i + 1]){
			return;
		}
		if(i < y - 2 && B[i] == B[i + 2]){
			return;
		}
//		cout << "ani = " << i << " : " << ans << endl;
	}
	if(0 < y - 1 && B[0] == B[1]){
		return;
	}
	if(0 < y - 2 && B[0] == B[2]){
		return;
	}
	ans ++;
	return;
}

bool check(ll x){
	string s = to_string(x);
	for(ll i = 1; i < s.size(); i ++){
		if(s[i] == s[i - 1]){
			return false;
		}
		if(i > 1 && s[i] == s[i - 2]){
			return false;
		}
	}
	return true;
}

void solve(){
	ll ans = 0;
	for(ll i = a; i <= b; i ++){
		ans += check(i);
	}
	cout << ans;
}

int main(){
	fast_io;
		
	cin >> a >> b;
	A = to_string(a);
	B = to_string(b);
	x = A.size(), y = B.size();
	
	reverse(A.begin(), A.end());
	reverse(B.begin(), B.end());
	
//	if(b - a <= 1e5){
//		solve();
//		return 0;
//	}
	
	if(x == y){
//		assert(0);
		ll ind = x - 1;
		while(ind >= 0 && A[ind] == B[ind]){
			if(ind < x - 1 && A[ind] == A[ind + 1]){
				cout << 0;
				return 0;
			}
			if(ind < x - 2 && A[ind] == A[ind + 2]){
				cout << 0;
				return 0;
			}
			ind --;
		}
		if(ind >= 0){
			ans += 1LL * ((ll)(B[ind] - '0') - (ll)(A[ind] - '0') - 1) * (ind == x - 1 ? 9 * pw(8, ind - 1) : pw(8, ind));
//			cout << "ans = " << ans << endl;
			solve_s(ind);
//			cout << "ans = " << ans << endl;
			solve_b(ind);
		}else
			ans = 1;
	}else{
		for(ll i = x + 1; i < y; i ++){
			ans += 1LL * 9 * 9 * pw(8, i - 2);
		}
//		cout << "ans = " << ans << endl;
		solve_s(x);
//		cout << "ans = " << ans << endl;
		solve_b(y);
	}
//	cout << ans + (x == 0);
	cout << ans - 2;
	
	return 0;
}

Compilation message

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