HDU - 3652 B-number(数位dp)

B-number

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.

InputProcess till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).OutputPrint each answer in a single line.Sample Input

13
100
200
1000

Sample Output

1
1
2
2



题意:满足含13且被13整除的个数。

“不要62”的变式,这里要“13”,统计连续两位数的另一种处理方式。


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int a[12];
int dp[12][2][2][15]; 

ll dfs(int pos,int pre,int one,int sta,bool p,bool limit){
    
    if(pos==-1) return sta==0&&p;
    if(!limit&&dp[pos][one][p][sta]>-1) return dp[pos][one][p][sta];
    int up=limit?a[pos]:9;
    int cnt=0;
    for(int i=0;i<=up;i++){
        if(pre==1&&i==3) cnt+=dfs(pos-1,i,i==1,(sta*10+i)%13,true,limit&&i==a[pos]);
        else cnt+=dfs(pos-1,i,i==1,(sta*10+i)%13,p,limit&&i==a[pos]);
    }
    if(!limit) dp[pos][one][p][sta]=cnt;
    return cnt;
}
int solve(int x){
    int pos=0;
    while(x){
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,0,0,false,true);
}
int main()
{
    int r;
    memset(dp,-1,sizeof(dp));
    while(~scanf("%d",&r)){
        printf("%d\n",solve(r));
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yzm10/p/10328613.html