提出 #36904284


ソースコード 拡げる

Copy
Copy
  1. #include <stdio.h>
  2.  
  3. #define MAX 3156
  4.  
  5. int N, P;
  6.  
  7. /* a + b mod P を返す */
  8. int add(int a, int b) {
  9. int ret = a + b;
  10. /* [0, P) の数を2個足した結果は 2*P 未満 */
  11. if (ret >= P) ret -= P;
  12. return ret;
  13. }
  14.  
  15. /* a - b mod P を返す */
  16. int sub(int a, int b) {
  17. if (b == 0) return a;
  18. /* b が正のとき、P - b は P 未満 */
  19. /* b が P 未満 のとき、P - b は正 */
  20. return add(a, P - b);
  21. }
  22.  
  23. /* a * b mod P を返す */
  24. int mul(int a, int b) {
  25. return (int)((long long)a * b % P);
  26. }
  27.  
  28. /* [もとの文字列を何文字決めたか][操作後の文字列が何文字になったか] */
  29. int memo[MAX][MAX];
  30. /* もとの文字列の長さの降順の累積和 */
  31. int memoSum[MAX][MAX];
  32.  
  33. int main(void) {
  34. int srcLen, destLen;
  35. if (scanf("%d%d", &N, &P) != 2) return 1;
  36.  
  37. /* 操作後の文字列が N 文字未満で、操作前の文字列が N 文字に到達した */
  38. for (destLen = 0; destLen < N; destLen++) {
  39. memo[N][destLen] = 1;
  40. memoSum[N][destLen] = 1;
  41. }
  42. /* 操作前の文字列の長い順に求めていく */
  43. for (srcLen = N - 1; srcLen >= 0; srcLen--) {
  44. /* 操作後の文字列が N 文字以上になったら条件を満たさないので、そこまで求めればいい */
  45. for (destLen = 0; destLen < N; destLen++) {
  46. int ans = 0;
  47. int i;
  48. int delta = 2; /* 操作後の文字列の長さが増える量 */
  49. int deltaLast = 9; /* 増える量が delta となる最後の置く長さ */
  50. /* 次に同じ文字を何文字置くか、それぞれを試す */
  51. for (i = 1; srcLen + i <= N; ) {
  52. /* 操作前の文字列が N 文字になるまでで打ち切る */
  53. if (srcLen + deltaLast > N) deltaLast = N - srcLen;
  54. /* 累積和を用いて加算する */
  55. ans = add(ans, sub(memoSum[srcLen + i][destLen + delta],
  56. memoSum[srcLen + deltaLast + 1][destLen + delta]));
  57. /* 次の部分について求める準備をする */
  58. i= deltaLast + 1;
  59. delta++;
  60. deltaLast = deltaLast * 10 + 9;
  61. }
  62.  
  63. if (srcLen == 0) {
  64. /* 最初は、好きな文字を選べる */
  65. ans = mul(ans, 26);
  66. } else {
  67. /* 前回使った文字以外の中から好きな文字を選べる */
  68. ans = mul(ans, 25);
  69. }
  70.  
  71. /* 結果をメモする */
  72. memo[srcLen][destLen] = ans;
  73. /* 累積和をとる */
  74. memoSum[srcLen][destLen] = add(memo[srcLen][destLen], memoSum[srcLen + 1][destLen]);
  75. }
  76. }
  77.  
  78. printf("%d\n", memo[0][0]);
  79. return 0;
  80. }
#include <stdio.h>

#define MAX 3156

int N, P;

/* a + b mod P を返す */
int add(int a, int b) {
	int ret = a + b;
	/* [0, P) の数を2個足した結果は 2*P 未満 */
	if (ret >= P) ret -= P;
	return ret;
}

/* a - b mod P を返す */
int sub(int a, int b) {
	if (b == 0) return a;
	/* b が正のとき、P - b は P 未満 */
	/* b が P 未満 のとき、P - b は正 */
	return add(a, P - b);
}

/* a * b mod P を返す */
int mul(int a, int b) {
	return (int)((long long)a * b % P);
}

/* [もとの文字列を何文字決めたか][操作後の文字列が何文字になったか] */
int memo[MAX][MAX];
/* もとの文字列の長さの降順の累積和 */
int memoSum[MAX][MAX];

int main(void) {
	int srcLen, destLen;
	if (scanf("%d%d", &N, &P) != 2) return 1;

	/* 操作後の文字列が N 文字未満で、操作前の文字列が N 文字に到達した */
	for (destLen = 0; destLen < N; destLen++) {
		memo[N][destLen] = 1;
		memoSum[N][destLen] = 1;
	}
	/* 操作前の文字列の長い順に求めていく */
	for (srcLen = N - 1; srcLen >= 0; srcLen--) {
		/* 操作後の文字列が N 文字以上になったら条件を満たさないので、そこまで求めればいい */
		for (destLen = 0; destLen < N; destLen++) {
			int ans = 0;
			int i;
			int delta = 2; /* 操作後の文字列の長さが増える量 */
			int deltaLast = 9; /* 増える量が delta となる最後の置く長さ */
			/* 次に同じ文字を何文字置くか、それぞれを試す */
			for (i = 1; srcLen + i <= N; ) {
				/* 操作前の文字列が N 文字になるまでで打ち切る */
				if (srcLen + deltaLast > N) deltaLast = N - srcLen;
				/* 累積和を用いて加算する */
				ans = add(ans, sub(memoSum[srcLen + i][destLen + delta],
					memoSum[srcLen + deltaLast + 1][destLen + delta]));
				/* 次の部分について求める準備をする */
				i= deltaLast + 1;
				delta++;
				deltaLast = deltaLast * 10 + 9;
			}

			if (srcLen == 0) {
				/* 最初は、好きな文字を選べる */
				ans = mul(ans, 26);
			} else {
				/* 前回使った文字以外の中から好きな文字を選べる */
				ans = mul(ans, 25);
			}

			/* 結果をメモする */
			memo[srcLen][destLen] = ans;
			/* 累積和をとる */
			memoSum[srcLen][destLen] = add(memo[srcLen][destLen], memoSum[srcLen + 1][destLen]);
		}
	}

	printf("%d\n", memo[0][0]);
	return 0;
}

提出情報

提出日時
問題 E - RLE
ユーザ mikecat
言語 C (GCC 9.2.1)
得点 500
コード長 2502 Byte
結果 AC
実行時間 232 ms
メモリ 75684 KB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 4
AC × 31
セット名 テストケース
Sample example_00.txt, example_01.txt, example_02.txt, example_03.txt
All example_00.txt, example_01.txt, example_02.txt, example_03.txt, test_00.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 7 ms 1632 KB
example_01.txt AC 2 ms 1640 KB
example_02.txt AC 1 ms 1652 KB
example_03.txt AC 232 ms 75636 KB
test_00.txt AC 49 ms 23956 KB
test_01.txt AC 5 ms 3752 KB
test_02.txt AC 73 ms 33012 KB
test_03.txt AC 47 ms 23068 KB
test_04.txt AC 1 ms 1932 KB
test_05.txt AC 18 ms 8728 KB
test_06.txt AC 66 ms 30464 KB
test_07.txt AC 70 ms 32764 KB
test_08.txt AC 25 ms 14720 KB
test_09.txt AC 151 ms 59568 KB
test_10.txt AC 226 ms 75604 KB
test_11.txt AC 48 ms 22752 KB
test_12.txt AC 39 ms 20648 KB
test_13.txt AC 86 ms 38924 KB
test_14.txt AC 86 ms 39748 KB
test_15.txt AC 2 ms 2988 KB
test_16.txt AC 11 ms 6624 KB
test_17.txt AC 55 ms 24808 KB
test_18.txt AC 81 ms 36640 KB
test_19.txt AC 211 ms 72680 KB
test_20.txt AC 225 ms 75604 KB
test_21.txt AC 227 ms 75680 KB
test_22.txt AC 225 ms 75684 KB
test_23.txt AC 218 ms 74144 KB
test_24.txt AC 219 ms 74356 KB
test_25.txt AC 224 ms 75284 KB
test_26.txt AC 1 ms 1648 KB


2022-11-30 (水)
22:27:54 +00:00