Spring技術內幕筆記2--我懶不寫了哈哈哈哈。
阿新 • • 發佈:2021-10-03
用f(x)f(x)來表示滿足下列條件的最小正整數aa:
- a≥xa≥x。
- aa的各個數位不包含除了44和77以外的其他數字。
現在,給定兩個整數l,r(l≤r)l,r(l≤r),請你計算f(l)+f(l+1)+…+f(r)f(l)+f(l+1)+…+f(r)的值。
輸入格式
一行,兩個整數l,rl,r。
輸出格式
一行,一個整數表示求得的和。
用f(x)來表示滿足下列條件的最小正整數aa:
- a≥x
- a的各個數位不包含除了4和7以外的其他數字。
現在,給定兩個整數l,r(l≤r)l,r(l≤r),請你計算f(l)+f(l+1)+…+f(r)f(l)+f(l+1)+…+f(r)的值。
資料範圍
前三個測試點滿足1≤l≤r≤10
所有測試點滿足1≤l≤r≤10^9
輸入樣例1:
2 7
輸出樣例1:
33
輸入樣例2:
7 7
輸出樣例2:
7
#include <bits/stdc++.h> using namespace std; typedef long long LL; vector<LL> S; void dfs(int u,LL x){ //此時數字位數為u,結果為x S.push_back(x); if(u==10)return ; dfs(u+1,x*10+4); dfs(u+1,x*10+7); } int main() {// S.push_back(0); dfs(0,0); sort(S.begin(),S.end()); LL l,r; cin>>l>>r; LL ans=0; for(int i=1;i<(int)S.size();i++) { LL a=S[i-1]+1,b=S[i]; // cout<<a<<' '<<b<<endl; if(r<a)break; if(b<l)continue; ans+=(min(b,r)-max(a,l)+1)*S[i]; } cout<<ans; return 0; }