(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
45,17,417 就不是幸运数。
定义
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(R−1)+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(1≤L≤R≤109),
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
1≤L≤R≤1000
对于
40
40
40%的数据,
1
≤
L
≤
R
≤
1
0
6
1≤L≤R≤10^6
1≤L≤R≤106
另有
20
20
20%的数据,
L
=
R
L=R
L=R
对于
100
100
100%的数据,
1
≤
L
≤
R
≤
1
0
9
1≤L≤R≤10^9
1≤L≤R≤109
提示
【样例 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;
}