这题在求回文串的基础上还要等凹。
可以视为LightOJ 1205的升级版,不知道如何写回文串的数位DP可以打开链接LightOJ 1205
因此这题只需要加多2维,最高位的数字以及前一位(用于判断升降) (此思想来自于点击打开链接)
在回文串的前半部分不能大于pre,后半段不能小于pre。
然后最后判断是否全为一个数字就可以了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <climits>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <sstream>
#define INF 0x3f3f3f3f
#define LL long long
#define fora(i,a,n) for(int i=a;i<=n;i++)
#define fors(i,n,a) for(int i=n;i>=a;i--)
#define sci(x) scanf("%d",&x);
#define scl(x) scanf("%lld",&x);
#define MAXN 100024
const double eps = 1e-8;
using namespace std;
LL t, l, r, k, z = 1;
LL dp[22][22][10][10][2], digits[22], num[22];
LL dfs(LL start, LL dex,LL pre,LL s_n, LL flag, LL p,LL same) {
LL sum = 0;
if (dex == 0) return flag&&!same;
if (dp[start][dex][pre][s_n][flag] && !p)
return dp[start][dex][pre][s_n][flag];
LL u = p ? digits[dex] : 9;
fora(i, 0, u) {
num[dex] = i;
if (start == dex&&i == 0) //前导0
sum += dfs(start - 1, dex - 1,9,0, flag, p&&i == u,1);
else if (flag && (start / 2 + 1) > dex) {//回文串后半部分
if (i < pre) continue;//不能小于前一个数字,否则不符合题目等凹
sum += dfs(start, dex - 1,i,num[start], num[start - dex + 1] == i, p&&i == u,same&&i==s_n);
}
else {// 回文串前半部分
if (i > pre) continue;//不能大于前一个数字,否则不符合题目等凹
sum += dfs(start, dex - 1,i,num[start], flag, p&&i == u,same&&i==num[start]);
}
}
if (!p) dp[start][dex][pre][s_n][flag] = sum;
return sum;
}
LL digit(LL n) {
k = 0;
while (n) {
digits[++k] = n % 10;
n /= 10;
}
return dfs(k, k,9,0, 1, 1,1);
}
int main() {
#ifdef DID
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
scl(t);
memset(dp, 0, sizeof(dp));
while (t--) {
scl(l); scl(r);
if (l>r) swap(l, r);
LL sum1 = digit(r);
LL sum2 = digit(l - 1);
printf("%lld\n", sum1 - sum2);
}
return 0;
}