https://ac.nowcoder.com/acm/contest/5086/E
题意:一串数字只有7和4,称之为幸运数字。给一个区间(包括端点)和一个函数next(x),即大于x的第一个幸运数字。求区间内的next(x)的和。
思路:打表,然后运用前缀和进行优化。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<queue> using namespace std; typedef long long ll; ll num[1000100],a[1000100],num_cnt=1,sum[1000100]; void bfs(){ ll cur; queue<ll>sui; sui.push(4); sui.push(7); while(1){ cur=sui.front(); sui.pop(); if(cur>4444444444) return ; num[num_cnt++]=cur; sui.push(cur*10+4); sui.push(cur*10+7); } } int main(){ ll r,l; cin>>r>>l; bfs(); for(int i=1;i<=num_cnt;i++){ sum[i]+=sum[i-1]+(num[i]-num[i-1])*num[i]; } ll r1=lower_bound(num+1,num+1+num_cnt,r)-num; ll l1=lower_bound(num+1,num+1+num_cnt,l)-num; ll sum1=(num[r1]-r)*num[r1]+sum[l1]-sum[r1]-(num[l1]-l)*num[l1]+num[r1]; cout<<sum1<<endl; return 0; }