日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【EOlymp - 2908】SumThem All(数位统计,tricks)

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 308357171

Output 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。