Title Link: http://www.lydsy.com/JudgeOnline/problem.php?id=1026
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 want to know,
What is 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.
Thoughts: Digital DP The first question, adjusted a day .....
Exercises
The digit DP,F[I][J] represents the length of I, and the windy number of the first and the the end of J.
Then the first simple preprocessing of the F array.
And then what?
Assuming that the number that is required is ABCD, then we can first calculate the number of four numbers that do not contain ABCD (i.e. A is not thousands, B is not in the hundreds, etc.).
Then, figure out the number of one by one included (that is, a in thousands, b in the Hundred, and so on).
If the two neighbors are only <2, break.
By the Solve (m) the weight does not count m, so m should add 1.
Then, solve (x) calculates the number of windy of the 1~x-1, as long as solve (m+1)-solve (n) is done.
Code
#include <cstdio>using namespacestd;inti,j,k,n,m,f[ One][Ten];inlineintAbsintx) {returnX>0? x:-x;}voidinit () { for(i=1; i<=9; i++) f[1][i]=1; f[1][0]=1; for(i=2; i<= One; i++){ for(j=0; j<=9; j + +){ for(k=0; k<=9; k++) if(ABS (J-K) >=2) f[i][j]+=f[i-1][k]; }}}inlineintSolveintx) { intlen=0, a[ the],t=x,ans=0; while(t) a[++len]=t%Ten, t/=Ten; for(i=1; i<a[len];i++) ans+=F[len][i]; for(i=len-1; i>=1; i--) for(j=1; j<=9; j + +) ans+=F[i][j]; for(i=len-1; i>=1; i--){ for(j=0; j<a[i];j++) if(ABS (a[i+1]-J) >=2) ans+=F[i][j]; if(ABS (a[i]-a[i+1]) <2) Break; } returnans;}intMain () {init (); scanf ("%d%d",&n,&m); printf ("%d\n", Solve (m+1)-solve (n)); return 0;}
Number of Bzoj1026windy