1. 程式人生 > >[HDU2089] 不要62 [數位dp]

[HDU2089] 不要62 [數位dp]

[ L i n k \frak{Link} ]


真好啊
比插頭dp好看多了(


#include<cstdio>
#include<iostream> #include<algorithm> #include<cstdlib> #include<cmath> #include<cstring> #include<cctype> using namespace std; int n,m,F[10][2]={},bit[10]={}; int dfs(int pos,int pre,bool lead,bool limit) { if(!pos)return 1; if(!limit&&F[pos][lead]
>0)return F[pos][lead]; int ret=0, up=limit?bit[pos]:9; for(register int i=0;i<=up;++i) { if(i==2&&pre==6)continue; if(i==4)continue;; ret+=dfs(pos-1,i,i==6,limit&&i==bit[pos]); } if(!limit)F[pos][lead]=ret; return ret; } int solve(int
x) { bit[0]=0; while(x) { bit[++bit[0]]=x%10; x/=10; } return dfs(bit[0],-1,true,true); } int main() { memset(F,-1,sizeof(F)); while(~scanf("%d%d",&n,&m)) { if(!(n||m))return 0; printf("%d\n",solve(m)-solve(n-1)); } return 0; }