$ SCOI2009 \ windy $ $ dp $ digit number

\(Sol\)

Digital \ (dp \) regular routine questions.

\ (dp [i] [j ] \) represent from low to high fill into the first \ (i \) position and the first \ (i \) digits for the \ (j \) the number of programs. The answer is \ (sol (r) -sol (l + 1 ). \) where \ (DP \) of the process is very simple, general calculation error is less than or equal \ (X \) a \ (Windy \) number, so briefly here this process:

First, a pretreatment \ (X \) digits \ (CT \) , \ (A [I] \) represents \ (X \) from low to high of \ (I \) digits is the number, then a first answer accumulated safe state \ (AS = + \ _ {I SUM =. 1. 1} ^ {}-CT \ J = SUM {_ ^. 1} {F}. 9 [I] [J] \) .

Then a second answer cumulative security state, which is the first \ (CT \) bits (from low to high) is a number less than \ (a [ct] \) of \ (AS = + \ sum_ {J} = ^. 1 A {[CT] -1} F [CT] [J] \) .

The last is the most error-prone areas is the answer statistical dangerous state. Suppose the current fill into the first \ (i \) bit and \ (i \) to \ (ct \) numbers are and \ (x \) , like, then the first \ (i-1 \) number of bits have filled the two limits less than one \ (a [-I. 1] \) , there is to the \ (a [i] \) is the difference between greater than or equal to the absolute value of \ (2 \) .

Will find the above statistics there are two vulnerabilities, one if the number \ (x \) is \ (windy \) number, it will be considered a little, the problem is solved, like a judgment. Let a problem is to assume that the current fill in the \ (I \) , and \ (ABS (A [I] -a [-I. 1]) <= 2 \) , then the next one will not fill \ (A [-I. 1] \) , then the answer will not continue statistics dangerous state, and directly \ (break \) away.

\(Code\)

Code

#include<bits/stdc++.h>
#define il inline
#define Ri register int
#define go(i,a,b) for(Ri i=a;i<=b;++i)
#define yes(i,a,b) for(Ri i=a;i>=b;--i)
#define e(i,u) for(Ri i=b[u];i;i=a[i].nt)
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define db double
#define inf 2147483647
using namespace std;
il int read()
{
    Ri x=0,y=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    return x*y;
}
int f[20][10],a[20];
il void init()
{
    go(i,0,9)f[1][i]=1;
    go(i,2,10)
    go(j,0,9)
    go(k,0,9)
    if(abs(j-k)>=2)f[i][j]+=f[i-1][k];
}
il int sol(Ri x)
{
    if(!x)return 0;
    Ri qvq=x,ct=0,ret=0;bool fl=1;
    while(qvq){a[++ct]=qvq%10;qvq/=10;}
    go(i,1,ct-1)
    go(j,1,9)ret+=f[i][j];
    go(i,1,a[ct]-1)ret+=f[ct][i];
    yes(i,ct,2)
    {
    go(j,0,a[i-1]-1)
    {
        if(abs(j-a[i])>=2)ret+=f[i-1][j];
    }
    if(abs(a[i]-a[i-1])<2){fl=0;break;}
    }
    return ret+fl;
}
int main()
{
    init();
    Ri l=read(),r=read();
    printf("%d\n",(sol(r)-sol(l-1)));
    return 0;
}

Guess you like

Origin www.cnblogs.com/forward777/p/11592770.html