Hdu2089: No 62 (Basic digit DP)

Source: Internet
Author: User

Define a legal number that cannot contain 4 or consecutive 62

The number of valid numbers in the specified range [n, m]

Analysis: Digital DP

DP [I] [J] indicates the number of valid I-bits with the highest bit J.

Then, transfer according to the question rules.

After the end of DP, count the total number in the range, and finally output the table.

Code:

 

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAX 100000000int n,k,m;int ans[1000010];int dp[10][10];int fun(int x){    int t=0;    int res=0;    int a[10];    while(x)    {        a[t++]=x%10;        x/=10;    }    for(int i=t;i;i--)    {        for(int j=0;j<a[i-1];j++)        {            if((!(j==2&&a[i]==6)))                res+=dp[i][j];        }        if((a[i-1]==2&&a[i]==6)||a[i-1]==4)            break;    }    return res;}void DP(){    for(int i=0;i<=9;i++)    {        if(i!=4)            dp[1][i]=1;        else            dp[1][i]=0;    }    for(int i=2;i<=7;i++)    {        for(int j=0;j<=9;j++)        {            for(int k=0;k<=9;k++)            {                if(j!=4&&(j!=6||k!=2))                    dp[i][j]+=dp[i-1][k];            }        }    }}void solve(){    for(int i=1;i<=1000001;i++)    {        ans[i]=fun(i);    }}int main(){    DP();    solve();    while(scanf("%d%d",&n,&m)&&(n+m))    {        printf("%d\n",ans[m+1]-ans[n]);    }    return 0;}

 

Hdu2089: No 62 (Basic digit 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.