Răspuns :
Salut! Cunosc problema si am sa te ajut cu solutia oficiala. Succes!
#include <cstdlib>
#include <fstream>
using namespace std;
long long COUNT_FIXED_LEN[19];
void preprocess()
{
COUNT_FIXED_LEN[0] = 1;
COUNT_FIXED_LEN[1] = 9;
COUNT_FIXED_LEN[2] = 9;
for (int i = 3; i < 19; i++)
COUNT_FIXED_LEN[i] = 10 * COUNT_FIXED_LEN[i - 2];
}
long countLessThan(long long n)
{
long long p = 100000000000000000;
long long len = 18;
long long count = 0;
long long first, last, half, step;
if (n < 0) return 0;
if (n == 0) return 1;
while (p > n)
{
p /= 10;
len--;
}
if (len == 1) return (n + 1);
first = n / p;
last = n % 10;
if (first > last)
{
n -= (last + 1);
if (p > n)
{
p /= 10;
len--;
}
}
half = 0;
step = 0;
while (len >= 1)
{
first = n / p;
last = n % 10;
if (first > last)
{
n -= (last + 1);
first = n / p;
}
half = (step == 0) ? (first - 1) : (half*10 + first);
n = (n - first*p) / 10;
p /= 100;
len -= 2;
step++;
}
len = (len != 0) ? 2*(step - 1) : 2*step - 1;
count = half + 1;
for (int i = 0; i <= len; i++)
count += COUNT_FIXED_LEN[i];
return count;
}
int main()
{
fstream f, g;
long long a, b;
f.open("palindromuri.in", ios::in);
g.open("palindromuri.out", ios::out);
preprocess();
f >> a >> b;
g << countLessThan(b) - countLessThan(a - 1);
f.close();
g.close();
return 0;
}
#include <cstdlib>
#include <fstream>
using namespace std;
long long COUNT_FIXED_LEN[19];
void preprocess()
{
COUNT_FIXED_LEN[0] = 1;
COUNT_FIXED_LEN[1] = 9;
COUNT_FIXED_LEN[2] = 9;
for (int i = 3; i < 19; i++)
COUNT_FIXED_LEN[i] = 10 * COUNT_FIXED_LEN[i - 2];
}
long countLessThan(long long n)
{
long long p = 100000000000000000;
long long len = 18;
long long count = 0;
long long first, last, half, step;
if (n < 0) return 0;
if (n == 0) return 1;
while (p > n)
{
p /= 10;
len--;
}
if (len == 1) return (n + 1);
first = n / p;
last = n % 10;
if (first > last)
{
n -= (last + 1);
if (p > n)
{
p /= 10;
len--;
}
}
half = 0;
step = 0;
while (len >= 1)
{
first = n / p;
last = n % 10;
if (first > last)
{
n -= (last + 1);
first = n / p;
}
half = (step == 0) ? (first - 1) : (half*10 + first);
n = (n - first*p) / 10;
p /= 100;
len -= 2;
step++;
}
len = (len != 0) ? 2*(step - 1) : 2*step - 1;
count = half + 1;
for (int i = 0; i <= len; i++)
count += COUNT_FIXED_LEN[i];
return count;
}
int main()
{
fstream f, g;
long long a, b;
f.open("palindromuri.in", ios::in);
g.open("palindromuri.out", ios::out);
preprocess();
f >> a >> b;
g << countLessThan(b) - countLessThan(a - 1);
f.close();
g.close();
return 0;
}