hdu 3652 数位dp

B-number

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


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


题意:给定一个数n,求1-n内含有13且能被13整除的数。

数位dp,比前面的多了一个限制条件是能被13整除,因此在dp时需要加一个mod,假如mod==0,说明能被13整除。

代码:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-7 12:33:32
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
int dp[15][15][3],num[20];
int dfs(int pos,int mod,int st,bool flag){
	if(pos==0)return st==2&&mod==0;
	if(flag&&dp[pos][mod][st]!=-1)return dp[pos][mod][st];
	int u=flag?9:num[pos];
	int ans=0;
	for(int d=0;d<=u;d++){
		int tmod=(mod*10+d)%13;
		if(st==2||(st==1&&d==3))ans+=dfs(pos-1,tmod,2,flag||d<u);
		else if(d==1)ans+=dfs(pos-1,tmod,1,flag||d<u);
		else ans+=dfs(pos-1,tmod,0,flag||d<u);
	}
	if(flag)dp[pos][mod][st]=ans;
	return ans;
}
int solve(int n){
	memset(dp,-1,sizeof(dp));
	int len=0;
	while(n){
		num[++len]=n%10;
		n/=10;
	}
	return dfs(len,0,0,0);
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
    int n;
	while(cin>>n)cout<<solve(n)<<endl;
     return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值