hdu 3652之数位dp

B-number

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


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

 

虽然自己能写出来,但是还是一知半解,网上也没有很好的解释,希望做多点这种题能够认识清楚

 

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=10+5;
int dp[MAX][13][3],digit[MAX];
/*
size表示输入的数的长度,则: 
dp[i][j][0]表示从size到i+1位不含有13且接下去长度<=i的模13为j的数的个数
dp[i][j][1]表示从size到i+1位不含有13但是第i+1位是1且接下去长度<=i的模13为j的数的个数
dp[i][j][2]表示从size到i+1位含有13且接下去长度<=i的模13为j的数的个数 
*/

void init(int n){
	digit[0]=0;
	while(n)digit[++digit[0]]=n%10,n=n/10;
	memset(dp,-1,sizeof dp);
}

//size代表后面几位数,mod代表当前总的余数,mark表示3种情况,flag标记的作用 
int digit_dfs(int size,int mod,int mark,bool flag){//记忆化搜索 
	if(!size)return !mod && mark == 2;//表示所有数搜完
	if(flag && dp[size][mod][mark] != -1)return dp[size][mod][mark];//表示后面长度可以任意数且已经搜索过
	int m=flag?9:digit[size];//判断当前这个数的范围
	int sum=0;
	for(int i=0;i<=m;++i){
		int Mod=(mod*10+i)%13;
		int Mark=mark;
		if(mark != 2 && i != 1)Mark=0;//第一种情况 
		if(mark != 2 && i == 1)Mark=1;//第二种情况 
		if(mark == 1 && i == 3)Mark=2;//第三种情况 
		sum+=digit_dfs(size-1,Mod,Mark,flag || i<m); 
	} 
	if(flag)dp[size][mod][mark]=sum;//记忆好,下次不用再搜索
	return sum; 
}

int main(){
	int n;
	while(cin>>n){
		init(n);
		cout<<digit_dfs(digit[0],0,0,0)<<endl;
	}
	return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值