結果
問題 | No.981 一般冪乗根 |
ユーザー |
![]() |
提出日時 | 2020-02-10 19:12:56 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,410 bytes |
コンパイル時間 | 1,168 ms |
コンパイル使用メモリ | 87,520 KB |
実行使用メモリ | 10,148 KB |
最終ジャッジ日時 | 2024-10-09 15:38:53 |
合計ジャッジ時間 | 196,649 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 TLE * 18 |
ソースコード
#include <vector>#include <iostream>#include <algorithm>#include <unordered_map>using namespace std;int binpow(int a, int b, int mod) {int ans = 1;while (b > 0) {if (b & 1) ans = 1LL * ans * a % mod;a = 1LL * a * a % mod;b >>= 1;}return ans;}int find_primitive_root(int p) {// Time complexity: O(p^(1/4+eps))int x = p - 1;vector<int> checks;for (int i = 2; i * i <= x; ++i) {if (x % i == 0) {while (x % i == 0) x /= i;checks.push_back((p - 1) / i);}}if (x > 1) {checks.push_back((p - 1) / x);}for (int i = 2; i < p; ++i) {bool ok = true;for (int j : checks) {if (binpow(i, j, p) == 1) {ok = false;break;}}if (ok) return i;}return -1;}int main() {int Q;cin >> Q;while (Q--) {int P, K, A;cin >> P >> K >> A;int g = find_primitive_root(P);int b = binpow(g, K, P);int bucket = 0;while (bucket * bucket < P) {++bucket;}int mul = 1;unordered_map<int, int> d;for (int i = 0; i < bucket; ++i) {if (d.find(mul) == d.end()) {d[mul] = i;}mul = 1LL * mul * b % P;}int pmul = 1, ans = -1;mul = binpow(mul, P - 2, P);for (int i = 0; i < bucket; ++i) {int target = 1LL * A * pmul % P;if (d.find(target) != d.end()) {ans = binpow(g, i * bucket + d[target], P);break;}pmul = 1LL * pmul * mul % P;}cout << ans << '\n';}return 0;}