Bzoj-1799 self 同类分布(数位DP)

Description

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

Input

Output

Sample Input

10 19

Sample Output

3

HINT

【约束条件】1 ≤ a ≤ b ≤ 10^18

Source



分析:数位DP,枚举一下所有数字之和。


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<ctime>  
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXN 100000
using namespace std;
typedef long long ll;
ll l,r,a[22],dp[22][172][172];
ll dfs(int pos,int p,int sta1,int sta2,int limit)
{
	if(pos == -1) return (sta1 == 0 && sta2 == p);
	if(sta2 > p || (pos+1)*9+sta2 < p) return 0;
	if(!limit && dp[pos][sta1][sta2] >= 0) return dp[pos][sta1][sta2];
	int up = limit? a[pos] : 9;
	ll ans = 0;
	for(int i = 0;i <= up;i++)
	{
		ans += dfs(pos-1,p,(sta1*10+i)%p,sta2+i,limit && i == up);			
	}  
	if(!limit) dp[pos][sta1][sta2] = ans;
	return ans;
}
ll solve(ll x)
{
	int pos = 0;
	while(x)
	{
		a[pos++] = x % 10;
		x /= 10;
	}
	if(!pos) return 0ll;
	ll ans = 0;
	for(int i = 1;i <= pos*9;i++) 
	{
		memset(dp,-1,sizeof(dp));
		ans += dfs(pos-1,i,0,0,1);
	}
	return ans;
}
int main()
{
	cin>>l>>r;
	cout<<solve(r)-solve(l-1)<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值