码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3652 B-number(数位dp)

时间:2015-04-23 17:38:18      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:hdu 3652 b-number   数位dp   

B-number

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


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
 

Recommend
lcy
 


求[1,n]内含有13且是%13==0的个数

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#define ll long long

using namespace std;

int n;
int dp[20][3][15];///dp[i][j][k] i:位数  j:状态  k:%13后的数
int num[20];

///0:不合法 1:1开头  2:合法
int new_s(int s,int d) {
    if(s==2)       return 2;
    if(s==1&&d==3) return 2;
    if(d==1)       return 1;
    return 0;
}

int dfs(int i,int s,int mod,bool e) {
    if(i<=0)    return s==2&&mod==0;
    if(!e&&dp[i][s][mod]!=-1)return dp[i][s][mod];
    int res=0;
    int u=e?num[i]:9;
    for(int d=0; d<=u; d++) {
        int Mod=(mod*10+d)%13;
        res+=dfs(i-1,new_s(s,d),Mod,e&&d==u);
    }
    return e?res:dp[i][s][mod]=res;
}

int main() {
    //freopen("in.txt","r",stdin);
    while(cin>>n) {
        memset(dp,-1,sizeof dp);
        memset(num,0,sizeof num);
        int len=1;
        while(n) {
            num[len++]=n%10;
            n/=10;
        }
        num[len]=0;
        int ans=dfs(len-1,0,0,1);
        cout<<ans<<endl;
    }
    return 0;
}


hdu 3652 B-number(数位dp)

标签:hdu 3652 b-number   数位dp   

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/45222989

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!