DAY5.1 A - Digital root

本文介绍了一种快速求解数字根的算法,该算法通过迭代求和的方式找到一个非负整数的数字根,即单个数字的值。具体而言,文章提供了一个公式,用于找出第k个正整数,其数字根为给定值x。通过分析和实践,本文展示了如何利用此算法解决数学作业中的一系列问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - Digital root

Today at the lesson of mathematics, Petya learns about the digital root.

The digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.

Let’s denote the digital root of xx as S(x)S(x). Then S(5)=5S(5)=5, S(38)=S(3+8=11)=S(1+1=2)=2S(38)=S(3+8=11)=S(1+1=2)=2, S(10)=S(1+0=1)=1S(10)=S(1+0=1)=1.

As a homework Petya got nn tasks of the form: find kk-th positive number whose digital root is xx.

Petya has already solved all the problems, but he doesn’t know if it’s right. Your task is to solve all nn tasks from Petya’s homework.

Input
The first line contains a single integer nn (1≤n≤1031≤n≤103) — the number of tasks in Petya’s homework. The next nn lines contain two integers kiki (1≤ki≤10121≤ki≤1012) and xixi (1≤xi≤91≤xi≤9) — ii-th Petya’s task in which you need to find a kiki-th positive number, the digital root of which is xixi.

Output
Output nn lines, ii-th line should contain a single integer — the answer to the ii-th problem.

Example
Input
3
1 5
5 2
3 1
Output
5
38
19

思路&过程

一开始纯朴素的模拟,各种爆炸。最后发现找规律,有公式,哭了。。。

#include<iostream>
using namespace std;

int main()
{
	int t,i,j,k,n;
	cin>>t;
	for(i=1;i<=t;i++)
	{
		cin>>n>>k;
		cout<<9*(n-1)+k<<endl;
	}
	return 0;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值