SPOJ MYQ10 10649. Mirror Number (数位dp)

SPOJ MYQ10 10649. Mirror Number (数位dp)

ACM

题目地址:SPOJ MYQ10 Mirror Number

题意:

求[a,b]中镜像回文的个数。

0 <= a<=b <= 10^44

分析:

看到题目和数据范围就知道是数位dp了。

很明显镜像回文只有0,1,8,跟回文的一题一样,在dfs的时候得开个辅助数组记录前面已经选择的数字。

注意还得去掉前导0。

代码:

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        10649_MYQ10.cpp
*  Create Date: 2014-08-02 14:20:31
*  Descripton:  MYQ10, digit dp
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define repf(i,a,b) for(int i=(a);i<=(b);i++)

typedef long long ll;

const int N = 50;

ll dp[N][N][2];		// dp[start][current][ismirror]
char bit[N], num[N];
int t, rec[N], len;

// the start pos, current pos, ismirror flag, is limit flag
// this function will use rec[] to record the prevent chosen numbers
ll dfs (int start, int cur, bool ismir, bool limit) {
	if (cur < 0)
		return ismir;
	if (!limit && dp[start][cur][ismir] != -1)
		return dp[start][cur][ismir];

	int mmax = limit ? bit[cur] - '0' : 9;
	ll ret = 0;
	repf (i, 0, mmax) {
		if (i == 0 || i == 1 || i == 8) {
			rec[cur] = i;
			if (start == cur && i == 0) {					// if have lead zero
				ret += dfs(start - 1, cur - 1, ismir, (limit && (i == mmax)));
			} else if (ismir && cur < (start + 1) / 2) {	// if ismirror and can judge
				ret += dfs(start, cur - 1, (i == rec[start - cur]), (limit && (i == mmax)));
			} else {
				ret += dfs(start, cur - 1, ismir, (limit && (i == mmax)));
			}
		}
	}
	return limit ? ret : dp[start][cur][ismir] = ret;
}

// check if first number is mirror
bool check() {
	repf (i, 0, len >> 1) {
		if (bit[i] != '0' && bit[i] != '1' && bit[i] != '8')
			return false;
		if  (bit[i] != bit[len - 1 - i])
			return false;
	}
	return true;
}

ll solve() {
	ll ans1, ans2;

	// first number
	scanf("%s", num);
	len = strlen(num);
	repf (i, 0, len - 1)
		bit[i] = num[len - 1 - i];
	bit[len] = 0;
	ans1 = dfs(len - 1, len - 1, 1, 1) - check();
//	cout << ans1 << endl;

	// second number
	scanf("%s", num);
	len = strlen(num);
	repf (i, 0, len - 1)
		bit[i] = num[len - 1 - i];
	bit[len] = 0;
	ans2 = dfs(len - 1, len - 1, 1, 1);
//	cout << ans2 << endl;

	cout << ans2 - ans1 << endl;
}

int main() {
	memset(dp, -1, sizeof(dp));
	scanf("%d", &t);
	while (t--) {
		solve();
	}
	return 0;
}

SPOJ MYQ10 10649. Mirror Number (数位dp),布布扣,bubuko.com

时间: 2025-01-22 22:23:11

SPOJ MYQ10 10649. Mirror Number (数位dp)的相关文章

SPOJ MYQ10 Mirror Number 数位dp&#39;

题目链接:点击打开链接 MYQ10 - Mirror Number A number is called a Mirror number if on lateral inversion, it gives the same number i.e it looks the same in a mirror. For example 101 is a mirror number while 100 is not. Given two numbers a and b, find the number

Hdu3079Balanced Number数位dp

枚举支点,然后就搞,记录之前的点的力矩和. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <s

多校5 HDU5787 K-wolf Number 数位DP

1 // 多校5 HDU5787 K-wolf Number 数位DP 2 // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d 3 // f 用作标记,当现在枚举的数小于之前的数时,就不用判断i与dig[pos]的大小 4 // 整体来说就,按位往后移动,每次添加后形成的数都小于之前的数,并且相邻k位不一样,一直深搜到cnt位 5 // http://blog.csdn.net/weizhuwyzc000/article/details/5209769

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length

SPOJ BALNUM Balanced Numbers(数位dp,状态压缩)

BALNUM - Balanced Numbers no tags 题目链接 Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a balanced number if: 1)      Every even digit appears an odd number of times in its decimal representation 2)   

hdu 5787 K-wolf Number 数位dp

数位DP 神模板 详解 为了方便自己参看,我把代码复制过来吧 // pos = 当前处理的位置(一般从高位到低位) // pre = 上一个位的数字(更高的那一位) // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, // 给计数器+1. // limit = 是否受限,也即当前处理这位能否随便取值.如567,当前处理6这位, // 如果前面取的是4,则当前这位可以取0-9.如果前面取的5,那么当前 // 这位就不能随便取,不然会超出这个数的范围,所以如果前面取

codeforces Hill Number 数位dp

http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format:  %lld   Java class name:  Main Description A Hill Number is a number whose digits possibly rise and then possibl

fzu 2109 Mountain Number 数位DP

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2109 题意: 如果一个>0的整数x,满足a[2*i+1] >= a[2*i]和a[2*i+2],则这个数为Mountain Number. 给出L, R,求区间[L, R]有多少个Mountain Number. 思路: 数位DP,判断当前是偶数位还是奇数位(从0开始),如果是偶数位,那么它要比前一个数的值小, 如果是奇数位,那么它要比前一个数的值大. 1 #include <iostream>

hdu3709---Balanced Number(数位dp)

Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot is placed at some digit of the nu