1. 程式人生 > >【數位dp】hdu2089 不要62

【數位dp】hdu2089 不要62

clu scanf spa print algo std can dfs fin

好吧,雖然是道水題但是是第一次接觸數位dp所以還是要記錄一下。

其實就是因為如果按數的大小枚舉的話很不方便所以就按數位枚舉並進行記憶化。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define s(x,y) scanf("%d%d",&x,&y)
#define p(x) printf("%d\n",x)
int dp[12][2],a[12],n,m;
int dfs(int x,bool
now,bool limit) { if (x==0) return 1; if ((dp[x][now]>-1)&&(!limit)) return dp[x][now]; int up=(limit?a[x]:9),tot=0; for (int i=0;i<=up;i++) { if (((now)&&(i==2))||(i==4)) continue; tot+=dfs(x-1,i==6,limit&&(i==up)); } return limit?tot:dp[x][now]=tot; }
int get(int x) { int cnt=0; while (x) { a[++cnt]=x%10; x=x/10; } return dfs(cnt,false,true); } int main() { for (int i=0;i<=11;i++) dp[i][0]=dp[i][1]=-1; while (~s(m,n)&&(n||m)) p(get(n)-get(m-1)); }

【數位dp】hdu2089 不要62