light oj 1205(数位DP)
题目描述:
求给定区间中的回文数有多少个?
首先明确一点,如果一个数是回文数,那么给这个数两边加上相同的数,那么这个数还是回文数。
根据这点就可以进行递推了,p[start][end]=9*p[start+1][end-1](start位不为0)+p[start-1][end](start位为0);
在设计dfs的时候,由于回文数是对称的,所以只需要一个变量cur(cur>mid)就可以表示从cur到cur对称的位置的回文数的个数;
d[start][cur]表示从start位到cur位时,回文数的个数。
这句话隐含的意思是最高位为start,枚举到第cur位,由于是回文数,所以当cur>mid时,[cur,start]位确定,他们的对称位[1,start+1-cur]也就确定了,
还需枚举[cur,mid]这些位;当cur=mid时任意枚举,对回文数没有影响,当cur<mid时,由于是回文数,已经确定了,不需枚举。
再增加一维表示当前枚举的数字是不是回文数(([start,cur]位是否为回文数);
d[start][cur][state]表示从start位到cur位时,状态为state时回文数的个数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define LL long long
LL n,m;
LL d[][][];
int digit[];
int num[];
LL dfs(int start,int cur,int Zero,int state,bool fp)
{
if(!cur)
return state;
if(!fp && d[start][cur][state]!= -)
return d[start][cur][state];
LL ret = ;int fpmax = fp ? digit[cur] : ;
for(int i=;i<=fpmax;i++)
{
if( (Zero &&(i==)) )
ret+=dfs(start-,cur-,Zero&&(i==),state, fp&& i==fpmax);
else
{
num[cur]=i;
if( (start & )== )
{
int mid=((start+)>>);
if(cur== mid)
{
ret+=dfs(start,cur-,,state,fp&& i==fpmax);
}
else if(cur < mid )
{
ret+=dfs(start,cur-,,state&& (num[mid*-cur]==i),fp&& i==fpmax);
}
else if(cur>mid)
{
ret+=dfs(start,cur-,,state,fp&& i==fpmax);
}
}
else
{
int mid=(start>>)+;
if(cur<mid)
{
ret+=dfs(start,cur-,,state&& (num[start+-cur]==i),fp&& i==fpmax);
}
else
{
ret+=dfs(start,cur-,,state,fp&& i==fpmax);
}
}
}
}
if(!fp) //如果是前缀,当前得到的ret的值,就不能代表dp[len][state]的值
d[start][cur][state] = ret;
return ret;
} LL f(LL n)
{
int len = ;
if(n==-)
return ;
while(n)
{
digit[++len] = n % ;
n /= ;
}
LL answer=dfs(len,len,,,true);
return answer;
}
void init()
{
memset(d,-,sizeof(d));
}
int main()
{
// freopen("test.txt","r",stdin);
int t;
scanf("%d",&t);
int Case=;
while(t--)
{
scanf("%lld%lld",&n,&m);
init();
if(n>m)
swap(n,m);
printf("Case %d: %lld\n",++Case,f(m)-f(n-));
}
return ;
}
light oj 1205(数位DP)的更多相关文章
- light oj 1032(数位DP)
求一段区间中,每个十进制数所对应的二进制数中连续的1的个数之和. 设dp[i][0]代表长度为i的二进制数,首位为0,所含有的连续的1的个数之和. dp[i][1]代表长度为i的二进制数,首位为1,所 ...
- light oj 1068 数位dp
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
- light oj 1205 - Palindromic Numbers 数位DP
思路:搜索的时候是从高位到低位,所以一旦遇到非0数字,也就确定了数的长度,这样就知道回文串的中心点. 代码如下: #include<iostream> #include<cstdio ...
- Light oj 1030 概率DP
D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768 ...
- lightoj 1205 数位dp
1205 - Palindromic Numbers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...
- light oj 1422 区间dp
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
- light oj 1084 线性dp
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...
- light 1205 - Palindromic Numbers(数位dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1205 题解:这题作为一个数位dp,是需要咚咚脑子想想的.这个数位dp方程可能不 ...
- light oj 1068 - Investigation 数位DP
思路:典型的数位DP!!! dp[i][j][k]:第i位,对mod取余为j,数字和对mod取余为k. 注意:由于32位数字和小于95,所以当k>=95时,结果肯定为0. 这样数组就可以开小点, ...
随机推荐
- 【ZJOI2017 Round1练习】D2T3 counter(线段树)
题意: 思路: 预处理出b[i]代表i位置之前比a[i]小的数的个数 以每个数为结尾的组数是线段树中(1,a[i]-1) 对于a[i]换到最后,相当于线段树中(a[i]+1,n)-- 交换后b[i]又 ...
- SHELL脚本运行的几种方法以及区别
#1 给脚本加上执行权限chmod u+x a.sh, 而后就可以直接用全路径来执行脚本了,比如当前文件夹下用./a.sh,如果如果脚本所在目录在PATH环境变量之中, 则直接用a.sh即可(这和运行 ...
- CSS属性操作一
CSS属性操作 一.CSS text 1.文本颜色:color 颜色属性被用来设置文字的颜色.颜色是通过CSS最经常的指定: • 十六进制值 - 如: #FF0000 • 一个RGB值 - 如: RG ...
- HDU 5700 区间交
枚举起点 二分终点 树状数组check #include<iostream> #include<cstring> #include<cmath> #include& ...
- HDU 2059 【DP】
题意: 中文. 思路: 这题不是自己的思想. 当对第i个点的最优值进行求解的时候一定存在最后一个加油的点j.这里j直接枚举. 另外将0和n+1个加油站定义为起点和终点. dp需要加强训练. #incl ...
- JAVA学习第六十四课 — 反射机制
Java反射机制是在执行状态中,对于随意一个类,都可以知道这个类的全部属性和方法,对于随意一个对象,都可以调用它的随意一个方法和属性,这样的动态获取的信息以及动态调用对象的方法的功能称为java ...
- Redis 入门指南
就是DBIdx
- APache POI emaple ()
Business Plan The BusinessPlan application creates a sample business plan with three phases, weekly ...
- 深度学习笔记之关于基本思想、浅层学习、Neural Network和训练过程(三)
不多说,直接上干货! 五.Deep Learning的基本思想 假设我们有一个系统S,它有n层(S1,…Sn),它的输入是I,输出是O,形象地表示为: I =>S1=>S2=>….. ...
- the first week study
1.In 1989, a man named Guido create "python" as a kind of computer languages. And now we u ...