結果

問題 No.1287 えぬけー
ユーザー tktk_snsntktk_snsn
提出日時 2021-04-22 23:15:08
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 970 ms / 2,000 ms
コード長 2,373 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 10,768 KB
実行使用メモリ 8,816 KB
最終ジャッジ日時 2023-09-17 10:24:01
合計ジャッジ時間 4,901 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,816 KB
testcase_01 AC 20 ms
8,628 KB
testcase_02 AC 19 ms
8,712 KB
testcase_03 AC 19 ms
8,748 KB
testcase_04 AC 20 ms
8,656 KB
testcase_05 AC 924 ms
8,720 KB
testcase_06 AC 936 ms
8,680 KB
testcase_07 AC 970 ms
8,724 KB
testcase_08 AC 916 ms
8,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


def extgcd(a, b):
    """
    拡張Euclidの互除法
    INPUT:
        a, b
    OUTPUT:
        d: gcd(a, b)
        (x, y): ax + by = d の解
    """
    if b == 0:
        return a, (1, 0)
    d, (y, x) = extgcd(b, a % b)
    y -= a // b * x
    return d, (x, y)


def crt(B, M):
    """
    中国剰余定理
    x ≡ b0 (mod M0)
    x ≡ b1 (mod M1)
    x ≡ b2 (mod M2)
    となるxを求める
    INPUT:
        B:[b0,b1,..]
        M:[m0,m1,..]
    OUTPUT:
        (x, mod) -> x:最小の答え(0<=x<mod)
                    mod:lcm(m0,m1,..)
        (0,-1) -> 解がない場合
    """
    x = 0
    mod = 1
    for b, m in zip(B, M):
        d, (p, _) = extgcd(mod, m)
        if (b - x) % d != 0:
            return 0, -1
        x += (b - x) // d * p % (m // d) * mod
        mod *= m // d
    return x % mod, mod


def invmod(a, m):
    """
    mを法とするaの逆元、つまり
    ab = 1 (mod m)となるbを返す
    """
    g, (x, y) = extgcd(a, m)
    return x % m


def euler_phi(N):
    """
    Euler totient function
    [1,N]の自然数のうちNと互いに素なものの個数
    """
    res = N
    for i in range(2, int(N ** 0.5) + 1):
        if N % i == 0:
            res -= res // i
            while N % i == 0:
                N //= i
    if N != 1:
        res -= res // i
    return res


def euler_phi_table(N):
    res = list(range(N))
    for i in range(2, N):
        if res[i] == i:
            for j in range(i, N, i):
                res[j] -= res[j] // i
    return res


def get_generator(p):
    """素数pの最小の原始根を返す"""
    fact = []
    phi = p - 1
    n = phi
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            fact.append(i)
            while n % i == 0:
                n //= i
    if n > 1:
        fact.append(n)

    for res in range(2, p + 1):
        ok = True
        for f in fact:
            ok &= pow(res, phi // f, p) != 1
            if not ok:
                break
        if ok:
            return res
    return -1


def main():
    T = int(input())
    mod = 10 ** 9 + 7
    for _ in range(T):
        X, K = map(int, input().split())
        _, (a, b) = extgcd(mod - 1, -K)
        N = pow(X, b, mod)
        print(N)


main()
0