#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(ll i=0; i<n; i++)
#define FOR(i, a, b) for(ll i = a; i<=b; i++)
#define FORD(i, a, b) for(ll i = a; i>=b; i--)
#define DEBUG(x) cerr<<#x<<" > "<<x<<endl;
#define endl '\n'
const ll MOD = 1e9 + 7, INF = 1e18, MX = 1e5 + 4;
ll a, b;
string s;
ll dp[20][2][11][11][4];
ll f(ll idx, bool b, ll pree, ll pre, ll x)
{
// cout << idx << " " << b << " " << pree << " " << pre << endl;
// DEBUG("x");
if(x >= 2)
{
if(pree == pre)
return 0;
}
if(idx == s.size())
return 1;
// DEBUG("x");
if(dp[idx][b][pree][pre][x] != -1)
return dp[idx][b][pree][pre][x];
// DEBUG("x");
ll ans = 0;
FOR(i, 0, 9)
{
if(b == 0 && i < s[idx] - '0')
{
if(i == pree && x >= 2)
continue;
if(i == pre && x)
continue;
ans += f(idx + 1, 1, pre, i, (!x ? i > 0 : min(x + 1, 2ll)));
}
if(b == 0 && i == s[idx] - '0')
{
// cout << pree << " " << pre << " " << i << " " << x << endl;
if(i == pree && x >= 2)
continue;
if(i == pre && x)
continue;
ans += f(idx + 1, 0, pre, i, (!x ? i > 0 : min(x + 1, 2ll)));
}
if(b == 1)
{
if(i == pree && x >= 2)
continue;
if(i == pre && x)
continue;
ans += f(idx + 1, 1, pre, i, (!x ? i > 0 : min(x + 1, 2ll)));
}
}
return dp[idx][b][pree][pre][x] = ans;
}
ll calc(ll x) // Palindrome free <= x
{
memset(dp, -1, sizeof(dp));
s = to_string(x);
return f(0, 0, 10, 10, 0);
}
int main()
{
// memset(dp, -1, sizeof(dp));
cin >> a >> b;
/* a--;
s = to_string(a);
ll ff = f(0, 0, 10, 10);
s = to_string(b);
memset(dp, -1, sizeof(dp));
ll fff = f(0, 0, 10, 10);
cout << fff - ff << endl;
*/
// cout << calc(123) << endl;
// return 0;
// calc(101);
//return 0;
cout << calc(b) - calc(a-1) << endl;
}
Compilation message
numbers.cpp: In function 'll f(ll, bool, ll, ll, ll)':
numbers.cpp:28:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(idx == s.size())
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2168 KB |
Output is correct |
2 |
Correct |
0 ms |
2168 KB |
Output is correct |
3 |
Correct |
0 ms |
2168 KB |
Output is correct |
4 |
Correct |
0 ms |
2168 KB |
Output is correct |
5 |
Correct |
0 ms |
2168 KB |
Output is correct |
6 |
Correct |
0 ms |
2168 KB |
Output is correct |
7 |
Correct |
0 ms |
2168 KB |
Output is correct |
8 |
Correct |
0 ms |
2168 KB |
Output is correct |
9 |
Correct |
0 ms |
2168 KB |
Output is correct |
10 |
Correct |
0 ms |
2168 KB |
Output is correct |
11 |
Correct |
0 ms |
2168 KB |
Output is correct |
12 |
Correct |
0 ms |
2168 KB |
Output is correct |
13 |
Correct |
0 ms |
2168 KB |
Output is correct |
14 |
Correct |
0 ms |
2168 KB |
Output is correct |
15 |
Correct |
0 ms |
2168 KB |
Output is correct |
16 |
Correct |
0 ms |
2168 KB |
Output is correct |
17 |
Correct |
0 ms |
2168 KB |
Output is correct |
18 |
Correct |
0 ms |
2168 KB |
Output is correct |
19 |
Correct |
0 ms |
2168 KB |
Output is correct |
20 |
Correct |
0 ms |
2168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2168 KB |
Output is correct |
2 |
Correct |
0 ms |
2168 KB |
Output is correct |
3 |
Correct |
0 ms |
2168 KB |
Output is correct |
4 |
Correct |
0 ms |
2168 KB |
Output is correct |
5 |
Correct |
0 ms |
2168 KB |
Output is correct |
6 |
Correct |
0 ms |
2168 KB |
Output is correct |
7 |
Correct |
0 ms |
2168 KB |
Output is correct |
8 |
Correct |
0 ms |
2168 KB |
Output is correct |
9 |
Correct |
0 ms |
2168 KB |
Output is correct |
10 |
Correct |
0 ms |
2168 KB |
Output is correct |
11 |
Correct |
0 ms |
2168 KB |
Output is correct |
12 |
Correct |
0 ms |
2168 KB |
Output is correct |
13 |
Correct |
0 ms |
2168 KB |
Output is correct |
14 |
Correct |
0 ms |
2168 KB |
Output is correct |
15 |
Correct |
0 ms |
2168 KB |
Output is correct |
16 |
Correct |
0 ms |
2168 KB |
Output is correct |
17 |
Correct |
0 ms |
2168 KB |
Output is correct |
18 |
Correct |
0 ms |
2168 KB |
Output is correct |
19 |
Correct |
0 ms |
2168 KB |
Output is correct |
20 |
Correct |
0 ms |
2168 KB |
Output is correct |
21 |
Correct |
0 ms |
2168 KB |
Output is correct |
22 |
Correct |
0 ms |
2168 KB |
Output is correct |
23 |
Correct |
0 ms |
2168 KB |
Output is correct |
24 |
Correct |
0 ms |
2168 KB |
Output is correct |
25 |
Correct |
0 ms |
2168 KB |
Output is correct |
26 |
Correct |
0 ms |
2168 KB |
Output is correct |
27 |
Correct |
0 ms |
2168 KB |
Output is correct |
28 |
Correct |
0 ms |
2168 KB |
Output is correct |
29 |
Correct |
0 ms |
2168 KB |
Output is correct |
30 |
Correct |
0 ms |
2168 KB |
Output is correct |
31 |
Correct |
0 ms |
2168 KB |
Output is correct |
32 |
Correct |
0 ms |
2168 KB |
Output is correct |
33 |
Correct |
0 ms |
2168 KB |
Output is correct |
34 |
Correct |
0 ms |
2168 KB |
Output is correct |
35 |
Correct |
0 ms |
2168 KB |
Output is correct |
36 |
Correct |
0 ms |
2168 KB |
Output is correct |
37 |
Correct |
0 ms |
2168 KB |
Output is correct |
38 |
Correct |
0 ms |
2168 KB |
Output is correct |
39 |
Correct |
0 ms |
2168 KB |
Output is correct |
40 |
Correct |
0 ms |
2168 KB |
Output is correct |
41 |
Correct |
0 ms |
2168 KB |
Output is correct |
42 |
Correct |
0 ms |
2168 KB |
Output is correct |
43 |
Correct |
0 ms |
2168 KB |
Output is correct |
44 |
Correct |
0 ms |
2168 KB |
Output is correct |
45 |
Correct |
0 ms |
2168 KB |
Output is correct |