I hope I can wake up a bit early tomorrow to go for a jogging.
My approach:
It just worked, but I didn't use the constraint
The second line of each test case contains the string
s
of lengthn
. The string is always the result of writing out the strings of a beautiful matrix.
def root(n: int) -> int:
li: int = 0
ri: int = n
while li < ri - 1:
mi: int = (li + ri) // 2
sq: int = mi * mi
if sq == n:
return mi
elif sq > n:
ri = mi
else:
li = mi
return li if li * li == n else -1
for _ in range(int(input())):
n: int = int(input())
s: str = input()
r = c = root(n)
if r == -1:
print("No")
continue
sq: str = ""
sq += "1" * r
for i in range(r - 2):
sq += "1" + "0" * (r - 2) + "1"
sq += "1" * r
if s == sq:
print("Yes")
else:
print("No")
Codeforces Round 970 (Div. 3) Editorial
Assume that string was created from the beautiful binary matrix with size
r * c
.
If
r <= 2
orc <= 2
, then the whole matrix consists of ‘1’. This means that the string will have only one character and this is the onlly case such happening. So, if the whole string is constructed out of ‘1’, we print “Yes” only if the size of the string is 4, since onlyr = c = 2
is a good matrix for us.
I can understand the solution so far.
Otherwise, we have at least one ‘0’ in the string. Let's look at what is the index of the first ‘0’.
If it has index
r + 1
, since the whole first line and first character of the first line equal to ‘1’, so now, we have a fixed value ofr
(index of the first ‘0’ minus 1) and the answer is “Yes” only ifr
is the square root ofn
.
I see. Because it's guaranteed that the input string is already a
beautiful one, calculating r
is enough to solve the
problem. Genius.
My solution of 2008A and 2008B are different from the optimal, but I managed to pass the tests. It's interesting.
I remember that someone said the flexibility of the solutions is an exciting point of competitive programming.
Rice 700 Salad 400 Protein shake 200 Bagels 500 Protein shake 200 Nori 100 Kombucha 100
Total 2200 kcal
pull-ups, push-ups
MUST:
TODO: