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个存储一下状态就行了.求各种进制能 ...
随机推荐
- 随手记录-linux-vim使用
- 第十二周PSP
- mybatis之insert语句报错Cause: java.sql.SQLException: sql injection violation, syntax error: ERROR. token : WHERE,
报错日志:org.springframework.jdbc.UncategorizedSQLException: Error updating database. Cause: java.sql.SQ ...
- Notes of Daily Scrum Meeting(11.14)
Notes of Daily Scrum Meeting(11.14) 今天是项目第三周的周五,按原计划这时我们的项目应该已经要进入尾声进行组装调试了,但由于之前放假还有队员们的 效率比较低的原因,我 ...
- Bing词典vs有道词典比对测试报告——功能篇之辅助功能,差异化功能及软件的效能
1.辅助功能: 和有道相比,必应的词典加入了换肤功能,用户可以选择喜欢的颜色,而且必应的皮肤也比较多,这一点设计给必应增色不少. 相对而言,有道则加入了调节客户端字体的大小,如下,也比较人性化 2.差 ...
- Class 2 四则运算2的设计思路
设计思路 1.主函数中有一个大的for循环,用户可以一直随机得到相应题目.在嵌套一个循环,其可以直接确定题目数量:定义两个变量,分别作为四则运算的两个运算数,用随机数函数得到两个数值:再利用随机生成函 ...
- 获取ios设备的udid
今天get的第二个技能~~~ UDID指的是设备的唯一设备识别符,ipa包未上架之前如果不添加udid是无法安装成功的.那么如何快速获取ios设备的udid呢? 今天get的方法是用蒲公英,网址:ht ...
- PAT 甲级 1151 LCA in a Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/1038430130011897856 The lowest common anc ...
- mysql数据优化--数据库结构的优化
1,比如存时间类型的就使用int类型 其中mysql的两个函数可以拿来使用 unix_timestamp 将时间日期转化为时间戳
- linux使用密文生成os账户
1.生成账户 [hufangrui@xxx ~]$ openssl passwd -1Password: Verifying - Password: $1$szzkROBZ$GYxffRLp8K5vW ...