I'm glad that the old problem has good performance, but I'm good at it, so I'd like to tighten my mind. I'm not good at DP, so I'd like to take measures against DP soon (I think there was something like a DP contest).
Find out if $ A \ times B $ is odd or even.
answerA.py
a,b=map(int,input().split())
print("Yes" if (a*b)%2==1 else "No")
It takes the strings as input in sequence and checks in order whether the first letter matches the end of the previous word. Also, store the character string in set and check if there is any cover.
answerB.py
n=int(input())
now=input()
s=set()
s.add(now)
for i in range(n-1):
now2=input()
if now[-1]!=now2[0]:
print("No")
break
now=now2
s.add(now)
else:
if len(s)==n:
print("Yes")
else:
print("No")
You can only move y → y + D, y-D, and since you are at coordinate X at the beginning, you can only visit coordinates where the distance from X is a multiple of D. Since we only need to consider the largest number in D, the greatest common divisor is the solution when considering the distance from the X coordinate of the coordinates $ x_1, x_2,…, x_n $.
answerC.py
from fractions import gcd
n,X=map(int,input().split())
x=list(map(int,input().split()))
ans=abs(X-x[0])
for i in range(1,n):
ans=gcd(abs(X-x[i]),ans)
print(ans)
First of all, it is difficult to make the number of coins even number for each square because you can move one coin for each square ** there are only even or odd number **. I found that there is no **. Therefore, I thought of an operation that allows you to make an even number of coins for all the squares **. Then, when paying attention to each line, I found that such an operation seems to be possible by checking the evenness of the number of coins in each square from the left end to the right end. In other words, if the number of coins in the square you are checking is odd, move one coin to the right, and if it is even, do not move it. This allows you to ** an even number of coins for every row, except for the rightmost square. Furthermore, you can maximize the number of squares with an even number of coins by moving the coins from top to bottom for the rightmost square (that is, the rightmost W column). Also, you cannot move coins in the (H, W) squares, but if the total number of coins on all the squares is odd, you can only make the number of coins in that square an odd number. On the contrary, if the total number of coins is even, the number of coins in the (H, W) squares will also be even, so the number of squares with even coins can be maximized. I understand.
✳︎ The last output was 1WA as an array. Don't neglect ** Check the output format **.
answerD.py
h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
ans=[]
for i in range(h):
for j in range(w-1):
if a[i][j]%2==1:
a[i][j+1]+=1
a[i][j]-=1
ans.append([i+1,j+1,i+1,j+2])
for i in range(h):
if a[i][w-1]%2==1:
if i!=h-1:
a[i+1][w-1]+=1
a[i][w-1]-=1
ans.append([i+1,w,i+2,w])
l=len(ans)
print(l)
for i in range(l):
print(" ".join(map(str,ans[i])))
Recommended Posts