提出 #35141954


ソースコード 拡げる

#include <iostream>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
ll P;

ll power(ll x, ll n) {
    ll res = 1;
    for (; n; n >>= 1, x = (x * x) % P)
        if (n & 1) res = (res * x) % P;
    return res;
}

ll modInv(ll x) { return power(x % P, P - 2); }

ll discreteLog(ll a, ll b) {
    a %= P, b %= P; ll n = (ll)sqrt(P) + 1;

    map<ll, ll> vals;
    for (ll p = 1; p <= n; ++p)
        vals[power(a, p * n)] = p;

    for (ll q = 0; q <= n; ++q) {
        ll cur = (power(a, q) * b) % P;
        if (vals.count(cur)) {
            ll ans = vals[cur] * n - q;
            return (ans + P - 1) % (P - 1);
        }
    }
    return -1;
}

ll getOrder(ll A) {
    ll res = P - 1;
    for (ll i = 1; i * i <= A; i++) {
        if ((P - 1) % i) { continue; }
        if (power(A, i) == 1) { return i; }
        if (power(A, (P - 1) / i) == 1) { res = (P - 1) / i; }
    }
    return res;
}

ll solve(ll A, ll B, ll S, ll G) {
    if (G == S) return 0;
    if (A == 0) return G == B ? 1 : -1;
    if (A == 1 && B == 0) return -1;
    if (A == 1) return ((G - S + P) * modInv(B)) % P;

    ll Y = (B * modInv(A - 1)) % P;
    G = (G + Y) % P; S = (S + Y) % P;
    if (G == 0 || S == 0) { return -1; }
    
    G = (G * modInv(S)) % P;
    ll res = discreteLog(A, G);
    if (res == -1) return -1;
    return res % getOrder(A);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t; cin >> t; while (t--) {
        ll A, B, S, G; cin >> P >> A >> B >> S >> G;
        cout << solve(A, B, S, G) << '\n';
    }
}

提出情報

提出日時
問題 G - Sequence in mod P
ユーザ ojijo
言語 C++ (GCC 9.2.1)
得点 0
コード長 1639 Byte
結果 WA
実行時間 1958 ms
メモリ 5652 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 600
結果
AC × 1
AC × 26
WA × 1
セット名 テストケース
Sample sample_01.txt
All 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, sample_01.txt
ケース名 結果 実行時間 メモリ
random_01.txt AC 4 ms 3552 KiB
random_02.txt AC 2 ms 3568 KiB
random_03.txt AC 2 ms 3628 KiB
random_04.txt AC 3 ms 3652 KiB
random_05.txt AC 1896 ms 5568 KiB
random_06.txt AC 1646 ms 5476 KiB
random_07.txt AC 1767 ms 5564 KiB
random_08.txt AC 1635 ms 5476 KiB
random_09.txt AC 1678 ms 5552 KiB
random_10.txt AC 1788 ms 5548 KiB
random_11.txt AC 1855 ms 5564 KiB
random_12.txt AC 1775 ms 5652 KiB
random_13.txt AC 1958 ms 5544 KiB
random_14.txt AC 1772 ms 5472 KiB
random_15.txt AC 576 ms 5592 KiB
random_16.txt WA 790 ms 5264 KiB
random_17.txt AC 1538 ms 5472 KiB
random_18.txt AC 1644 ms 5576 KiB
random_19.txt AC 1588 ms 5600 KiB
random_20.txt AC 1637 ms 5476 KiB
random_21.txt AC 1677 ms 5592 KiB
random_22.txt AC 1584 ms 5488 KiB
random_23.txt AC 1670 ms 5476 KiB
random_24.txt AC 1557 ms 5468 KiB
random_25.txt AC 1689 ms 5652 KiB
random_26.txt AC 1657 ms 5604 KiB
sample_01.txt AC 2 ms 3596 KiB