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.
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)
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")
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,
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
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
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