[PYTHON] Sumitomo Mitsui Trust Bank Programming Contest 2019 Review

It's been quite late to review, but I've solved all of them, so I'd like to write an article. I did four completes, but my impression was that I wanted to complete five because EF (especially F) was an easy time.

Problem A

answerA.py


m1,d1=map(int,input().split())
m2,d2=map(int,input().split())
if m1==m2:
    print(0)
else:
    print(1)

It just determines if m1 and m2 are different.

B problem

answerB.py


x=[]
for i in range(1,50001):
    x.append(int(i*1.08))

n=int(input())
for i in range(50000):
    if n==x[i]:
        print(i+1)
        break
else:
    print(":(")

The problem statement was a little long, so I took a little too much time. The output is changed depending on whether or not there is a corresponding number by packing all possible numbers in the first array x. Python is convenient because it is easy to write by using the else statement when changing the output depending on whether to break or not.

C problem

answerC.py


x=int(input())
y=x//100
z=x%100
if z<=y*5:
    print(1)
else:
    print(0)

Since it is in the form of 100 + k (integer of 0 <= k <= 5), you can see that you can buy up to x // 100 items. (It is necessary to maximize it because we want to maximize the range of the expression in the following sentence.) At this time, if x% 100 is within 0 ~ (x // 100) * 5, it will be exactly X yen. Since it can be done, it will be output according to whether it can be done or not. (I used my head more than the usual C problem, but I was surprised that a good number of people passed through.)

D problem

answerD.py


n=int(input())
s=[int(i) for i in input()]

def my_index(l,x,ind):
    '''
    l-Iterable object
    x-Value you want to find
    ind-Index you want to start looking for
Return value-Index if it exists, if it does not exist-1
    '''
    global n
    if ind==n:
        return -1
    for i in range(ind,n):
        if l[i]==x:
            return i
    else:
        return -1

c=0
for i in range(10):
    x1=my_index(s,i,0)
    if x1!=-1:
        for j in range(10):
            x2=my_index(s,j,x1+1)
            if x2!=-1:
                for k in range(10):
                    if my_index(s,k,x2+1)!=-1:
                        c+=1
print(c)

I was confused by N, but in the end there are only 10 * 10 * 10 3 digits, so I can see that all I have to do is check all of them. However, I created my_index function myself because using Python's index function will throw an error if the element doesn't exist in that object. Once you know this, you can find out if there are 10 * 10 * 10 different PINs in order from the front.

Postscript (12/11)

If you handle s as a character string without listing it, you can substitute it with the find function without preparing the my_index function.

E problem

answerE.py


#Counting from the front, what happens is uniquely determined
import collections

n=int(input())
b=[[0,0,0]]
a=[int(i) for i in input().split()]

def my_index(l, x):
    if x in l:
        return l.index(x)
    else:
        return -1

for i in range(n):
    k=my_index(b[-1],a[i])
    if k==-1:
        print(0)
        break
    b.append([b[-1][j]+1 if j==k else b[-1][j] for j in range(3)])
else:
    x=1
    for i in range(n):
        x=x*b[i].count(a[i])%1000000007
    print(x)

The impression that E was the most difficult. (I solved it after seeing the answer.) If the number of people wearing hats of each color is set as ai, bi, ci, and xi, yi, zi are set in descending order of the number of people, the remarks of the 1st to ith people are used as the basis. This makes it possible to uniquely determine the values of xi, yi, and zi. (For details, see Answer. You can find out by moving your hand.) The array b contains the combinations of xi, yi, and zi up to the i-th person (at the 1st origin), and by moving i from 0 to n-1, the combination is obtained for each i. Here, if the number of people (a [i]) mentioned in the i + 1th statement exists in b [i], the number of hats of each color in the i + 1th statement and the ith person is Although it can be said that there is no contradiction, the element of the corresponding number of people is incremented by +1 and can be b [i + 1]. However, there is a case (k = -1) where the number of hats of each color up to the i-1st person is inconsistent with the remark of the i-th person, so in that case 0 is output and break is performed. Up to this point, the number of color hats for each i is calculated, but if there are multiple a [i] in b [i], there are multiple colors mentioned in the i + 1th statement. The answer can be found by multiplying the possibilities by a [i] in each b [i].

F problem

answerF.py


import math
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
#Relative velocity
c1,c2=a1-b1,a2-b2
#Distance away
d1,d2=abs(c1*t1),-abs(c2*t2)

if (c1>0 and c2>0) or (c1<0 and c2<0) or d1+d2>0:
    print(0)
elif d1+d2==0:
    print("infinity")
else:
    x=d1//(-(d1+d2))
    y=d1%(-(d1+d2))
    if y==0:
        print(2*x)
    else:
        print(2*x+1)

I didn't see any problems during the contest, but I was very disappointed because it was surprisingly easy to solve after the contest. Since we will meet each other, we should consider the relative position, and first find the relative velocity (c1 and c2). From here, we will branch off the conditions. IMG_0089.PNG

First, considering patterns that are not finite times, there are the above four patterns. When c1 and c2 are both positive (pattern ①) and c1 and c2 are both negative (pattern ②), they are always separated and will not meet. Even if the displacement of the relative position in T1 time is larger than the displacement of the relative position in T2 time (Pattern ③), they will always be separated from each other, so they will not meet. Also, if the displacement of the relative position in T1 time is the same as the displacement of the relative position in T2 time (pattern ④), the relative position does not change in one cycle of T1 and T2, so it continues infinitely.

Finally, consider the case of meeting a finite number of times. At this time, the distance between T1 and T2 is different for positive and negative, so take the absolute value and make it easy to think of it as positive for T1 and negative for T2. (The rest of the explanation will be handwritten.) IMG_0090.PNG As mentioned above, I was able to count the number of encounters in all cases.

Summary

For the time being, let's take a look at the last problem. Make a firm judgment as to whether or not it can be solved. (Sometimes I drop it without seeing a problem that seems to be solvable, and sometimes I try to solve an apparently difficult problem.) It's the same as mathematics for taking an examination, thinking carefully even if you are impatient.

Recommended Posts

Sumitomo Mitsui Trust Bank Programming Contest 2019 Review
AtCoder Sumitomo Mitsui Trust Bank Programming Contest 2019 Participation Report
[Python] Sumitomo Mitsui Trust Bank Programming Contest 2019 C (How to use DP) [AtCoder]
Keyence Programming Contest 2020 Review
NOMURA Programming Contest 2020 Review
HHKB Programming Contest 2020 Review
Tokio Marine & Nichido Programming Contest 2020 Review
yukicoder contest 259 Review
yukicoder contest 264 Review
Acing Programming Contest 2020
yukicoder contest 261 Review
yukicoder contest 267 Review
HHKB Programming Contest 2020
yukicoder contest 266 Review
yukicoder contest 263 Review
yukicoder contest 268 Review
AtCoder Beginner Contest 152 Review
AtCoder Regular Contest 105 Review
AtCoder Beginner Contest 160 Review
AtCoder Beginner Contest 178 Review
AtCoder Beginner Contest 166 Review
AtCoder Beginner Contest 167 Review
AtCoder Beginner Contest 164 Review
AtCoder Beginner Contest 169 Review
AtCoder Grand Contest 048 Review
After "Diverta 2019 Programming Contest"
AtCoder Beginner Contest 171 Review
AtCoder Beginner Contest 182 Review
AtCoder Beginner Contest 180 Review
AtCoder Beginner Contest 177 Review
AtCoder Beginner Contest 168 Review
AtCoder Grand Contest 045 Review
Keyence Programming Contest 2021 Notes
AtCoder Grand Contest 044 Review
AtCoder Beginner Contest 179 Review
AtCoder Beginner Contest 172 Review
AtCoder Regular Contest 106 Review
AtCoder Beginner Contest 176 Review
AtCoder Grand Contest 046 Review
AtCoder Beginner Contest 175 Review
AtCoder Beginner Contest 174 Review
AtCoder Beginner Contest 153 Review
AtCoder Beginner Contest 156 Review
AtCoder Beginner Contest 161 Review
AtCoder Beginner Contest 170 Review
AtCoder Regular Contest 104 Review
AtCoder Beginner Contest 165 Review
AtCoder Beginner Contest 173 Review
AtCoder Beginner Contest 155 Review
AtCoder Beginner Contest 162 Review