1026: [Scoi2009]windy number

Source: Internet
Author: User

Time limit:1 Sec Memory limit:162 MB
submit:9670 solved:4445
[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.

The approximate meaning of windy number is: like 135,6913,13579, so the difference of every two adjacent numbers ≥2

Digital DP:

Direct speculation, it is impossible to preserve the 2000000000 state, and a more ingenious approach must be adopted.

you can set the DP array to: F[i][j],i represents the number of digits , J represents the highest number

Thus the DP equation can be obtained:

\[f[i][j]=\sum_{k=0}^{k\leq 9} f[i-1][k]\left (\left | k-j \right |\geq 2\right) \]

But this only calculates the result of the highest level J, unable to find the result in the range, then can convert the thought, find out the result of 1~b minus 1~a-1, is not the answer?

Set Len to indicate number of digits, T[i] represents the number of the I-bit

First, the ANS plus the 1~len-1 bit situation

Then ans plus the highest digit number for the 1~t[len]-1 case

Then and so on, coupled with the situation of the second highest 1~t[len-1]-1, coupled with the situation of the next highest bit 1~t[len-2]-1 ...

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cmath>5 using namespacestd;6 7 #defineLL Long Long8 9 intb;TenLL f[ -][Ten]; One  ALL Solve (intt) - { -LL ans=0; the     intlen=0, t[ -]; -      while(t) -     { -t[++len]=t%Ten; +T/=Ten; -     } +      for(intI=1; i<len;i++) A          for(intj=1; j<=9; j + +) atans+=F[i][j]; -      for(intI=1; i<t[len];i++) -ans+=F[len][i]; -      for(inti=len-1; i>=1; i--) -     { -          for(intj=0; j<t[i];j++) in             if(ABS (j-t[i+1]) >=2) -ans+=F[i][j]; to         if(ABS (t[i]-t[i+1]) <2) Break; +     } -     returnans; the } *  $ intMain ()Panax Notoginseng { -scanf"%d%d",&a,&b); the      for(intI=0; i<=9; i++) f[1][i]=1; +      for(intI=2; i<= the; i++) A          for(intj=0; j<=9; j + +) the              for(intk=0; k<=9; k++) +                 if(ABS (K-J) >=2) -f[i][j]+=f[i-1][k]; $Cout<<solve (b +1)-solve (a) <<Endl; $     return 0; -}

1026: [Scoi2009]windy number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.