第 233 场 AtCoder Regular Contest++
B
欧几里得算法函数调用次数
Count of Euclid Function Call
AtCoder
⌀ 2549
原题
← AC →

欧几里得算法函数调用次数AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given a positive integer $N$ and integers $n,m$ satisfying $0 \leq n \lt m \leq N$.

给定一个正整数 $N$,以及满足 $0 \leq n \lt m \leq N$ 的整数 $n,m$。AI (CI翻译)

The function $f$ defined by the following pseudocode computes the greatest common divisor of non-negative integers $i$ and $j$ by the Euclidean algorithm.

以下伪代码所定义的函数 $f$ 使用欧几里得算法计算非负整数 $i$ 和 $j$ 的最大公约数。AI (CI翻译)

function f(i, j)
    if i = 0 then
        return j
    end if

    return f(j mod i, i)
end function

function f(i, j)
    if i = 0 then
        return j
    end if

    return f(j mod i, i)
end function
AI (CI翻译)

Here, j mod i denotes the remainder when $j$ is divided by $i$. Suppose that $f(i,j)$ is executed once for every pair of integers $(i,j)$ satisfying $0 \leq i\leq j \leq N$. Find the number of times $f(n,m)$ is called, modulo $998244353$.

其中,j mod i 表示 $j$ 除以 $i$ 所得的余数。 假设对于每一对满足 $0 \leq i\leq j \leq N$ 的整数 $(i,j)$,函数 f(i,j) 都会被执行一次。求 f(n,m) 被调用的次数,结果对 $998244353$ 取模。AI (CI翻译)

More formally, find the integer printed by the following pseudocode, modulo $998244353$.

更形式化地说,求以下伪代码输出的整数对 $998244353$ 取模后的结果。AI (CI翻译)

answer ← 0

function f(i, j)
    if (i, j) = (n, m) then
        answer ← answer + 1
    end if

    if i = 0 then
        return j
    end if

    return f(j mod i, i)
end function

for i = 0 to N do
    for j = i to N do
        f(i, j)
    end for
end for

output answer

answer ← 0

function f(i, j)
    if (i, j) = (n, m) then
        answer ← answer + 1
    end if

    if i = 0 then
        return j
    end if

    return f(j mod i, i)
end function

for i = 0 to N do
    for j = i to N do
        f(i, j)
    end for
end for

output answer
AI (CI翻译)

Solve $T$ test cases for each input.

对于每组输入,求解 $T$ 个测试用例。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \leq T \leq 10^5$
  • $1 \leq N \leq 10^{18}$
  • $0 \leq n \lt m \leq N$
  • The sum of $\left\lfloor\frac{N}{m}\right\rfloor$ over all test cases is at most $10^6$.
  • All input values are integers.
  • $1 \leq T \leq 10^5$
  • $1 \leq N \leq 10^{18}$
  • $0 \leq n \lt m \leq N$
  • 所有测试用例中 $\left\lfloor\frac{N}{m}\right\rfloor$ 的总和不超过 $10^6$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入由标准输入给出,格式如下:AI (CI翻译)

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
AI (CI翻译)

Each test case $\mathrm{case}_t$ is given in the following format:

每个测试用例 $\mathrm{case}_t$ 的格式如下:AI (CI翻译)

N n m

N n m
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Print $T$ lines in total. The $t$-th line should contain the answer for the $t$-th test case.

总共输出 $T$ 行。第 $t$ 行应包含第 $t$ 个测试用例的答案。AI (CI翻译)

样例

Input #1
5
5 1 2
233 100 199
1000000 233 234
57306 0 1
57307 0 1
Output #1
4
1
5563182
998216169
25736
In the first test case, $(N,n,m)=(5,1,2)$. When $f(2,3)$ is executed, the calls are made in the order $f(2,3) \rightarrow f(1,2) \rightarrow f(0,1)$, so $f(1,2)$ is called once. When $f(i,j)$ is executed for each of $(i,j)=(1,2),(2,3),(2,5),(3,5)$, $f(1,2)$ is called once in each case, so the answer for the first test case is $4$.
题目信息
题号
arc233_b
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。