HDU - 3652 B-number

B-number

找出 \([1, X]\) 中满足:

  1. 子串含 "13"
  2. 能被 \(13\) 整除的数
    的个数

错误日志: \(B == 1\) 时也可以转移到 \(1\) (没有分析清楚转移)


Solution

数位\(dp\)
满足两个性质: 能被 \(13\) 整除 , 子串含 \(13\)
状态设计: \(dp[maxn][0/1/2][0 - 12]\) 两个状态,
状态一
\(0 -->\) 无特殊性质
\(1 -->\) 数以 \(1\) 结尾
\(2 -->\) 数中含 \("13"\) 子串
状态二
次数对 \(13\) 取模的结果

状态二比较好转移, 状态一转移如下:

LL cmd = 0;
if((B == 0 || B == 1) && i == 1)cmd = 1;
else if(B == 1 && i == 3)cmd = 2;
else if(B == 2)cmd = 2;

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#define LL long long
#define REP(i, x, y) for(LL i = (x);i <= (y);i++)
using namespace std;
LL RD(){
    LL out = 0,flag = 1;char c = getchar();
    while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
    while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
    return flag * out;
    }
const LL maxn = 19;
LL num[maxn];
LL dp[maxn][3][maxn];//数位,是否含13,除13余数多少
LL DP(LL Index, LL B, LL mod, bool limit){
	if(Index == 0)return ((B == 2) && (mod == 0));
	if(!limit && dp[Index][B][mod] != -1)return dp[Index][B][mod];
	LL ans = 0, up = limit ? num[Index] : 9;
	REP(i, 0, up){
		LL cmd = 0;
		if((B == 0 || B == 1) && i == 1)cmd = 1;
		else if(B == 1 && i == 3)cmd = 2;
		else if(B == 2)cmd = 2;
		ans += DP(Index - 1, cmd, ((mod * 10 + i) % 13 + 13) % 13, limit && i == num[Index]);
		}
	if(!limit)dp[Index][B][mod] = ans;
	return ans;
	}
LL solve(LL x){
	LL len = 0;
	while(x){
		num[++len] = x % 10;
		x /= 10;
		}
	return DP(len, 0, 0, 1);
	}
int main(){
	memset(dp, -1, sizeof(dp));
	LL x;
	while(scanf("%lld", &x) != EOF){
		printf("%lld\n", solve(x));
		}
	return 0;
	}
posted @ 2018-10-11 20:03  Tony_Double_Sky  阅读(136)  评论(0编辑  收藏  举报