Submission #18897228


Source Code Expand

Copy
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;
#define mod(x) (x % MOD + MOD) % MOD

const int dx[4] = {+1, 0, -1, 0};
const int dy[4] = {0, -1, 0, +1};

ll gcd(ll a, ll b)
{
    if (a < b)
        return gcd(b, a);
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

// ax + by = 1 (a and p are coprime)
ll extgcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll d = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

// 1. Fermat's little theorem
// a^(p-1) = 1 (mod p) -> a * a^(p-2) = 1 (mod p)
// NOTE: p must be a prime number

// 2. the extension of Euclidean Algorithm
// solve ax + py = 1 (mod p)
ll mod_inv(ll a, ll p)
{
    // mod_inv exists only if a and p are coprime
    if (gcd(a, p) != 1)
        return -1;

    ll x, y;
    extgcd(a, p, x, y);

    x %= p;
    if (x < 0)
        x += p;
    return x;
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    for (int i = 0; i < t; i++)
    {
        ll n, s, k;
        cin >> n >> s >> k;
        // kx = -s (mod n)
        ll c = n - s;
        ll d = gcd(gcd(k, c), n);
        k = k / d, c = c / d, n = n / d;
        if (mod_inv(k, n) == -1)
            cout << -1 << endl;
        else
            cout << (mod_inv(k, n) * c) % n << endl;
    }
}

Submission Info

Submission Time
Task E - Throne
User emonosuke
Language C++ (GCC 9.2.1)
Score 500
Code Size 1474 Byte
Status AC
Exec Time 6 ms
Memory 3628 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 32
Set Name Test Cases
Sample sample_01.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, sample_01.txt
Case Name Status Exec Time Memory
hand_01.txt AC 6 ms 3624 KB
random_01.txt AC 4 ms 3552 KB
random_02.txt AC 3 ms 3628 KB
random_03.txt AC 3 ms 3576 KB
random_04.txt AC 2 ms 3516 KB
random_05.txt AC 2 ms 3524 KB
random_06.txt AC 3 ms 3628 KB
random_07.txt AC 2 ms 3596 KB
random_08.txt AC 2 ms 3532 KB
random_09.txt AC 2 ms 3476 KB
random_10.txt AC 2 ms 3532 KB
random_11.txt AC 2 ms 3580 KB
random_12.txt AC 2 ms 3528 KB
random_13.txt AC 3 ms 3548 KB
random_14.txt AC 2 ms 3556 KB
random_15.txt AC 2 ms 3556 KB
random_16.txt AC 2 ms 3628 KB
random_17.txt AC 2 ms 3516 KB
random_18.txt AC 2 ms 3504 KB
random_19.txt AC 2 ms 3596 KB
random_20.txt AC 2 ms 3628 KB
random_21.txt AC 2 ms 3552 KB
random_22.txt AC 2 ms 3580 KB
random_23.txt AC 2 ms 3552 KB
random_24.txt AC 2 ms 3592 KB
random_25.txt AC 5 ms 3580 KB
random_26.txt AC 5 ms 3552 KB
random_27.txt AC 2 ms 3504 KB
random_28.txt AC 2 ms 3628 KB
random_29.txt AC 2 ms 3628 KB
random_30.txt AC 2 ms 3476 KB
sample_01.txt AC 3 ms 3548 KB