[Answer example (python3)] ABS (AtCoder Beginners Selection) of atcoder

Introduction

I haven't done competitive programming lately, so I wanted to solve a problem as simple as a brain teaser.

I have already solved ABC (AtCoder Beginner Contest) of AtCoder, Speaking of which, I hadn't done a collection of questions for AtCoder beginners called ABS (AtCoder Beginners Selection), so I took this opportunity to solve all 10 questions. ..

PracticeA: Welcome to AtCoder Just answer according to the question sentence! There is no problem if simple input / output processing can be performed.

a = int(input())
b,c = map(int, input().split())
s = str(input())
 
print(a+b+c, s)

ABC086A: Product In the same way, answer according to the question sentence.

a,b = map(int, input().split())
 
sum = a*b
if sum % 2 ==0:
    print("Even")
else:
    print("Odd")

ABC081A: Placing Marbles Similarly, answer according to the question text.

s = str(input())
 
print(s.count('1'))

ABC081B: Shift only I rushed into the B problem of ABC. I will use my head a little from this area. When you understand what to look for from the problem statement, All you have to do is factor each $ A_1, \ dots, A_N $ into prime factors and find the smallest powerful number of 2. I implemented it with reference to here.

import collections
 
 
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
 
n = int(input())
l = list(map(int, input().split()))
 
prime_list = [ dict(collections.Counter(prime_factorize(i)).items()) for i in l ]
try:
    count_2_list = [ d[2] for d in prime_list ]
    print(min(count_2_list))
except KeyError:
    print(0)

The code is getting longer. Since it asks for the power of 2, it was smarter to convert it to a binary number and process it. Reference) https://qiita.com/watyanabe164/items/5c26fd9d8d244c5f2483

ABC087B: Coins Since A, B, and C were 50 or less, I simply searched all.

def equal_x_count(p,q,r,z):
    count = 0
    for i in range(p+1):
        for j in range(q+1):
            for k in range(r+1):
                if 500*i + 100*j + 50*k == z:
                   count += 1
    return count
 
 
a = int(input())
b = int(input())
c = int(input())
x = int(input())
 
print(equal_x_count(a,b,c,x))

ABC083B: Some Sums Convert n assigned as a numeric type to a string type, The character string extracted for each digit was digitized again and processed.

n, a, b = map(int, input().split())
 
count = 0
for i in range(n+1):
    sum_each_digit = sum([ int(j) for j in str(i)])
    if a <= sum_each_digit and sum_each_digit <= b:
        count += i
 
print(count)

ABC088B: Card Game for Two Assign input to the list and sort the list in descending order Add even numbers (0,2,4), subtract odd numbers (1,3,5)

n = int(input())
a = list(map(int, input().split()))
 
sorted_a = sorted(a, reverse=True)
alice_list = []
bob_list = []
 
for i in range(n):
    if i % 2 == 0:
        alice_list.append(sorted_a[i])
    else:
        bob_list.append(sorted_a[i])
 
d = sum(alice_list) - sum(bob_list)
 
print(d)

ABC085B: Kagami Mochi It's easy if you convert what you get in the list into a set and count the number of elements.

n = int(input())
d = [int(input()) for _ in range(n)]
 
print(len(set(d)))

ABC085C: Otoshidama All you have to do is search through the entire search.

n, y = map(int, input().split())
 
for i in range(n+1):
    for j in range(n-i+1):
        if 10000*i + 5000*j + 1000*(n-i-j) == y:
            print(i,j,n-i-j)
            exit()
print(-1,-1,-1)

ABC049C: Daydream

Pay attention to the end of the input S, cut off the end if there is a match, and put S in it. Repeat this.

s = str(input())
 
while True:
    if s[-5:] == 'dream' or s[-5:] == 'erase':
        s = s[:-5]
    elif s[-6:] == 'eraser':
        s = s[:-6]
    elif s[-7:] == 'dreamer':
        s = s[:-7]
    elif s == '':
        print('YES')
        break
    else:
        print('NO')
        break

ABC086C: Traveling If you were at the coordinates ($ x, $ y) at time $ t_n, you can see if you can move to the coordinates ($ x', $ y') at time $ t_ (n + 1). Let's consider the case of 1 <= t <= n.

--Is there enough time to move to the coordinates ($ x', $ y')? -Is the shortest time that can be moved the same as the travel time? ――If you consume two times, it will return to the original position.

If you read the above prerequisites from the problem statement, you can implement it with the following code.

import numpy as np
import sys
 
n = int(input())
l = [ list(map(int, input().split())) for _ in range(n)]
 
l.insert(0, [0,0,0])
 
for i in range(n):
    d = np.array(l[i+1]) - np.array(l[i])
    time, move = d[0], abs(d[1])+abs(d[2])
    if move > time or (time - move) % 2 != 0:
        print('No')
        sys.exit()
 
print('Yes')

Thanks to the implementation of numpy in AtCoder, it was relatively easy to implement.

Finally

ABS can answer well without any algorithmic knowledge. The above is an example of the answer, so if you want to code smarter, please check other model answers.

Recommended Posts

[Answer example (python3)] ABS (AtCoder Beginners Selection) of atcoder
Answer to AtCoder Beginners Selection by Python3
AtCoder Beginners Selection memorandum
AtCoder Beginners Selection Experience (2nd time)
# 2 Python beginners challenge AtCoder! ABC085C --Otoshidama
Memorandum of beginners Python "isdigit" movement
Learn the basics of Python ① Beginners
[Python] Minutes of study meeting for beginners (7/15)
atCoder 173 Python
Memorandum of python beginners About inclusion notation
Example of 3D skeleton analysis by Python
[Python] Solve 10 past elite problems of Atcoder
Atcoder standard input set for beginners (python)
An example of the answer to the reference question of the study session. In python.
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Example of taking Python> function> * args as arguments
The answer of "1/2" is different between python2 and 3
[Python] 90 degree clockwise rotation, 90 degree counterclockwise rotation, 180 degree rotation of matrix [AtCoder]
Easy understanding of Python for & arrays (for super beginners)
Example of reading and writing CSV with Python
Basic story of inheritance in Python (for beginners)
Introduction of Python
AtCoder ABC187 Python
Basics of Python ①
AtCoder ABC188 Python
Basics of python ①
Beginners practice Python
Python beginner's note
AtCoder ABC 175 Python
Introduction of Python
python beginners tried to predict the number of criminals
[Language processing 100 knocks 2020] Summary of answer examples by Python
Implementation example of simple LISP processing system (Python version)
It turned blue after 9 months of Atcoder history (python)
Ruby, Python code fragment execution of selection in Emacs
Reproduce the execution example of Chapter 4 of Hajipata in Python
Review of atcoder ABC158, up to question E (Python)
Reproduce the execution example of Chapter 5 of Hajipata in Python