Bzoj 1026: [Scoi2009]windy number "digital DP"

Source: Internet
Author: User

Forget limit can't remember wa ...
Typical digital DP, which becomes work (R)-work (L-1), then Dfs records w current position, la last number selected, LM current bit is upper limit, OK current bit can not consider the case of difference greater than or equal to 2 (front all is 0)
Then, for LM and OK are 0 of the case of memory can be
Ah Bzoj don't know why give Cmath under the ABS award CE ... It's okay to switch to algorithm.

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int N=15;long long l,r,f[N][N],a[N],tot;long long dfs(int w,int la,int lm,int ok){    if(!lm&&!ok&&f[w][la])        return f[w][la];    if(!w)        return 1;    long long r=0;    if(lm)    {        for(int i=0;i<=a[w];i++)            if(abs(la-i)>1||ok)                r+=dfs(w-1,i,i==a[w],ok&&i==0);    }    else    {        for(int i=0;i<=9;i++)            if(abs(la-i)>1||ok)                r+=dfs(w-1,i,0,ok&&i==0);    }    if(!lm&&!ok)        return f[w][la]=r;    else        return r;}long long wk(long long x){    tot=0;    while(x)        a[++tot]=x%10,x/=10;    return dfs(tot,13,1,1);}int main(){    scanf("%lld%lld",&l,&r);    printf("%lld\n",wk(r)-wk(l-1));    return 0;}

Bzoj 1026: [Scoi2009]windy number "digital DP"

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.