题目 › usaco-1443

Roundabout Rounding

USACO 2024 December · 铜组 · 2024-12 · L1 基础 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

来源: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

auto(max)

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

auto(high)

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

auto(max)

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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 37 行
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()