Submission #473606

# Submission time Handle Problem Language Result Execution time Memory
473606 2021-09-15T17:16:52 Z rainboy Mate (COCI18_mate) C
80 / 100
2000 ms 5496 KB
#include <stdio.h>
#include <string.h>

#define N	2000
#define A	26
#define MD	1000000007

int vv[N + 1], ff[N + 1], gg[N + 1];

void init() {
	int i;

	ff[0] = gg[0] = 1;
	for (i = 1; i <= N; i++) {
		vv[i] = i == 1 ? 1 : (long long) vv[i - MD % i] * (MD / i + 1) % MD;
		ff[i] = (long long) ff[i - 1] * i % MD;
		gg[i] = (long long) gg[i - 1] * vv[i] % MD;
	}
}

long long choose(int n, int k) {
	return (long long) ff[n] * gg[k] % MD * gg[n - k] % MD;
}

int main() {
	static char cc[N + 1];
	static int ans[A][A][N + 1];
	int n, q, i, l, a, b;

	init();
	scanf("%s%d", cc, &q), n = strlen(cc);
	for (a = 0; a < A; a++)
		for (b = 0; b < A; b++)
			for (l = 2; l <= n; l++) {
				long long x;
				int k;

				x = 0, k = 0;
				for (i = n - 1; i >= l - 2; i--) {
					if (cc[i] == a + 'a')
						x = (x + choose(i, l - 2) * k) % MD;
					if (cc[i] == b + 'a')
						k++;
				}
				ans[a][b][l] = x;
			}
	while (q--) {
		static char s[4];
		int l;

		scanf("%d%s", &l, s);
		printf("%d\n", ans[s[0] - 'a'][s[1] - 'a'][l]);
	}
	return 0;
}

Compilation message

mate.c: In function 'main':
mate.c:31:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |  scanf("%s%d", cc, &q), n = strlen(cc);
      |  ^~~~~~~~~~~~~~~~~~~~~
mate.c:51:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |   scanf("%d%s", &l, s);
      |   ^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 3388 KB Output is correct
2 Correct 8 ms 3276 KB Output is correct
3 Correct 9 ms 3276 KB Output is correct
4 Correct 13 ms 3404 KB Output is correct
5 Correct 71 ms 5236 KB Output is correct
6 Correct 71 ms 5496 KB Output is correct
7 Correct 66 ms 4996 KB Output is correct
8 Correct 76 ms 4924 KB Output is correct
9 Execution timed out 2091 ms 3804 KB Time limit exceeded
10 Execution timed out 2072 ms 3868 KB Time limit exceeded