BZOJ1799 [Ahoi2009]self 同类分布

Description

给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数。

Sample Input

10 19

Sample Output

3

HINT

1 ≤ a ≤ b ≤ 10^18


dp[i][j][k] 表示 第 i 位 数位和为 j 并且 取余后为 k 的符合条件的数字个数

枚举数字位的和,从1~162(max),这样就可以直接判断了

/*************************************************************************
	 > Author: wzw-cnyali
	 > Created Time: 2017/9/13 17:04:14
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>

#prag\
ma GCC optimize("O3")

using namespace std;

typedef long long LL;

typedef unsigned long long uLL;

#define REP(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define DREP(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define EREP(i, a) for(register int i = (be[a]); i != -1; i = nxt[i])
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mem(a, b) memset((a), b, sizeof(a))

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

template <class T>
T read(T sum = 0, T fg = 0)
{
	char c = getchar();
	while(c < '0' || c > '9') { fg |= c == '-'; c = getchar(); }
	while(c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); }
	return fg ? -sum : sum;
}

const int Size = 21;

LL dp[Size][163][163];

LL dfs(int *a, int pos, int sum1, int sum2, int k, int lim)
{
	if(sum1 < 0) return 0;
	if(pos == -1) return sum1 == 0 && sum2 == 0;
	if(!lim && dp[pos][sum1][sum2] != -1) return dp[pos][sum1][sum2];
	LL res = 0;
	REP(i, 0, lim ? a[pos] : 9)
	{
		res += dfs(a, pos - 1, sum1 - i, (sum2 * 10 + i) % k, k, lim && i == a[pos]);
	}
	if(!lim) dp[pos][sum1][sum2] = res;
	return res;
}

int a[Size], alen;
int b[Size], blen;

LL solve(LL L, LL R)
{
	alen = 0; do { a[alen++] = L % 10; L /= 10; }while(L);
	blen = 0; do { b[blen++] = R % 10; R /= 10; }while(R);

	LL res = 0;
	REP(i, 1, max(alen, blen) * 9)
	{
		mem(dp, -1);
		res += dfs(b, blen - 1, i, 0, i, 1) - dfs(a, alen - 1, i, 0, i, 1);
	}
	return res;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	LL L = read<LL>(), R = read<LL>();
	printf("%lld\n", solve(L - 1, R));
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值