Here is the link to the contest. All problems are from Codeforces' problemset.
A. Balanced Subsequence
First we need to find the frequencies of the first $$$k$$$ alphabets in the string. Let the minimum frequency among these frequencies be $$$m$$$. Then we cannot select $$$m + 1$$$ characters of one kind, and we can definitely select $$$m$$$ characters of each kind, hence the answer is given by min(frequency of first k characters) * $$$k$$$.
import sys
from collections import Counter
input = lambda: sys.stdin.readline().rstrip()
n, k = map(int, input().split())
s = input()
count = Counter(s)
ans = float("inf")
for i in range(k):
char = chr(i + ord('A'))
ans = min(ans, count[char])
ans *= k
print(ans)
B. Tzuyu and Sana
C. Yet Another Monocarp's Problem
Let's find out the total damage for a fixed value of $$$k$$$. Since the effect of the poison from the $$$i$$$-th attack deals damage $$$min(k,a_{i+1}−a_i)$$$ seconds for $$$i<n$$$ and $$$k$$$ seconds for $$$i=n$$$, then the total damage is $$$k + \sum_{i=1}^{n-1} \min(k, a_{i+1} - a_i)$$$. We can see that the higher the value of $$$k$$$, the greater the total sum. So we can do a binary search on $$$k$$$ and find the minimum value when the sum is greater than or equal to $$$h$$$.
import sys
def solve():
n, h = map(int, sys.stdin.readline().strip().split())
a = list(map(int, sys.stdin.readline().strip().split()))
def checker(k):
cur = k
for i in range(n - 1):
cur += min(k, a[i + 1] - a[i])
return cur >= h
low = 1
high = h
while low <= high:
mid = (low + high)//2
if checker(mid):
high = mid - 1
else:
low = mid + 1
return low
t = int(sys.stdin.readline().strip())
for _ in range(t):
print(solve())
D. Soldiers
Firstly we have to note that the second soldier should choose only prime numbers. If he choose a composite number $$$x$$$ that is equal to $$$p \cdot q$$$, he can choose first $$$p$$$, then $$$q$$$ and get a better score. So our task is to find a number of prime factors in factorization of $$$n$$$. Now we have to note the factorization of number $$$a! / b!$$$ is this same as factorization of numbers $$$(b + 1)*(b + 2)*...*(a - 1)*a$$$. Let's count the number of prime factors in the factorization of every number from $$$2$$$ to $$$5000000$$$.
First, we use Sieve of Eratosthenes to find a prime divisor of each of these numbers. Then we can calculate a number of prime factors in factorization of a using the formula: $$$primeFactors[a] = primeFactors[a / primeDivisor[a]] + 1$$$ When we know all these numbers, we can use a prefix sum, and then answer for sum on interval.
import sys
input = sys.stdin.readline
dp = [0] * 5000005
def solve():
n, m = map(int, input().split())
print(dp[n] - dp[m])
def sieve(n):
primes = [True] * (n + 1)
for i in range(2, n + 1):
if not primes[i]:
continue
num = i * 2
while num <= n:
primes[num] = False
num += i
ans = [i for i in range(2, n + 1) if primes[i]]
return ans
def preCompute():
ans = sieve(5000000)
for a in ans:
dp[a] = 1
q = int(input())
for i in range(2, 5000000):
if dp[i]:
continue
d = 0
while ans[d] * ans[d] <= i:
if i % ans[d] == 0:
dp[i] = dp[i // ans[d]] + 1
break
d += 1
for i in range(1, 5000000):
dp[i] += dp[i - 1]
for i in range(q):
solve()
if __name__ == "__main__":
preCompute()
E. LCM Game
It is a simple problem, but many competitors used some wrong guesses and failed. First of all, we should check if $$$n$$$ is at most $$$3$$$ and then we can simply output $$$1,2,6$$$.
Now there are two cases: When n is odd, the answer is obviously $$$n(n-1)(n-2)$$$. When n is even, we can still get at least $$$(n-1)(n-2)(n-3)$$$, so these three numbers in the optimal answer would not be very small compared to $$$n$$$. So we can just iterate every $$$3$$$ number triple in $$$[n-50,n]$$$ and update the answer.
from math import lcm
N = int(input())
ans = 0
if N >= 3:
ans = lcm(N, N - 1, N - 2)
if N >= 4:
ans = lcm(N - 1, N - 2, N - 3)
min_n = max(1, N - 50)
for i in range(min_n, N + 1):
for j in range(min_n, N + 1):
for k in range(min_n, N + 1):
ans = max(ans, lcm(i, j, k))
print(ans)