結果
| 問題 | No.551 夏休みの思い出(2) |
| コンテスト | |
| ユーザー |
square1001
|
| 提出日時 | 2020-03-08 01:55:20 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,457 bytes |
| 記録 | |
| コンパイル時間 | 761 ms |
| コンパイル使用メモリ | 78,424 KB |
| 実行使用メモリ | 8,704 KB |
| 最終ジャッジ日時 | 2024-10-15 16:22:32 |
| 合計ジャッジ時間 | 8,109 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 2 |
| other | WA * 27 TLE * 1 -- * 19 |
ソースコード
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int binpow(int a, int b, int m) {
int ans = 1;
while (b > 0) {
if (b & 1) ans = 1LL * ans * a % m;
a = 1LL * a * a % m;
b >>= 1;
}
return ans;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int P, R, Q;
cin >> P >> R >> Q;
int inv2 = binpow(2, P - 2, P);
int inv4 = 1LL * inv2 * inv2 % P;
int b = 1;
while (b * b < P) ++b;
vector<pair<int, int> > seq;
int pmul = 1;
for (int i = 0; i < b; ++i) {
seq.push_back(make_pair(pmul, i));
pmul = 1LL * pmul * R % P;
}
sort(seq.begin(), seq.end());
while (Q--) {
int A, B, C;
cin >> A >> B >> C;
int inva = binpow(A, P - 2, P);
B = 1LL * B * inva % P;
C = 1LL * C * inva % P;
C = (C - 1LL * B * B % P * inv4 % P + P) % P;
if (C == 0) {
int ans = 1LL * B * (P - inv2) % P;
cout << ans << '\n';
}
else {
C = (P - C) % P;
int mul = 1, res = -1;
for (int i = 0; i < b; ++i) {
int g = 1LL * C * binpow(mul, P - 2, P) % P;
int ptr = lower_bound(seq.begin(), seq.end(), make_pair(g, 0)) - seq.begin();
if (ptr != b && seq[ptr].first == g) {
res = i * b + seq[ptr].second;
break;
}
mul = 1LL * mul * pmul % P;
}
if (res % 2 == 1) {
cout << -1 << '\n';
}
else {
int ans = binpow(R, res / 2, P);
int d = 1LL * B * (P - inv2) % P;
cout << (ans + d) % P << ' ' << (P - ans + d) % P << '\n';
}
}
}
return 0;
}
square1001