(纪中)2175. 幸运数(sum)

(File IO): input:sum.in output:sum.out
时间限制: 1000 ms 空间限制: 131072 KB 具体限制
Goto ProblemSet


题目描述
4 4 4 7 7 7是味味的幸运数字。幸运数是那些只由幸运数字组成的正整数。如¥47,4774是幸运数,而 45 , 17 , 417 45,17,417 4517417 就不是幸运数。
定义 n e x t ( x ) next(x) next(x)为大于或等于 x x x的最小的幸运数。
味味对以下表达式的值很感兴趣 :
n e x t ( L ) + n e x t ( L + 1 ) + . . . + n e x t ( R − 1 ) + n e x t ( R ) 。 next(L)+next(L+1)+...+next(R-1)+next(R)。 next(L)+next(L+1)+...+next(R1)+next(R)
现在告诉你L和R的值,希望你能帮助味味计算出这个表达式的值。


输入
输入文件 s u m . i n sum.in sum.in仅一行包含两个正整数L和 R ( 1 ≤ L ≤ R ≤ 1 0 9 ) R(1≤L≤R≤10^9 ) R(1LR109) L L L R R R的值之间用一个空格分隔。

输出
输出文件 s u m . o u t sum.out sum.out 只有一行一个整数,表示表达式的值。


样例输入
【样例输入1】
2 7

【样例输入2】
7 7

样例输出
【样例输出1】
33

【样例输出2】
7


数据范围限制
对于 20 20 20%的数据, 1 ≤ L ≤ R ≤ 1000 1≤L≤R≤1000 1LR1000
对于 40 40 40%的数据, 1 ≤ L ≤ R ≤ 1 0 6 1≤L≤R≤10^6 1LR106
另有 20 20 20%的数据, L = R L=R L=R
对于 100 100 100%的数据, 1 ≤ L ≤ R ≤ 1 0 9 1≤L≤R≤10^9 1LR109


提示
【样例 1 说明】
n e x t ( 2 ) + n e x t ( 3 ) + n e x t ( 4 ) + n e x t ( 5 ) + n e x t ( 6 ) + n e x t ( 7 ) = 4 + 4 + 4 + 7 + 7 + 7 = 33 next(2)+next(3)+next(4)+next(5)+next(6)+next(7)=4+4+4+7+7+7=33 next(2)+next(3)+next(4)+next(5)+next(6)+next(7)=4+4+4+7+7+7=33
【样例 2 说明】
n e x t ( 7 ) = 7 next(7)=7 next(7)=7


解题思路
先dfs出幸运数字,(因为dfs时是按顺序存的幸运数字,所以不用再排序一遍)。然后模拟。


代码

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
long long l,r,ans,t,s,t1,x,ii;
long long d[5000];
void dfs(long long x,long long y)
{
    if(x==1)
    {
        d[++t1]=y;
        return;
    }
    dfs(x-1,y*10+4);
    dfs(x-1,y*10+7);
}
int main()
{
	freopen("sum.in","r",stdin);
      freopen("sum.out","w",stdout);
    scanf("%lld%lld",&l,&r);
    for(int i=1; i<=12; i++)
        dfs(i,0);
    t=1,ii=l;
    while(l>d[t])
        t++;
    while(ii<=r)
    {
        x=min(d[t]-ii,r-ii)+1; 
		s+=d[t]*x;
        ii+=x;
        t++;
    }
    printf("%lld",s);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值