Bzoj 1026 SCOI2009 Windy number

Source: Internet
Author: User

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.

This is the first digital DP question I wrote, I am very happy in my heart >_<

Because I can not find a good job on the internet, so I think it's best for me to write another one, though not necessarily well written

First, we can preprocess the number of windy in the absence of any limitations. DP[I][J] Indicates the number of scenarios for which I am filling j, and I write the transfer from backward to forward, obviously dp[i][j] is equal to Sigma (Dp[i+1][k]) (ABS (K-J) >=2).

Next, we can deal with the number of scenarios where f[i] represents the maximum value of the bit in the first position. Set this number I bit is a[i], then transfer to F[i]=sigma (Dp[i+1][k]) (K<=a[i+1] && abs (A[I]-K) >=2)

Then we can consider how to count the answers. Since the number of windy between [l,r] is required, it is clear that the prefix and the difference can be converted. Set solve (i) to a number of windy of less than or equal to I, then Ans=solve (r)-solve (L-1)

So we just need to know the number of windy that is less than or equal to X. Let's think about that.

First of all, we can first find this number the first not 0 of the ground position W. This position can then obviously be 1~a[w]-1, and this part of the answer is Sigma (Dp[w][i]) (1<=i<a[w]). Then, when this position is put a[w] The number of scenarios is f[w]

But then we have to deal with the leading 0. We can see that only the leading 0 is more than the number of W to be counted. So, we'll count Sigma (Dp[i][j]) (i>w && 1<=j<=9). And then it's the output.

Here's the code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 #defineFile (s) freopen (S ".", "R", stdin), Freopen (S ". Out", "w", stdout)7 #defineINF 21474836478 9 using namespacestd;TentypedefLong LongLlg; One  A inta[2][ -],g[ -][Ten],n,ans,x,y,z; - intf[ -]; -  the voidInitintx) { -     intnow=0, ww=1; -      while(!A[X][WW] && ww<=n) ww++; -Memset (G,0,sizeof(g)); +Memset (F,0,sizeof(f)); f[n]=1; -      for(intI=0; i<=9; i++) g[n][i]=1; +      for(inti=n-1; i;i--){ A          for(intj=0; j<=9; j + +) at              for(intk=0; k<=9; k++) -                 if(ABS (J-K) >=2) g[i][j]+=g[i+1][k]; -          for(intj=0; j<a[x][i+1];j++) -             if(ABS (A[X][I]-J) >=2) f[i]+=g[i+1][j]; -         if(ABS (a[x][i]-a[x][i+1]) >=2) f[i]+=f[i+1]; -     } in      for(inti=ww+1; i<=n;i++) -          for(intj=1; j<=9; j + +) now+=G[i][j]; to     if(ww<=N) { +          for(intI=1; i<a[x][ww];i++) now+=G[ww][i]; -now+=F[WW]; the     } *     if(x) Ans+=now;Elseans-=Now ; $ }Panax Notoginseng  - intMain () { theFile ("a"); +scanf"%d%d",&x,&y); AX--;z=y; while(z) n++,z/=Ten; the      for(inti=n;i;i--){ +a[0][i]=x%Ten, x/=Ten; -a[1][i]=y%Ten, y/=Ten; $     } $Init0); Init1); -printf"%d", ans); -     return 0; the}

Bzoj 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.