about summary refs log tree commit diff
path: root/others/other/digit.c
blob: d59241a525ad09214c64f5a4c37721ccf4e0a4c6 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char DIGITS[] = "0123456789ABCDEF";

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

char idx(char *c)
{
	return (char *) bsearch(c, DIGITS, 16, sizeof(char), cmp) - DIGITS;
}

int main()
{
	FILE *f = fopen("DIGIT.INP", "r");
	char i, x[1], m[128];
	long l, j;
	unsigned long long *em, *k, *tmp, s[16];

	em = calloc(16, sizeof(unsigned long long));
	k = calloc(16, sizeof(unsigned long long));
	fscanf(f, "%s\n%ld\n%c", m, &l, x);
	fclose(f);

	for (i = 0; i < strlen(m); em[idx(m + i++)]++);
	memcpy(s, em, sizeof(unsigned long long) << 4);

	for (j = 1; j < l; j++) {
		for (i = 0; i < 16; i++)
			if (em[i]) {
				k[i]++;
				do {
					k[em[i] & 15]++;
					em[i] >>= 4;
				} while (em[i]);
			}
		tmp = k;
		k = em;
		em = tmp;
		for (i = 0; i < 16; i++)
			s[i] += em[i];
		puts("");
	}

	if (l)
		for (i = 0; i < 16; i++)
			if (em[i]) {
				s[i]++;
				do {
					s[em[i] & 15]++;
					em[i] >>= 4;
				} while (em[i]);
			}

	f = fopen("DIGIT.OUT", "w");
	fprintf(f, "%Ld\n", s[idx(x)]);
	fclose(f);

	return 0;
}