【LightOJ - 1205】Palindromic Numbers
【链接】https://cn.vjudge.net/problem/LightOJ-1205
【题意】
【题解】
其中start表示是从哪一位开始进行扫描的,这个东西用来处理前导0;
cur表示当前搜索到了第几位数字.
ok表示当前搜索到的字符串是否为回文.
xiao则表示是否出现已经搜索的某一位是小于所给的数字的对应位,如果有的话,之后的每一位枚举就能一直到9了.
然后我们可以这样写记搜。
设f[i][j][k]表示从start位开始搜索,然后当前搜索到第cur位,k=0表示是回文串,k=1表示不是回文串.(当然还没搜完,只能说它可能是回文串)的回文串个数.
可以肯定,如果xiao==1了,则无论数字是什么,后面的回文串个数都是一样的了
比如
所给数字
9323
先倒过来
3239
假设我们枚举第一位为1
1xxx
则这个时候start = 4,cur = 3 (我们是倒过来的),然后因为第一位小于3,所以xiao=1
则这个时候,后面3个位置,实际上只有第2位是需要枚举的了,因为后面的两位肯定是和前面的两位一样的.
(而且每一位都可以0..9任意选)
也就是说,这个时候,答案已经和所给的数字没有任何关系了.它是一个通式
也即这个时候往后算出来的答案f[start][cur][ok],在后序的搜索中如果遇到,是可以直接返回值的.
(注意只有在xiao==1的时候才能做记搜,因为如果xiao==0,显然之后位是有限制的,可能不是每一位都是0..9了)
【错的次数】
【反思】
【代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 100; LL a,b;
int c[N+10],temp[N+10],dp[N+10][N+10][2]; LL dfs(int start,int cur,bool ok,bool xiao){
if (cur < 1) return ok;
int limit = (xiao?9:c[cur]);
if (xiao && dp[start][cur][ok]!=-1)
return dp[start][cur][ok];
LL ret = 0;
rep1(i,0,limit){
temp[cur] = i;
if (i == 0 && start == cur){
ret += dfs(start-1,cur-1,ok,xiao||(i < limit));
}else
if (ok && cur < (start+1)/2 + 1)
ret += dfs(start,cur-1,temp[start-cur+1]==temp[cur],xiao||(i<limit));
else
ret += dfs(start,cur-1,ok,xiao||(i<limit));
}
if (xiao) dp[start][cur][ok] = ret;
return ret;
} LL f(LL x){
if (x < 0) return 0;
int len = 0;
while (x){
c[++len] = x%10;
x/=10;
}
return dfs(len,len,1,0);
} int main(){
//Open();
//Close();
ms(dp,255);
int T,kk = 0;
ri(T);
while (T--){
rl(a),rl(b);
if (a > b) swap(a,b);
os("Case ");oi(++kk);os(": ");ol(f(b)-f(a-1));puts("");
}
return 0;
}
【LightOJ - 1205】Palindromic Numbers的更多相关文章
- LightOJ - 1205:Palindromic Numbers (数位DP&回文串)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- LightOJ - 1396 :Palindromic Numbers (III)(逐位确定法)
Vinci is a little boy and is very creative. One day his teacher asked him to write all the Palindrom ...
- 【HDU 4722】 Good Numbers
[题目链接] 点击打开链接 [算法] f[i][j]表示第i位,数位和对10取模余j的数的个数 状态转移,计算答案都比较简单,笔者不再赘述 [代码] #include<bits/stdc++.h ...
- 【Codeforces 1036C】Classy Numbers
[链接] 我是链接,点我呀:) [题意] 让你求出只由3个非0数字组成的数字在[li,ri]这个区间里面有多少个. [题解] 只由3个非0数字组成的数字在1~10^18中只有60W个 dfs处理出来之 ...
- 【Codeforces 300C】Beautiful Numbers
[链接] 我是链接,点我呀:) [题意] 让你找到长度为n的数字 这个数字只由a或者b组成 且这n个数码的和也是由a或者b组成的 求出满足这样要求的数字的个数 [题解] 枚举答案数字中b的个数为y,那 ...
- 【CSU 1556】Pseudoprime numbers
题 Description Jerry is caught by Tom. He was penned up in one room with a door, which only can be op ...
- 【数位dp】Beautiful Numbers @2018acm上海大都会赛J
目录 Beautiful Numbers PROBLEM 题目描述 输入描述: 输出描述: 输入 输出 MEANING SOLUTION CODE Beautiful Numbers PROBLEM ...
- 【UVA - 136】Ugly Numbers(set)
Ugly Numbers Descriptions: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
- 【Aizu - ALDS1_1_C】Prime Numbers(素数筛法)
Prime Numbers Descriptions: A prime number is a natural number which has exactly two distinct natur ...
随机推荐
- 基于MVC4+EasyUI的Web开发框架经验总结(13)--DataGrid控件实现自己主动适应宽带高度
在默认情况下,EasyUI的DataGrid好像都没有具备自己主动宽度的适应功能,通常是指定像素宽度的.可是使用的人员计算机的屏幕分辨率可能不一样,因此导致有些地方显示太大或者太小,总是不能达到好的预 ...
- vue21 slot占位
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue6 发请求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java引用被设置为null的疑惑
a=null; public class C { protected A webDigester = new A(" first one "); public void test( ...
- CetnOS6 网络配置,主机名配置
CetnOS6 网络配置,主机名配置 一.通过命令ifconfig -a 查看可用网络设备 二.通过网络配置文件/etc/sysconfig/network-scripts/ifcfg-eth0 配置 ...
- 各消息队列对比,Kafka深度解析,众人推荐,精彩好文!
http://blog.csdn.net/allthesametome/article/details/47362451
- 剑指offer_面试题6_重建二叉树(分解步骤,逐个击破)
题目:输入某二叉树的前序遍历和中序遍历的结果.请重建出该二叉树.如果输入的前序遍历和中序遍历的结果中都不含反复的数字. 比如:输入前序遍历 {1,2,4,7,3,5,6,8} 和中序遍历序列 {4,7 ...
- poj2486--Apple Tree(树状dp)
Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7789 Accepted: 2606 Descri ...
- EularProject 48: 利用数组求和
Problem 48 The series, 11+22+33+...+1010=10405071317. Find the last ten digits of the series, 11+22+ ...
- 作为刚開始学习的人应该怎样来学习FPGA
FPGA作为一种高新的技术.已经逐渐普及到了各行各业.不管是消费类.通信类.电子行业都无处不在它的身影,从1985年第一颗FPGA诞生至 今,FPGA已经历了将近20多个年头,从当初的几百个门电路到如 ...