Question: windy number
Time limit: 1 Sec Memory Limit: 512 MB
Face questions
Title Description
It defines a number of Windy Windy: without leading zeros and adjacent to at least the difference between two numbers
The number of positive integers is called Windy.
Windy want to know, in A and B between, including A and B, a total of how many Windy number?
Input Format
Two line numbers, respectively, A, B .
Output Format
Output An integer that represents the answer.
Sample input
1 10
Sample Output
9
answer
My Digital dp entry title, ah, in fact, quite easy.
Set f [i] [j] denotes the i fill bits, the maximum number is the number of bits of windy j.
So do not consider what God hath pilot 0 of issues to consider temporary position gap of at least 2 issues directly and vigorously dp calculated all the values.
Then do it again vigorously dp, j from 1-9 cycles accumulate answer it.
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #define int long long #define rint register int #define ll long long using namespace std; int f[20][15]={0},a,b; ll pw[15]; void prework() { pw[0]=1; for(rint i=1;i<=12;++i)pw[i]=pw[i-1]*10; for (RINT I = 0 ; I <= . 9 ; I ++) F [ . 1 ] [I] = . 1 ; for (I = RINT 2 ; I <= . 11 ; I ++) // enumerator bits for (RINT = J 0 ; J <= . 9 ; J ++) // enumeration highest for (RINT K = 0 ; K <= . 9 ; K ++) // a most significant bit of the state enumeration, views this state high IF (ABS (JK)> = 2 ) F [I] [J] + = F [I- . 1 ] [K]; } int COUNT ( int X) { int W = 0 , Y, ANS =0,pre; while(pw[w]<=x)++w;//求位数 for(rint i=1;i<w;++i)//枚举位数 for(rint j=1;j<=9;++j)//枚举最高位 ans+=f[i][j]; y=x/pw[w-1]; for(rint i=1;i<y;++i)ans+=f[w][i]; pre=y; x%=pw[w-1]; for(rint i=w-1;i>=1;--i) { y=x/pw[i-1]; for(rint j=0;j<y;++j) if(abs(j-pre)>=2) ans+=f[i][j]; if(abs(pre-y)<2)break; pre=y; x%=pw[i-1]; } return ans; } signed main() { scanf("%lld %lld",&a,&b); prework(); cout<<count(b+1)-count(a)<<endl; return 0; }