【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 ...
随机推荐
- Runtime类中的freeMemory,totalMemory,maxMemory等几个方法
最近在网上看到一些人讨论到Java.lang.Runtime类中的freeMemory(),totalMemory(),maxMemory ()这几个方法的一些题目,很多人感到很迷惑,为什么,在jav ...
- legend---六、php脚本变量的生命周期是怎样的
legend---六.php脚本变量的生命周期是怎样的 一.总结 一句话总结:应该是脚本结束变量的生命周期就完了 1.外部js找不到元素是怎么回事? 1 function myDailyTaskFin ...
- ZOJ 2588 Burning Bridges(求桥的数量,邻接表)
题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588 Burning Bridges Time Limit: 5 ...
- BZOJ1901 ZOJ2112 线段树+treap (线段树套线段树)
BZOJ1901: 线段树套线段树做法: (外层线段树 里层动态开节点的权值线段树) 有一个小小的trick 可以省掉二分变成nlog^2n的 就是把查询的区间都取出来- logn个一起走- 2016 ...
- 学习《TensorFlow实战Google深度学习框架 (第2版) 》中文PDF和代码
TensorFlow是谷歌2015年开源的主流深度学习框架,目前已得到广泛应用.<TensorFlow:实战Google深度学习框架(第2版)>为TensorFlow入门参考书,帮助快速. ...
- Idea下mybatis的错误—Module not specified
IDEA下使用maven的mybatis常见错误 错误类型一:导入项目引起的错误Module not specified 错误提示:idea Error Module not specified. 错 ...
- OpenJDK源码研究笔记(十三):Javac编译过程中的上下文容器(Context)、单例(Singleton)和延迟创建(LazyCreation)3种模式
在阅读Javac源码的过程中,发现一个上下文对象Context. 这个对象用来确保一次编译过程中的用到的类都只有一个实例,即实现我们经常提到的"单例模式". 今天,特意对这个上下文 ...
- C语言之基本算法39—字符串经典操作
//字符串概念! /* ================================================================== 题目:练习字符串的 1.输入输出 ...
- elasticsearch index 之 create index(二)
创建索引需要创建索引并且更新集群index matedata,这一过程在MetaDataCreateIndexService的createIndex方法中完成.这里会提交一个高优先级,AckedClu ...
- 3.索引与string进行映射实现高效查找
#include <iostream> #include <typeindex>//类型索引 #include <unordered_map>//红黑树 #incl ...