結果

問題 No.1287 えぬけー
ユーザー finefine
提出日時 2020-11-14 00:09:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,482 bytes
コンパイル時間 1,743 ms
コンパイル使用メモリ 171,772 KB
実行使用メモリ 9,996 KB
最終ジャッジ日時 2023-09-30 04:26:38
合計ジャッジ時間 5,957 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
9,996 KB
testcase_01 AC 42 ms
5,620 KB
testcase_02 AC 49 ms
5,396 KB
testcase_03 AC 74 ms
5,372 KB
testcase_04 RE -
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

const ll MOD = 1000000007;

ll modpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

// y * y >= x を満たす最小の整数yを求める
ll calcSquare(ll x) {
    ll ng = 0, ok = x;
    while (ok - ng > 1) {
        ll mid = (ng + ok) / 2;
        (mid * mid >= x ? ok : ng) = mid;
    }
    return ok;
}

// g^x == y (mod p) の解xを求める(なければ-1を返す)
ll modlog(ll g, ll y, ll p = MOD) {
    g %= p;
    y %= p;

    if (g == 1 && y != 1) return -1;
    if (y == 1) return 0;

    ll sq = calcSquare(p);

    map<ll, ll> baby_table;
    for (ll i = 0, b = 1; i < sq; i++) {
        baby_table[b] = i;
        (b *= g) %= p;
    }

    ll inv_gsq = modpow(modpow(g, p - 2, p), sq, p);

    for (ll i = 0; i < sq; i++) {
        if (baby_table.find(y) != baby_table.end()) {
            return i * sq + baby_table[y];
        }
        (y *= inv_gsq) %= p;
    }
    return -1;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll g = 5;

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        ll x, K;
        cin >> x >> K;
        ll y = modlog(modpow(g, K), x);
        assert(y >= 0);
        y %= (MOD - 1);
        ll ans = modpow(g, y);
        cout << ans << newl;
    }

    return 0;
}
0