about summary refs log tree commit diff
path: root/codechef
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-12-16 21:13:07 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-12-16 21:13:07 +0700
commitc1008fe39217be7f91f0ea23483e747bfbc5743e (patch)
treec4f921bccd9e8d66e3aadd9a01ede3300d73afa6 /codechef
parent8a9d6282fcb863c67d6623f5c883ef703721cccd (diff)
downloadcp-c1008fe39217be7f91f0ea23483e747bfbc5743e.tar.gz
The good, the bad and the ugly
Diffstat (limited to 'codechef')
-rw-r--r--codechef/binadd.c51
-rwxr-xr-xcodechef/binadd.py9
-rwxr-xr-xcodechef/binxor.py28
-rw-r--r--codechef/plmu.c22
-rw-r--r--codechef/subsplay.c21
-rwxr-xr-xcodechef/watscore.py5
6 files changed, 136 insertions, 0 deletions
diff --git a/codechef/binadd.c b/codechef/binadd.c
new file mode 100644
index 0000000..8a2f56f
--- /dev/null
+++ b/codechef/binadd.c
@@ -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;
+}
diff --git a/codechef/binadd.py b/codechef/binadd.py
new file mode 100755
index 0000000..3ab0bab
--- /dev/null
+++ b/codechef/binadd.py
@@ -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)
diff --git a/codechef/binxor.py b/codechef/binxor.py
new file mode 100755
index 0000000..6f87d56
--- /dev/null
+++ b/codechef/binxor.py
@@ -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))))
diff --git a/codechef/plmu.c b/codechef/plmu.c
new file mode 100644
index 0000000..04c4b0e
--- /dev/null
+++ b/codechef/plmu.c
@@ -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;
+}
diff --git a/codechef/subsplay.c b/codechef/subsplay.c
new file mode 100644
index 0000000..691a04e
--- /dev/null
+++ b/codechef/subsplay.c
@@ -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;
+}
diff --git a/codechef/watscore.py b/codechef/watscore.py
new file mode 100755
index 0000000..d45faf2
--- /dev/null
+++ b/codechef/watscore.py
@@ -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')