C. Vlad and a Sum of Sum of Digits
time limit per test: 0.5 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Please note that the time limit for this problem is only 0.5 seconds per test.
Vladislav wrote the integers from 1 1 1 to n n n, inclusive, on the board. Then he replaced each integer with the sum of its digits.
What is the sum of the numbers on the board now?
For example, if n = 12 n=12 n=12 then initially the numbers on the board are:
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.
1,2,3,4,5,6,7,8,9,10,11,12.
Then after the replacement, the numbers become:
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
1
,
2
,
3.
1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3.
1,2,3,4,5,6,7,8,9,1,2,3.
The sum of these numbers is
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
1
+
2
+
3
=
51
1+2+3+4+5+6+7+8+9+1+2+3=51
1+2+3+4+5+6+7+8+9+1+2+3=51. Thus, for
n
=
12
n=12
n=12 the answer is
51
51
51.
Input
The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1≤t≤104) — the number of test cases.
The only line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1≤n≤2⋅105) — the largest number Vladislav writes.
Output
For each test case, output a single integer — the sum of the numbers at the end of the process.
Example
inputCopy
7
12
1
2
3
1434
2024
200000
outputCopy
51
1
3
6
18465
28170
4600002
AC代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
//#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=998244353;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;
int t;
int dp[N];
int s(int x)
{
int cnt = 0;
while(x){
cnt += x % 10;
x /= 10;
}
return cnt;
}
int main()
{
cin >> t;
for(int i = 1; i <= 200005; i ++)
{
dp[i] = dp[i - 1] + s(i);// dp记忆化
}
while(t --){
int n;
cin >> n;
cout << dp[n] << endl;
}
return 0;
}