Skip to content

Instantly share code, notes, and snippets.

@MinecraftFuns
Created February 9, 2021 13:17
Show Gist options
  • Save MinecraftFuns/8ecae1ca0531d6c867636f2cc603b547 to your computer and use it in GitHub Desktop.
Save MinecraftFuns/8ecae1ca0531d6c867636f2cc603b547 to your computer and use it in GitHub Desktop.
constexpr int DIGIT = 50;
namespace sol
{
long long f[DIGIT][DIGIT][2];
int a[DIGIT];
int _temp[DIGIT];
inline bool valid(const int &digit)
{
return digit == 0 || digit == 1 || digit == 8;
}
inline bool valid(const char &digit)
{
return digit == '0' || digit == '1' || digit == '8';
}
long long dfs(int pos, int start, bool flag, bool limit)
{
if (pos == -1)
{
return flag;
}
if (!limit && f[pos][start][flag] != -1)
{
return f[pos][start][flag];
}
long long res = 0;
register int up = limit ? a[pos] : 9;
for (register int i = 0; i <= up; i++)
{
if (valid(i))
{
register bool st = (pos == start && i == 0);
register bool _cac = flag && !st && pos < ((start + 1) >> 1);
register bool fl = _cac ? _temp[start - pos] == i : flag;
_temp[pos] = i;
res += dfs(pos - 1, st ? start - 1 : start, fl, limit && (i == up));
}
}
if (!limit)
{
f[pos][start][flag] = res;
}
return res;
}
inline long long cal(char *str)
{
register int len = strlen(str);
for (register int i = 0; i < len; i++)
{
a[i] = (int)str[len - i - 1] - (int)'0';
}
a[len] = 0;
return dfs(len - 1, len - 1, 1, 1);
}
} // namespace sol
namespace ma
{
using namespace sol;
int T;
char L[DIGIT], R[DIGIT];
inline void init()
{
memset(f, -1, sizeof(f));
}
inline void in()
{
init();
io::read(T);
for (register int _ = 0; _ < T; _++)
{
scanf("%s %s", L, R);
long long ans = cal(R) - cal(L);
register int len = strlen(L);
bool flag = 1;
for (register int i = 0; i < len; i++)
{
if (!valid(L[i]) || (L[i] != L[len - i - 1]))
{
flag = 0;
break;
}
}
if (flag)
{
ans++;
}
io::writeln(ans);
}
}
} // namespace ma
int main()
{
ma::in();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment