欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Palindromic Numbers LightOJ - 1205 数位dp 求回文数

发布时间:2023/12/4 编程问答 49 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Palindromic Numbers LightOJ - 1205 数位dp 求回文数 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

传送门

文章目录

  • 题意:
  • 思路:

题意:

[l,r][l,r][l,r]中有多少个回文数。

思路:

裸的数位dpdpdp啦,记dp[pos][pre][state]dp[pos][pre][state]dp[pos][pre][state]表示到了第pospospos位,回文是从第preprepre位开始的,并且当前是否为回文串statestatestate。这样就可以把要求的数字特征表示出来,让后还要记录一下选的数是多少,在pos<=(pre−1)/2pos<=(pre-1)/2pos<=(pre1)/2的时候就需要判断是否为回文串了。
好长时间没写数位dpdpdp了,solvesolvesolve传值的时候LLLLLL传成了intintint

//#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; } /**/

总结

以上是生活随笔为你收集整理的Palindromic Numbers LightOJ - 1205 数位dp 求回文数的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。