Submission #647390

# Submission time Handle Problem Language Result Execution time Memory
647390 2022-10-02T11:09:07 Z ymm Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
1 ms 340 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 20;

int cnt_lt_pre(int *msd, int i)
{
	int ans = 0;
	ans += i>0 && msd[i-1] < msd[i];
	ans += i>1 && msd[i-2] < msd[i];
	return ans;
}

bool is_equal(int *msd, int i)
{
	if (i>0 && msd[i] == msd[i-1])
		return 1;
	if (i>1 && msd[i] == msd[i-2])
		return 1;
	return 0;
}

ll cnt_lt_arr(int *msd, int len, int first_lt)
{
	ll ans = 1;
	if (msd == NULL) {
		Loop (i,0,len)
			ans *= i<2? 9: 8;
		return ans;
	}
	Loop (i,0,first_lt) {
		if (is_equal(msd, i))
			return 0;
	}
	ans *= msd[first_lt] - cnt_lt_pre(msd, first_lt) - (first_lt == 0);
	Loop (i,first_lt+1,len)
		ans *= max(8ll, 10-i);
	return ans;
}

pair<int *, int> make_arr(ll x)
{
	int *ans = new int[N];
	int len = 0;
	while (x) {
		ans[len++] = x%10;
		x /= 10;
	}
	reverse(ans, ans+len);
	return {ans, len};
}

ll cnt_lt(ll x)
{
	if (x == 0)
		return 0;
	auto [arr, len] = make_arr(x);
	ll ans = 1 /* 0 is valid too */;
	Loop (i,1,len)
		ans += cnt_lt_arr(NULL, i, 0);
	Loop (i,0,len)
		ans += cnt_lt_arr(arr, len, i);
	delete[](arr);
	return ans;
}

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	ll l, r;
	cin >> l >> r;
	cout << cnt_lt(r+1) - cnt_lt(l) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 0 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 324 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct