牛客寒假算法基础集训营3 处女座和小姐姐(三)(数位dp)

10 篇文章 0 订阅

链接:https://ac.nowcoder.com/acm/contest/329/G
来源:牛客网
 

经过了选号和漫长的等待,处女座终于拿到了给小姐姐定制的手环,小姐姐看到以后直呼666!

处女座其实也挺喜欢6这个数字的,实际上他做手环的时候选取的k=6。所以他对于包含数码6的数字极其敏感。每次看到像4567这样的数字的时候他的心就像触电了一样,想起了小姐姐。

现在你要给处女座展示一系列数字,你想知道他的内心会激动多少次。对于同一个数字,他最多只会激动一次,即如果这个数是66666,他还是只会激动一次。

输入描述:

一行包括两个数字l,r,表示你给处女座展示的数字范围为[l,r]。

输出描述:

一行一个整数,表示处女座内心激动的次数。

示例1

输入

复制

10 20

输出

复制

1

备注:

0\leqslant l\leqslant r\leqslant 10^{18}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
ll dp[30][3];
ll a[30];
ll dfs(int pos,int sta,int limit)
{
    if(pos<0)return sta?1:0;
    if(!limit&&dp[pos][sta]!=-1)return dp[pos][sta];
    ll ans=0;
    int up=limit?a[pos]:9;
    for(int i=0;i<=up;i++)
    {
        ans+=dfs(pos-1,sta||i==6,limit&&i==up);
        //cout<<ans<<endl;
    }
    if(!limit)dp[pos][sta]=ans;
    return ans;
}
ll solve(ll x)
{
    int t=0;
    ll kk=x;
    do{
        a[t++]=x%10;
        x/=10;
    }while(x);
    return dfs(t-1,0,1);
}
int main()
{
    memset(dp,-1,sizeof(dp));
    ll x,y;
    cin>>x>>y;
    printf("%lld\n",solve(y)-solve(x-1));
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值