#include "bits/stdc++.h"
using namespace std;
// #define endl '\n'
// #define double long double
#define int long long
// int MOD = 1000 * 1000 * 1000 + 7;
// int MOD = 998244353;
// const int MXN = 100005;
const int MXN = 20;
int digits[MXN];
void load(int a) {
for (int i=19; i>=0; i--) {
digits[i] = a % 10;
a /= 10;
}
}
int dp[MXN][2][MXN][10][10];
void dpclear() {
for (int i=0; i<MXN; i++) {
for (int j=0; j<2; j++) {
for (int k=0; k<MXN; k++) {
for (int a=0; a<10; a++) {
for (int b=0; b<10; b++) {
dp[i][j][k][a][b] = -1;
}
}
}
}
}
}
int solve(int at, bool atmin, int placed, int last1, int last2) {
if (at == MXN) return 1; // finished
// cout << at << ' ' << atmin << ' ' << placed << ' ' << last1 << ' ' << last2 << endl;
if (dp[at][atmin][placed][last1][last2] != -1) return dp[at][atmin][placed][last1][last2];
int ans = 0;
for (int i=0; i<=9; i++) {
if (atmin && i > digits[at]) {
// violates range
continue;
}
else {
bool mncond = atmin && (i == digits[at]);
if (placed == 0) {
// first digit, no restriction
// cout << "tried placing: " << i << endl;
if (i == 0) ans += solve(at+1, mncond, 0, i, 0);
else ans += solve(at+1, mncond, placed+1, i, 0);
}
else if (placed == 1) {
// second digit, only first restrict
if (i != last1) {
// cout << "tried placing: " << i << endl;
ans += solve(at+1, mncond, placed+1, i, last1);
}
}
else {
// any other digit, all restrictions
if (i != last1) {
if (i != last2) {
// cout << "tried placing: " << i << endl;
ans += solve(at+1, mncond, placed+1, i, last1);
}
}
}
}
}
return dp[at][atmin][placed][last1][last2] = ans;
}
bool can(int a) {
if (a <= 10) return true;
string s = "";
while (a > 0) {
int x = a % 10;
a /= 10;
s += ((char) '0' + x);
}
for (int i=0; i<s.size()-1; i++) {
if (s[i] == s[i+1]) return false;
}
for (int i=0; i<s.size()-2; i++) {
if (s[i] == s[i+2]) return false;
}
return true;
}
signed main() {
int a, b;
cin >> a >> b;
load(b);
dpclear();
int upto = solve(0, 1, 0, 0, 0);
load(a);
dpclear();
int unto = solve(0, 1, 0, 0, 0);
cout << upto - unto + can(a) << endl;
}
Compilation message
numbers.cpp: In function 'bool can(long long int)':
numbers.cpp:85:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<s.size()-1; i++) {
~^~~~~~~~~~~
numbers.cpp:88:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<s.size()-2; i++) {
~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
2 |
Correct |
5 ms |
896 KB |
Output is correct |
3 |
Correct |
6 ms |
1024 KB |
Output is correct |
4 |
Correct |
5 ms |
964 KB |
Output is correct |
5 |
Correct |
5 ms |
896 KB |
Output is correct |
6 |
Correct |
5 ms |
896 KB |
Output is correct |
7 |
Correct |
8 ms |
1024 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
5 ms |
896 KB |
Output is correct |
10 |
Correct |
5 ms |
896 KB |
Output is correct |
11 |
Correct |
5 ms |
896 KB |
Output is correct |
12 |
Correct |
5 ms |
768 KB |
Output is correct |
13 |
Correct |
5 ms |
1024 KB |
Output is correct |
14 |
Correct |
5 ms |
896 KB |
Output is correct |
15 |
Correct |
5 ms |
896 KB |
Output is correct |
16 |
Correct |
5 ms |
896 KB |
Output is correct |
17 |
Correct |
5 ms |
896 KB |
Output is correct |
18 |
Correct |
5 ms |
896 KB |
Output is correct |
19 |
Correct |
6 ms |
896 KB |
Output is correct |
20 |
Correct |
5 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1024 KB |
Output is correct |
2 |
Correct |
6 ms |
896 KB |
Output is correct |
3 |
Correct |
6 ms |
896 KB |
Output is correct |
4 |
Correct |
6 ms |
1024 KB |
Output is correct |
5 |
Correct |
5 ms |
896 KB |
Output is correct |
6 |
Correct |
6 ms |
896 KB |
Output is correct |
7 |
Correct |
5 ms |
896 KB |
Output is correct |
8 |
Correct |
5 ms |
1024 KB |
Output is correct |
9 |
Correct |
5 ms |
896 KB |
Output is correct |
10 |
Correct |
5 ms |
1024 KB |
Output is correct |
11 |
Correct |
5 ms |
896 KB |
Output is correct |
12 |
Correct |
5 ms |
896 KB |
Output is correct |
13 |
Correct |
5 ms |
896 KB |
Output is correct |
14 |
Correct |
5 ms |
1024 KB |
Output is correct |
15 |
Correct |
5 ms |
1024 KB |
Output is correct |
16 |
Correct |
6 ms |
1024 KB |
Output is correct |
17 |
Correct |
12 ms |
896 KB |
Output is correct |
18 |
Correct |
7 ms |
896 KB |
Output is correct |
19 |
Correct |
6 ms |
896 KB |
Output is correct |
20 |
Correct |
7 ms |
1024 KB |
Output is correct |
21 |
Correct |
7 ms |
896 KB |
Output is correct |
22 |
Correct |
6 ms |
896 KB |
Output is correct |
23 |
Correct |
6 ms |
896 KB |
Output is correct |
24 |
Correct |
6 ms |
896 KB |
Output is correct |
25 |
Correct |
7 ms |
896 KB |
Output is correct |
26 |
Correct |
6 ms |
896 KB |
Output is correct |
27 |
Correct |
6 ms |
896 KB |
Output is correct |
28 |
Correct |
7 ms |
896 KB |
Output is correct |
29 |
Correct |
6 ms |
1024 KB |
Output is correct |
30 |
Correct |
6 ms |
1024 KB |
Output is correct |
31 |
Correct |
6 ms |
1024 KB |
Output is correct |
32 |
Correct |
6 ms |
1024 KB |
Output is correct |
33 |
Correct |
6 ms |
1024 KB |
Output is correct |
34 |
Correct |
6 ms |
896 KB |
Output is correct |
35 |
Correct |
6 ms |
896 KB |
Output is correct |
36 |
Correct |
6 ms |
896 KB |
Output is correct |
37 |
Correct |
6 ms |
1024 KB |
Output is correct |
38 |
Correct |
7 ms |
896 KB |
Output is correct |
39 |
Correct |
7 ms |
1024 KB |
Output is correct |
40 |
Correct |
6 ms |
896 KB |
Output is correct |
41 |
Correct |
6 ms |
896 KB |
Output is correct |
42 |
Correct |
8 ms |
896 KB |
Output is correct |
43 |
Correct |
6 ms |
896 KB |
Output is correct |
44 |
Correct |
6 ms |
896 KB |
Output is correct |
45 |
Correct |
7 ms |
896 KB |
Output is correct |