1. 程式人生 > 實用技巧 >數位DP

數位DP

1.[SCOI2009] windy 數

模板題

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 inline int read()
 4 {
 5     int x=0;char c=getchar();
 6     for(;!isdigit(c);c=getchar());
 7     for(;isdigit(c);c=getchar()) x=x*10+c-'0';
 8     return x;
 9 } 
10 int a,b;
11 int dp[12][12],s[12];
12 inline int dfs(int
now,int pre,int up,int oo) 13 { 14 if(now==0) return 1; 15 if(!up&&dp[now][pre]!=-1) return dp[now][pre]; 16 int maxn=up?s[now]:9; 17 int res=0; 18 for(int i=0;i<=maxn;i++) 19 { 20 if(abs(i-pre)<2) continue; 21 if(oo&&i==0) res+=dfs(now-1,-5
,0,1); 22 else res+=dfs(now-1,i,(up&&i==maxn),0); 23 } 24 if(!up&&!oo) dp[now][pre]=res; 25 return res; 26 } 27 inline int part(int x) 28 { 29 int tot=0; 30 while(x) 31 { 32 s[++tot]=x%10;x/=10; 33 } 34 memset(dp,-1,sizeof(dp)); 35 return
dfs(tot,-5,1,1); 36 } 37 int main() 38 { 39 a=read();b=read(); 40 printf("%d\n",part(b)-part(a-1)); 41 return 0; 42 }
View Code