5816: 幸运数III
时间限制: 1 Sec 内存限制: 128 MB提交: 100 解决: 26
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
4 和 7 是味味的幸运数字。幸运数是那些只由幸运数字组成的正整数。如 47,477 是幸运数,而 5,17,417 就不是幸运数。
定义 next(x)为大于或等于 x 的最小的幸运数。
味味对以下表达式的值很感兴趣 :
next(L)+next(L+1)+...+next(R-1)+next(R)。
现在告诉你 L 和 R 的值,希望你能帮助味味计算出这个表达式的值。
定义 next(x)为大于或等于 x 的最小的幸运数。
味味对以下表达式的值很感兴趣 :
next(L)+next(L+1)+...+next(R-1)+next(R)。
现在告诉你 L 和 R 的值,希望你能帮助味味计算出这个表达式的值。
输入
输入仅一行包含两个正整数 L 和 R(1≤L≤R≤109 ),L 和 R 的值之间用一个空格分隔。
输出
输出只有一行一个整数,表示表达式的值。
样例输入
2 7
样例输出
33
提示
next(2)+next(3)+next(4)+next(5)+next(6)+next(7)=4+4+4+7+7+7=33
对于 20%的数据,1≤L≤R≤1000
对于 40%的数据,1≤L≤R≤106
另有 20%的数据,L=R
对于 100%的数据,1≤L≤R≤109
来源
#include<stdio.h>
#include<string.h>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
long long xy[10000];
int xx[2]={4, 7}, cnt;
struct node{
long long x, step;
};
void bfs()
{
queue<node>q;
node a, b;
a.x=a.step=cnt=0;
q.push(a);
while(!q.empty())
{
a=q.front();q.pop();
if(a.step==11)
return ;
for(int i=0;i<2;i++)
{
b.x=a.x*10+xx[i];
b.step=a.step+1;
q.push(b);
xy[cnt++]=b.x;
}
}
}
int main()
{
bfs();
long long l, r, ans=0, kk;
scanf("%lld%lld", &l, &r);
for(int i=0;i<cnt;i++)
if(xy[i]>=l)
{
kk=i;
break;
}
for(int i=l;i<=r;i++)
{
if(xy[kk]<i)
kk++;
ans+=xy[kk];
}
printf("%lld\n", ans);
return 0;
}
/**************************************************************
Problem: 5816
User: ldu_reserver201701
Language: C++
Result: 正确
Time:952 ms
Memory:1332 kb
****************************************************************/