1026: [Scoi2009]windy number time limit:1 Sec Memory limit:162 MB
submit:6392 solved:2854
[Submit] [Status] [Discuss] 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.
Analysis: The first time to do the digital DP problem, for me still have a certain degree of difficulty.
First of all, the meaning of the topic, windy number is for example, 135,13 the number of adjacent components of the number of the difference greater than 2. The data given by a B is very large, so it is not possible to represent each digit in the state, so it is necessary to take some characteristic quantity as the state. Then set the f[i][j] Is the number of windy of J for the top I bit. Obviously, f[i][j] = SUM (f[i-1][k]) |k-j| >= 2.
The topic lets us ask for an interval of number of windy, think of the prefix and, with 0 to R windy number minus 0 to the number of L-1windy. So the question is, how do you find the number of windy in the 0 to L interval? The state that we define is a macroscopic state, and the direct accumulation may result in a number that accumulates beyond the interval, and therefore needs to be discussed in categories.
Suppose we need to ask 0 to X (expressed in an array) of the number of windy of the interval, X has t-bit, we first find out the number of windy number of t-1 bit, because these windy number is definitely smaller than X, will not exceed this interval, and then find the length of T, the highest bit less than x[0] number of windy, No more than this interval. The last statistic length is T, the highest bit is x[0] number of windy, how to count? The enumeration I from 0 to x[1]-1, plus the number of the highest bit length of t-1, does not exceed this interval, and then the number of windy of the highest bit x[1] is similar to the recursive process. If ABS (X[0]-x[1]) < 2, the highest bit is x[0], Second high for x[1] windy number no longer exist, direct exit, to the last one, if there is windy number, the number of windy +1 can be.
It's a little bit abstract, and the code might be easier to understand:
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>using namespacestd;Long LongT, a, B;Long Longf[ the][ One],shu[ the];voidinit () {memset (F,0,sizeof(f)); for(inti =0; I <=9; i++) f[1][i] =1; for(inti =2; I <=Ten; i++) for(intj =0; J <=9; J + +) for(intK =0; K <=9; k++) if(ABS (J-K) >=2) F[i][j]+ = F[i-1][k];}Long LongSolveLong Longx) {memset (Shu,0,sizeof(SHU)); if(x = =0) return 0; Long LongK =0, ans =0; while(x) {shu[++K] = x%Ten; X/=Ten; } for(inti =1; I <= K-1; i++) for(intj =1; J <=9; J + +) ans+=F[i][j]; for(inti =1; i < shu[k]; i++) ans+=F[k][i]; for(inti = k-1; I >=1; i--) { for(intj =0; J <= Shu[i]-1; J + +) if(ABS (J-shu[i +1]) >=2) ans+=F[i][j]; if(ABS (Shu[i +1]-shu[i]) <2) Break; if(i = =1) ans+=1; } returnans;}intMain () {scanf ("%lld%lld", &a, &b); Init (); printf ("%lld", solve (b)-Solve (A-1)); //while (1); return 0;}
bzoj1026 [Scoi2009]windy number