Submission #465577

# Submission time Handle Problem Language Result Execution time Memory
465577 2021-08-16T12:15:38 Z rainboy Klavir (COCI17_klavir) C
160 / 160
296 ms 65536 KB
#include <stdio.h>
#include <stdlib.h>

#define N	1000000
#define MD	1000000007

int *ej[N + 1], eo[N + 1];

void append(int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

int main() {
	static int aa[N], dp[N + 1], dq[N + 1], ff[N + 1];
	int n, m, i, j;

	scanf("%d%d", &m, &n);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (i = 0; i <= n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	ff[0] = -1, ff[1] = 0;
	for (i = 0, j = 1; j < n; i++, j++) {
		while (i >= 0 && aa[i] != aa[j])
			i = ff[i];
		ff[j + 1] = i + 1;
		append(i + 1, j + 1);
	}
	dp[0] = 0;
	for (i = 1; i <= n; i++) {
		int o;

		if (i > 1)
			dq[i - 1] = (dq[i - 1] + dq[ff[i - 1]]) % MD;
		dp[i] = ((long long) (dp[i - 1] + 1) * m - dq[i - 1] + MD) % MD;
		dq[i - 1] = (dq[i - 1] + dp[i]) % MD;
		for (o = eo[i]; o--; ) {
			j = ej[i][o];
			dq[j - 1] = (dq[j - 1] - dp[i] + MD) % MD;
		}
	}
	for (i = 1; i <= n; i++)
		printf("%d\n", dp[i]);
	return 0;
}

Compilation message

klavir.c: In function 'append':
klavir.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
klavir.c: In function 'main':
klavir.c:21:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |  scanf("%d%d", &m, &n);
      |  ^~~~~~~~~~~~~~~~~~~~~
klavir.c:23:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 7296 KB Output is correct
2 Correct 296 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 286 ms 65536 KB Output is correct
2 Correct 287 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 284 ms 65536 KB Output is correct
2 Correct 276 ms 65536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 284 ms 65536 KB Output is correct
2 Correct 268 ms 65536 KB Output is correct