Test instructions: A positive integer that does not contain a leading 0 and has a difference of at least 2 of the adjacent two digits is called the windy number.
Windy would like to know the total number of windy between A and B, including a and B?
IDEA: Digital DP
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd;intdp[ the][Ten];//Dp[i][j] Indicates the number of windy with a length of I and a maximum of Jvoidinit () {memset (DP,0,sizeof(DP)); inti,j,k; for(j=0;j<Ten; ++j) dp[1][j]=1; for(i=2; i<=Ten;++i) for(j=0; j<=9;++j) { for(k=0; k<=j-2; ++k) dp[i][j]+=dp[i-1][k]; for(k=j+2; k<=9; ++k) dp[i][j]+=dp[i-1][k]; }}intbit[ the];intCalcintN) { if(n==0)return 0; intlen=0, I,j; while(n) {bit[++len]=n%Ten; N/=Ten; } Bit[len+1]=-Ten; BOOLflag=true; intans=0; for(i=1; i<len;++i)//first count the length less than Len for(j=1; j<=9;++j) Ans+=Dp[i][j]; for(j=1; j<bit[len];++j) Ans+=dp[len][j];//Highest bit for(i=len-1; i>=1;--i) { for(j=0; j<bit[i];++j)if(ABS (bit[i+1]-J) >=2) ans+=Dp[i][j]; if(ABS (bit[i+1]-bit[i]) <2) {flag=false; Break; } } if(flag) ++ans;//Plus n itself returnans;}intMain () {init (); intb; while(~SCANF ("%d%d",&a,&b)) {printf ("%d\n", Calc (b)-calc (A-1)); } return 0;}
View Code
UESTC Windy number (digital DP)