1. 程式人生 > 其它 >AtCoder題解 —— AtCoder Beginner Contest 186 —— E - Throne —— 歐幾里得求逆元

AtCoder題解 —— AtCoder Beginner Contest 186 —— E - Throne —— 歐幾里得求逆元

技術標籤:OJ題解# AtCoder題解AtCoder題解ABC186E題Throne

題目相關

題目連結

AtCoder Regular 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。

題目分析

又是一個純粹的數學題,歐幾里得求逆元。

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)?不敢確定。

空間複雜度

O(1)。