#abc290d. D - Marking

D - Marking

Score : 400400 points

问题描述

NN 个正方形按顺序排列,编号从 00(N1)(N-1)。Snuke 将按照以下步骤标记每个正方形。

  1. 标记编号为 00 的正方形。
  2. 重复执行以下步骤 i - iii 共 (N1)(N-1) 次。
    1. 将变量 xx 初始化为 (A+D)modN(A+D) \bmod N,其中 AA 是上次标记的正方形的索引。
    2. 当正方形 xx 已被标记时,重复将 xx 替换为 (x+1)modN(x+1) \bmod N
    3. 标记正方形 xx

找出 Snuke 第 KK 次标记的正方形的索引。

给定 TT 个测试用例,请分别找出它们的答案。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN squares indexed 00 through (N1)(N-1) arranged in a line. Snuke is going to mark every square by the following procedure.

  1. Mark square 00.
  2. Repeat the following steps i - iii (N1)(N-1) times.
    1. Initialize a variable xx with (A+D)modN(A+D) \bmod N, where AA is the index of the square marked last time.
    2. While square xx is marked, repeat replacing xx with (x+1)modN(x+1) \bmod N.
    3. Mark square xx.

Find the index of the square that Snuke marks for the KK-th time.

Given TT test cases, find the answer for each of them.

Constraints

  • 1T1051\leq T \leq 10^5
  • 1KN1091\leq K\leq N \leq 10^9
  • 1D1091\leq D \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format, where testi\mathrm{test}_i denotes the ii-th test case:

TT

test1\mathrm{test}_1

test2\mathrm{test}_2

\vdots

testT\mathrm{test}_T

Each test case is given in the following format:

NN DD KK

Output

Print TT lines.

The ii-th (1iT)(1\leq i \leq T) line should contain the answer to the ii-th test case.

Sample Input 1

9
4 2 1
4 2 2
4 2 3
4 2 4
5 8 1
5 8 2
5 8 3
5 8 4
5 8 5

Sample Output 1

0
2
1
3
0
3
1
4
2

If N=4N=4 and D=2D=2, Snuke marks the squares as follows.

  1. Mark square 00.
  2. (11-st iteration) Let x=(0+2)mod4=2x=(0+2)\bmod 4=2. Since square 22 is not marked, mark it.
    (22-nd iteration) Let x=(2+2)mod4=0x=(2+2)\bmod 4=0. Since square 00 is marked, let x=(0+1)mod4=1x=(0+1)\bmod 4=1. Since square 11 is not marked, mark it.
    (33-rd iteration) Let x=(1+2)mod4=3x=(1+2)\bmod 4=3. Since square 33 is not marked, mark it.

update @ 2024/3/10 12:07:04