about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xcodechef/cash.py4
-rwxr-xr-xcodechef/chefrail.py48
-rwxr-xr-xcodechef/longcook.py20
-rwxr-xr-xcodechef/nochange.lua36
-rwxr-xr-xcodechef/snug-fit.py5
-rw-r--r--codechef/theatre.cc54
6 files changed, 167 insertions, 0 deletions
diff --git a/codechef/cash.py b/codechef/cash.py
new file mode 100755
index 0000000..b9463fc
--- /dev/null
+++ b/codechef/cash.py
@@ -0,0 +1,4 @@
+#!/usr/bin/env python
+for t in range(int(input())):
+    n, k = map(int, input().split())
+    print(sum(map(int, input().split())) % k)
diff --git a/codechef/chefrail.py b/codechef/chefrail.py
new file mode 100755
index 0000000..d70a637
--- /dev/null
+++ b/codechef/chefrail.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+from itertools import count, takewhile
+from typing import Iterator, Set
+
+
+def binary(i: int, h: int) -> Iterator[int]:
+    while h % i == 0:
+        yield i
+        i <<= 1
+
+
+def rail(x: Set[int], y: Set[int]) -> int:
+    result = 0
+    for h in map(abs, x):
+        result += -h in y and h in y
+        if h < 2: continue
+
+        k, z = h, list(takewhile(lambda j: j < h, binary(1, h*h)))
+        while k % 2 == 0: k >>= 1
+        for i in takewhile(lambda j: j*j <= k, count(3, 2)):
+            start, stop = 0, len(z)
+            while k % i == 0:
+                k //= i
+                z.extend(i*j for j in z[start:stop] if i*j < h)
+                start, stop = stop, len(z)
+                z.extend(i*j for j in z[start:stop] if i*j < h)
+                start, stop = stop, len(z)
+        if k > 1:
+            start, stop = 0, len(z)
+            z.extend(k*j for j in z[start:stop] if k*j < h)
+            start, stop = stop, len(z)
+            z.extend(k*j for j in z[start:stop] if k*j < h)
+
+        for i in z:
+            j = h * h // i
+            result += (-i in y and j in y) + (i in y and -j in y)
+    return result
+
+
+for t in range(int(input())):
+    input()
+    x = set(map(int, input().split()))
+    y = set(map(int, input().split()))
+
+    zero = 0 in x or 0 in y
+    if 0 in x: x.remove(0)
+    if 0 in y: y.remove(0)
+    print((zero and len(x)*len(y)) + rail(x, y) + rail(y, x))
diff --git a/codechef/longcook.py b/codechef/longcook.py
new file mode 100755
index 0000000..d57d9f2
--- /dev/null
+++ b/codechef/longcook.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+from calendar import isleap
+from datetime import datetime
+
+OVERLAP = [datetime(i or 400, 2, 7).weekday()
+           in ([4] if isleap(i or 400) else [4, 5]) for i in range(400)]
+OOP = sum(OVERLAP)
+
+
+def longcook(start, end):
+    delta = end - start
+    if delta <= 0: return 0
+    return delta//400*OOP + sum(OVERLAP[y % 400]
+                                for y in range(start, start + delta%400))
+
+
+for t in range(int(input())):
+    m1, y1 = map(int, input().split())
+    m2, y2 = map(int, input().split())
+    print(longcook(y1+(m1>2), y2+(m2!=1)))
diff --git a/codechef/nochange.lua b/codechef/nochange.lua
new file mode 100755
index 0000000..818959c
--- /dev/null
+++ b/codechef/nochange.lua
@@ -0,0 +1,36 @@
+#!/usr/bin/env lua
+local read = io.read
+local print = print
+local table = {concat = table.concat, insert = table.insert}
+
+for t = 1, read('*n') do
+  local n, p = read('*n', '*n')
+  local d = {}
+  for i = 1, n do table.insert(d, read('*n')) end
+
+  local unanswered, previous = true, 1
+  for k, v in ipairs(d) do
+    if p % v ~= 0 then
+      local c = {}
+      for i = 1, k-1 do table.insert(c, 0) end
+      table.insert(c, p//v+1)
+      for i = k+1, n do table.insert(c, 0) end
+      print(('YES %s'):format(table.concat(c, ' ')))
+      unanswered = false
+      break
+    end
+
+    if v % previous ~= 0 then
+      local c = {}
+      for i = 1, k-2 do table.insert(c, 0) end
+      table.insert(c, v//previous+1)
+      table.insert(c, p//v-1)
+      for i = k+1, n do table.insert(c, 0) end
+      print(('YES %s'):format(table.concat(c, ' ')))
+      unanswered = false
+      break
+    end
+    previous = v
+  end
+  if unanswered then print('NO') end
+end
diff --git a/codechef/snug-fit.py b/codechef/snug-fit.py
new file mode 100755
index 0000000..58e1700
--- /dev/null
+++ b/codechef/snug-fit.py
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+for t in range(int(input())):
+    input()
+    print(sum(map(min, sorted(map(int, input().split())),
+                  sorted(map(int, input().split())))))
diff --git a/codechef/theatre.cc b/codechef/theatre.cc
new file mode 100644
index 0000000..ea576f8
--- /dev/null
+++ b/codechef/theatre.cc
@@ -0,0 +1,54 @@
+#include <algorithm>
+#include <iostream>
+#include <map>
+#include <string>
+#include <vector>
+
+using namespace std;
+
+int
+main()
+{
+  int k, total_profit {0};
+  string movies {"ABCD"};
+  vector<int> showtimes {3, 6, 9, 12};
+  vector<int> prices {25, 50, 75, 100};
+
+  cin >> k;
+  while (k--)
+    {
+      map<char, map<int, int>> requests;
+      int n, t, profit {-400};
+      char m;
+
+      cin >> n;
+      while (n--)
+        {
+          cin >> m >> t;
+          requests[m][t]++;
+        }
+
+      for (int i = 0; i < 24; ++i)
+        {
+          for (int j = 0; j < 24; ++j)
+            {
+              vector<int> tickets {requests[movies[0]][showtimes[0]],
+                                   requests[movies[1]][showtimes[1]],
+                                   requests[movies[2]][showtimes[2]],
+                                   requests[movies[3]][showtimes[3]]};
+              int p {0};
+
+              sort (tickets.begin(), tickets.end());
+              for (int l = 0; l < 4; ++l)
+                p += tickets[l] ? tickets[l]*prices[l] : -100;
+              profit = max (p, profit);
+              next_permutation (showtimes.begin(), showtimes.end());
+            }
+          next_permutation (movies.begin(), movies.end());
+        }
+
+      cout << profit << endl;
+      total_profit += profit;
+    }
+  cout << total_profit << endl;
+}