Lucky Number Ⅱ ---------------------------- Thinking (dfs)

Insert picture description here
Insert picture description here
Analysis:
playing table found that more than 1,000 meet the requirements within 1e9

So I just searched it out
and lived for a calculation

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005];
int cnt;
void dfs(ll x)
{
	if(x>4444444444) return ;
	a[++cnt]=x;
	dfs(x*10+4);
	dfs(x*10+7);
 } 

int main()
{
	ll l,r;
	dfs(4);
	dfs(7);
	cin>>l>>r;
	sort(a+1,a+1+cnt); 
	ll sum=0;
	for(int i=1;i<=cnt;i++)
	{
		if(a[i]>=l)
		{
			if(a[i]<r)
			{
				sum=sum+(a[i]-l+1)*a[i];
				l=a[i]+1; 
			 }
			 else
			 {
			 	sum=sum+(r-l+1)*a[i];
			 	break;
			 }
		}
	}
	cout<<sum<<endl;
}

Published 572 original articles · praised 14 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/105388227