HDU 2089-no 62 (entry digital DP)

Don't 62

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 44379    Accepted Submission(s): 16453


Problem Description
Hangzhou people call those who are stupid and sticky as 62 (laoer).
The Hangzhou Transportation Administration often expands some taxi license plates. There is good news recently that the license plates will no longer contain unlucky numbers. In this way, the psychological barriers of individual taxi drivers and passengers can be eliminated. Serve the public safely.
The unlucky numbers are all numbers that contain 4 or 62. For example:
62315 73418 88914
are all unlucky numbers. However, although 61152 contains 6 and 2, it is not 62 consecutive numbers, so it is not among the inauspicious numbers.
Your task is to infer how many new taxis the Traffic Management Bureau will actually have license plates for each time a license plate interval number is given.
 

Input
The input is an integer pair n and m (0<n≤m<1000000). If an integer pair that is all 0 is encountered, the input ends.
 

Output
For each integer pair, output a statistical number that does not contain an unlucky number, and this number occupies a row.
 

Sample Input
  
   
   
1 100 0 0
 

Sample Output
  
   
   
80
                                                                                                                                                                                                                                       
//数位DP
//这道题可以处理long long范围的
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int dp[25][2];                       //dp[i][0]表示的是第i-1位不是6时且没有限制时,第i位以及后面位数能组成合法数的个数(dp[i][1]表示的是第i-1位是6时)
int a[25];                           //拆成一个一个的数

//4个参数分别是当前位,前一位的数值,前一位的数值是否为6,有无受前面数字的限制
int dfs(int pos, int pre, int stu, bool limit)
{
	//到个位的时候,对应的数肯定是只有1个了
	if(pos==-1)
		return 1;
	//已经计算过的了,优化
	if(!limit && dp[pos][stu]!=-1)
		return dp[pos][stu];
	//判断有无限制, 如果有限制,那当前位只能取0-a[pos], 如果没有限制, 就可以取0-9,up存的就是上限
	int up = limit?a[pos]:9;
	//记录最终结果
	int temp = 0;
	//取值,0-up
	for(int i=0; i<=up; i++)
	{
		//把不合法的排除
		if(i==4 || (pre==6 && i==2))
			continue;
		temp += dfs(pos-1, i, i==6, limit && i==a[pos]);
	}
	if(!limit)
		dp[pos][stu] = temp;
	return temp;
}

int slove(int r)
{
	int pos = 0;
	while(r)
	{
		a[pos++] = r%10;
		r/=10;
	}
	//初始是,最高位, 前一位的数当做-1不受影响,前一位不等于6,第一位有受限制
	return dfs(pos-1, -1, 0, true);
}

int main()
{
	int l, r;
	while(scanf("%d%d", &l, &r), l+r)
	{
	    memset(dp, -1, sizeof(dp));
		printf("%d\n", slove(r)-slove(l-1));
	}
}



Guess you like

Origin blog.csdn.net/qq_31281327/article/details/76675100