diff options
author | Raphael McSinyx <vn.mcsinyx@gmail.com> | 2017-08-10 16:47:49 +0700 |
---|---|---|
committer | Raphael McSinyx <vn.mcsinyx@gmail.com> | 2017-08-10 17:31:31 +0700 |
commit | 4051a87fc1b1772644c647c5c7e7a158f2728108 (patch) | |
tree | 5efdfd1ca6e773229db29e549ab75ac77f7dc2a6 /others/other/digit.c | |
parent | c63bd23b97a4bbb0c7bd30c871ad7feaad344e0d (diff) | |
download | cp-4051a87fc1b1772644c647c5c7e7a158f2728108.tar.gz |
Add others/other/digit.c
Diffstat (limited to 'others/other/digit.c')
-rw-r--r-- | others/other/digit.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/others/other/digit.c b/others/other/digit.c new file mode 100644 index 0000000..d59241a --- /dev/null +++ b/others/other/digit.c @@ -0,0 +1,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; +} |