lightoj 1205 数位dp
1205 - Palindromic Numbers
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
Output for Sample Input |
4 1 10 100 1 1 1000 1 10000 |
Case 1: 9 Case 2: 18 Case 3: 108 Case 4: 198 |
PROBLEM SETTER: JANE ALAM JAN
题意:
求区间内回文串数的个数
//开个数组存一下回文串,还要处理前导零。sta表示到目前为止是否合法 #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; int t,bit[20],num[20]; ll f[20][20][2],P,Q; ll dfs(int pos,int s,int sta,bool limit) { if(pos==0) return sta; if(!limit&&f[pos][s][sta]!=-1) return f[pos][s][sta]; int max_b=(limit?bit[pos]:9); ll ans=0; for(int i=0;i<=max_b;i++){ num[pos]=i; if(pos==s&&i==0) ans+=dfs(pos-1,s-1,sta,limit&&(i==max_b)); else if(sta&&pos<=s/2) ans+=dfs(pos-1,s,num[s-pos+1]==i,limit&&(i==max_b)); else ans+=dfs(pos-1,s,sta,limit&&(i==max_b)); } if(!limit) f[pos][s][sta]=ans; return ans; } ll solve(ll x) { if(x<0) return 0; int pos=0; while(x){ bit[++pos]=x%10; x/=10; } return dfs(pos,pos,1,1); } int main() { memset(f,-1,sizeof(f)); scanf("%d",&t); for(int cas=1;cas<=t;cas++){ scanf("%lld%lld",&P,&Q); if(P>Q) swap(P,Q); printf("Case %d: %lld\n",cas,solve(Q)-solve(P-1)); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?