답안 #361822

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
361822 2021-01-31T16:58:09 Z usuyus Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
5 ms 364 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll solve(ll x) {
	int a[20], n = 0;
	while (x) a[n++] = x % 10, x /= 10;

	ll ans = 0;
	for (int i=1; i<n; i++) {
		if (i == 1) ans += 10;
		else {
			ll cur = 9 * 9;
			for (int j=0; j<i-2; j++) cur *= 8;
			ans += cur;
		}
	}

	bool self = true;

	for (int i=n-1; i>=0; i--) {
		if (i < n-2 && a[i+1] == a[i+2]) {self = false; break;}
		if (i < n-3 && a[i+1] == a[i+3]) {self = false; break;}

		for (int j=0; j<a[i]; j++) {
			if (i == n-1) {
				if (j == 0) continue;

				ll cur = 9;
				for (int k=0; k<i-1; k++) cur *= 8;
				ans += cur;
			} else if (i == n-2) {
				if (j == a[i+1]) continue;

				ll cur = 1;
				for (int k=0; k<i; k++) cur *= 8;
				ans += cur;
			} else {
				if (j == a[i+1] || j == a[i+2] || a[i+1] == a[i+2]) continue;

				ll cur = 1;
				for (int k=0; k<i; k++) cur *= 8;
				ans += cur;
			}
		}
	}

	if (self && n >= 2) self = a[0] != a[1];
	if (self && n >= 3) self = a[0] != a[2];

	return ans + self;
}

bool check(ll x) {
	int a[20], n = 0;
	while (x) a[n++] = x % 10, x /= 10;

	for (int i=0; i<n; i++) {
		if (i>0 && a[i] == a[i-1]) return false;
		if (i>1 && a[i] == a[i-2]) return false;
	}

	return true;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);
	ll a, b; cin>>a>>b;
	
	if (b-a <= 1000000) {
		ll ans = 0;
		for (ll i=a; i<=b; i++) ans += check(i);
		cout<<ans<<endl;
	} else cout<<solve(b) - solve(a-1)<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 5 ms 364 KB Output is correct
4 Correct 2 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 364 KB Output is correct
14 Correct 2 ms 364 KB Output is correct
15 Correct 2 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 5 ms 364 KB Output is correct
20 Correct 2 ms 364 KB Output is correct
# 결과 실행 시간 메모리 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 0 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 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 0 ms 364 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
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