數位dp模板

不要 62

題目描述

杭州人稱那些傻乎乎粘嗒嗒的人爲 62(音:laoer)。

杭州交通管理局經常會擴充一些的士車牌照,新近出來一個好消息,以後上牌照,不再含有不吉利的數字了,這樣一來,就可以消除個別的士司機和乘客的心理障礙,更安全地服務大衆。

不吉利的數字爲所有含有  62或 4 的號碼。例如: 都屬於不吉利號碼。但是, 雖然含有  6和2 ,但不是62  連號,所以不屬於不吉利數字之列。

你的任務是,對於每次給出的一個牌照區間號,推斷出交管局今後又要實際上給多少輛新的士車上牌照了。

輸入格式

輸入的都是整數對 ,如果遇到都是 n,m 的整數對,則輸入結束。

輸出格式

對於每個整數對,輸出一個不含有不吉利數字的統計個數,該數值佔一行位置。

樣例

樣例輸入

1 100
0 0

樣例輸出

80

數據範圍與提示0<= n,m<=1e7

對於全部數據,。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int digit[20];
ll dp[20][2];//第一個數位 
//if6是否當前爲6
//limit是否上界 
ll  dfs(int len, bool if6, bool limit) {
	if(len == 0) {
		return 1ll;
	} //只是個位時直接返回
	if(!limit && dp[len][if6]) return dp[len][if6];
	ll cnt = 0, up_bound = (limit?digit[len]:9);
	for(int i = 0;i <= up_bound;++i) {
		if(if6 && i == 2)continue;//剪掉62 
		if(i == 4)continue;//剪掉4
		cnt += dfs(len-1, i==6, limit&&i==up_bound); //i==6是判斷是否前一位爲6的
	}
	if(!limit) dp[len][if6] = cnt;
	return cnt;
}
ll solve(ll num) {
	int k = 0;
	while(num) {
		digit[++k] = num%10;
		num /= 10;
	}
	return dfs(k, false, true);
} //數位dp板子
int main() {
	ll n, m;
	while(cin>>n>>m && n+m) {
		cout << solve(m)-solve(n-1) << endl; 
	}  
	return 0;
}

Bomb

roblem Description

The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?

 

 

Input

The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker.

 

 

Output

For each test case, output an integer indicating the final points of the power.

 

 

Sample Input


 

3

1

50

500

 

 

Sample Output


 

0 1 15

Hint

From 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499", so the answer is 15.

 

 

Author

fatboy_cw@WHU

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int digit[20];
ll dp[20][2];//第一個數位 
//if4是否當前爲4
//limit是否上界 
ll  dfs(int len, bool if4, bool limit) {
    if(len == 0) {
        return 1ll;
    } 
    if(!limit && dp[len][if4]) return dp[len][if4];
    ll cnt = 0, up_bound = (limit?digit[len]:9);
    for(int i = 0;i <= up_bound;++i) {
        if(if4 && i == 9)continue;//剪掉49 
        cnt += dfs(len-1, i==4, limit&&i==up_bound); //較上一題改i==4
    }
    if(!limit) dp[len][if4] = cnt;
    return cnt;
}
ll solve(ll num) {
    int k = 0;
    while(num) {
        digit[++k] = num%10;
        num /= 10;
    }
    return dfs(k, false, true);
} 
int main() {
    int t;
    cin >> t;
    while(t--) {
        ll n;
        cin >> n;
        cout << n+1-solve(n) << endl; 
    } 
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章