[PYTHON] ABC164

I participated in AtCorder Beginner Contest 164. It was ABC's 3 questions AC. I'm using Python.

A problem

When the wolf W is more than the sheep S, the sheep will be attacked by the wolf, so it is unsafe.

S, W = map(int,input().split()
if S <= W:
    print("unsafe")
else:
    print("safe")

B problem

Let t be the number of turns required for Takahashi to have 0 physical strength, and a be the number of turns required for Aoki to have 0 physical strength. Takahashi wins when Aoki's physical strength becomes 0 first, so t is greater than a. Even if it takes the same turn to reach 0 health, Takahashi will win first.

A, B, C, D = map(int,input().split())
import math
t = math.ceil(A / D)
a = math.ceil(C / B)
if t >= a:
    print("Yes")
else:
    print("No")

C problem

Store the obtained prizes in List S. Create a list without duplicates and find the number of elements.

N = int(input())
S =[input() for i in range(N)]
import collections
c = collections.Counter(S)
print(len(c))

D problem

Find all multiples of 2019 between 1 and 200,000 and put them in the list baisuu. We will search in order as if it contains 2019 → 4038. However, if the same number is included at the beginning and end, such as 18171 (= 2019 * 9), it was not possible to confirm that 181718171 contains two 18171.

S = input()
import re
baisuu = [2019 * i for i in range(200000 // 2019)]
count = [len(re.findall(str(i), str(S)))
         for i in baisuu if str(i) in str(S)]

A multiple of 2019 means that the remainder divided by 2019 is 0. Rather than moving both when specifying the range, the amount of calculation can be reduced by fixing it to the right end.

As a good example, consider whether S = 12114 is divisible by 2019. Find the remainder while fixing the right edge and shifting the left edge one by one. 1 → 1 41 → 41 141 → 141 1141 → 1141 21141 → 951 121141 → 1 The remainders match when they are 1 digit and when they are 6 digits. This indicates that the number of digits 6 to 2 (1 + 1) is a multiple of 2119.

a = 2019 * A + y b = 2019 * B + y a - b = 2019 * (A * B) a --b is 10 to the nth power. Because 10 and 2019 are relatively prime, a --b is a multiple of 2019.

Therefore, by calculating all the remainders and counting the number of duplicates, it is possible to obtain a number that is a multiple of 2019. Furthermore, if you divide by 2019 and the remainder becomes 0, it will be the case of 2019 even if there is no duplication. The answer below is TLE.

S = list(input())
S.reverse()
amari = []
A = 0
c = 0
for i in range(len(S)):
    a = (int(A) + int(S[i]) *(10 ** i)) % 2019
    amari.append(a)
    if a == 0:
        c += 1
    A = a
print(c + len(amari) - len(set(amari)))

Recommended Posts

ABC164
ABC174
ABC175
ABC170
ABC182
ABC153
ABC146 Impressions
AtCoder ABC176
ABC167 WriteUp
AtCoder ABC177
Beginner ABC154 (Python)
Beginner ABC156 (Python)
abc154 participation report
abc155 participation report
AtCoder ABC 174 Python
AtCoder ABC187 Python
AtCoder ABC188 Python
Beginner ABC155 (Python)
Beginner ABC157 (Python)
AtCoder ABC 175 Python
Looking back on ABC155
Atcoder ABC115 Past Exercises
Solve ABC169 in Python
ABC147 C --HonestOrUnkind2 [Python]