codeforces Lucky Sum
http://codeforces.com/problemset/problem/121/A
用队列打表,将所有的4,7的元素都计算出来。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 using namespace std; 3 const int maxn = 55000; 4 const __int64 pow = 10000000005; 5 __int64 ans[maxn]; 6 7 void solve() 8 { 9 ans[0]=4; 10 ans[1]=7; 11 __int64 pre,rear,temp; 12 pre=0; rear=2; 13 while(pre<rear) 14 { 15 temp=ans[pre]*10+ans[0]; 16 if(temp>pow) 17 break; 18 else 19 ans[rear++]=temp; 20 temp=ans[pre]*10+ans[1]; 21 if(temp>pow) 22 break; 23 else 24 ans[rear++]=temp; 25 pre++; 26 } 27 } 28 29 int main() 30 { 31 __int64 l,r,i,j,pre,rear,sum; 32 solve(); 33 while(cin>>l>>r) 34 { 35 sum=0; 36 if(r<=4) 37 { 38 for(i=l;i<=r;i++) 39 sum+=4; 40 cout<<sum<<endl; 41 continue; 42 } 43 for(i=0;i<maxn;i++) 44 if(ans[i]>=l) 45 break; 46 pre=i; 47 for(;i<maxn;i++) 48 if(ans[i]>=r) 49 break; 50 rear=i; 51 if(rear==pre) 52 { 53 sum=(r-l+1)*ans[pre]; 54 } 55 else 56 { 57 sum+=(ans[pre]-l+1)*ans[pre]; 58 sum+=(r-ans[rear-1])*ans[rear]; 59 for(i=pre+1;i<rear;i++) 60 sum+=(ans[i]-ans[i-1])*ans[i]; 61 } 62 cout<<sum<<endl; 63 } 64 return 0; 65 }