結果

問題 No.1492 01文字列と転倒
ユーザー startcppstartcpp
提出日時 2021-04-30 23:28:47
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 61 ms / 4,000 ms
コード長 1,525 bytes
コンパイル時間 1,186 ms
コンパイル使用メモリ 30,380 KB
実行使用メモリ 5,540 KB
最終ジャッジ日時 2023-09-26 08:35:53
合計ジャッジ時間 3,198 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 60 ms
5,456 KB
testcase_03 AC 56 ms
5,540 KB
testcase_04 AC 52 ms
5,388 KB
testcase_05 AC 44 ms
5,080 KB
testcase_06 AC 47 ms
5,296 KB
testcase_07 AC 50 ms
5,260 KB
testcase_08 AC 61 ms
5,532 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 0 ms
4,380 KB
testcase_14 AC 46 ms
5,172 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 44 ms
5,176 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 37 ms
5,008 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#define rep(i, n) for(i = 0; i < n; i++)

int n, m;
int dp[2][101][4951];

int snuke(int n, int i) {
	if (i < 2 * n - i) return i;
	return 2 * n - i;
}

//途中は良いカッコ列じゃなくて良いので、転倒数が結構増えることに注意
int rng(int n, int i) {
	int a = ((i + 1) / 2) * (i / 2);
	int b = n * (n - 1) / 2;
	if (a < b) return a;
	return b;
}

signed main() {
	int i, j, k;
	
	scanf("%d%d", &n, &m);
	
	dp[0][0][0] = 1;
	rep(i, 2 * n) {
		int snu1 = snuke(n, i);
		int rin1 = rng(n, i);
		int snu2 = snuke(n, i + 1);
		int rin2 = rng(n, i + 1);
		
		for (j = 0; j <= snu1; j++) {
			if ((i - j) & 1) continue;
			int one = (i - j) / 2;
			
			if (j + 1 <= snu2) {
				int *ptr1s = &dp[1][j + 1][one];
				int *ptr0s = &dp[0][j][0];
				int *ptr1e = ptr1s + rin1 + 1;
				while (ptr1s != ptr1e) {
					*ptr1s += *ptr0s;
					if (*ptr1s >= m) *ptr1s -= m;
					ptr1s++; ptr0s++;
				}
			}
			if (j - 1 >= 0) {
				int *ptr1s = &dp[1][j - 1][0];
				int *ptr0s = &dp[0][j][0];
				int *ptr1e = ptr1s + rin1 + 1;
				while (ptr1s != ptr1e) {
					*ptr1s += *ptr0s;
					if (*ptr1s >= m) *ptr1s -= m;
					ptr1s++; ptr0s++;
				}
			}
		}
		
		rep(j, snu2 + 1) {
			int *ptr0s = &dp[0][j][0];
			int *ptr1s = &dp[1][j][0];
			int *ptr0e = ptr0s + rin2 + 1;
			while (ptr0s != ptr0e) {
				*ptr0s = *ptr1s;
				*ptr1s = 0;
				ptr0s++; ptr1s++;
			}
		}
	}
	
	rep(i, n * n + 1) {
		if (i > rng(n, 2 * n)) { printf("0\n"); continue; }
		printf("%d\n", dp[0][0][i]);
	}
	return 0;
}
0