数位DP。。。。

Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

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 (inclusive).

Sample Input

4

1 10

100 1

1 1000

1 10000

Sample Output

Case 1: 9

Case 2: 18

Case 3: 108

Case 4: 198

Source

Problem Setter: Jane Alam Jan

[Submit]   [Go Back]   [Status]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long int LL; int a[70];
LL dp[70][70]; LL dfs(int len,int l,int r,bool limit,bool ok)
{
if(l<r) return !limit||(limit&&ok);
if(!limit&&~dp[len][l])
return dp[len][l];
LL ret=0;
int mx=limit?a[l]:9;
for(int i=0;i<=mx;i++)
{
if(l==len-1&&i==0)
continue;
int g=ok;
if(g) g=a[r]>=i;
else g=a[r]>i;
ret+=dfs(len,l-1,r+1,limit&&i==mx,g);
}
if(!limit)
dp[len][l]=ret;
return ret;
} LL gaoit(LL n)
{
if(n<0) return 0;
if(n==0) return 1;
int len=0;
while(n){a[len++]=n%10;n/=10;}
LL ret=1;
for(int i=len;i>=1;i--)
ret+=dfs(i,i-1,0,i==len,1);
return ret;
} int main()
{
int T_T,cas=1;
cin>>T_T;
memset(dp,-1,sizeof(dp));
while(T_T--)
{
LL x,y;
cin>>x>>y;
if(x>y) swap(x,y);
printf("Case %d: %lld\n",cas++,gaoit(y)-gaoit(x-1));
}
return 0;
}

LightOJ 1205 Palindromic Numbers的更多相关文章

  1. light 1205 - Palindromic Numbers(数位dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1205 题解:这题作为一个数位dp,是需要咚咚脑子想想的.这个数位dp方程可能不 ...

  2. light oj 1205 - Palindromic Numbers 数位DP

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

  3. lightoj 1205 数位dp

    1205 - Palindromic Numbers    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...

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

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

  5. LightOJ - 1396 :Palindromic Numbers (III)(逐位确定法)

    Vinci is a little boy and is very creative. One day his teacher asked him to write all the Palindrom ...

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

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

  7. Lightoj1205——Palindromic Numbers(数位dp+回文数)

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

  8. xtu summer individual 1 E - Palindromic Numbers

    E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %l ...

  9. Palindromic Numbers LightOJ - 1205

    题目大意: 求区间内的回文数个数 题目思路: 数位dp,先枚举前一半数字,然后填上相应的后一半数字. #include<cstdio> #include<cstring> #i ...

随机推荐

  1. 【SSH 基础】浅谈Hibernate关系映射(4)

    继上篇博客 多对多关联映射(单向) 多对多对象关系映射,须要增加一张新表完毕基本映射. Hibernate会自己主动生成中间表 Hibernate使用many-to-many标签来表示多对多的关联,多 ...

  2. html+css实现登录界面

    <!DOCTYPE html> <style type="text/css"> body{ background-color: #555555; } #ti ...

  3. centos 6.5 升级内核 linux 3.12.17

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G) 系统版本:Linux centos 2.6.32-431.el6.x86_64(Centos-6.5-x86_64-mi ...

  4. Java Executor 框架

    Java Executor 框架 Executor框架是指java5中引入的一系列并发库中与executor相关的功能类,包括Executor.Executors. ExecutorService.C ...

  5. HUNNU11351:Pythagoras's Revenge

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11351&courseid=0 Problem des ...

  6. python学习笔记之四:条件,循环和其他语句

    前面已经介绍过几种基本语句(print,import,赋值语句),下面我们来介绍条件语句,循环语句. 一. print和import的更多信息 1.1 使用逗号输出 A.打印多个表达式,用逗号隔开,会 ...

  7. fragment android

    在Eoe中看到了 一个关于的 详细讲解,相信对 学Fragment 有帮助 android fragment基础与源码案例: Fragment动画效果 http://www.eoeandroid.co ...

  8. SVM-SVM概述

    (一)SVM背景资料简介 支持向量机(Support Vector Machine)这是Cortes和Vapnik至1995首次提出,样本.非线性及高维模式识别中表现出很多特有的优势,并可以推广应用到 ...

  9. 【iOS】使用SQLite与FMDB

    iOS中的SQLite与Android中的一模一样,仅仅是调用方法有差异.假设单从调用来讲,Android封装的一套helper更好用一些,而iOS原生的用C语言的几个函数在操作,比較麻烦.只是引入第 ...

  10. 从最大似然到EM算法浅解

    从最大似然到EM算法浅解 zouxy09@qq.com http://blog.csdn.net/zouxy09 机器学习十大算法之中的一个:EM算法.能评得上十大之中的一个,让人听起来认为挺NB的. ...