結果

問題 No.551 夏休みの思い出(2)
ユーザー kazumakazuma
提出日時 2018-03-25 06:04:23
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 4,000 ms
コード長 1,897 bytes
コンパイル時間 2,111 ms
コンパイル使用メモリ 199,968 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-07 11:08:59
合計ジャッジ時間 6,112 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 5 ms
4,384 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 27 ms
4,384 KB
testcase_28 AC 27 ms
4,380 KB
testcase_29 AC 24 ms
4,384 KB
testcase_30 AC 26 ms
4,384 KB
testcase_31 AC 23 ms
4,384 KB
testcase_32 AC 26 ms
4,380 KB
testcase_33 AC 26 ms
4,380 KB
testcase_34 AC 25 ms
4,380 KB
testcase_35 AC 25 ms
4,384 KB
testcase_36 AC 25 ms
4,384 KB
testcase_37 AC 30 ms
4,384 KB
testcase_38 AC 29 ms
4,380 KB
testcase_39 AC 27 ms
4,380 KB
testcase_40 AC 28 ms
4,380 KB
testcase_41 AC 27 ms
4,384 KB
testcase_42 AC 29 ms
4,384 KB
testcase_43 AC 27 ms
4,384 KB
testcase_44 AC 28 ms
4,380 KB
testcase_45 AC 27 ms
4,384 KB
testcase_46 AC 29 ms
4,380 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int mod_pow(int a, int b, int m) {
	int ret = 1;
	while (b) {
		if (b & 1) ret = (ll)ret * a % m;
		a = (ll)a * a % m;
		b >>= 1;
	}
	return ret;
}

bool is_square(int a, int p) {
	return mod_pow(a, (p - 1) >> 1, p) == 1;
}

int mod_sqrt(int a, int p) {
	if (a == 0) return 0;
	if (p == 2) return a;
	if (!is_square(a, p)) return -1;
	int b = 2;
	while (is_square(((ll)b * b - a + p) % p, p)) b++;
	int w = ((ll)b * b - a + p) % p;

	auto mul = [&](pair<int, int> u, pair<int, int> v) {
		int a = ((ll)u.first * v.first + (ll)u.second * v.second % p * w) % p;
		int b = ((ll)u.first * v.second + (ll)u.second * v.first) % p;
		return make_pair(a, b);
	};

	// (b + sqrt(b^2-a))^(p+1)/2
	int e = (p + 1) / 2;
	auto ret = make_pair(1, 0);
	auto v = make_pair(b, 1);
	while (e) {
		if (e & 1) ret = mul(ret, v);
		v = mul(v, v);
		e >>= 1;
	}
	return ret.first;
}

ll mod_inv(ll a, ll md = 1e9 + 7) {
	ll b = md, u = 1, v = 0;
	while (b) {
		ll t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= md;
	if (u < 0) u += md;
	return u;
}

int P;

pair<int, int> solve(ll a, ll b, ll c) {
	a = a * 2 % P;
	c = c * 2 % P;
	int rt = mod_sqrt((b * b % P + P - a * c % P) % P, P);
	if (rt == -1) return make_pair(-1, -1);
	pair<int, int> res(rt, (P - rt) % P);
	res.first = (res.first + P - b) % P * mod_inv(a, P) % P;
	res.second = (res.second + P - b) % P * mod_inv(a, P) % P;
	if (res.first > res.second) swap(res.first, res.second);
	return res;
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int R, Q;
	cin >> P >> R >> Q;
	while (Q--) {
		int A, B, C;
		cin >> A >> B >> C;
		auto res = solve(A, B, C);
		if (res.first == -1) {
			printf("%d\n", -1);
		}
		else if (res.first == res.second) {
			printf("%d\n", res.first);
		}
		else {
			printf("%d %d\n", res.first, res.second);
		}
	}
	return 0;
}
0