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

4

1 10

100 1

1 1000

1 10000

Sample Output

Case 1: 9

Case 2: 18

Case 3: 108

Case 4: 198

题意:求区间的回文串数量。

思路:从两头向中间靠,前缀是否小于原数用tag表示,后缀是否小于原数用ok表示,注意后缀尽管后面的比原位大,但是前面的小一点可以抵消其效果。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
ll dp[][][][]; int d[],cnt;
ll get(int bg,int l,int r,int tag,bool ok)
{
if(r>l) return !tag||(tag&&ok);
if(!tag&&dp[bg][l][tag][ok]) return dp[bg][l][tag][ok];
int lim=tag?d[l]:; ll res=;
rep(i,,lim){
if(bg==l&&i==) continue;
bool g=ok;
if(ok) g=i<=d[r];
else g=i<d[r];
res+=get(bg,l-,r+,tag&&(i==lim),g);
}
return tag?res:dp[bg][l][tag][ok]=res;
}
ll cal(ll x)
{
if(x<) return 0LL;if(x==) return 1LL;
ll res=; cnt=;
while(x) d[++cnt]=x%,x/=;
rep(i,,cnt) res+=get(i,i,,i==cnt,true);
return res;
}
int main()
{
int T,C=; ll L,R; scanf("%d",&T);
while(T--){
scanf("%lld%lld",&L,&R); if(L>R) swap(L,R);
printf("Case %d: %lld\n",++C,cal(R)-cal(L-));
}
return ;
}

有部分数组没有必要:

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
ll dp[][]; int d[],cnt;
//tag维护前缀是否小于,ok维护后缀是否小于。维护二者不一样。
ll get(int bg,int l,int r,int tag,bool ok)
{
if(r>l) return !tag||(tag&&ok);
if(!tag&&dp[bg][l]) return dp[bg][l];
int lim=tag?d[l]:; ll res=;
rep(i,,lim){
if(bg==l&&i==) continue;
bool g=ok;
if(ok) g=i<=d[r];
else g=i<d[r];
res+=get(bg,l-,r+,tag&&(i==lim),g);
}
return tag?res:dp[bg][l]=res;
}
ll cal(ll x)
{
if(x<) return 0LL;if(x==) return 1LL;
ll res=; cnt=;
while(x) d[++cnt]=x%,x/=;
rep(i,,cnt) res+=get(i,i,,i==cnt,true);
return res;
}
int main()
{
int T,C=;ll L,R; scanf("%d",&T);
while(T--){
scanf("%lld%lld",&L,&R); if(L>R) swap(L,R);
printf("Case %d: %lld\n",++C,cal(R)-cal(L-));
}
return ;
}

LightOJ - 1205:Palindromic Numbers (数位DP&回文串)的更多相关文章

  1. light oj 1205 - Palindromic Numbers 数位DP

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

  2. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. poj 1159 dp回文串

    题意:添加最少的字符使之成为回文串 #include<cstdio> #include<iostream> #include<algorithm> #include ...

  4. 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】

    O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...

  5. poj 3280 Cheapest Palindrome ---(DP 回文串)

    题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...

  6. HDU4745--区间DP+回文串

    这题的题意为,给你一个环状的字符串,有两只兔子分别从某任意的石头上开始跳跃.一只顺时针跳.一只逆时针跳.两只兔子每一次落脚处石头的质量都相同.兔子要一步一步的跳,且不能跳到之前跳到过的地方.总的来说, ...

  7. CodeForces-245H:Queries for Number of Palindromes(3-14:区间DP||回文串)

    Times:5000ms: Memory limit:262144 kB 给定字符串S(|S|<=5000),下标由1开始.然后Q个问题(Q<=1e6),对于每个问题,给定L,R,回答区间 ...

  8. Leetcode0005--Longest Palindromic Substring 最长回文串

    [转载请注明]http://www.cnblogs.com/igoslly/p/8726771.html 来看一下题目: Given a string s, find the longest pali ...

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

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

随机推荐

  1. MonkeyScript使用教程

    原文地址https://www.cnblogs.com/yizhou-xu/p/8072813.html 原文地址https://www.cnblogs.com/YatHo/p/7205162.htm ...

  2. ACM ICPC, Damascus University Collegiate Programming Contest(2018) Solution

    A:Martadella Stikes Again 水. #include <bits/stdc++.h> using namespace std; #define ll long lon ...

  3. STM32之独立版USB(Host)驱动+MSC+Fatfs移植

    源:STM32之独立版USB(Host)驱动+MSC+Fatfs移植 STM32之USB驱动库详解(架构+文件+函数+使用说明+示例程序)

  4. Python笔记 #18# Pandas: Grouping

    10 Minutes to pandas 引 By “group by” we are referring to a process involving one or more of the foll ...

  5. CentOS下 Nginx1.13.5 + PHP7.1.10 + MySQL5.7.19 源码编译安装

    一.安装Nginx ①安装依赖扩展 # yum -y install wget openssl* gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng l ...

  6. 2018-2019-1 20189215 《Linux内核原理与分析》第八周作业

    可执行程序工作原理 <庖丁解牛>第七章书本知识总结 "目标文件"是指编译器生成的文件,"目标"指的是目标平台,例如x86或x64,它决定了编译器使用 ...

  7. 20172305 2018-2019-1 《Java软件结构与数据结构》第三周学习总结

    20172305 2018-2019-1 <Java软件结构与数据结构>第三周学习总结 教材学习内容总结 本周内容主要为书第五章内容: 队列 线性集合(元素从一端加入,另一端删除) 先进先 ...

  8. HDU 6315 Naive Operations(线段树+区间维护)多校题解

    题意:a数组初始全为0,b数组题目给你,有两种操作: 思路:dls的思路很妙啊,我们可以将a初始化为b,加一操作改为减一,然后我们维护一个最小值,一旦最小值为0,说明至少有一个ai > bi,那 ...

  9. UVa 11825 黑客的攻击(状态压缩dp)

    https://vjudge.net/problem/UVA-11825 题意: 假设你是一个黑客,侵入了一个有着n台计算机(编号为0,1,...,n-1)的网络.一共有n种服务,每台计算机都运行着所 ...

  10. 一起动手打造个人娱乐级linux

    我们使用电脑,一直以来用的都是windows,但是对于像我这种爱折腾的人来说,尝试使用linux系统应该是一种不错的体验.说到linux,许多人可能都没听过,或者知道的人对它印象是这样的: 然而,li ...