lightoj 1205 数位dp
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
Output for Sample Input |
|
4 1 10 100 1 1 1000 1 10000 |
Case 1: 9 Case 2: 18 Case 3: 108 Case 4: 198 |
//开个数组存一下回文串,还要处理前导零。sta表示到目前为止是否合法
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int t,bit[],num[];
ll f[][][],P,Q;
ll dfs(int pos,int s,int sta,bool limit)
{
if(pos==) return sta;
if(!limit&&f[pos][s][sta]!=-) return f[pos][s][sta];
int max_b=(limit?bit[pos]:);
ll ans=;
for(int i=;i<=max_b;i++){
num[pos]=i;
if(pos==s&&i==)
ans+=dfs(pos-,s-,sta,limit&&(i==max_b));
else if(sta&&pos<=s/)
ans+=dfs(pos-,s,num[s-pos+]==i,limit&&(i==max_b));
else ans+=dfs(pos-,s,sta,limit&&(i==max_b));
}
if(!limit) f[pos][s][sta]=ans;
return ans;
}
ll solve(ll x)
{
if(x<) return ;
int pos=;
while(x){
bit[++pos]=x%;
x/=;
}
return dfs(pos,pos,,);
}
int main()
{
memset(f,-,sizeof(f));
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
scanf("%lld%lld",&P,&Q);
if(P>Q) swap(P,Q);
printf("Case %d: %lld\n",cas,solve(Q)-solve(P-));
}
return ;
}
lightoj 1205 数位dp的更多相关文章
- lightoj 1021 (数位DP)
题意:给你一个b进制的数,再给你一个十进制数k,你可以重新排列b进制数的每一位得到其他b进制数,问你这些数中有多少可以整除k? 思路:数位dp. #include <cstdio> #in ...
- LightOJ - 1032 数位DP
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...
- light oj 1205(数位DP)
题目描述: 求给定区间中的回文数有多少个? 首先明确一点,如果一个数是回文数,那么给这个数两边加上相同的数,那么这个数还是回文数. 根据这点就可以进行递推了,p[start][end]=9*p[sta ...
- Lightoj 1068(数位DP)
求一段区间中被k整除,各个位数相加之和被k整除的数的个数. 这不是重点,重点是k太大了,最大值有10000,所以不能直接开那么大的数组. 仔细分析一下可以发现,由于数最大是2的31次方(2147483 ...
- Lightoj 1140(数位DP)
求一个区间内的数含有多少个0. dp[len][pre]表示长度为len的数,含有pre个0. 需要加一个标记,来表示前缀是否为0(可以是一串连续的0),如果前缀一直为0,就一直搜,如果前缀不为0,就 ...
- light 1205 - Palindromic Numbers(数位dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1205 题解:这题作为一个数位dp,是需要咚咚脑子想想的.这个数位dp方程可能不 ...
- LightOJ 1032 - Fast Bit Calculations 数位DP
http://www.lightoj.com/volume_showproblem.php?problem=1032 题意:问1~N二进制下连续两个1的个数 思路:数位DP,dp[i][j][k]代表 ...
- LightOJ 1068 Investigation (数位dp)
problem=1068">http://www.lightoj.com/volume_showproblem.php?problem=1068 求出区间[A,B]内能被K整除且各位数 ...
- lightoj 1021 - Painful Bases(数位dp+状压)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1021 题解:简单的数位dp由于总共就只有16个存储一下状态就行了.求各种进制能 ...
随机推荐
- 1.centos6.8安装docker
简介环境安装依赖安装步骤1.删除旧版本的docker2.安装docker2.1 yum安装docker2.2 tar安装2.3 在线脚本事实证明以上的安装方式都行不通 参考文档:https://doc ...
- Vue实现双向绑定的原理以及响应式数据
一.vue中的响应式属性 Vue中的数据实现响应式绑定 1.对象实现响应式: 是在初始化的时候利用definePrototype的定义set和get过滤器,在进行组件模板编译时实现water的监听搜集 ...
- oracle和mysql在sql中生成uuid的方法
1,oracle sys_guid() 2,mysql uuid()
- c# 程序重启设定
问题情境: 程序随着时间运行,越来越大.暂时想到的两种方法,一是反攻代码,查看占内存大的函数,是不是没有回收.再就是暴力设定程序定时重启. 解决原理: 定时重启:暂设定timer,时间匹配执行rest ...
- Python学习二
list [ ] Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素 classmates = ['Michael', 'Bob', 'Tracy ...
- Linux环境下Web环境搭建——Nginx
1.安装依赖 yum -y install make gcc gcc-c++ ncurses-devel ##编译环境 yum -y install zlib zlib-devel openssl ...
- Sprint7
进展:根据昨天查到的资料,今天开始编写闹钟部分的代码,主要实现了闹钟添加事件显示时间主界面.
- 软工实践-Beta 冲刺 (4/7)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...
- Software Defined Networking(Week 2, part 2)
History of SDN 1.3 - 1.4 课程地址 Network Virtualization 网络可虚拟化,可以说是SDN的一项核心内容,同样也源自很多先前的技术和思想.我们先讨论何为网络 ...
- 网络1711-12&信管1711-12 图 作业评分
先放上本次作业的推荐博客,以及评分细则总表,在最后,会放几张图表对本学期大家的成果进行一个小小的总结,有兴趣的同学可以看看,感受一下自己这个学期的积累和进步.(主要针对网络的同学,信管的同学只有两次作 ...