結果

問題 No.984 Inversion
ユーザー square1001square1001
提出日時 2020-03-02 00:52:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,076 bytes
コンパイル時間 493 ms
コンパイル使用メモリ 67,132 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-03 23:49:10
合計ジャッジ時間 1,613 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 6 ms
4,376 KB
testcase_18 AC 7 ms
4,380 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 7 ms
4,380 KB
testcase_22 AC 6 ms
4,380 KB
testcase_23 AC 6 ms
4,376 KB
testcase_24 AC 7 ms
4,380 KB
testcase_25 AC 6 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;
int solve(int P, int N) {
	if (P == 2) return 0;
	int B = 0;
	while (1LL * B * B < P) ++B;
	int pos = -1;
	for (int i = 1; i <= B; ++i) {
		int val = 1LL * i * N % P;
		if (val <= 2 * B) {
			pos = i;
			break;
		}
		else if (val >= P - 2 * B) {
			return solve(P, P - N) ^ (1LL * (P - 1) * (P - 2) / 2 % 2);
		}
	}
	if (pos == -1) return -1;
	int delta = 1LL * pos * N % P;
	int ans = 0;
	for (int i = 0; i < pos; ++i) {
		long long ini = 1LL * i * N % P;
		for (int j = 0; j < 2 * B / pos + 1; ++j) {
			long long L = 1LL * j * P + (P + 1LL) / 2, R = 1LL * j * P + P;
			long long pl = max(L - ini + delta - 1, 0LL) / delta * pos + i, pr = max(R - ini + delta - 1, 0LL) / delta * pos + i;
			if (pl >= (P + 1LL) / 2) pl = 1LL * ((P + 1LL) / 2 + pos - 1 - i) / pos * pos + i;
			if (pr >= (P + 1LL) / 2) pr = 1LL * ((P + 1LL) / 2 + pos - 1 - i) / pos * pos + i;
			ans += (pr - pl) / pos;
		}
	}
	return ans % 2;
}
int main() {
	int P, N;
	cin >> P >> N;
	int ans = solve(P, N);
	cout << ans << endl;
	return 0;
}
0