hdu3652 B-number

Problem Description
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.
 

Input
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
 

Output
Print each answer in a single line.
 

Sample Input
   
   
   
   
13 100 200 1000
 

Sample Output
   
   
   
   
1 1 2 2
 

Author
wqb0039
 

Source
The 35th ACM/ICPC Asia Regional Chengdu Site —— Online Contest


去年成都网赛的一题,表示现在才会做,鸭梨颇大……

好吧,一个记忆化搜索,发现都有现成的模板了……orz

代码

#include <stdio.h>
#include <string.h>

int digit[15];
int dp[15][15][15][2];

int DFS(int pos,int pre,int mod,bool have,bool inf)
{
    int i,j;
    if (pos==-1) return (have && !mod);
    if (!inf && dp[pos][pre][mod][have]!=-1) return dp[pos][pre][mod][have];
    int ans=0;
    int end=inf?digit[pos]:9;
    for (i=0;i<=end;i++)
    {
        if (pre==1 && i==3) ans+=DFS(pos-1,3,(mod*10+i)%13,1,inf && (i==digit[pos]));
        else ans+=DFS(pos-1,i,(mod*10+i)%13,have,inf && (i==digit[pos]));
    }
    if (!inf)
    {
        dp[pos][pre][mod][have]=ans;
    }
    return ans;

}

int Calc(int t)
{
    int pos=0;
    while(t)
    {
        digit[pos++]=t%10;
        t/=10;
    }
    return DFS(pos-1,0,0,0,1);

}

int main()
{
    int i,j,n;
    memset(dp,-1,sizeof(dp));
    while(scanf("%d",&n)!=EOF)
    {
        printf("%d\n",Calc(n));
    }
    return 0;
}


你可能感兴趣的:(hdu3652 B-number)