Submission #36904284
Source Code Expand
#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;
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - RLE |
| User | mikecat |
| Language | C (GCC 9.2.1) |
| Score | 500 |
| Code Size | 2502 Byte |
| Status | AC |
| Exec Time | 232 ms |
| Memory | 75684 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 500 / 500 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| 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 |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| example_00.txt | AC | 7 ms | 1632 KiB |
| example_01.txt | AC | 2 ms | 1640 KiB |
| example_02.txt | AC | 1 ms | 1652 KiB |
| example_03.txt | AC | 232 ms | 75636 KiB |
| test_00.txt | AC | 49 ms | 23956 KiB |
| test_01.txt | AC | 5 ms | 3752 KiB |
| test_02.txt | AC | 73 ms | 33012 KiB |
| test_03.txt | AC | 47 ms | 23068 KiB |
| test_04.txt | AC | 1 ms | 1932 KiB |
| test_05.txt | AC | 18 ms | 8728 KiB |
| test_06.txt | AC | 66 ms | 30464 KiB |
| test_07.txt | AC | 70 ms | 32764 KiB |
| test_08.txt | AC | 25 ms | 14720 KiB |
| test_09.txt | AC | 151 ms | 59568 KiB |
| test_10.txt | AC | 226 ms | 75604 KiB |
| test_11.txt | AC | 48 ms | 22752 KiB |
| test_12.txt | AC | 39 ms | 20648 KiB |
| test_13.txt | AC | 86 ms | 38924 KiB |
| test_14.txt | AC | 86 ms | 39748 KiB |
| test_15.txt | AC | 2 ms | 2988 KiB |
| test_16.txt | AC | 11 ms | 6624 KiB |
| test_17.txt | AC | 55 ms | 24808 KiB |
| test_18.txt | AC | 81 ms | 36640 KiB |
| test_19.txt | AC | 211 ms | 72680 KiB |
| test_20.txt | AC | 225 ms | 75604 KiB |
| test_21.txt | AC | 227 ms | 75680 KiB |
| test_22.txt | AC | 225 ms | 75684 KiB |
| test_23.txt | AC | 218 ms | 74144 KiB |
| test_24.txt | AC | 219 ms | 74356 KiB |
| test_25.txt | AC | 224 ms | 75284 KiB |
| test_26.txt | AC | 1 ms | 1648 KiB |