#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll d[2][13][12][11];
int bas[21];
ll kc[20];
bool pal(int a){
int last1 = -2, last2 = -1;
while(a){
int cur = a % 10;
if(cur == last1 || cur == last2){
return 0;
}
last1 = last2;
last2 = cur;
a /= 10;
}
return 1;
}
ll ite(int say){
ll ans = 0;
if(say == 0){
for(int j = 0; j <= 11; j++){
for(int k = 0; k <= 10; k++){
for(int l = 0; l <= 9; l++){
if(k == 10 && l == 0) continue;
ans += d[0][j][k][l];
}
}
}
}
for(int i = 1; i <= say; i++){
ans = 0;
memset(d[i&1], 0, sizeof(d[i&1]));
for(int j = 0; j <= 11; j++){
for(int k = 0; k <= 10; k++){
for(int l = 0; l <= 9; l++){
if(l == 0 && k == 10) continue;
if(j == k || k == l || j == l) continue;
for(int z = 0; z <= 12; z++){
d[i&1][j][k][l] += d[i&1^1][z][j][k];
}
ans += d[i&1][j][k][l];
}
}
}
}
return ans;
}
ll cn(ll x){
if(x < 0) return 0;
if(x <= 9) return x + 1;
ll y = x;
int idx = 0;
while(y){
bas[idx++] = y % 10;
y /= 10;
}
reverse(bas, bas + idx);
ll ans = 0;
for(int i = 1; i < idx; i++){
ans += kc[i];
}
ll cur = 0;
cerr<<x<<" "<<idx<<endl;
for(int i = 0; i < idx; i++){
if(pal(cur)){
memset(d[0], 0, sizeof(d[0]));
if(i == 0){
for(int j = 1; j < bas[i]; j++){
d[0][11][10][j] = 1;
}
ans += ite(idx - (i + 1));
}else if(i == 1){
for(int j = 0; j < bas[i]; j++){
if(j == bas[i - 1]) continue;
d[0][10][bas[i - 1]][j] = 1;
}
ans += ite(idx - (i + 1));
}else{
for(int j = 0; j < bas[i]; j++){
if(j == bas[i - 1] || j == bas[i - 2]) continue;
d[0][bas[i - 2]][bas[i - 1]][j] = 1;
}
ans += ite(idx - (i + 1));
}
}else{
break;
}
cur *= 10;
cur += bas[i];
}
cerr<<x<<" "<<ans<<endl;
ans += pal(x);
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
#endif
ll a, b;
cin>>a>>b;
d[0][12][11][10] = 1;
for(int i = 1; i < 10; i++){
memset(d[i&1], 0, sizeof(d[i&1]));
for(int j = 0; j <= 11; j++){
for(int k = 0; k <= 10; k++){
for(int l = 0; l <= 9; l++){
if(l == 0 && k == 10) continue;
if(j == k || k == l || j == l) continue;
for(int z = 0; z <= 12; z++){
if(z == j || z == k || j == k) continue;
d[i&1][j][k][l] += d[i&1^1][z][j][k];
}
kc[i] += d[i&1][j][k][l];
}
}
}
}
kc[1]++;
cout<<cn(b) - cn(a - 1)<<endl;
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message
numbers.cpp: In function 'll ite(int)':
numbers.cpp:54:47: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
54 | d[i&1][j][k][l] += d[i&1^1][z][j][k];
| ~^~
numbers.cpp: In function 'int main()':
numbers.cpp:142:47: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
142 | d[i&1][j][k][l] += d[i&1^1][z][j][k];
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
3 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
4 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
28 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
36 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
37 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
38 |
Incorrect |
4 ms |
392 KB |
Output isn't correct |
39 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
40 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
41 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
45 |
Incorrect |
4 ms |
416 KB |
Output isn't correct |