結果

問題 No.1779 Magical Swap
ユーザー 👑 ygussanyygussany
提出日時 2021-12-08 00:37:23
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,828 bytes
コンパイル時間 1,179 ms
コンパイル使用メモリ 30,068 KB
実行使用メモリ 12,176 KB
最終ジャッジ日時 2023-09-23 07:47:55
合計ジャッジ時間 2,596 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 24 ms
4,380 KB
testcase_05 AC 11 ms
4,380 KB
testcase_06 AC 12 ms
4,380 KB
testcase_07 AC 22 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 17 ms
10,240 KB
testcase_10 AC 41 ms
12,176 KB
testcase_11 AC 26 ms
10,856 KB
testcase_12 AC 15 ms
10,144 KB
testcase_13 AC 34 ms
11,636 KB
testcase_14 AC 27 ms
5,400 KB
testcase_15 AC 30 ms
4,376 KB
testcase_16 AC 28 ms
4,380 KB
testcase_17 AC 40 ms
9,788 KB
testcase_18 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

char flag[100003] = {};

typedef struct List {
	struct List *next;
	int v, num;
} list;

#define HASH 1000003
const int H_Mod = HASH;

int solve(int N, int A[], int B[])
{
	int h, i, j;
	static int q[200003], head, tail;
	static list *hash[HASH] = {}, d[200003], *p;
	for (i = 1, j = 0, tail = 0; i <= N; i++) {
		if (i == 1 || (flag[i] == 0 && i * 2 > N)) {
			if (A[i] != B[i]) {
				for (head = 0; head < tail; head++) hash[q[head] % H_Mod] = NULL;
				return 0;
			}
		} else {
			h = A[i] % H_Mod;
			for (p = hash[h]; p != NULL; p = p->next) if (p->v == A[i]) break;
			if (p != NULL) p->num++;
			else {
				q[tail++] = A[i];
				d[j].v = A[i];
				d[j].num = 1;
				d[j].next = hash[h];
				hash[h] = &(d[j++]);
			}
			h = B[i] % H_Mod;
			for (p = hash[h]; p != NULL; p = p->next) if (p->v == B[i]) break;
			if (p != NULL) p->num--;
			else {
				q[tail++] = B[i];
				d[j].v = B[i];
				d[j].num = -1;
				d[j].next = hash[h];
				hash[h] = &(d[j++]);
			}
		}
	}
	for (head = 0; head < tail; head++) {
		h = q[head] % H_Mod;
		for (p = hash[h]; p != NULL; p = p->next) {
			if (p->num != 0) {
				for (; head < tail; head++) hash[q[head] % H_Mod] = NULL;
				return 0;
			}
		}
		hash[h] = NULL;
	}
	return 1;
}

int main()
{
	int i, j, t, T, N, A[100003], B[100003];
	for (i = 2; i <= 100000; i++) {
		if (flag[i] != 0) continue;
		for (j = i * 2; j <= 100000; j += i) flag[j] = 1;
	}
	scanf("%d", &T);
	for (t = 1; t <= T; t++) {
		scanf("%d", &N);
		if (N > 100000) return -1;
		for (i = 1; i <= N; i++) {
			scanf("%d", &(A[i]));
			if (A[i] <= 0 || A[i] > 1000000000) return -1;
		}
		for (i = 1; i <= N; i++) {
			scanf("%d", &(B[i]));
			if (B[i] <= 0 || B[i] > 1000000000) return -1;
		}
		if (solve(N, A, B) == 0) printf("No\n");
		else printf("Yes\n");
	}
	fflush(stdout);
	return 0;
}
0