Review of AtCoder Beginner Contest 167, up to question E (Python)

This is a review article for beginners of competition professionals.

The solution I write here is written while looking at the commentary and other people's submissions. It may not be what you actually submitted.

A - Registration

The string T is the question of answering whether the string S has a single character at the end.

We compared by slicing T with T [: -1].

S = input()
T = input()

if S == T[:-1]:
  print('Yes')
else:
  print('No')

B - Easy Linear Programming

There are $ A $ for cards labeled 1, $ B $ for cards labeled 0, and $ C $ for cards labeled -1. The maximum value when choosing $ K $ from this is a question to answer.

Priority should be given to counting in the order of 1, 0, -1. However, since it is $ A, B, C \ leq2 \ times10 ^ 9 $, it will hurt if you try to put it in an array or turn it with for. (One loss)

A, B, C, K = map(int, input().split())
out = min(A, K) - max(0, K-A-B)
print(out)

C - Skill Up

The problem is to find the lowest price in a combination where all the elements summed for each number exceed $ X $.

The limit of $ N, M \ leq 12 $ allows for full coverage. Parallel computing was done by using ʻitertools.product ()and thennumpy` for the full search.

import itertools
import numpy as np

N, M, X = map(int, input().split())
items = np.array([list(map(int, input().split())) for _ in range(N)], dtype=np.int32)

out = 10**18
for c in itertools.product([False, True], repeat=N):
    tmp = np.sum(items[np.where(c)], axis=0)
    if M == np.count_nonzero(tmp[1:] >= X):
      out = min(out, tmp[0])

if out == 10**18:
  print(-1)
else:
  print(out) 

D - Teleporter

Each town has one warp destination. The question is to teleport K times from the first town and answer the town to arrive.

If you search properly from the limit of $ K \ leq10 ^ {18} $, it is TLE. I gave up. See other people's answers.

It should be noted that the range of K is $ K \ leq 10 ^ {18} $, but the range of N is $ N \ leq 2 \ times 10 ^ {5} $. In other words, this search always loops.

First, search normally. Save the time t_zero that first arrived at each town as an array, and calculate the time for one lap when you reach the same town a second time. Take the remainder term of the time of one lap from K. Reuse the previously done calculations without having to search again for the remaining time k. I passed with the following code.

N, K = map(int, input().split())
A = list(map(int, input().split()))

t_zero = [-1] * N

t = 0
a = 1

while t_zero[a-1] == -1:
  if t == K:
    break
  t_zero[a-1] = t
  t += 1
  a = A[a-1]
else:
  k = (K - t_zero[a-1]) % (t - t_zero[a-1])
  a = t_zero.index(t_zero[a-1] + k) + 1
print(a)

E - Colorful Blocks

It is a problem to color-code the blocks arranged in a horizontal row. However, the same color can be allowed to be next to each other up to K, and how many combinations are there?

There are $ M \ times (M-1) ^ {N-1} $ combinations in which N colors are arranged so that the M colors are not next to each other.

When K group blocks can be next to each other, it can be said that $ N-K $ blocks are arranged so that the colors do not line up. Also, how to select the combination of adjacent blocks is $ _ {N-1} C _ {K} $ pieces.

In other words, you can calculate the following formula.

\sum M\times (M-1)^{N-k-1} \times _{N-1} C _ k

The implementation is as follows. However, during the production, I could not speed up the calculation well and could not solve it in time.

N, M, K = map(int, input().split())
out = 0
mod = 998244353

c = 1

for k in range(K+1):
  out += M * pow(M-1, N-k-1, mod) * c
  out %= mod
  c *= (N-k-1) * pow(k+1, mod-2, mod)
  c %= mod
print(out)

here $ Y^{p-2} \mod p \equiv {1\over Y} $ (A variant of Fermat's Little Theorem) is used to speed up the calculation of $ Y ^ {-1} $.

That's all for this article.

Recommended Posts

Review of AtCoder Beginner Contest 159, up to question E (Python)
Review of AtCoder Beginner Contest 163, up to question E (Python)
Review of AtCoder Beginner Contest 164, up to question E (Python)
Review of AtCoder Beginner Contest 162, up to question E (Python)
Review of AtCoder Beginner Contest 154, up to question E (Python)
Review of AtCoder Beginner Contest 160, up to question E (Python)
Review of AtCoder Beginner Contest 167, up to question E (Python)
Review of AtCoder Beginner Contest 157, up to question E (Python)
Review of AtCoder Beginner Contest 161, up to question E (Python)
Review of AtCoder Beginner Contest 155, up to question E (Python)
Review of AtCoder Beginner Contest 156, up to question E (Python)
Review of AtCoder Beginner Contest 166, up to question E (Python)
Review of AtCoder Beginner Contest 165, up to question E (Python)
atcoder Review of Panasonic Programming Contest 2020, up to question E (Python)
Review of atcoder ABC158, up to question E (Python)
AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 062 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 117 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 076 Review of past questions
AtCoder Beginner Contest 089 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
AtCoder Beginner Contest 047 Review of past questions
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 083 Review of past questions
AtCoder Beginner Contest 048 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 Review of past questions
AtCoder Beginner Contest 088 Review of past questions
AtCoder Beginner Contest 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions
AtCoder Beginner Contest 053 Review of past questions