hdu 2089 数位dp入门
题意:中文题
思路:数位dp入门题
AC代码:
#include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "map" #include "algorithm" #include "stdio.h" #include "math.h" #pragma comment(linker, "/STACK:102400000,102400000") #define ll long long #define endl ("\n") #define bug(x) cout<<x<<" "<<"UUUUU"<<endl; #define mem(a,x) memset(a,x,sizeof(a)) #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define ft (frist) #define sd (second) #define lrt (rt<<1) #define rrt (rt<<1|1) using namespace std; const long long INF = 1e18+1LL; const int inf = 1e9+1e8; const int N=1e3+100; const ll mod=1e9+7; int bit[20],dp[20][2]; int dfs(int pos, int pre, bool f, bool limit){ if(pos==0) return 1; int up=limit?bit[pos]:9; if(!limit && dp[pos][f]!=-1) return dp[pos][f]; int ret=0; for(int i=0; i<=up; ++i){ if(i==4) continue; if(i==2 && pre==6) continue; ret+=dfs(pos-1, i, i==6, limit && i==bit[pos]); } if(!limit) dp[pos][f]=ret; return ret; } int solve(int x){ int t=0; while(x>0){ bit[++t]=x%10; x/=10; } return dfs(t, -1, 0, 1); } int main(){ ll l,r; mem(dp,-1); while(cin>>l>>r && l&&r){ cout<<solve(r)-solve(l-1)<<endl; } return 0; }