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 

1 10 
100 1 
1 1000 
1 10000 
Output for Sample Input 
Case 1: 9 
Case 2: 18 
Case 3: 108 
Case 4: 198

代码:

 #include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[],tmp[];
ll dp[][][];
ll dfs(int start,int pos,int ok,bool limit)
{
if(pos<)
return ok;
if(!limit&&dp[pos][ok][start]!=-)
return dp[pos][ok][start];
ll ans=;
int up=limit?a[pos]:;
for(int d=; d<=up; ++d)
{
tmp[pos]=d;
if(start==pos&&d==)
ans+=dfs(start-,pos-,ok,limit&&d==up);
else if(ok&&pos<(start+)/)
ans+=dfs(start,pos-,tmp[start-pos]==d,limit&&d==up);
else
ans+=dfs(start,pos-,ok,limit&&d==up);
}
if(!limit)
dp[pos][ok][start]=ans;
return ans;
}
ll solve(ll x)
{
memset(a,,sizeof(a));
int cnt=;
while(x!=)
{
a[cnt++]=x%;
x/=;
}
return dfs(cnt-,cnt-,,);
}
int main()
{
memset(dp,-,sizeof(dp));
int t,cnt=;
scanf("%d",&t);
while(t--)
{
ll x,y;
scanf("%lld%lld",&x,&y);
if(x>y)
swap(x,y);
printf("Case %d: %lld\n",cnt++,solve(y)-solve(x-));
}
return ;
}

Lightoj1205——Palindromic Numbers(数位dp+回文数)的更多相关文章

  1. LightOJ - 1205:Palindromic Numbers (数位DP&回文串)

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  2. light oj 1205 - Palindromic Numbers 数位DP

    思路:搜索的时候是从高位到低位,所以一旦遇到非0数字,也就确定了数的长度,这样就知道回文串的中心点. 代码如下: #include<iostream> #include<cstdio ...

  3. 回文数 第N个回文数

    判断回文数还是不难,如果能转为字符串就更简单了. 如果是求第N个回文数呢. 12321是一个回文数,这里先考虑一半的情况. 回文数的个数其实是有规律的.如: 1位回文数: 9个 2位回文数: 9个 3 ...

  4. [暑假集训--数位dp]LightOj1205 Palindromic Numbers

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  5. [Swust OJ 797]--Palindromic Squares(回文数水题)

    题目链接:http://acm.swust.edu.cn/problem/797/ Time limit(ms): 1000 Memory limit(kb): 10000   Description ...

  6. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) (进制转换,回文数)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  7. POJ2402 Palindrome Numbers 回文数

    题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是 ...

  8. POJ2402 Palindrome Numbers第K个回文数——找规律

    问题 给一个数k,给出第k个回文数  链接 题解 打表找规律,详见https://www.cnblogs.com/lfri/p/10459982.html,差别仅在于这里从1数起. AC代码 #inc ...

  9. POJ 2402 Palindrome Numbers(LA 2889) 回文数

    POJ:http://poj.org/problem?id=2402 LA:https://icpcarchive.ecs.baylor.edu/index.php?option=com_online ...

随机推荐

  1. [图形学] 习题8.6 线段旋转后使用Cohen-Sutherland算法裁剪

    习题8.6 生成一条比观察窗口对角线还长的线段动画,线段重点位于观察窗口中心,每一帧的线段在上一帧基础上顺时针旋转一点,旋转后用Cohen-Sutherland线段裁剪算法进行裁剪. 步骤: 1 视口 ...

  2. OpenCV探索之路(十八):使用imwrite调整保存的图片质量

    近日在用opencv做一些图像处理的操作时,需要对一些高分辨率的图像进行保存.比如,在操作一个容量为230M的图像后,并对该图像保存为JPG格式后,发现图像容量变为80M了!针对这个问题,忙了大半天, ...

  3. 自定义MapReduce中数据类型

    数据类型(都实现了Writable接口) BooleanWritable 布尔类型 ByteWritable 单字节数值 DoubleWritable 双字节数值 FloatWritable 浮点数 ...

  4. Postgresql快速写入/读取大量数据(.net)

    环境及测试 使用.net驱动npgsql连接post数据库.配置:win10 x64, i5-4590, 16G DDR3, SSD 850EVO. postgresql 9.6.3,数据库与数据都安 ...

  5. 将git版本号编译进程序

    问题的提出 不管是什么版本管理工具,每一条提交记录都会有一个对应的版本号,一般是一个整数,git是一个hash字符串.不管怎样,这个版本号是唯一的,有时候我们在程序运行的时候会在日志里面输出程序的版本 ...

  6. nyoj_600:花儿朵朵(树状数组+坐标离散化)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=600 只附代码好了 #include<bits/stdc++.h> using name ...

  7. Linux之grep及正则表达式

    grep简介 grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.通常grep有三种版本grep.egrep(等同于grep -E)和fgrep.egrep为扩展的g ...

  8. FTP publisher plugin插件

    说明:这个插件可以将构建的产物(例如:Jar)发布到FTP中去. 官方说明:FTP publisher plugin 安装步骤: 系统管理→管理插件→可选插件→Artifact Uploaders→F ...

  9. [肯定不知道]PeopleSoft中PSADMIN你不知道的秘密

    PeopleSoft psadmin工具是用于管理PS App server,process scheduler 和 web server节点的.可以使用一些设置菜单选项来管理或配置上面提到的任何组件 ...

  10. 将Asp.Net Core和corefx移植到.Net 4.0

    引言 因为工作内容的原因需要兼容 XP,而 XP 最多支持到.Net Framework 4.0.因此无法享受到 .Net Core 带来的一堆很好用的库,好在无论 corefx 还是 Asp.Net ...