首页 > 代码库 > 数位DP [HDU 3652] B-number

数位DP [HDU 3652] B-number

B-number

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2668    Accepted Submission(s): 1467

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
 
数位DP模板题、只不过好久好久没写数位DP了、都忘了
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int bit[15];int dp[15][15][3];int dfs(int pos,int s1,int s2,bool limit)  //s1代表余数,s2代表状态{    if(pos==-1)    {        return (s1==0 && s2==2);    }    if(!limit && dp[pos][s1][s2]!=-1) return dp[pos][s1][s2];    int ans=0;    int end=limit?bit[pos]:9;    for(int i=0;i<=end;i++)    {        int nows2=0;        if(s2==0)        {            if(i==1) nows2=1;        }        else if(s2==1)        {            if(i==1) nows2=1;            else if(i==3) nows2=2;        }        else if(s2==2) nows2=2;        ans+=dfs(pos-1,(s1*10+i)%13,nows2,limit && i==end);    }    if(!limit) dp[pos][s1][s2]=ans;    return ans;}int cal(int n){    int len=0;    while(n)    {        bit[len++]=n%10;        n/=10;    }    return dfs(len-1,0,0,1);}int main(){    int n;    memset(dp,-1,sizeof(dp));    while(scanf("%d",&n)!=EOF)    {        printf("%d\n",cal(n));    }    return 0;}

 

 

数位DP [HDU 3652] B-number