1
0
Fork 0

The good, the bad and the ugly

This commit is contained in:
Nguyễn Gia Phong 2019-12-16 21:13:07 +07:00
parent 8a9d6282fc
commit c1008fe392
7 changed files with 137 additions and 1 deletions

View File

@ -40,8 +40,8 @@ Phiên bản các trình dịch sử dụng test:
| Java | OpenJDK 11+ |
| Lua | Lua 5.1+ |
| Pascal | Free Pascal 2.6.4+ |
| Raku | Rakudo 2018.12+ |
| Python | Python 3.5+ |
| Raku | Rakudo 2018.12+ |
| Scheme | GNU Guile 2.0.11+ |
SICP không chỉ dùng Guile để chạy Scheme mà còn sử dụng Racket (`#lang sicp`)

51
codechef/binadd.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#define SIZE (100001 * sizeof(int))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
int main()
{
size_t t;
int *a = malloc(SIZE), *b = malloc(SIZE), c;
*a = *b = 0;
scanf("%zu", &t);
while (t--) {
size_t lena = 1, lenb = 1;
for (scanf(" "); (c = getchar()) > 42; a[lena++] = c - 48);
for (scanf(" "); (c = getchar()) > 42; b[lenb++] = c - 48);
size_t len = (lena > lenb) ? lena : lenb;
for (int *n = a + len, *e = a + lena, *p = b + lenb;
n-- > a; *n = (e-- > a && *e) << 1 | (p-- > b && *p));
int tmp = 0;
size_t result = 0, sum = 0;
for (int *n = a + len; n-- > a;)
switch (*n | tmp) {
case 1:
result = MAX(result, 1);
break;
case 3:
tmp = 4;
break;
case 4:
result = MAX(result, sum + 2);
sum = tmp = 0;
break;
case 5:
case 6:
sum++;
break;
case 7:
if (n[1] < 3) {
result = MAX(result, sum + 2);
sum = 0;
}
}
printf("%zu\n", result);
}
return 0;
}

9
codechef/binadd.py Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env python3
for t in range(int(input())):
e, p = input(), input()
a, b = int(e, 2), int(p, 2)
count = 0
while b:
a, b = a^b, (a&b)<<1
count += 1
print(count)

28
codechef/binxor.py Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env python3
from functools import reduce
from itertools import accumulate
from typing import Iterable, Iterator
MOD = 1_000_000_007
LENGTH = 100_001
def add(x: int, y: int) -> int: return (x + y) % MOD
def mul(x: int, y: int) -> int: return x * y % MOD
def khalid(iterable: Iterable[int]) -> Iterator[int]:
yield 1
yield from iterable
modinv = [pow(i, MOD-2, MOD) for i in range(1, LENGTH)]
facinv = tuple(khalid(accumulate(modinv, mul)))
fac = tuple(khalid(accumulate(range(1, LENGTH), mul)))
for t in range(int(input())):
n = int(input())
a, b = (min(b.count(c) for c in '01')
for b in (input() for i in range(2)))
print(reduce(add, (mul(mul(fac[n], facinv[i]), facinv[n - i])
for i in range(abs(a-b), a+b+1, 2))))

22
codechef/plmu.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
int main()
{
char t;
size_t n, tmp, zeros, twos;
scanf("%hhd", &t);
while (t--) {
scanf("%zu", &n);
for (size_t i = zeros = twos = 0; i < n; ++i) {
scanf("%zu", &tmp);
if (!tmp)
zeros++;
else if (tmp == 2)
twos++;
}
printf("%zu\n", zeros * (zeros - 1) + twos * (twos - 1) >> 1);
}
return 0;
}

21
codechef/subsplay.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
int main()
{
int t, n;
scanf("%d", &t);
while (t--) {
int k = 0, hope[26] = {0};
scanf("%d ", &n);
for (int i = 0; i < n; ++i) {
int c = getchar() - 'a';
if (hope[c] - i > k)
k = hope[c] - i;
hope[c] = n + i;
}
printf("%d\n", k);
}
return 0;
}

5
codechef/watscore.py Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env python3
print(*(sum(dict(sorted((int(p), p in '12345678' and int(s))
for p, s in (input().split()
for i in range(int(input()))))).values())
for t in range(int(input()))), sep='\n')