답안 #471554

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
471554 2021-09-09T18:24:09 Z rainboy Dojave (COCI17_dojave) C
140 / 140
429 ms 24916 KB
#include <stdio.h>

#define M	20
#define N	(1 << M)
#define MD	0x7fffffff

unsigned int Z = 12345;

int rand_() {
	return (Z *= 3) >> 1;
}

int X, Y, ppx[N + 1], ppy[N + 1];

void init() {
	int i;

	X = rand_() % (MD - 3) + 3;
	Y = rand_() % (MD - 3) + 3;
	ppx[0] = ppy[0] = 1;
	for (i = 1; i <= N; i++) {
		ppx[i] = (long long) ppx[i - 1] * X % MD;
		ppy[i] = (long long) ppy[i - 1] * Y % MD;
	}
}

long long xy[N + 1];

int compare(int i, int j) {
	return xy[i] == xy[j] ? (i & 2) - (j & 2) : (xy[i] < xy[j] ? -1 : 1);
}

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k) {
			int c = compare(ii[j], i_);

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

int main() {
	static int aa[N], ii[N + 1];
	int n, m, i, j, x, y;
	long long ans;

	init();
	scanf("%d", &m), n = 1 << m;
	if (m == 1) {
		printf("2\n");
		return 0;
	}
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (i = 0, x = 0, y = 0; i < n; i++) {
		if (aa[i] < (aa[i] ^ n - 1))
			x = ((long long) x + ppx[aa[i]]) % MD, y = ((long long) y + ppy[aa[i]]) % MD;
		else
			x = ((long long) x - ppx[aa[i] ^ n - 1]) % MD, y = ((long long) y - ppy[aa[i] ^ n - 1]) % MD;
		if (x < 0)
			x += MD;
		if (y < 0)
			y += MD;
		xy[i + 1] = (long long) x * MD + y;
	}
	for (i = 0; i <= n; i++)
		ii[i] = i;
	sort(ii, 0, n + 1);
	ans = (long long) (n + 1) * n / 2;
	for (i = 0; i <= n; i = j) {
		j = i + 1;
		while (j <= n && compare(ii[j], ii[i]) == 0)
			j++;
		ans -= (long long) (j - i) * (j - i - 1) / 2;
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

dojave.c: In function 'main':
dojave.c:69:26: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   69 |   if (aa[i] < (aa[i] ^ n - 1))
      |                        ~~^~~
dojave.c:72:39: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   72 |    x = ((long long) x - ppx[aa[i] ^ n - 1]) % MD, y = ((long long) y - ppy[aa[i] ^ n - 1]) % MD;
      |                                     ~~^~~
dojave.c:72:86: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   72 |    x = ((long long) x - ppx[aa[i] ^ n - 1]) % MD, y = ((long long) y - ppy[aa[i] ^ n - 1]) % MD;
      |                                                                                    ~~^~~
dojave.c:61:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |  scanf("%d", &m), n = 1 << m;
      |  ^~~~~~~~~~~~~~~
dojave.c:67:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8396 KB Output is correct
2 Correct 10 ms 8396 KB Output is correct
3 Correct 10 ms 8396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8396 KB Output is correct
2 Correct 10 ms 8396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8524 KB Output is correct
2 Correct 11 ms 8520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8524 KB Output is correct
2 Correct 13 ms 8536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 8760 KB Output is correct
2 Correct 15 ms 8760 KB Output is correct
3 Correct 14 ms 8856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 9460 KB Output is correct
2 Correct 29 ms 9504 KB Output is correct
3 Correct 45 ms 10532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 9512 KB Output is correct
2 Correct 48 ms 10500 KB Output is correct
3 Correct 90 ms 12532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 12488 KB Output is correct
2 Correct 96 ms 12492 KB Output is correct
3 Correct 94 ms 12584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 402 ms 24812 KB Output is correct
2 Correct 419 ms 24852 KB Output is correct
3 Correct 272 ms 24772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 429 ms 24900 KB Output is correct
2 Correct 390 ms 24840 KB Output is correct
3 Correct 360 ms 24916 KB Output is correct
4 Correct 413 ms 24808 KB Output is correct