(Jizhong) 2175. Lucky number (sum)

(File IO): input: sum.in output: sum.out
time limit: 1000 ms space constraints: 131072 KB specific restrictions
Goto ProblemSet


Title Description
4 4 and 7 7 is a lucky number flavor taste. Lucky number is a positive integer of only those lucky numbers. ¥ 47,4774 is as lucky number, and 45 17 417 45,17,417 is not a lucky number.
definition n e x t ( x ) next(x) is greater than or equal to x x minimum number of lucky.
Taste taste very interested in the value of the expression:
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)。
Now tell you the value of L and R, and I hope you can help flavor taste calculated value of the expression.


Enter the
input file s u m . i n sum.in only line containing two positive integers and L R ( 1 L R 1 0 9 ) R(1≤L≤R≤10^9 ) L L and R R separated by a space between the values.

Output
Output file s u m . o u t sum.out only one line an integer representing the value of the expression.


Sample input
[1] Input Sample
27

Sample input [2]
77

Sample output
[output 1] Sample
33

[2] Output Sample
7


Data range limits
for 20 20 % of the data, 1 L R 1000 1≤L≤R≤1000
For 40 40 % of the data, 1 L R 1 0 6 1≤L≤R≤10^6
Another 20 20 % of the data, L = R L=R
for 100 100 % of the data, 1 L R 1 0 9 1≤L≤R≤10^9


Tips
[description] Sample 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
[description] Sample 2
n e x t ( 7 ) = 7 next(7)=7


Problem-solving ideas
first dfs a lucky number, (because dfs is sequentially stored lucky number, so no need to sort it again). Then simulation.


Code

#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;
}
Published 119 original articles · won praise 8 · views 4933

Guess you like

Origin blog.csdn.net/kejin2019/article/details/104607346