Question link: http://acm.uestc.edu.cn/problem.php? PID = 1, 1307
Windy number time limit: 1000 MS Memory limit: 65536 KB Solved: 104 Tried: 720 Submitstatusbest solutionbackdescription
Windy defines the number of windy.
A positive integer that does not contain leading zero and the difference between two adjacent numbers is at least 2 is called the windy number.
Windy: How many windy numbers are there between A and B, including a and B?
Input
Contains two integers, a B.
1 <= A <= B <= 2000000000.
Output
Contains an integer: the number of windy numbers in the closed interval [a, B.
Sample Input
1 10
Sample output
9
Source
Windy
For a simple digital DP, the difference between two adjacent numbers must be greater than or equal to 2.
/* * UESTC 1307 * Number of windy: Excluding leading 0, the difference between two adjacent numbers is at least a positive integer of 2 * calculate the number of windy between [a, B, 1 <= A <= B <= 2000000000 */ # Include <Iostream> # Include < String . H> # Include <Stdio. h> # Include <Algorithm> Using Namespace STD; Int DP [15 ] [ 10 ]; // DP [I] [J] indicates the number of windy numbers whose length is I and whose highest bit is J. Void Init () {memset (DP, 0 , Sizeof (DP )); For ( Int I = 0 ; I < 10 ; I ++ ) DP [ 1 ] [I] = 1 ; For ( Int I = 2 ; I <= 10 ; I ++ ) For ( Int J = 0 ; J < 10 ; J ++ ){ For ( Int K =0 ; K <= J- 2 ; K ++ ) DP [I] [J] + = DP [I- 1 ] [K]; For ( Int K = J + 2 ; K < 10 ; K ++ ) DP [I] [J] + = DP [I- 1 ] [K] ;}} Int Bit [ 20 ]; Int Calc ( Int N ){ If (N = 0 ) Return 0 ; Int Len = 0 ; While (N) {bit [ ++ Len] = n % 10 ; N /= 10 ;} Bit [Len + 1 ] =- 10 ; Int Ans = 0 ; Bool Flag = True ; For ( Int I = 1 ; I <Len; I ++) // Calculate the number of characters whose length is less than Len For ( Int J = 1 ; J <= 9 ; J ++ ) Ans + = DP [I] [J]; For ( Int J = 1 ; J <bit [Len]; j ++) // Highest bit Ans + = DP [Len] [J]; For (Int I = len- 1 ; I> = 1 ; I -- ){ For ( Int 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 ++ ; Return Ans ;} Int Main (){ // Freopen ("in.txt", "r", stdin ); // Freopen ("out.txt", "W", stdout ); Int A, B; Init (); While (Scanf ( " % D " , & A, & B) = 2 ) {Printf ( " % D \ n " , Calc (B)-Calc (- 1 ));} Return 0 ;}