Submission #384472

# Submission time Handle Problem Language Result Execution time Memory
384472 2021-04-01T17:42:42 Z negar_a Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 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){
	ll res = 1;
	for(ll i = 0; i < y; i ++){
		res *= x;
	}
	return res;
}

bool Y(int l, int r, int m){
	return (m > l && m < r);
}

void solve_s(ll st){
	if(st < x - 1 && A[st] == A[st + 1]){
		return;
	}
	if(st < x - 2 && A[st] == A[st + 2]){
		return;
	}
	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]);
		ans += 1LL * t * ((x > 1) ? (i == x - 1 ? 9 * pw(8, i - 1) : pw(8, i)) : 1);
		if(i < x - 1 && A[i] == A[i + 1]){
			return;
		}
		if(i < x - 2 && A[i] == A[i + 2]){
			return;
		}
	}
//	cout << "va " << 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){
	if(st < y - 1 && B[st] == B[st + 1]){
		return;
	}
	if(st < y - 2 && B[st] == B[st + 2]){
		return;
	}
	for(ll i = st - 1; i >= 0; i --){
		ll t = 1LL * (B[i] - '0' - (i == y - 1)) - (i < y - 1 && B[i + 1] < B[i]) - (i < y - 2 && B[i + 2] < B[i]);
		ans += 1LL * t * ((y > 1) ? (i == y - 1 ? 9 * pw(8, i - 1) : pw(8, i)) : 1);
		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;
}

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(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){
			ll t = 1LL * (B[ind] - A[ind]) - 1 - (ind + 1 < x && Y(A[ind], B[ind], A[ind + 1])) - (ind + 2 < x && Y(A[ind], B[ind], A[ind + 2]));
			ans += 1LL * t * (ind == x - 1 ? 9 * pw(8, ind - 1) : pw(8, ind));
//			cout << "ans0 : " << ans << " " << ind << endl;
			solve_s(ind);
//			cout << "ans1 : " << ans << " " << ind << endl;
			solve_b(ind);
//			cout << "ans2 : " << ans << " " << ind << endl;
		}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 : " << ans << endl;
	}
	
	cout << ans;
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 372 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is 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 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 364 KB Output is correct