【CodeForces - 121A 】【Lucky Sum 】

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/82985060

题目:

Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Let next(x) be the minimum lucky number which is larger than or equals x. Petya is interested what is the value of the expression next(l) + next(l + 1) + ... + next(r - 1) + next(r). Help him solve this problem.

Input

The single line contains two integers l and r (1 ≤ l ≤ r ≤ 109) — the left and right interval limits.

Output

In the single line print the only number — the sum next(l) + next(l + 1) + ... + next(r - 1) + next(r).

Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.

Examples

Input

2 7

Output

扫描二维码关注公众号,回复: 3575187 查看本文章
33

Input

7 7

Output

7

Note

In the first sample: next(2) + next(3) + next(4) + next(5) + next(6) + next(7) = 4 + 4 + 4 + 7 + 7 + 7 = 33

In the second sample: next(7) = 7

题意:

就是给定l,r左右界,然后问区间next函数的和,已知next函数的和就是找到第一个大于等于本身的只含4 7的数字。

解题思路:

思维题目,cf的,不得不说自己一开始就想错了,居然想自己把区间内的数字逐位比较,但是1e9的数据范围瞬间,绝对会T 的,就卡死了,后来自己去看题解,是用很巧妙的dfs 或者循环把所有含4 7的数字都找出来。(= =),之后就循环寻找判断第一个大于等于这个数字的含4 7 的数字。乘以此段区间的长度就行,最后有个点就是把区间长度需要用n去减,别的没啥了。

ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn  = 1024;
ll luck[maxn];
ll f(int n)
{
	if(n==0)
		return 0;
	int i;
	ll ans=0;
	for(int i=1;i<=maxn;i++)
	{
		if(luck[i]<n)
		{
			ans+=luck[i]*(luck[i]-luck[i-1]);
		}
		else
		{
			ans+=luck[i]*(n-luck[i-1]);
			break; 
		}
	}
	return ans;
}
int main()
{
	luck[1]=4;
	luck[2]=7;
	int t=3;
	for(int i=1;i<512;i++)
	{
		luck[t++]=luck[i]*10+4;
		luck[t++]=luck[i]*10+7;
	}
	int l,r;
	scanf("%d%d",&l,&r);
	printf("%lld\n",f(r)-f(l-1));
	return 0;
} 

解题总结:使用的是循环寻找的4 7位的数字并存在数组里,这个问题不是很难,是自己的思维固化了,平时训练满不足,跟不上题目的跳跃了,需要多做些思维题目。要不然真的会GG的

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/82985060