about summary refs log tree commit diff
path: root/others/153x/006.c
blob: 4d8947f5bde3d1a5e3c8d14ffff6c0ff8abcc4a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char N, P[10] = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31}, **A, *B, *D, *E;
long C = 0;

int cmp(const void *x, const void *y)
{
	return (int) (*(char *) x - *(char *) y);
}

void circle(FILE *f, char idx)
{
	char i, tmp;

	if (idx == N * 2) {
		tmp = *D + D[idx - 1];
		if (bsearch(&tmp, P, 10, 1, cmp) != NULL) {
			C++;
			E = realloc(E, C * N * 2);
			memcpy(E + (C - 1) * N * 2, D, N * 2);
		}
		return;
	}

	for (i = 1; i < *A[D[idx - 1] - 1]; i++) {
		tmp = A[D[idx - 1] - 1][i];
		if (B[tmp - 1]) {
			B[tmp - 1] = 0;
			D[idx] = tmp;
			circle(f, idx + 1);
			B[tmp - 1] = 1;
		}
	}
}

int main()
{
	FILE *f = fopen("CIRCLE.INP", "r");
	long i;
	char j;

	fscanf(f, "%hhd", &N);
	fclose(f);

	A = malloc(N * 2 * sizeof(char *));
	for (i = 0; i < N * 2; i++) {
		A[i] = malloc(sizeof(char));
		*A[i] = 1;
		for (j = 0; j < 10; j++)
			if (i + 1 < P[j] && P[j] < N * 2 + i + 2) {
				A[i] = realloc(A[i], ++*A[i]);
				A[i][*A[i] - 1] = P[j] - i - 1;
			}
	}

	B = calloc(N * 2, sizeof(char));
	for (i = 1; i < N * 2; i++)
		B[i] = 1;
	D = malloc(N * 2);
	D[0] = 1;
	E = malloc(1);
	circle(f, 1);

	f = fopen("CIRCLE.OUT", "w");
	fprintf(f, "%ld\n", C);
	for (i = 0; i < C * N * 2; i++) {
		for (; i % (N * 2) < N * 2 - 1; i++)
			fprintf(f, "%hhd ", E[i]);
		fprintf(f, "%hhd\n", E[i]);
	}
	fclose(f);

	return 0;
}