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. User implements HttpSessionBindingListener

    public class User implements HttpSessionBindingListener { private int id; private String userName; p ...

  2. Java读取property配置文件

    读取配置文件已经成了Java程序员工作的一项必备技能. 配置文件的优点: 可维护性好 怎么个可维护性好呢? 它会让程序中变化的地方很灵活的配置,不需要修改代码. Java程序部署到服务器上去之后就变成 ...

  3. 设计模式的征途—14.职责链(Chain of Responsibility)模式

    相信大家都玩过类似于“斗地主”的纸牌游戏,某人出牌给他的下家,下家看看手中的牌,如果要不起,则将出牌请求转发给他的下家,其下家再进行判断.一个循环下来,如果其他人都要不起该牌,则最初的出牌者可以打出新 ...

  4. 2017年1月1日 App Store中的所有应用都必须启用 App Transport Security安全功能

    2017年1月1日 App Store中的所有应用都必须启用 App Transport Security安全功能,否则极有可能被拒! 在WWDC 2016开发者大会上,苹果宣布了一个最后期限:到20 ...

  5. 整理的 matplotlib 绘图笔记

  6. jquery 禁止herf跳转,并执行相应的js代码

    <a class="oh_btn bg_3" href="javascript:void(0);" onclick="myfun(this)&q ...

  7. ffmpeg.exe下载

    下载链接:http://pan.baidu.com/s/1cGTe6y

  8. VMware workstation批量创建虚拟机和自动化安装操作系统(二)

    一. 简述 在上一篇<VMware workstation批量创建虚拟机和自动化安装操作系统(一)>中,主要介绍了VMware workstation自定义创建虚拟机的过程,和一些其他的有 ...

  9. Java GC - 监控回收行为与日志分析

    1. 简介 在上一篇介绍<Java GC - 垃圾回收机制>, 本文将介绍如何监控 Javc GC 行为,同时涉及一些GUI工具的使用(虽然有些已经很老并不再更新),监控GC在于判断JVM ...

  10. Spring-mybatis没有了XXXmapper.java和Dao的实现类还有Service的实现类

    对于刚学过框架的同学可能知道,mybatis有两种主要的配置文件: SqlMapConfig.xml(mybatis全局配置文件,名称不固定,用来配置运行环境(数据源.事务) XXXmapper.xm ...