Description
Windy defines a windy number. A positive integer that does not contain a leading 0 and the 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?
Input
Contains two integers, A B.
Output
An integer.
Sample Input"Input Sample One"
1 10
"Input Sample Two"
theSample Output"Output Example One"
9
"Output Example II"
-HINT
"Data size and conventions"
100% data, meet 1 <= A <= B <= 2000000000.
The puzzle: It's obviously a digital DP. We set F[I][J] to indicate the number of I digits, the highest bit is the number of J's qualifying numbers. So
F[i][j]=sigma (F[i-1][k]) (ABS (J-K) >=2);
Then the direct set of the digital DP is good.
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib >using namespace Std;int x,f[50][10],l,r,up;int work (int x) {if (x==0) return 0; int t (0), ans (0), W[101],pre; while (x>0) {t++;w[t]=x%10;x/=10;} for (int. i=1;i<t;i++) for (int j=1;j<=9;j++) ANS+=F[I][J]; for (int j=1;j<w[t];j++) ANS+=F[T][J]; PRE=W[T]; for (int i=t-1;i>0;i--) {if (I==1&&abs (w[i]-pre) >=2) ans++; for (int j=0;j<=min (pre-2,w[i]-1); j + +) Ans+=f[i][j]; for (int j=pre+2;j<=w[i]-1;j++) ANS+=F[I][J]; if (ABS (W[I]-PRE) <2) break; Pre=w[i]; } return ans; int main () {scanf ("%d%d", &l,&r); for (int i=0;i<=9;i++) f[1][i]=1; X=r; while (x>0) {up++;x/=10;} for (int i=2;i<=up;i++) for (int. j=0;j<=9;j++) {for (int k=j+2;k<=9;k++) f[i][j]+=f[i-1][k] ; for (int k=j-2;k>=0;k--) f[i][j]+=f[i-1][K]; } printf ("%d", work (R)-work (L-1)); }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"bzoj1026" "SCOI2009" "Windy number" "Digital DP"