hdu3652B-number(数位dp)

B-number

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


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
 
 
 

题意:找出1~n范围内含有13并且能被13整除的数字的个数

思路:http://blog.csdn.net/libin56842/article/details/10026063

#include<cstdio>
#include<iostream>
#include<cstring>

using namespace std;
int dit[15],f[15][15][3];

int dfs(int pos,int mod,int have,int lim)
{
    int num,ans,mod_x,have_x;
    if(pos<=0) return mod==0 && have==2;
    if(!lim && f[pos][mod][have]!=-1) return f[pos][mod][have];
    num=lim?dit[pos]:9;ans=0;
    for(int i=0;i<=num;i++)
    {
        mod_x=(mod*10+i)%13; have_x=have;
        if(have==0 && i==1) have_x=1;
        if(have==1 && i!=1) have_x=0;
        if(have==1 && i==3) have_x=2;
        ans+=dfs(pos-1,mod_x,have_x,lim&&i==num);
    }
    if(!lim) f[pos][mod][have]=ans;
    return ans;
}

int main()
{
    int n,len;
    while(~scanf("%d",&n))
    {
        memset(dit,0,sizeof dit);
        memset(f,-1,sizeof f);len=0;
        while(n)
        {
            dit[++len]=n%10;
            n/=10;
        }dit[len+1]=0;
        printf("%d
",dfs(len,0,0,1));
    }
    return 0;
}
 
折花枝,恨花枝,准拟花开人共卮,开时人去时。 怕相思,已相思,轮到相思没处辞,眉间露一丝。
原文地址:https://www.cnblogs.com/L-Memory/p/7190494.html