A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this problem you will be given two integers i j, you have to find the number of palindromic numbers between i and j (inclusive).
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a line containing two integers i j (0 ≤ i, j ≤ 1017).
Output
For each case, print the case number and the total number of palindromic numbers between i and j (inclusive).
Sample Input
4
1 10
100 1
1 1000
1 10000
Sample Output
Case 1: 9
Case 2: 18
Case 3: 108
Case 4: 198
写了一整天的一道题
自己的想法tle了
对于数位dp最重要的还是要找处dp数组所要记录的状态
在保证所有满足dp状态的值都符合记录的值的情况下,尽量压缩dp数组的大小(数位dp的时间复杂度就是填满dp数组的复杂度)
再看了题解以后写过了,但对于dp数组所记录的状态自己是想不出的
希望以后彻底懂了之后会来填坑
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
//long long power[20]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,10000000000000};
long long dp[20][20][2];//数位 位数;
long long a[20];//sum当前位数和 k枚举的位数长度
long long num[20];
long long dfs(int lim,int pos,int sta,int k)
{
long long ans=0;
if(pos==0)
return sta;
if(!lim&&dp[pos][k][sta]!=-1) return dp[pos][k][sta];
int s=lim?a[pos]:9;
for(int i=0;i<=s;i++)
{
num[pos]=i;
if(k==pos&&i==0) ans+=dfs(lim&&i==s,pos-1,sta,k-1);
else if(sta&&pos<=(k+1)/2)
{
ans+=dfs(lim&&i==s,pos-1,sta&&num[k+1-pos]==i,k);
}
else
{
ans+=dfs(lim&&i==s,pos-1,sta,k);
}
}
if(!lim) return dp[pos][k][sta]=ans;
return ans;
}
long long solve(long long x)
{
int cnt=0;
while(x!=0)
{
cnt++;
a[cnt]=x%10;
x/=10;
}
return dfs(1,cnt,1,cnt);
}
int main()
{
memset(dp,-1,sizeof(dp));
int t;
int cas=0;
scanf("%d",&t);
while(t--)
{
cas++;
long long temp1,temp2;
scanf("%lld%lld",&temp1,&temp2);
if(temp1>temp2) swap(temp1,temp2);
printf("Case %d: %lld\n",cas,solve(temp2)-solve(temp1-1));
}
}