提出 #36903869


ソースコード 拡げる

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 mul(int a, int b) {
  17. return (int)((long long)a * b % P);
  18. }
  19.  
  20. /* 同じ文字が len 文字連続した文字列に対して操作を行ったとき、何文字になるかを求める */
  21. int elementLength(int len) {
  22. int ans = 1; /* 最初にアルファベットを1個おく */
  23. /* 文字数の十進表現の長さを求める */
  24. do {
  25. ans++;
  26. len /= 10;
  27. } while (len > 0);
  28. return ans;
  29. }
  30.  
  31. /* [もとの文字列を何文字決めたか][操作後の文字列が何文字になったか] */
  32. int memo[MAX][MAX];
  33. char memoValid[MAX][MAX];
  34.  
  35. int calc(int srcLen, int destLen) {
  36. int ans = 0;
  37. int i;
  38. /* 操作後の文字列の長さが N 以上になったため、条件を満たさない */
  39. if (destLen >= N) return 0;
  40. /* 操作後の文字列の長さが N 未満のまま操作前の文字列が N 文字になったので、条件を満たす */
  41. if (srcLen >= N) return 1;
  42. /* メモがあるなら、その値を返す */
  43. if (memoValid[srcLen][destLen]) return memo[srcLen][destLen];
  44.  
  45. /* 次に同じ文字を何文字置くか、それぞれを試す */
  46. for (i = 1; srcLen + i <= N; i++) {
  47. ans = add(ans, calc(srcLen + i, destLen + elementLength(i)));
  48. }
  49.  
  50. if (srcLen == 0) {
  51. /* 最初は、好きな文字を選べる */
  52. ans = mul(ans, 26);
  53. } else {
  54. /* 前回使った文字以外の中から好きな文字を選べる */
  55. ans = mul(ans, 25);
  56. }
  57.  
  58. /* 結果をメモして返す */
  59. memo[srcLen][destLen] = ans;
  60. memoValid[srcLen][destLen] = 1;
  61. return ans;
  62. }
  63.  
  64. int main(void) {
  65. if (scanf("%d%d", &N, &P) != 2) return 1;
  66. printf("%d\n", calc(0, 0));
  67. return 0;
  68. }
#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 mul(int a, int b) {
	return (int)((long long)a * b % P);
}

/* 同じ文字が len 文字連続した文字列に対して操作を行ったとき、何文字になるかを求める */
int elementLength(int len) {
	int ans = 1; /* 最初にアルファベットを1個おく */
	/* 文字数の十進表現の長さを求める */
	do {
		ans++;
		len /= 10;
	} while (len > 0);
	return ans;
}

/* [もとの文字列を何文字決めたか][操作後の文字列が何文字になったか] */
int memo[MAX][MAX];
char memoValid[MAX][MAX];

int calc(int srcLen, int destLen) {
	int ans = 0;
	int i;
	/* 操作後の文字列の長さが N 以上になったため、条件を満たさない */
	if (destLen >= N) return 0;
	/* 操作後の文字列の長さが N 未満のまま操作前の文字列が N 文字になったので、条件を満たす */
	if (srcLen >= N) return 1;
	/* メモがあるなら、その値を返す */
	if (memoValid[srcLen][destLen]) return memo[srcLen][destLen];

	/* 次に同じ文字を何文字置くか、それぞれを試す */
	for (i = 1; srcLen + i <= N; i++) {
		ans = add(ans, calc(srcLen + i, destLen + elementLength(i)));
	}

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

	/* 結果をメモして返す */
	memo[srcLen][destLen] = ans;
	memoValid[srcLen][destLen] = 1;
	return ans;
}

int main(void) {
	if (scanf("%d%d", &N, &P) != 2) return 1;
	printf("%d\n", calc(0, 0));
	return 0;
}

提出情報

提出日時
問題 E - RLE
ユーザ mikecat
言語 C (GCC 9.2.1)
得点 0
コード長 1909 Byte
結果 TLE
実行時間 3308 ms
メモリ 15028 KB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 500
結果
AC × 3
TLE × 1
AC × 11
TLE × 20
セット名 テストケース
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 4 ms 1708 KB
example_01.txt AC 1 ms 1636 KB
example_02.txt AC 1 ms 1664 KB
example_03.txt TLE 3308 ms 15000 KB
test_00.txt TLE 3308 ms 14168 KB
test_01.txt AC 26 ms 3272 KB
test_02.txt TLE 3308 ms 13888 KB
test_03.txt TLE 3308 ms 14176 KB
test_04.txt AC 5 ms 1864 KB
test_05.txt AC 295 ms 6628 KB
test_06.txt TLE 3308 ms 13904 KB
test_07.txt TLE 3308 ms 13768 KB
test_08.txt AC 1084 ms 10140 KB
test_09.txt TLE 3308 ms 13980 KB
test_10.txt TLE 3308 ms 15028 KB
test_11.txt TLE 3308 ms 14152 KB
test_12.txt AC 2646 ms 13372 KB
test_13.txt TLE 3308 ms 13812 KB
test_14.txt TLE 3308 ms 13800 KB
test_15.txt AC 12 ms 2800 KB
test_16.txt AC 137 ms 5280 KB
test_17.txt TLE 3308 ms 13964 KB
test_18.txt TLE 3308 ms 13688 KB
test_19.txt TLE 3308 ms 14776 KB
test_20.txt TLE 3308 ms 14952 KB
test_21.txt TLE 3308 ms 14928 KB
test_22.txt TLE 3308 ms 14900 KB
test_23.txt TLE 3308 ms 14856 KB
test_24.txt TLE 3308 ms 14804 KB
test_25.txt TLE 3308 ms 14952 KB
test_26.txt AC 7 ms 1704 KB


2022-11-30 (水)
21:10:54 +00:00