結果
問題 | No.981 一般冪乗根 |
ユーザー | 👑 hos.lyric |
提出日時 | 2020-02-07 21:32:14 |
言語 | D (dmd 2.106.1) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,694 bytes |
コンパイル時間 | 1,002 ms |
コンパイル使用メモリ | 129,256 KB |
実行使用メモリ | 13,288 KB |
最終ジャッジ日時 | 2024-06-22 04:56:33 |
合計ジャッジ時間 | 16,532 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | TLE | - |
testcase_01 | -- | - |
testcase_02 | -- | - |
testcase_03 | -- | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
evil_60bit1.txt | -- | - |
evil_60bit2.txt | -- | - |
evil_60bit3.txt | -- | - |
evil_hack | -- | - |
evil_hard_random | -- | - |
evil_hard_safeprime.txt | -- | - |
evil_hard_tonelli0 | -- | - |
evil_hard_tonelli1 | -- | - |
evil_hard_tonelli2 | -- | - |
evil_hard_tonelli3 | -- | - |
evil_sefeprime1.txt | -- | - |
evil_sefeprime2.txt | -- | - |
evil_sefeprime3.txt | -- | - |
evil_tonelli1.txt | -- | - |
evil_tonelli2.txt | -- | - |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } uint xrand() { static uint x = 314159265, y = 358979323, z = 846264338, w = 327950288; uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8; } // !!!old library!!! Int mod(Int)(Int a, Int m) { if ((a %= m) < 0) a += m; return a; } Int gcd(Int)(Int a, Int b) { return (b != 0) ? gcd(b, a % b) : a; } Int lcm(Int)(Int a, Int b) { return a / gcd(a, b) * b; } Int gojo(Int)(Int a, Int b, out Int x, out Int y) { if (b != 0) { Int g = gojo(b, a % b, y, x); y -= (a / b) * x; return g; } x = 1; y = 0; return a; } Int modInv(Int)(Int a, Int m) { Int x, y; gojo(a, m, x, y); return mod(x, m); } long modLogP(long a, long b, long m) { a = mod(a, m); b = mod(b, m); if (m == 1) return 0; long k, ak = 1; Tuple!(long,long)[] as; for (k = 0; k * k < m; ++k) { as ~= tuple(ak, k); ak = (ak * a) % m; } as.sort; ak = modInv(ak, m); for (long i = 0; i < k; ++i) { int pos = as.lowerBound(tuple(b, 0L)); if (pos < as.length && as[pos][0] == b) return i * k + as[pos][1]; b = (b * ak) % m; } return -1; } long power(long a, long e, long m) { long x = a % m, y = 1 % m; for (; e; e >>= 1) { if (e & 1) (y *= x) %= m; (x *= x) %= m; } return y; } void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { const P = readLong(); const K = readLong(); const A = readLong(); debug { writeln("P = ", P); stdout.flush; } long[] qs; long n = P - 1; for (long q = 2; q^^2 <= n; ++q) { if (n % q == 0) { qs ~= q; do { n /= q; } while (n % q == 0); } } if (n > 1) { qs ~= n; } debug { writeln("qs = ", qs); stdout.flush; } long g; for (; ; ) { g = 1 + xrand() % (P - 1); bool ok = true; foreach (q; qs) { ok = ok && (power(g, (P - 1) / q, P) != 1); } if (ok) { break; } } debug { writeln("g = ", g); stdout.flush; } long ans = -1; const b = modLogP(g, A, P); const d = gcd(K, P - 1); if (b % d == 0) { const y = (modInv(K / d, (P - 1) / d) * (b / d)) % ((P - 1) / d); ans = power(g, y, P); } writeln(ans); } } } catch (EOFException e) { } }