Codeforces 121A Lucky Sum

Lucky Sum

Time Limit: 2000ms

Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 121A
64-bit integer IO format: %I64d      Java class name: (Any)

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 %I64dspecificator.

Sample Input

Input

2 7

Output

33

Input

7 7

Output

7

Hint

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

Source

Codeforces Beta Round #91 (Div. 1 Only)

解题:只包含4或者7的数字很少的。搜一遍就是了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 20000;
18 LL d[maxn];
19 int tot = 1;
20 void dfs(LL num,int cur) {
21     d[tot++] = num;
22     if(cur > 11) return;
23     dfs(num*10+4,cur+1);
24     dfs(num*10+7,cur+1);
25 }
26 LL sum(LL x) {
27     if(x == 0) return 0;
28     LL temp = 0;
29     for(int i = 1; i < tot; i++) {
30         if(x >= d[i])
31             temp += d[i]*(d[i]-d[i-1]);
32         else {
33             temp += d[i]*(x-d[i-1]);
34             break;
35         }
36     }
37     return temp;
38 }
39 int main() {
40     dfs(4,0);
41     dfs(7,0);
42     sort(d+1,d+tot);
43     LL lt,rt;
44     while(~scanf("%I64d %I64d",&lt,&rt))
45         printf("%I64d\n",sum(rt)-sum(lt-1));
46     return 0;
47 }

时间: 2024-08-07 17:00:02

Codeforces 121A Lucky Sum的相关文章

CodeForces E. Lucky Array 幸运数列

CodeForces    E. Lucky Array  幸运数列 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 n

Codeforces 396B On Sum of Fractions 数论

题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/20076297 题目大意:给出一个n,ans = ∑(2≤i≤n)1/(v(i)*u(i)), v(i)为不大于i的最大素数,u(i)为大于i的最小素数, 求ans,输出以分式形式. 解题思路:一开始看到这道题1e9,暴力是不可能了,没什么思路,后来在纸上列了几项,突然想到高中时候求等差数列时候用到

Codeforces 396B On Sum of Fractions 规律题

题目链接:点击打开链接 我们把 1 / { u(i)*v(i) }拆开->  (1/(u(i)-v(i)) * ( 1/v(i) - 1/u(i) ) 若n +1  是素数,则显然(1/(u(i)-v(i)) * ( 1/v(i) - 1/u(i) ) 这样完全相同的式子有 u(i)-v(i) 个 那么就可以把前面系数约掉,那么剩下的式子就是 1/2 - 1/(n+1) 若不是,则我们找到第一个<=n的素数,即v(n) 和第一个>n的素数,即 u(n) 然后前面的 2-v(n)求和,即

Lucky Sum (dfs打表)

Lucky Sum 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 mi

CodeForces 146A Lucky Ticket

Lucky Ticket Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 146A Description Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal

『题解』Codeforces121A Lucky Sum

更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description 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\)

codeforces 85D D. Sum of Medians Vector的妙用

D. Sum of Medians Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/85/D Description In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive el

Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】

题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解: 其实就是要找一些数字,使得之和mod m为0. 开一个vector,存当前已经能够构成的数字之和mod m之后的值. 一开始vector为空,然后枚举n个数字a[i],对于每个数字枚举当前vector中的值v[i],将没有出现过的(a[i]+v[i])%m值加入vector中. 最后判断下vec

Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)

F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Let D(x) be the number of positive divisors of a positive integer x. For example, D(2)?=?2 (2 is divisible by 1 and 2), D(6)?