HDU 3652 B-number (数位DP)

来源:互联网 发布:白帽seo技术 编辑:程序博客网 时间:2024/06/03 14:21



B-number

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


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
131002001000
 

Sample Output
1122
 

Author
wqb0039
 
题意:求1 - n个数里面有几个含有13并且能被13整除的数字

思路:含有13很好求,关键是怎么计算能被13整除的,数位DP搞得都是递归,一位一位的递归,其实除法也是一位一位的放下做啊。。。dfs里面+一个函数,表示前面数字%13的余数,当前数字就是 *10 %13了。。算到最后一位,看看是不是0就可以了


<span style="font-size:14px;">#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn = 12;int dp[maxn][13][3], bit[maxn];int dfs(int len, int pre, int Mod, int limit){    if(len < 1) return pre == 2 && Mod == 0;  //如果含有13并且余数等于0    if(!limit && dp[len][Mod][pre] != -1) return dp[len][Mod][pre];     int last = limit ? bit[len] : 9;    int ans = 0;    for(int i = 0; i <= last; i++)    {        int temp = (Mod * 10 + i) % 13;  //从最高位开始的,所以等于模拟除法的过程        if(pre == 0 || pre == 1&& i != 3)              ans += dfs(len-1, i == 1, temp, limit && i == last);        else            ans += dfs(len-1, 2, temp, limit && i == last);  //如果含有13说明第一个条件满足了,把他染成2    }      if(!limit) dp[len][Mod][pre] = ans;    return ans;}int cal(int n){    int k = 0;    while(n)    {        bit[++k] = n % 10;        n /= 10;    }    memset(dp, -1, sizeof(dp));    return dfs(k, 0, 0, 1);}int main(){    int n;    while(~scanf("%d", &n))    {        printf("%d\n", cal(n));    }    return 0;}</span>


1 0
原创粉丝点击