結果

問題 No.551 夏休みの思い出(2)
ユーザー square1001square1001
提出日時 2020-03-08 01:55:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,457 bytes
コンパイル時間 839 ms
コンパイル使用メモリ 78,704 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-23 15:55:22
合計ジャッジ時間 7,625 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 TLE -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0