1026: [Scoi2009]windy number time limit:1 Sec Memory limit:162 MB
submit:5230 solved:2353
[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.
SourceSolution
Digital DP Bare Topic
F[I][J] Indicates the number of satisfied conditions where the highest bit of the number of bits is J
Just transfer it, and pay attention when you're 0.
Code
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;intL,r;intf[ -][Ten];voidprework () { 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];}intCalc (intx) { if(x==0)return 0; intdigit[ One],len=0, ans=0; while(x) {digit[++len]=x%Ten; X/=Ten;} for(intI=1; i<len; i++) for(intj=1; j<=9; J + +) ans+=F[i][j]; for(intI=1; i<=digit[len]-1; i++) ans+=F[len][i]; for(inti=len-1; i>=1; i--) { if(i==1&& ABS (digit[i+1]-digit[i]) >=2) ans++; for(intj=0; j<=digit[i]-1; J + +) if(ABS (digit[i+1]-J) >=2) ans+=F[i][j]; if(ABS (digit[i+1]-digit[i]) <2) Break; } returnans;}intMain () {scanf ("%d%d",&l,&R); Prework (); printf ("%d\n", Calc (R)-calc (l1)); return 0;}
SB-digit DP series: Water is no longer water ...
"BZOJ-1026" Windy digital DP