Lightoj1205——Palindromic Numbers(数位dp+回文数)
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
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+回文数)的更多相关文章
- LightOJ - 1205:Palindromic Numbers (数位DP&回文串)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- light oj 1205 - Palindromic Numbers 数位DP
思路:搜索的时候是从高位到低位,所以一旦遇到非0数字,也就确定了数的长度,这样就知道回文串的中心点. 代码如下: #include<iostream> #include<cstdio ...
- 回文数 第N个回文数
判断回文数还是不难,如果能转为字符串就更简单了. 如果是求第N个回文数呢. 12321是一个回文数,这里先考虑一半的情况. 回文数的个数其实是有规律的.如: 1位回文数: 9个 2位回文数: 9个 3 ...
- [暑假集训--数位dp]LightOj1205 Palindromic Numbers
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- [Swust OJ 797]--Palindromic Squares(回文数水题)
题目链接:http://acm.swust.edu.cn/problem/797/ Time limit(ms): 1000 Memory limit(kb): 10000 Description ...
- 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 ...
- POJ2402 Palindrome Numbers 回文数
题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是 ...
- POJ2402 Palindrome Numbers第K个回文数——找规律
问题 给一个数k,给出第k个回文数 链接 题解 打表找规律,详见https://www.cnblogs.com/lfri/p/10459982.html,差别仅在于这里从1数起. AC代码 #inc ...
- POJ 2402 Palindrome Numbers(LA 2889) 回文数
POJ:http://poj.org/problem?id=2402 LA:https://icpcarchive.ecs.baylor.edu/index.php?option=com_online ...
随机推荐
- angular popover的触发问题;
popover 一般如下用法; <div uib-popover="内容" popover-animation="false" popover-appen ...
- Jenkins构建Android项目持续集成之单元测试及代码覆盖率
单元测试 在软件开发中一直在推崇TDD(测试驱动开发),但是一直不能被有效的执行或者并不是真正的测试驱动开发(先开发后写单元测试),因为我们懒!而Android开发又是大多应用层面的开发,很多都是和视 ...
- Hibernate update 和 merge 、saveOrUpdate的区别
this.getSession().update(obj); this.getSession().merge(obj); this.getSession().saveOrUpdate(obj); 1. ...
- 【SqlServer系列】开启Sqlserver远程访问
1 概述 已发布[SqlServer系列]文章如下: [SqlServer系列]SQLSERVER安装教程 [SqlServer系列]数据库三大范式 [SqlServer系列]表单查询 [SqlS ...
- python cookbook第三版学习笔记十:类和对象(一)
类和对象: 我们经常会对打印一个对象来得到对象的某些信息. class pair: def __init__(self,x,y): self.x=x self. ...
- Java项目集成SAP BO
SAP BO报表查看需要登录SAP BO系统,为了方便公司希望将BO报表集成到OA系统中,所以参考网上资料加上与SAP BO的顾问咨询整理出一套通过Java来集成SAP BO的功能. SAPBO中的报 ...
- nyoj_68:三点顺序(计算几何)
题目链接 根据 AB*AC的值进行判断即可(ps,结果为0时不构成三角形) #include<iostream> #include<cstdio> #include<cs ...
- 一步一步学Vue(五)
本篇是是vue路由的开篇,会以一个简单的demo对vue-router进行一个介绍,主要覆盖以下几个常用场景: 1.路由跳转 2.嵌套路由 3.路由参数 1.Vue-Router 一般来说,路由定义就 ...
- iOS事件响应链(Responder Chain)
概述 在iOS中,视图的层级一般都是 父视图->添加各种子视图.这时候某个视图(子视图)上有个按钮,需要我们交互.但是有时候我们会发现无论如何都没有反应.这时候可能就是我们对iOS的事件传递响应 ...
- C#的基础数据类型
一.概述 C# 的类型系统是统一的,因此任何类型的值都可以按对象处理.C# 中的每个类型直接或间接地从 object 类类型派生,而 object 是所有类型的最终基类.C#的数据类型主要分为三类:值 ...