답안 #870987

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
870987 2023-11-09T16:17:51 Z rainboy Lucky Numbers (RMI19_lucky) C
100 / 100
34 ms 16472 KB
#include <stdio.h>
#include <string.h>

#define N	100000
#define H_	17	/* H_ = ceil(log2(N)) */
#define N_	(1 << H_)
#define MD	1000000007

int st[N_ * 2][4][4], n_;

void put(int i, int d) {
	memset(st[i], 0, sizeof st[i]);
	if (d == 0)
		st[i][0][0] = 1;
	else if (d == 1)
		st[i][0][1] = 1, st[i][0][2] = 1;
	else
		st[i][0][0] = 1, st[i][0][2] = d - 1, st[i][0][3] = 1;
	if (d == 0)
		st[i][1][0] = 1;
	else if (d == 1)
		st[i][1][1] = 1, st[i][1][2] = 1;
	else if (d == 2)
		st[i][1][0] = 1, st[i][1][2] = 1, st[i][1][3] = 1;
	else if (d == 3)
		st[i][1][2] = 2, st[i][1][3] = 1;
	else
		st[i][1][0] = 1, st[i][1][2] = d - 2, st[i][1][3] = 1;
	st[i][2][2] = 9, st[i][2][3] = 1;
	st[i][3][2] = 8, st[i][3][3] = 1;
}

void pul(int i) {
	int l = i << 1, r = l | 1, u, v, w;

	memset(st[i], 0, sizeof st[i]);
	for (u = 0; u < 4; u++)
		for (v = u < 2 ? 0 : 2; v < 4; v++)
			for (w = v < 2 ? 0 : 2; w < 4; w++)
				st[i][u][w] = (st[i][u][w] + (long long) st[l][u][v] * st[r][v][w]) % MD;
}

void build(char *cc, int n) {
	int i;

	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	for (i = 0; i < n; i++)
		put(n_ + i, cc[i]);
	for (i = n_ - 1; i > 0; i--)
		pul(i);
}

void update(int i, int d) {
	put(i += n_, d);
	while (i > 1)
		pul(i >>= 1);
}

void apply(int *aa, int bb[][4]) {
	static int cc[4];
	int u, v;

	memset(cc, 0, sizeof cc);
	for (u = 0; u < 4; u++)
		for (v = u < 2 ? 0 : 2; v < 4; v++)
			cc[v] = (cc[v] + (long long) aa[u] * bb[u][v]) % MD;
	memcpy(aa, cc, sizeof cc);
}

int query(int l, int r) {
	static int qu[H_ + 1], aa[4];
	int cnt, u, x;

	memset(aa, 0, sizeof aa), aa[0] = 1;
	cnt = 0;
	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			apply(aa, st[l++]);
		if ((r & 1) == 0)
			qu[cnt++] = r--;
	}
	while (cnt--)
		apply(aa, st[qu[cnt]]);
	x = 0;
	for (u = 0; u < 4; u++)
		x = (x + aa[u]) % MD;
	return x;
}

int main() {
	static char cc[N + 1];
	int n, q, i, l, r, d;

	scanf("%d%d%s", &n, &q, cc);
	for (i = 0; i < n; i++)
		cc[i] -= '0';
	build(cc, n);
	printf("%d\n", query(0, n - 1));
	while (q--) {
		int t;

		scanf("%d", &t);
		if (t == 1) {
			scanf("%d%d", &l, &r), l--, r--;
			printf("%d\n", query(l, r));
		} else {
			scanf("%d%d", &i, &d), i--;
			update(i, d);
		}
	}
	return 0;
}

Compilation message

lucky.c: In function 'main':
lucky.c:96:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |  scanf("%d%d%s", &n, &q, cc);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
lucky.c:104:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |   scanf("%d", &t);
      |   ^~~~~~~~~~~~~~~
lucky.c:106:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |    scanf("%d%d", &l, &r), l--, r--;
      |    ^~~~~~~~~~~~~~~~~~~~~
lucky.c:109:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |    scanf("%d%d", &i, &d), i--;
      |    ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 432 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 432 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1628 KB Output is correct
2 Correct 12 ms 3768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1628 KB Output is correct
2 Correct 12 ms 3768 KB Output is correct
3 Correct 27 ms 14416 KB Output is correct
4 Correct 22 ms 14028 KB Output is correct
5 Correct 23 ms 16220 KB Output is correct
6 Correct 24 ms 16148 KB Output is correct
7 Correct 24 ms 16472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 432 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 7 ms 1628 KB Output is correct
8 Correct 12 ms 3768 KB Output is correct
9 Correct 11 ms 1368 KB Output is correct
10 Correct 16 ms 3760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 432 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 7 ms 1628 KB Output is correct
8 Correct 12 ms 3768 KB Output is correct
9 Correct 27 ms 14416 KB Output is correct
10 Correct 22 ms 14028 KB Output is correct
11 Correct 23 ms 16220 KB Output is correct
12 Correct 24 ms 16148 KB Output is correct
13 Correct 24 ms 16472 KB Output is correct
14 Correct 11 ms 1368 KB Output is correct
15 Correct 16 ms 3760 KB Output is correct
16 Correct 25 ms 14016 KB Output is correct
17 Correct 25 ms 14084 KB Output is correct
18 Correct 27 ms 16244 KB Output is correct
19 Correct 29 ms 16220 KB Output is correct
20 Correct 34 ms 16264 KB Output is correct