【DP】 HDOJ 3652 B-number

数位DP。。dp[i][j][k][l]代表长度为i,各位数和为j,前面一个数为k,l代表是否满足包含13。。

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 500005
#define maxm 300005
#define eps 1e-3
#define mod 9999677
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
//head

int dp[20][20][20][2];
int digit[20];
int ten[20];
int n;

int dfs(int pos, int sum, int pre, int flag, int limit)
{
	if(dp[pos][sum][pre][flag] != -1 && !limit) return dp[pos][sum][pre][flag];
	if(pos == 0) {
		if(sum == 0 && flag) return true;
		else return false;
	}
	int a = 0, b = limit ? digit[pos] : 9, ans = 0;
	for(int i = a; i <= b; i++)
		ans += dfs(pos-1, (sum+i*ten[pos])%13, i, flag | (pre == 1 && i == 3), limit && i == b);
	if(!limit) dp[pos][sum][pre][flag] = ans;
	return ans;
}

void work()
{
	int cnt = 0;
	while(n) digit[++cnt] = n % 10, n /= 10;
	printf("%d\n", dfs(cnt, 0, 0, 0, 1));
}

int main()
{
	ten[1] = 1;
	for(int i = 2; i <= 12; i++) ten[i] = ten[i-1] * 10;
	memset(dp, -1, sizeof dp);
	while(scanf("%d", &n)!=EOF) {
		work();
	}
	
	
	return 0;
}


你可能感兴趣的:(hdoj,数位dp)