数位DP练习
输入
输出
1 100
0 0
80
设f[i][0/1]表示i位的前一位是不是6
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
using namespace std ;
int read() {
int x = 0 , f = 1 ; char s = getchar() ;
while(s > '9' || s < '0') {if(s == '-') f = -1 ; s = getchar() ;}
while(s <='9' && s >='0') {x = x * 10 + (s-'0'); s = getchar() ;}
return x*f ;
}
int f[15][3] ;
int l , r , num[15] ;
int dfs(int pos ,int pre ,int _6 ,int limit) {
if(!pos) return 1 ;
if(!limit && f[pos][_6] != -1) return f[pos][_6] ;
int up = limit? num[pos] : 9 ;
int ans = 0 ;
for(int i = 0 ; i <= up ; i ++) {
if(pre == 6 && i == 2) continue ;
if(i == 4) continue ;
ans += dfs(pos-1,i,i==6,limit && i == num[pos]) ;
}
if(!limit) f[pos][_6] = ans ;
return ans ;
}
int calc(int x) {
int len = 0 ;
while(x) {
num[++len] = x%10 ;
x /= 10 ;
}
memset(f,-1,sizeof f) ;
return dfs(len,-1,0,1) ;
}
int main () {
l = read() , r = read() ;
cout << calc(r) - calc(l-1) << endl ;
return 0 ;
}