3933:The Almost Lucky Numbers

地址

题目描述

John and Brus believe that the digits 4 and 7 are lucky and all others are not. According to them, an almost lucky number is a number that contains at most one non-lucky digit in its decimal representation. Return the total number of almost lucky numbers between a and b, inclusive.

输入格式

Each line contains two integers a,b<=10^16

输出

For each line of input,output the total number of almost lucky numbers between a and b, inclusive.

样例输入

4 7
8 19

样例输出

4
4

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

typedef long long ll;

ll dp[20][20][20][20];//i为,不是4和7的个数,4的个数,7的个数
ll l,r;
int num[20],len;


inline ll dfs(int i,int s,int p,int q,bool e,bool pre){
    if(i==-1){
        if(s<=1) return 1;
        return 0;
    }
    if(!e && dp[i][s][p][q]!=-1) return dp[i][s][p][q];
    int v=(e?num[i]:9);
    ll ans=0;
    for(int j=0;j<=v;j++){
        int news=s;
        if(j!=4&&j!=7){
            if(j!=0||(!pre&&j==0))
                news++;
        }
        ans+=dfs(i-1,news,p+(j==4),q+(j==7),e&&(j==v),(j==0&&pre));
    }
    if(!e) dp[i][s][p][q]=ans;
    return ans;
}
inline ll solve(ll n){
    if(n<0) return 0;
    if(n==0) return 1;
    len=0;
    while(n){
        num[len++]=n%10;
        n/=10;
    }
    return dfs(len-1,0,0,0,1,1);
}

int main(){

    memset(dp,-1,sizeof(dp));
    int ans=0;
    while(~scanf("%lld%lld",&l,&r)){
            printf("%lld\n",solve(r)-solve(l-1));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值