CF2204E.Sum of Digits (and Again)

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

For a positive (strictly greater than 00) integer xx, the string S(x)S(x) is formed through the following process:

  1. initially, this string is empty;
  2. then, the decimal representation of the number xx without leading zeros is appended to it on the right;
  3. after that, if x≤9x \le 9, the process ends. Otherwise, xx is replaced by the sum of the digits of xx, and the process returns to step 22;

For example:

  • S(75)S(75) is 75123;
  • S(30)S(30) is 303;
  • S(9)S(9) is 9.

You are given a string ss consisting of digits. Your task is to rearrange the characters in this string so that it forms the string S(x)S(x) for some number xx. Removing characters and/or adding new characters is not allowed. If the given string ss is already the string S(x)S(x) for some number xx, you may leave it unchanged.

对于一个正整数 xx(严格大于 00),字符串 S(x)S(x) 通过以下过程生成:

  1. 初始时,该字符串为空;
  2. 然后,将 xx 的十进制表示(不含前导零)追加到该字符串的右侧;
  3. 接着,若 x≤9x \le 9,则过程结束;否则,将 xx 替换为其各位数字之和,并返回第 2 步;

例如:

  • S(75)S(75) 是 75123;
  • S(30)S(30) 是 303;
  • S(9)S(9) 是 9。

给定一个仅由数字组成的字符串 ss。你的任务是重新排列该字符串中的字符,使其构成某个数 xx 对应的字符串 S(x)S(x)。不允许删除或添加任何字符。如果给定的字符串 ss 已经是某个数 xx 对应的 S(x)S(x),你可以保持其不变。

输入格式

The first line contains one integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

Each test case consists of one string containing ss (1≤∣s∣≤1051 \le |s| \le 10^5) — a sequence of digits.

Additional constraints on the input:

  • the sum of lengths of ss over all test cases does not exceed 10510^5;
  • it is possible to rearrange the digits in ss to obtain S(x)S(x) for some positive integer xx.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例由一个字符串 ss(1≤∣s∣≤1051 \le |s| \le 10^5)组成 —— 一串数字。

输入的额外约束条件:

  • 所有测试用例中字符串 ss 的长度总和不超过 10510^5;
  • 字符串 ss 中的数字可以被重新排列,从而得到某个正整数 xx 对应的 S(x)S(x)。

输出格式

For each test case, output one string — ss after rearranging the characters. If there are multiple valid answers, output any of them.

对于每个测试用例,输出一个字符串——即重排字符后的 ss。如果存在多个合法的答案,输出其中任意一个即可。

输入输出样例

  • 输入#1

    5
    12735
    1
    011
    99999299999999299959999999999999
    4621467

    输出#1

    75123
    1
    101
    99999999999999999999999999992529
    6442167

输入解题思路,AI测评打分。不知道怎么写?

首页