【BZOJ】【1026】【SCOI2009】Windy数
数位DP
cxlove基础数位DP第三题
= =预处理是个很有用的东西!然后就是分类讨论!
1 /************************************************************** 2 Problem: 1026 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:0 ms 7 Memory:1272 kb 8 ****************************************************************/ 9 10 //BZOJ 1026 11 #include<cmath> 12 #include<vector> 13 #include<cstdio> 14 #include<cstring> 15 #include<cstdlib> 16 #include<iostream> 17 #include<algorithm> 18 #define rep(i,n) for(int i=0;i<n;++i) 19 #define F(i,j,n) for(int i=j;i<=n;++i) 20 #define D(i,j,n) for(int i=j;i>=n;--i) 21 #define pb push_back 22 using namespace std; 23 int getint(){ 24 int v=0,sign=1; char ch=getchar(); 25 while(!isdigit(ch)) {if(ch=='-') sign=-1; ch=getchar();} 26 while(isdigit(ch)) {v=v*10+ch-'0'; ch=getchar();} 27 return v*sign; 28 } 29 const int N=1e7+10,INF=~0u>>2; 30 const double eps=1e-8; 31 /*******************template********************/ 32 int dp[15][15]; 33 void init(){ 34 F(i,0,9) dp[1][i]=1; 35 F(i,2,10) 36 F(j,0,9) 37 F(k,0,9) 38 if (abs(j-k)>=2) dp[i][j]+=dp[i-1][k]; 39 } 40 int solve(int n){ 41 int len=0,bit[15]; 42 for(;n;n/=10) bit[++len]=n%10; 43 bit[len+1]=0; 44 int ans=0; 45 //长度为1~len-1的 46 F(i,1,len-1) 47 F(j,1,9) 48 ans+=dp[i][j]; 49 //长度为len,但最高位没达到上界 50 F(j,1,bit[len]-1) ans+=dp[len][j]; 51 //长度为len,且最高位达到上界 52 D(i,len-1,1){ 53 F(j,0,bit[i]-1) 54 if (abs(j-bit[i+1])>=2) ans+=dp[i][j]; 55 if (abs(bit[i]-bit[i+1])<2) break; 56 } 57 return ans; 58 } 59 int main(){ 60 init(); 61 int l,r; 62 while(scanf("%d%d",&l,&r)!=EOF) 63 printf("%d\n",solve(r+1)-solve(l)); 64 return 0; 65 } 66