【EOlymp - 2908】SumThem All(数位统计,tricks)
題干:
Find the sum of all the digits in all the integers between?lowerBound?and?upperBound?inclusive.
Input
Each line contains two integers?lowerBound?and?upperBound?(0?≤?lowerBound?≤?upperBound?≤?2·109).
Output
For each test case print in a separate line the sum of all the digits in all the integers between?lowerBound?and?upperBound?inclusive.
Example 1
Input example
0 3 14 53 24660 308357171Output example
6 296 11379854844題目大意;
多組輸入,每組輸出L和R,求L~R的數位和。
解題報告:
按位統計,統計時組合數算一下就好了。對每一個位a[p],枚舉i分三種情況討論,i<a[p] ,i==a[p]? ,i>a[p]。分別求一下就好。
注意i==a[p]的時候不能和i<a[p]的形式統一,而且也不能像代碼中那樣用tmp記錄、、因為那個只能從前往后讀入的時候用,從后往前的時候肯定不行啊。
這題相當于做了一個簡單的數位dp。(復現了一部分過程)
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; ll solve(ll x) { ll res = 0,xx=x;if(x<=0) return 0;ll tmp = 1,k=1;while(x) {for(int i = 1; i<=9; i++) {if(i < x%10) res += i*(xx / (k*10)) * k + i*k; else if(i == x%10) res += i*(xx / (k*10))*k+i*(xx%k+1);//(tmp-tmp%10)/10;else res += i*(xx / (k*10)) * k;}tmp = tmp*10+x%10;x/=10; k*=10;//10的k次方 }return res; } int main() {ll L,R;while(~scanf("%lld%lld",&L,&R)) {printf("%lld\n",solve(R) - solve(L-1));}return 0 ; }?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【EOlymp - 2908】SumThem All(数位统计,tricks)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【牛客 - 370A】签到题(线段树扫描
- 下一篇: 【2019牛客暑期多校训练营(第六场)-