SPOJ MYQ10 (数位DP)
题意
询问区间[a,b]中的Mirror Number的个数,其中Mirror Number是指把它横着翻转后还能表示同样的数字。
思路
注意这个可不是回文数。。除了0,1,8,别的数字翻转过后就不是数字了。所以策略就是记忆化按位搜索,每位只搜0,1,8,最后再判断是否回文,统计即可。这个判断回文是个小麻烦,因为它需要和前面的位相比较,所以用一个全局数组tmp[]表示枚举的每位的数字。
回过头来我觉得设置全局变量似乎不是一个好方法……它应该会破坏DP数组的区间唯一性……
代码
[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;
typedef long long LL;
typedef vector VI;
typedef set SETI;
typedef queue QI;
typedef stack SI;
char num[50];
LL dp[47][47][2];
int tmp[50];
LL dfs(int pos, int start, bool flag, bool limit){
if (pos==-1) return flag;
if (!limit && ~dp[pos][start][flag]) return dp[pos][start][flag];
int end = limit?(num[pos]-48):9;
LL res = 0;
for (int i = 0; i <= end; i ++)
if (i == 0 || i == 1 || i == 8){
bool st = (start==pos && i == 0);
bool next_flag = flag;
if (flag){
if (!st && pos < (start+1)/2)
next_flag = (i == tmp[start-pos]);
}
tmp[pos] = i;
res += dfs(pos-1, st?start-1:start, next_flag, limit&&(i==end));
}
return limit ? res : dp[pos][start][flag] = res;
}
LL solve(char x[]){
int len = strlen(x);
for (int i = 0; i < len; i ++){
num[i] = x[len-1-i];
}
num[len] = 0;
return dfs(len-1, len-1, 1, 1);
}
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int t;
char a[50], b[50];
scanf("%d", &t);
getchar();
MEM(dp, -1);
while(t --){
scanf("%s %s", a, b);
LL res = solve(b)-solve(a);
int len = strlen(a);
bool ok = true;
for (int i = 0; i < len; i ++){
if ((a[i] != '0' && a[i] != '1' && a[i] != '8')||(a[i] != a[len-1-i])){
ok = false;
break;
}
}
if (ok)
res ++;
printf("%lld\n", res);
}
return 0;
}
[/cpp]
SPOJ MYQ10 (数位DP)的更多相关文章
- SPOJ KPSUM ★(数位DP)
题意 将1~N(1<=N<=10^15)写在纸上,然后在相邻的数字间交替插入+和-,求最后的结果.例如当N为12时,答案为:+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2=5 ...
- CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)
题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...
- SPOJ - BALNUM 数位dp
题意:求某一区间内的平衡数个数(指一个数,其中出现过的数,如果是偶数,那么必须出现奇数次,反之偶数次) 题解:用三进制来枚举(0到9)所有情况,0代表没有出现,1代表出现奇数次,2代表出现偶数次dp[ ...
- 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)
BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...
- 【SPOJ 1182】 SORTBIT - Sorted bit squence (数位DP)
SORTBIT - Sorted bit squence no tags Let's consider the 32 bit representation of all integers i from ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- SPOJ BALNUM Balanced Numbers (数位dp)
题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...
- 数位DP:SPOJ KPSUM - The Sum
KPSUM - The Sum One of your friends wrote numbers 1, 2, 3, ..., N on the sheet of paper. After that ...
- [数位dp] spoj 10738 Ra-One Numbers
题意:给定x.y.为[x,y]之间有多少个数的偶数位和减去奇数位和等于一. 个位是第一位. 样例: 10=1-0=1 所以10是这种数 思路:数位dp[i][sum][ok] i位和为sum 是否含有 ...
随机推荐
- docker——容器(container)
容器相关命令一览表: docker create docker run docker start/stop/restart docker attach/exec docker rm docker ex ...
- Delphi APP 開發入門(三)簡易計算機
Delphi APP 開發入門(三)簡易計算機 分享: Share on facebookShare on twitterShare on google_plusone_share 閲讀次數:68 ...
- XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem D. Great Again
题目: Problem D. Great AgainInput file: standard inputOutput file: standard outputTime limit: 2 second ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (set维护)
注意题目保证不会有一个矩形完全包括另一个矩形的情况 时间序上从后往前看,一个坐标\((x,y)\)加进来之前,如果已经有\(x_i<x\),则对结果的贡献为\(x-x_i\);若不存在\(x_i ...
- [转]美国最大婚恋交友网站eHarmony的机器学习实践
转自:http://www.csdn.net/article/2015-03-19/2824267 上周,我去洛杉矶参加了一个机器学习的meetup,一位主讲是eHarmony公司(美国最大的婚恋交友 ...
- [转]Earth Mover's Distance (EMD)
转自:http://www.sigvc.org/bbs/forum.php?mod=viewthread&tid=981 Earth Mover's Distance (EMD)原文: htt ...
- 实战DVWA!
DVWA漏洞训练系统,来个大图^-^ 1.首先试了下DVWA的命令执行漏洞command execution 这是我在Low级别上测试的,另外附上low级别代码: <?php if( i ...
- C#数组的Map、Filter、Reduce操作
在Javascript.Python等语言里,Map.Filter和Reduce是数组的常用方法,可以让你在实现一些数组操作时告别循环,具有很高的实用价值.它们三个的意义大家应该都清楚,有一个十分形象 ...
- 联想笔记本电脑的F1至F12键盘问题。怎么设置才能不按FN就使用F1
在BIOS中有相应调整开关,开机时进入BIOS----CONFIG----Keyboard/Mouse----Change to "f1-f12 keys"选项设置为Legacy ...
- std::tuple作为参数invoke调用函数
template<typename Function, typename Tuple, std::size_t... Index> decltype(auto) invoke_impl(F ...