题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Bessie the cow is back in school! She has started doing her math homework in which she is tasked to round positive integers to powers of $10$.
To round a positive integer $a$ to the nearest $10^b$, where $b$ is a positive integer, Bessie first locates the $b$'th digit from the right. Let $x$ denote this digit.
If $x \geq 5$, Bessie adds $10^b$ to $a$.
Then, Bessie sets all the digits including and to the right of the $b$'th digit from the right to be $0$.
For instance, if Bessie wanted to round $456$ to the nearest $10^2$ (hundred), Bessie would first locate the $2$nd digit from the right which is $5$. This means $x = 5$. Then since $x \geq 5$, Bessie adds $100$ to $a$. Finally, Bessie sets all the digits in $a$ to the right of and including the $2$nd digit from the right to be $0$, resulting in $500$.
However, if Bessie were to round $446$ to the nearest $10^2$, she would end up with $400$.
After looking at Bessie's homework, Elsie thinks she has invented a new type of rounding: chain rounding. To chain round to the nearest $10^b$, Elsie will first round to the nearest $10^1$, then the nearest $10^2$, and so on until the nearest $10^b$.
Bessie thinks Elsie is wrong, but is too busy with math homework to confirm her suspicions. She tasks you to count how many integers $x$ at least $2$ and at most $N$ ($1 \leq N \leq 10^{9}$) exist such that rounding $x$ to the nearest $10^P$ is different than chain rounding to the nearest $10^P$, where $P$ is the smallest integer such that $10^P \geq x$.
INPUT FORMAT (input arrives from the terminal / stdin):
You have to answer multiple test cases.
The first line of input contains a single integer $T$ ($1 \leq T \leq 10^5$) denoting the number of test cases. $T$ test cases follow.
The first and only line of input in every test case contains a single integer $N$. All $N$ within the same input file are guaranteed to be distinct.
OUTPUT FORMAT (print output to the terminal / stdout):
Output $T$ lines, the $i$'th line containing the answer to the $i$'th test case. Each line should be an integer denoting how many integers at least $2$ and at most $N$ exist that are different when using the two rounding methods.
SAMPLE INPUT:
4 1 100 4567 3366
SAMPLE OUTPUT:
0 5 183 60
Consider the second test case in the sample. $48$ should be counted because $48$ chain rounded to the nearest $10^2$ is $100$ ($48\to 50\to 100$), but $48$ rounded to the nearest $10^2$ is $0$.
In the third test case, two integers counted are $48$ and $480$. $48$ chain rounds to $100$ instead of to $0$ and $480$ chain rounds to $1000$ instead of $0$. However, $67$ is not counted since it chain rounds to $100$ which is $67$ rounded to the nearest $10^2$.
SCORING:
- Inputs 2-4: $N\le 10^3$
- Inputs 5-7: $N\le 10^6$
- Inputs 8-13: No additional constraints.
Problem credits: Weiming Zhou
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 5.1k平均输出 token(含思考) ↓
- 32.7输出速度 tokens/s ↑
- 158 秒延迟 ↓
- 0.20×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 2Pylint 问题 ↓
- 9.26Pylint 得分 ↑
- 6圈复杂度 ↓
- 66.9可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 12.0k平均输出 token(含思考) ↓
- 203.7输出速度 tokens/s ↑
- 60 秒延迟 ↓
- 0.24×CPU 时间 / 官方解 ↓
- 不可测时间复杂度(相对官方解)
- 2Pylint 问题 ↓
- 9.31Pylint 得分 ↑
- 8圈复杂度 ↓
- 70.4可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 16.0k平均输出 token(含思考) ↓
- 138.0输出速度 tokens/s ↑
- 118 秒延迟 ↓
- 0.09×CPU 时间 / 官方解 ↓
- 更优时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 9.09Pylint 得分 ↑
- 7圈复杂度 ↓
- 67.2可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def main(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return t = data[0] ns = data[1:1 + t] max_n = max(ns, default=1) # For P >= 2, the differing values form the interval # [444...445, 499...999], with P digits in each endpoint pattern. intervals = [] lower = 45 p = 2 while lower <= max_n: upper = 5 * 10 ** (p - 1) - 1 intervals.append((lower, upper)) lower = 10 * lower - 5 p += 1 answers = [] for n in ns: count = 0 for lower, upper in intervals: if lower > n: break count += min(n, upper) - lower + 1 answers.append(str(count)) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": main()
import sys def main(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return t = data[0] queries = data[1:1 + t] max_n = max(queries) if queries else 0 intervals = [] pow10 = 10 # 10^m for m >= 1 while True: # Valid numbers for this m are: # from 4*10^m + t_m to 5*10^m - 1, # where t_m = (4*10^m + 5) // 9. t_m = (4 * pow10 + 5) // 9 start = 4 * pow10 + t_m end = 5 * pow10 - 1 if start > max_n: break intervals.append((start, end)) pow10 *= 10 answers = [] for n in queries: ans = 0 for start, end in intervals: if start > n: break ans += min(n, end) - start + 1 answers.append(str(ans)) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": main()
import sys from bisect import bisect_left def solve(): data = sys.stdin.buffer.read().split() if not data: return t = int(data[0]) MAX_P = 9 # 10^9 is the maximum possible value of N power10 = [1] * (MAX_P + 1) for p in range(1, MAX_P + 1): power10[p] = power10[p - 1] * 10 threshold = [0] * (MAX_P + 1) upper = [0] * (MAX_P + 1) prefix = [0] * (MAX_P + 1) # prefix[p] counts all differing integers whose power is at most 10^p. for p in range(1, MAX_P + 1): if p == 1: threshold[p] = 5 else: threshold[p] = 4 * power10[p - 1] + threshold[p - 1] upper[p] = 5 * power10[p - 1] prefix[p] = prefix[p - 1] + upper[p] - threshold[p] search_powers = power10[1:] answers = [] for i in range(1, t + 1): n = int(data[i]) # Smallest p such that 10^p >= n. # For n = 1 this gives p = 1, which correctly yields an answer of 0. p = bisect_left(search_powers, n) + 1 answer = prefix[p - 1] # Add the qualifying integers from the current power-of-ten range. end = min(n, upper[p] - 1) if end >= threshold[p]: answer += end - threshold[p] + 1 answers.append(str(answer)) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": solve()