C. Vlad and a Sum of Sum of Digits

time limit per test

0.5 seconds

memory limit per test

256 megabytes

Please note that the time limit for this problem is only 0.5 seconds per test.

Vladislav wrote the integers from 11 to nn, 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=12n=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.

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.

The sum of these numbers is 1+2+3+4+5+6+7+8+9+1+2+3=511+2+3+4+5+6+7+8+9+1+2+3=51. Thus, for n=12n=12 the answer is 5151.

Input

The first line contains an integer tt (1≤t≤1041≤t≤104) --- the number of test cases.

The only line of each test case contains a single integer nn (1≤n≤2⋅1051≤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

Input

Copy

复制代码

7

12

1

2

3

1434

2024

200000

Output

Copy

复制代码
51
1
3
6
18465
28170
4600002

解题说明:此题其实是求数列之和,两位数以上就需要把数字全部加起来求和,为了保证时间,可以先把输入范围1-200000内的结果全部计算出来,后面根据输入的n值直接输出答案即可。

cpp 复制代码
#include<stdio.h>

int digitsum(int n)
{
	if (n == 0)
	{
		return 0;
	}
	int m = n % 10;
	return m + digitsum(n / 10);
}



int main() 
{
	int t;
	scanf("%d", &t);
	int ar[200001];
	ar[0] = 1;
	for (int i = 0; i < 200001; i++)
	{
		ar[i] = ar[i - 1] + digitsum(i + 1);
	}
	while (t--) 
	{
		int n;
		scanf("%d", &n);
		printf("%d\n", ar[n - 1]);
	}
	return 0;
}
相关推荐
@宁兰23 分钟前
算法实现总结 - C/C++
c语言·c++·算法
Doro再努力1 小时前
2025_11_14洛谷【入门1】数据结构刷题小结
前端·数据结构·算法
蒙奇D索大1 小时前
【算法】回溯算法精讲:从深度优先搜索到剪枝优化
经验分享·笔记·算法·深度优先·剪枝·改行学it
QTreeY1231 小时前
yolov5/8/9/10/11/12/13+deep-oc-sort算法的目标跟踪实现
人工智能·算法·yolo·目标检测·计算机视觉·目标跟踪
_OP_CHEN1 小时前
算法基础篇:(六)基础算法之双指针 —— 从暴力到高效的优化艺术
c++·算法·acm·优化算法·双指针·oj题·算法蓝桥杯
cs麦子1 小时前
C语言--详解--指针--下
c语言·数据结构·算法
Tisfy2 小时前
LeetCode 2536.子矩阵元素加 1:二维差分数组
算法·leetcode·矩阵
北邮刘老师2 小时前
智能家居,需要的是“主控智能体”而不是“主控节点”
人工智能·算法·机器学习·智能体·智能体互联网
oioihoii2 小时前
C++中有双向映射数据结构吗?Key和Value能否双向查找?
数据结构·c++·算法
nnn__nnn2 小时前
图像分割技术全解析:从传统算法到深度学习的视觉分割革命
深度学习·算法·计算机视觉