forked from sameer2800/Lightoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1205-palindromic_numbers.cpp
130 lines (100 loc) · 2.3 KB
/
1205-palindromic_numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
ll arr[80];
ll dp[80][80][80];
vector <pair <ll, ll> > v;
ll func(int ind, string y, int n, int xlow, int xhigh)
{
if (ind == (n + 1) / 2 ) {
if (xlow <= xhigh)
return 1;
else
return 0;
}
if (dp[ind][xlow][xhigh] != -1)
return dp[ind][xlow][xhigh];
ll ans = 0, start;
if (ind == 0)
start = 1;
else
start = 0;
for (int d = start; d <= 9; d++) {
int leftmost_lo = xlow, leftmost_hi = xhigh;
if (d < y[ind] - '0' and ind < leftmost_lo) leftmost_lo = ind;
if (d < y[n - ind - 1 ] - '0' and (n - ind - 1) < leftmost_lo) leftmost_lo = n - ind - 1;
if (d > y[ind] - '0' and ind < leftmost_hi) leftmost_hi = ind;
if (d > y[n - ind - 1] - '0' and (n - ind - 1) < leftmost_hi) {
leftmost_hi = n - ind - 1 ;
}
ans += func(ind + 1, y, n, leftmost_lo, leftmost_hi);
}
return dp[ind][xlow][xhigh] = ans;
}
void precompute()
{
string loc = "";
int i = 0;
while (i < 22) {
memset(dp, -1, sizeof dp);
loc = loc + "9";
arr[i + 1] = func(0, loc, loc.length(), loc.length(), loc.length());
//cout << arr[i + 1] << endl;
i++;
}
}
int palcheck(string k)
{
string newk = k;
reverse(k.begin(), k.end());
return k == newk ? 1 : 0;
}
string n, m;
void swap1()
{
if (n.length() > m.length())
swap(n, m);
else if (n.length() == m.length()) {
if (n > m)
swap(n, m);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/sameer/Documents/sameer/input.sam", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
ll i, j, k, t, cont, a, b;
cin >> t;
ll cases = t;
precompute();
while (t--) {
cin >> n >> m;
swap1();
//cout << n << " " << m << endl;
ll lenn = n.length() , lenm = m.length();
memset(dp, -1, sizeof dp);
ll ans = func(0, m, m.length(), m.length(), m.length() );
//cout << ans << " " << m << endl;
for (i = 1; i < m.length(); i++) {
ans += arr[i];
//cout << "ekj" << arr[i] << endl;
}
//cout << ans << endl;
memset(dp, -1, sizeof dp);
ll ans2 = func(0, n, n.length(), n.length(), n.length());
if (ans2 != 0)
ans2 -= palcheck(n);
for (i = 1; i < n.length(); i++) {
ans2 += arr[i];
}
//cout << ans2 << endl;
int k1 = atoi(n.c_str());
if (k1 == 0)
ans++;
cout << "Case " << cases - t << ": " << ans - ans2 << endl;
}
return 0;
}