hud 2089 싫어 62 (디지털 dp)

6582 단어 dp
#include<stdio.h>

#include<string.h>

#include<math.h>

#define max 10

int dp[max][3];

int number[max];

//dp[i][0]  i            

//dp[i][1]  i       2   

//dp[i][2]  i      4    62   

void init()

{

    memset(dp,0,sizeof(dp));

    dp[0][0]=1;

    for(int i=1;i<max;i++)

    {

        dp[i][0]=dp[i-1][0]*9-dp[i-1][1];   //  4    6+2.. 

        dp[i][1]=dp[i-1][0]; //2+,

        dp[i][2]=dp[i-1][2]*10+dp[i-1][0]+dp[i-1][1];

    }

}

int len_num(int num)

{

    int i=0;

    memset(number,0,sizeof(number));

    while(num)

    {

        ++i;

        number[i]=num%10;

        num/=10;

    }

    return i;

}

int solve(int num)

{

    int i,flag=0,count=0;

    int len=len_num(num);

    number[len+1]=0;

    for(i=len;i>0;i--)

    {

        count+=dp[i-1][2]*number[i];

        if(flag)

        {

            count+=dp[i-1][0]*number[i];

        }

        else

        {

            if(number[i]>4) count+=dp[i-1][0];

            if(number[i]>6) count+=dp[i-1][1];

            if(number[i]>2&&number[i+1]==6) count+=dp[i][1];//  62                 2      

            if((number[i]==2&&number[i+1]==6)||number[i]==4)

            flag=1; 

        }

    }

    return num-count;

}

int main(void)

{

    init();

    int n,m,i,j;

    while(scanf("%d%d",&n,&m)&&n&&m)

    {

        printf("%d
",solve(m+1)-solve(n)); } return 0; }

좋은 웹페이지 즐겨찾기