ABC186E

博客围绕椅子圈移动问题展开,有N个椅子围成圈,一人初始坐在离王位S的椅子上,每次顺时针移动K个椅子。通过设移动x次转y圈,得出方程kx + ny = -s,利用拓展欧几里得算法求解最小正整数解x,若s%d!=0则无解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

We have N chairs arranged in a circle, one of which is a throne.
Takahashi is initially sitting on the chair that is S chairs away from the throne in the clockwise direction. Now, he will repeat the move below.
Move: Go to the chair that is K chairs away from the chair he is currently sitting on in the clockwise direction.
After how many moves will he be sitting on the throne for the first time? If he is never going to sit on it, report -1 instead.
You are asked to solve T test cases.
1≤T≤100
2≤N≤1e9
1≤S<N
1≤K≤1e9

输入:

Input is given from Standard Input in the following format. The first line is in the format below:
T
Then, the following T lines represent T test cases. Each of these lines is in the format below:
N S K

输出:

For each test case, print the answer in its own line.

样例:

Sample Input 1

4
10 4 3
1000 11 2
998244353 897581057 595591169
10000 6 14

Sample Output 1

2
-1
249561088
3571

In the first test case, we have 10 chairs, and Takahashi is initially sitting on the chair that is 4 chairs away from the throne in the clockwise direction. He will be sitting on the throne after 2 moves of moving 3 chairs

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值