結果

問題 No.1482 Swap Many Permutations
ユーザー 👑 ygussanyygussany
提出日時 2021-03-28 19:30:57
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 405 ms / 2,000 ms
コード長 1,951 bytes
コンパイル時間 904 ms
コンパイル使用メモリ 31,072 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 15:47:28
合計ジャッジ時間 7,076 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,380 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 0 ms
4,384 KB
testcase_07 AC 0 ms
4,376 KB
testcase_08 AC 0 ms
4,376 KB
testcase_09 AC 0 ms
4,376 KB
testcase_10 AC 0 ms
4,376 KB
testcase_11 AC 0 ms
4,376 KB
testcase_12 AC 50 ms
4,380 KB
testcase_13 AC 16 ms
4,380 KB
testcase_14 AC 51 ms
4,380 KB
testcase_15 AC 48 ms
4,376 KB
testcase_16 AC 20 ms
4,384 KB
testcase_17 AC 44 ms
4,380 KB
testcase_18 AC 12 ms
4,380 KB
testcase_19 AC 43 ms
4,380 KB
testcase_20 AC 54 ms
4,376 KB
testcase_21 AC 170 ms
4,380 KB
testcase_22 AC 159 ms
4,380 KB
testcase_23 AC 200 ms
4,380 KB
testcase_24 AC 90 ms
4,380 KB
testcase_25 AC 113 ms
4,380 KB
testcase_26 AC 159 ms
4,384 KB
testcase_27 AC 405 ms
4,380 KB
testcase_28 AC 89 ms
4,376 KB
testcase_29 AC 49 ms
4,376 KB
testcase_30 AC 251 ms
4,376 KB
testcase_31 AC 50 ms
4,380 KB
testcase_32 AC 159 ms
4,376 KB
testcase_33 AC 404 ms
4,380 KB
testcase_34 AC 91 ms
4,376 KB
testcase_35 AC 50 ms
4,376 KB
testcase_36 AC 114 ms
4,380 KB
testcase_37 AC 9 ms
4,376 KB
testcase_38 AC 82 ms
4,380 KB
testcase_39 AC 26 ms
4,376 KB
testcase_40 AC 17 ms
4,380 KB
testcase_41 AC 174 ms
4,380 KB
testcase_42 AC 12 ms
4,380 KB
testcase_43 AC 12 ms
4,380 KB
testcase_44 AC 12 ms
4,376 KB
testcase_45 AC 14 ms
4,380 KB
testcase_46 AC 15 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 998244353;

long long div_mod(long long x, long long y, long long z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

long long pow_mod(int n, int k, int M)
{
	long long N, ans = 1;
	for (N = n; k > 0; k >>= 1, N = N * N % M) if (k & 1) ans = ans * N % M;
	return ans;
}

void merge_sort(int x[], int n)
{
	static int y[1000] = {};
	if (n <= 1) return;
	merge_sort(&(x[0]), n/2);
	merge_sort(&(x[n/2]), (n+1)/2);
	
	int i, p, q;
	for (i = 0, p = 0, q = n/2; i < n; i++) {
		if (p >= n/2) y[i] = x[q++];
		else if (q >= n) y[i] = x[p++];
		else y[i] = (x[p] < x[q])? x[p++]: x[q++];
	}
	for (i = 0; i < n; i++) x[i] = y[i];
}

int main()
{
	int i, N, M, B[100001], C[100001];
	scanf("%d %d", &N, &M);
	for (i = 1; i <= N; i++) scanf("%d %d", &(B[i]), &(C[i]));
	
	int j, k, div[1000], par[40010], total = 0;
	M--;
	for (i = 1, k = 0; i * i <= M; i++) {
		if (M % i != 0) continue;
		div[k++] = i;
		if (M / i != i) div[k++] = M / i;
	}
	M++;
	merge_sort(div, k);
	for (i = 1; i < M; i++) {
		for (j = 0; j < k; j++) if (pow_mod(i, div[j], M) == 1) break;
		if ((M - 1) / div[j] % 2 == 0) par[i] = 0;
		else par[i] = 1;
	}
	total ^= par[C[1]] ^ par[C[2]];
	
	long long d_fact[3] = {(long long)M * (M - 1) % Mod, (long long)M * (M - 1) % Mod - 1, 1}, tmp = d_fact[0] - 2;
	printf("1\n");
	if (B[1] == B[2] && C[1] == C[2]) printf("0\n");
	else printf("%lld\n", d_fact[0]);
	for (i = 2, j = 0, d_fact[0] = d_fact[0] * div_mod(1, 2, Mod) % Mod; i < N; i++, j ^= 1) {
		total ^= par[C[i+1]];
		d_fact[j] = d_fact[j] * (tmp--) % Mod;
		if (tmp < 0) tmp += Mod;
		if (j == 0) printf("%lld\n", d_fact[0] * d_fact[1] % Mod);
		else {
			d_fact[2] = d_fact[2] * (Mod - i) % Mod;
			if (total == 1) printf("%lld\n", d_fact[0] * (d_fact[1] + d_fact[2]) % Mod);
			else printf("%lld\n", d_fact[0] * (d_fact[1] + Mod - d_fact[2]) % Mod);
		}
	}
	fflush(stdout);
	return 0;
}
0