题意:
求 [ l , r ] [l,r] [l,r]中有多少个回文数。
思路:
裸的数位
d
p
dp
dp啦,记
d
p
[
p
o
s
]
[
p
r
e
]
[
s
t
a
t
e
]
dp[pos][pre][state]
dp[pos][pre][state]表示到了第
p
o
s
pos
pos位,回文是从第
p
r
e
pre
pre位开始的,并且当前是否为回文串
s
t
a
t
e
state
state。这样就可以把要求的数字特征表示出来,让后还要记录一下选的数是多少,在
p
o
s
<
=
(
p
r
e
−
1
)
/
2
pos<=(pre-1)/2
pos<=(pre−1)/2的时候就需要判断是否为回文串了。
好长时间没写数位
d
p
dp
dp了,
s
o
l
v
e
solve
solve传值的时候
L
L
LL
LL传成了
i
n
t
int
int。
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
LL l,r;
int a[N],num[N],tot;
LL f[30][30][2];
LL dfs(int pos,int pre,int state,int flag)
{
if(pos==-1) return state;
if(flag&&f[pos][pre][state]!=-1) return f[pos][pre][state];
int x=flag? 9:a[pos];
LL ans=0;
for(int i=0;i<=x;i++)
{
num[pos]=i;
if(i==0&&pos==pre) ans+=dfs(pos-1,pre-1,state,flag||i<x);
else if(pos<=(pre-1)/2&&state) ans+=dfs(pos-1,pre,state&&(num[pre-pos]==i),flag||i<x);
else ans+=dfs(pos-1,pre,state,flag||i<x);
}
if(flag) f[pos][pre][state]=ans;
return ans;
}
LL solve(LL x)
{
tot=0;
if(x<0) return 0;
if(x==0) return 1;
while(x)
{
a[tot++]=x%10;
x/=10;
}
return dfs(tot-1,tot-1,1,0);
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
memset(f,-1,sizeof(f));
int _; scanf("%d",&_);
for(int __=1;__<=_;__++)
{
scanf("%lld%lld",&l,&r);
if(l>r) swap(l,r);
printf("Case %d: %lld\n",__,solve(r)-solve(l-1));
}
return 0;
}
/*
*/
带前导零,更好看点的代码:
// Problem: Palindromic Numbers
// Contest: Virtual Judge - LightOJ
// URL: https://vjudge.net/problem/LightOJ-1205
// Memory Limit: 65 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
LL x,y;
int a[300],tot;
int f[30][30][2];
int num[30];
LL dp(int pos,int st,int state,int lead,int flag) {
if(pos==0) return state;
if(f[pos][st][state]!=-1&&lead&&flag) return f[pos][st][state];
LL ans=0;
int x=flag? 9:a[pos];
for(int i=0;i<=x;i++) {
num[pos]=i;
if(!lead) {
ans+=dp(pos-1,i==0? st:pos,state,lead||i>0,flag||i<x);
} else {
if(pos<=st/2&&state) ans+=dp(pos-1,st,state&&(i==num[st-pos+1]),lead,flag||i<x);
else ans+=dp(pos-1,st,state,lead,flag||i<x);
}
}
if(lead&&flag) f[pos][st][state]=ans;
return ans;
}
LL solve(LL x) {
if(x<0) return 0;
if(x==0) return 1;
tot=0;
while(x) a[++tot]=x%10,x/=10;
return dp(tot,0,1,0,0);
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
memset(f,-1,sizeof(f));
int _; scanf("%d",&_);
for(int i=1;i<=_;i++) {
LL a,b;
scanf("%lld%lld",&a,&b); if(a>b) swap(a,b);
printf("Case %d: %lld\n",i,solve(b)-solve(a-1));
}
return 0;
}
/*
*/