HDU 3652 B-number 数位dp

G - B-number
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 3652
Appoint description:  System Crawler  (2016-04-22)

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
 数位dp flag表示状态 mod判断是否能整除
ACcode:
#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
int dp[25][25][3];
int date[25];
int dfs(int pos,int mod,int flag,bool limit){
    if(!pos)return mod==0&&flag==2;
    if(!limit&&dp[pos][mod][flag]!=-1)return dp[pos][mod][flag];
    int ret=0;
    int ed=limit?date[pos]:9;
    for(int i=0;i<=ed;i++){
        int tmp=flag;
        int modd=(mod*10+i)%13;
        if(flag==1&&i==3)
            tmp=2;
        else if(flag==0&&i==1)
            tmp=1;
        else if(flag==1&&i!=1&&i!=3)
            tmp=0;
        ret+=dfs(pos-1,modd,tmp,limit&&i==ed);
    }
    return limit?ret:dp[pos][mod][flag]=ret;
}
void doit(int n){
    int len=0;
    while(n){
        date[++len]=n%10;
        n/=10;
    }
    printf("%d\n",dfs(len,0,0,1));
}
int main(){
    int n;memset(dp,-1,sizeof(dp));
    while(~scanf("%d",&n))doit(n);
}


你可能感兴趣的:(C++,HDU)