题目大意:
求区间内的回文数个数
题目思路:
数位dp,先枚举前一半数字,然后填上相应的后一半数字。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#define MAXSIZE 1000005
#define LL long long using namespace std; LL dp[][][];
int temp[MAXSIZE],bit[MAXSIZE]; LL dfs(int pos,int st,int pre,int limit)
{
if(pos < )
return ;
if(!limit && dp[pos][st][pre]!=-)
return dp[pos][st][pre];
int len = limit?bit[pos]:;
LL ans = ;
for(int i=;i<=len;i++)
{
temp[pos] = i;
if(pre== && i==)
{
ans += dfs(pos-,st-,,i==len&&limit);
} else if(pos > st/) //枚举前一半
{
ans += dfs(pos-,st,,i==len&&limit);
} else if(temp[pos] == temp[st-pos+]) //填上后一半
{
ans += dfs(pos-,st,,i==len&&limit);
}
} if(!limit)
dp[pos][st][pre] = ans;
return ans;
} LL Solve(LL n)
{
int pos = ;
memset(dp,-,sizeof(dp));
while(n)
{
bit[++pos] = n%;
n /= ;
}
LL ans = dfs(pos,pos,,);
return ans;
} int main()
{
int T,cns=;
LL n,m;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
if(n > m)
swap(n,m);
LL ans = Solve(m) - Solve(n-);
printf("Case %d: %lld\n",cns++,ans);
}
return ;
}