[PYTHON] AtCoder Beginner Contest 105 Review of past questions

Time required

スクリーンショット 2020-05-16 13.48.21.png

Impressions

I couldn't finish it until the end due to some errands, but I think it was a set that could be completed quickly even if I didn't have time if I was calm. It would be nice to be able to remove it firmly when using such a set.

Problem A

If you reduce the difference in the number of rice crackers, the difference will be at most one (I will not prove it, but I think it is obvious). Therefore, consider the case where the difference is 0, but in this case n is divisible by k. If this is output, it will be as follows.

answerA.py


n,k=map(int,input().split())
print(1 if n%k!=0 else 0)

B problem

Search for possible combinations where n is not large. Assuming how many $ 7 donuts you bought, you can just buy a $ 4 cake with the rest of the money.

answerB.py


n=int(input())
for i in range(n//7+1):
    if (n-i*7)%4==0:
        print("Yes")
        break
else:
    print("No")

C problem

I shouldn't have seen the problem and had the impression that I should do it properly. I want to remember the basics of ** thinking carefully and then solving **. In the answer, I solve it by using ** it is uniquely determined by deciding from the lower digit ** (I did not notice at all ...), but I ** conversely decide from the upper digit and solve it. It was **.

When deciding from the upper digits, I noticed that the range of numbers that can be represented narrows. That is,|S_0 \times (-2)^0+S_1 \times (-2)^1+…+S_{k-1} \times (-2)^{k-1}|<|S_k \times (-2)^k|Is true.

Except for patterns where this does not hold, the answer is that when you decide from the upper digits and add each digit, it will eventually become equal to n, so conversely subtract each digit from n to make it 0. I thought about it.

By doing this, the absolute value when the kth digit is decided is|S_k \times (-2)^k|The pattern should be the one excluding the above cases.

There are multiple candidates, so ** you can save the candidates when you have decided up to a certain digit with deque and search for the answer with BFS **.

Also, at this time, the specific amount of calculation is troublesome, so I have not calculated it, but since it can be clearly omitted by pruning, I implemented it without calculating it.

answerC.py


from itertools import dropwhile
from collections import deque
n=int(input())
ans=deque([["",n]])
def bfs(d):
    global ans
    l=len(ans)
    for i in range(l):
        x=ans.popleft()
        new1=[x[0]+"0",x[1]]
        if abs(new1[1])<abs((-2)**d):
            ans.append(new1)
        new2=[x[0]+"1",x[1]-(-2)**d]
        if abs(new2[1])<abs((-2)**d):
            ans.append(new2)
    if d!=0:
        bfs(d-1)
bfs(32)

for i in ans:
    if i[1]==0:
        ans=list(dropwhile(lambda x:x=="0",i[0]))
        if len(ans)==0:
            print(0)
        else:
            print("".join(ans))
        break

D problem

I think it's a similar issue to the one that came up at ABC the other day (I forgot which one).

You will be asked if the ** continuous part ** is a multiple of M, so it seems good to consider whether the subtraction of the cumulative sum is a multiple of M ** (** If you calculate the interval many times) Is the cumulative sum, if it seems impossible, BIT or seg tree **). That is, when (l, r) is given as a set, $ S [l, m] = A_l + A_ {l + 1} +… + A_m $, and $ S [l, m] = S [1, m ] -S [1, l-1] \ (1 \ leqq l \ leqq r \ leqq N ) You can think of the total number of pairs of (l, m) that hold $. Furthermore, if $ S [1,0] = 0 $, then $ S [l, m] = S [1, m] -S [1, l] \ (0 \ leqq l <r \ leqq N ) You can think of the total number of pairs that are $.

That is, consider $ S [1, k] % m $ with k = 0 to N, save how many cumulative sums (x) are the remainders (using Counter), and use each remainder. Just calculate $ _x C _2 $ and use the sum as the answer.

answerD.py


from itertools import accumulate
from collections import Counter
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n,m=map(int,input().split())
s=list(map(lambda x:x%m,list(accumulate(list(map(int,input().split()))))))
s.append(0)
d=Counter(s)
ans=0
for i in d:
    if d[i]>=2:
        ans+=combinations_count(d[i],2)
print(ans)

answerD_shortest.py


n,m,*c=map(int,open(0).read().split());d={0:1};r=s=0
for i in c:s+=i;x=d.get(s%m,0);r+=x;d[s%m]=x+1
print(r)

Recommended Posts

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 113 Review of past questions
AtCoder Beginner Contest 074 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 054 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 069 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 093 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
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 071 Review of past questions
AtCoder Beginner Contest 064 Review of past questions
AtCoder Beginner Contest 082 Review of past questions
AtCoder Beginner Contest 084 Review of past questions
AtCoder Beginner Contest 068 Review of past questions
AtCoder Beginner Contest 058 Review of past questions
AtCoder Beginner Contest 043 Review of past questions