#include <iostream>
#include <random>
#include <chrono>
#include <algorithm>
#include <cstring>
#define rep(i, x) for(int i = 0; i < x; i++)
using namespace std;
typedef long long ll;
ll MOD = (ll)1e9 + 7;
const int N = 1e5+5, inf = 1e9+5;
ll add(ll x, ll y) { x += y; if (x >= MOD) return x - MOD; return x; }
ll sub(ll x, ll y) { x -= y; if (x < 0) return x + MOD; return x; }
ll mult(ll x, ll y) { return (x * y) % MOD; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll dp[20][2][11][11];
vector<int> num;
// function for [0, x]
ll f(int pos, bool less, int slast, int last) {
if (pos == num.size()) return 1;
if (dp[pos][less][slast][last] != -1) return dp[pos][less][slast][last];
ll ans = 0;
for (int digit = 0; digit <= 9; digit++) {
if (!less && digit > num[pos]) break;
if (slast == digit || last == digit) continue;
// if last is a leading zero (i.e. a 10) then the current zero is also a leading zero.
ans += f(pos+1, less || digit < num[pos], last, last == 10 && digit == 0 ? 10 : digit);
}
return dp[pos][less][slast][last] = ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
ll a, b; cin >> a >> b;
while (b > 0) {
num.push_back(b%10);
b/=10;
}
reverse(begin(num), end(num));
memset(dp, -1, sizeof(dp));
ll b_ans = f(0, false, 10, 10); // 10 is leading zero
num.clear();
a--;
while (a > 0) {
num.push_back(a%10);
a/=10;
}
reverse(begin(num), end(num));
memset(dp, -1, sizeof(dp));
ll a_ans = f(0, false, 10, 10); // 10 is leading zero
cout << b_ans-a_ans << endl;
}
Compilation message
numbers.cpp: In function 'll f(int, bool, int, int)':
numbers.cpp:21:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (pos == num.size()) return 1;
~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
396 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
396 KB |
Output is correct |
4 |
Incorrect |
3 ms |
428 KB |
Output isn't correct |
5 |
Correct |
4 ms |
400 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
392 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
392 KB |
Output is correct |
11 |
Correct |
2 ms |
412 KB |
Output is correct |
12 |
Correct |
4 ms |
356 KB |
Output is correct |
13 |
Correct |
2 ms |
472 KB |
Output is correct |
14 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
432 KB |
Output is correct |
18 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
304 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
428 KB |
Output is correct |
5 |
Correct |
3 ms |
304 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
392 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
484 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
392 KB |
Output is correct |
14 |
Correct |
2 ms |
356 KB |
Output is correct |
15 |
Correct |
2 ms |
388 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
388 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
432 KB |
Output is correct |
20 |
Correct |
3 ms |
356 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
428 KB |
Output is correct |
33 |
Correct |
3 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
3 ms |
396 KB |
Output is correct |
38 |
Correct |
4 ms |
428 KB |
Output is correct |
39 |
Correct |
3 ms |
384 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
3 ms |
384 KB |
Output is correct |
43 |
Correct |
2 ms |
356 KB |
Output is correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
45 |
Correct |
3 ms |
420 KB |
Output is correct |