AtCoder题解 —— AtCoder Beginner Contest 186 —— E - Throne —— 欧几里得求逆元

题目相关

题目链接

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 1T100
  • 2 ≤ N ≤ 1 0 9 2 ≤N≤10^9 2N109
  • 1 ≤ S < N 1≤S<N 1S<N
  • 1 ≤ K ≤ 1 0 9 1≤K≤10^9 1K109

题解报告

题目翻译

有 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×NY×NX×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=S3
最终,本题就是不断缩小 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)。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
AtCoder Beginner Contest 134 是一场 AtCoder 的入门级比赛,以下是每道的简要题解: A - Dodecagon 目描述:已知一个正十二边形的边长,它的面积。 解思路:正十二边形的内角为 $150^\circ$,因此可以将正十二边形拆分为 12 个等腰三角形,通过三角形面积公式计算面积即可。 B - Golden Apple 目描述:有 $N$ 个苹果和 $D$ 个盘子,每个盘子最多可以装下 $2D+1$ 个苹果,最少需要多少个盘子才能装下所有的苹果。 解思路:每个盘子最多可以装下 $2D+1$ 个苹果,因此可以将苹果平均分配到每个盘子中,可以得到最少需要 $\lceil \frac{N}{2D+1} \rceil$ 个盘子。 C - Exception Handling 目描述:给定一个长度为 $N$ 的整数序列 $a$,除了第 $i$ 个数以外的最大值。 解思路:可以使用两个变量 $m_1$ 和 $m_2$ 分别记录最大值和次大值。遍历整个序列,当当前数不是第 $i$ 个数时,更新最大值和次大值。因此,最后的结果应该是 $m_1$ 或 $m_2$ 中较小的一个。 D - Preparing Boxes 目描述:有 $N$ 个盒子和 $M$ 个物品,第 $i$ 个盒子可以放入 $a_i$ 个物品,每个物品只能放在一个盒子中。现在需要将所有的物品放入盒子中,每次操作可以将一个盒子内的物品全部取出并分配到其他盒子中,最少需要多少次操作才能完成任务。 解思路:首先可以计算出所有盒子中物品的总数 $S$,然后判断是否存在一个盒子的物品数量大于 $\lceil \frac{S}{2} \rceil$,如果存在,则无法完成任务。否则,可以用贪心的思想,每次从物品数量最多的盒子中取出一个物品,放入物品数量最少的盒子中。因为每次操作都会使得物品数量最多的盒子的物品数量减少,而物品数量最少的盒子的物品数量不变或增加,因此这种贪心策略可以保证最少需要的操作次数最小。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值