BZOJ-1799

题目地址:点此传送
同步博客 https://cn.shcoc.ooo/2018/09/05/BZOJ-1799/#more
题目
Problem Description
给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数。
input
Output
Sample Input
10 19
Sample Output
3
HINT
【约束条件】 1ab1018 1 ≤ a ≤ b ≤ 10 18
题意

看题(略)

思路

数位dp,我感觉很暴力,但是自己不会….
dp[pos][sum][val],其中pos代表第几位,sum是剩下位数需要凑够的数,val是现在模mod后剩下的值,为什么说暴力呢? 这道题就是暴力枚举所以可能出现的位数和,也就是mod,每次换一个mod就得清一次dp(没办法dp没办法再开mod大了),所以我感觉还是有点暴力的……

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e4 + 10;
ll dp[20][200][200];
ll d[20];
ll dfs(int pos,int sum,int val,int mod,int limit){

    if(sum-pos*9-9>0) return 0;
    if(pos<0) return sum==0&&val==0;
    if(sum<0) return 0;
    if(!limit&&dp[pos][sum][val]!=-1) return dp[pos][sum][val];
    ll ans=0;
    ll end=limit?d[pos]:9;
    for(int i=0;i<=end;i++){

        if(sum-i>=0){
            ans+=dfs(pos-1,sum-i,(val*10+i)%mod,mod,limit&(i==end));
        }

    }
    if(!limit)
        dp[pos][sum][val]=ans;

    return ans;



}
ll solve(ll x){
    ll cnt=0;
    ll ans=0;
    while(x){

        d[cnt++]=x%10;
        x/=10;

    }
    for(int i=1;i<=cnt*9;i++){
        memset(dp,-1,sizeof(dp));
        ans+=dfs(cnt-1,i,0,i,1);
    }

    return ans;

}
int main()
{

    ll a,b;
    scanf("%lld%lld",&a,&b);
    printf("%lld\n",solve(b)-solve(a-1));
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值