题目相关
题目链接
AtCoder Beginner Contest 186 E 题,https://atcoder.jp/contests/abc186/tasks/abc186_e。
Problem Statement
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.
Input
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
Output
For each test case, print the answer in its own line.
Sample 1
Sample Input 1
4
10 4 3
1000 11 2
998244353 897581057 595591169
10000 6 14
Sample Output 1
2
-1
249561088
3571
Explaination
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 in the clockwise direction.
In the second test case, he will never sit on the throne, so we should print -1.
Constraints
- 1 ≤ T ≤ 100 1≤ T≤100 1≤T≤100
- 2 ≤ N ≤ 1 0 9 2 ≤N≤10^9 2≤N≤109
- 1 ≤ S < N 1≤S<N 1≤S<N
- 1 ≤ K ≤ 1 0 9 1≤K≤10^9 1≤K≤109
题解报告
题目翻译
有 N 个椅子围成一个圈,其中一个是王位。开始的时候,高桥坐在离王位 S 的椅子上,顺时针方向。他将重复以下动作。
移动:顺时针移动到和自己位置距离为 K 的椅子。
问,经过多少次移动,高桥能第一次做到王位。如果不行,请输出 -1。
题目分析
又是一个纯粹的数学题,欧几里得求逆元。老有人私信问,为什么本题是一个欧几里得求逆元。下面对本题的思路做以下详细分析。
假设高桥可以移动
X
X
X 次后,第一次到达王座。同时,这些移动一共进行了
Y
Y
Y 圈。根据题目的输入
N
,
S
,
K
N, S, K
N,S,K,这样我们可以写出如下的方程:
X
×
K
+
S
=
Y
×
N
⇒
Y
×
N
−
X
×
K
=
S
(
1
)
X \times K + S = Y \times N \Rightarrow Y \times N - X \times K = S \qquad \qquad \qquad (1)
X×K+S=Y×N⇒Y×N−X×K=S(1)
我们对
N
N
N 和
K
K
K 使用扩展最大公约数,我们可以推出新的方程:
Y
′
×
N
+
X
′
×
K
=
g
c
d
(
N
,
K
)
(
2
)
{Y}' \times N + {X}' \times K = gcd(N, K)\qquad \qquad \qquad (2)
Y′×N+X′×K=gcd(N,K)(2)
如果
g
c
d
(
N
,
K
)
gcd(N,K)
gcd(N,K) 不能整除
S
S
S,则方程 (2) 是无解的。
如果方程 (2) 有解,我们可以进一步缩小
Y
′
,
K
′
{Y}', {K}'
Y′,K′ 的范围,通过在方程 (2) 两边都乘以
S
g
c
d
(
N
,
K
)
\frac{S}{gcd(N,K)}
gcd(N,K)S,这样我们可以得到方程 (3)。
Y
′
′
×
N
+
X
′
′
×
K
=
S
(
3
)
{Y}'' \times N + {X}'' \times K = S\qquad \qquad \qquad (3)
Y′′×N+X′′×K=S(3)
最终,本题就是不断缩小
Y
Y
Y,而得到答案
X
X
X。
AC 参考代码
//https://atcoder.jp/contests/abc186/tasks/abc186_e
//E - Throne
#include <bits/stdc++.h>
using namespace std;
//如果提交到OJ,不要定义 __LOCAL
#define __LOCAL
typedef long long ll;
ll reduce(ll a, ll mod) {
return (a%=mod)<0?a+mod:a;
}
//finds x, y such that ax + by = gcd(a, b)
ll euclidEx(ll a, ll b, ll &x, ll &y) {
if (b) {
ll d=euclidEx(b, a%b, y, x);
y-=a/b*x;
return d;
} else {
x=1;
y=0;
return a;
}
}
int main() {
#ifndef __LOCAL
//这部分代码需要提交到OJ,本地调试不使用
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#endif
int t;
cin>>t;
while (t--) {
ll n,s,k;
cin>>n>>s>>k;
s=n-s;
ll g=__gcd(n, k);
if (0!=s%g) {
cout<<"-1\n";
continue;
}
//找到最小的正整数x,满足 kx=s mod n
ll x,y;
euclidEx(k, n, x, y);
//kx+ny=g
x *= (s/g);
y *= (s/g);
//ans is x%(n/g)
x = reduce(x, n/g);
cout<<x<<"\n";
}
#ifdef __LOCAL
//这部分代码不需要提交到OJ,本地调试使用
system("pause");
#endif
return 0;
}
时间复杂度
O(T)?不敢确定,虽然使用了递归,但是 g c d ( N , K ) gcd(N,K) gcd(N,K) 的次数是常数项。
空间复杂度
O(1)。