Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) — the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) — the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
t = int(input()) for _ in range(t): input() s = [int(x) for x in input().split()] mysum = sum([x for x in s if x <= 2048]) print("YES" if mysum >= 2048 else "NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) — the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) — the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
from sys import stdin q=int(stdin.readline().strip()) for i in range(q): n=int(stdin.readline().strip()) s=list(map(int,stdin.readline().strip().split())) flag=False for j in range(12): x=2**j s1=[] cnt=0 for k in range(len(s)): if s[k]==x: cnt+=1 else: s1.append(s[k]) if s[k]==2048: flag=True y=cnt//2 s1=s1+[x*2]*y s=s1.copy() if flag: print("YES") else: print("NO")
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) — the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) — the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
for _ in range(int(input())): N = int(input()) num = list(map(int, input().split())) num.sort(reverse=True) x = 0 ans = 'NO' for k in num: if k > 2048: continue else: x += k if x == 2048: ans = 'YES' print(ans)
You are playing a variation of game 2048. Initially you have a multiset $s$ of $n$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $s$, remove them from $s$ and insert the number equal to their sum into $s$. For example, if $s = \{1, 2, 1, 1, 4, 2, 2\}$ and you choose integers $2$ and $2$, then the multiset becomes $\{1, 1, 1, 4, 4, 2\}$. You win if the number $2048$ belongs to your multiset. For example, if $s = \{1024, 512, 512, 4\}$ you can win as follows: choose $512$ and $512$, your multiset turns into $\{1024, 1024, 4\}$. Then choose $1024$ and $1024$, your multiset turns into $\{2048, 4\}$ and you win. You have to determine if you can win this game. You have to answer $q$ independent queries. -----Input----- The first line contains one integer $q$ ($1 \le q \le 100$) – the number of queries. The first line of each query contains one integer $n$ ($1 \le n \le 100$) — the number of elements in multiset. The second line of each query contains $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2^{29}$) — the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. -----Output----- For each query print YES if it is possible to obtain the number $2048$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). -----Example----- Input 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 Output YES YES NO NO YES YES -----Note----- In the first query you can win as follows: choose $512$ and $512$, and $s$ turns into $\{1024, 64, 1024\}$. Then choose $1024$ and $1024$, and $s$ turns into $\{2048, 64\}$ and you win. In the second query $s$ contains $2048$ initially.
Q = int(input()) def solve(): N = int(input()) S = list(map(int, input().split())) S = sorted(S) while len(S) > 1: if S[0] == 2048: return True if S[0] == S[1]: S = [2*S[0]] + S[2:] else: S = S[1:] S = sorted(S) return 2048 in S for _ in range(Q): if solve(): print("YES") else: print("NO")
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
for __ in range(int(input())): n = int(input()) print((n + 1) // 2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
from math import ceil t = int(input()) for _ in range(t): n = int(input()) print(int(ceil(n/2)))
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
for _ in range(int(input())): print((int(input())+1)//2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
tests = int(input()) for _ in range(tests): n = int(input()) print((n + 1) // 2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
t = int(input()) for i in range(t): n = int(input()) print((n + 1) // 2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
t = int(input()) for test in range(t): a = int(input()) print((a+1)//2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
t=int(input()) for _ in range(t): n=int(input()) print((n+1)//2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
#list(map(int,input().split())) t=int(input()) for _ in range(t): n=int(input()) print((n+1)//2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
# for _ in range(1): for _ in range(int(input())): # a, b = map(int, input().split()) n = int(input()) # arr = list(map(int, input().split())) # s = input() x = (n + 1) // 2 print(x)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
from math import ceil for _ in range(int(input())): print(ceil(int(input())/2))
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
for _ in range(int(input())): n = int(input()) print((n + 1) // 2) #n, m = map(int, input().split()) '''A = list(map(int, input().split())) Ans = 0 for i in range(len(A)):'''
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
from math import * def r1(t): return t(input()) def r2(t): return [t(i) for i in input().split()] for _ in range(r1(int)): n = r1(int) print((n + 1) // 2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
t = int(input()) for _ in range(t): n = int(input()) print((n+1)//2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
q = int(input()) for _ in range(q): a = int(input()) print((a+1)//2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
import sys import math def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def MI(): return map(int, sys.stdin.readline().split()) def SI(): return sys.stdin.readline().strip() t = II() for q in range(t): n = II() print(math.ceil(n/2))
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
import sys import math import itertools import functools import collections import operator import fileinput import copy ORDA = 97 # a def ii(): return int(input()) def mi(): return list(map(int, input().split())) def li(): return [int(i) for i in input().split()] def lcm(a, b): return abs(a * b) // math.gcd(a, b) def revn(n): return str(n)[::-1] def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True def convn(number, base): newnumber = 0 while number > 0: newnumber += number % base number //= base return newnumber def cdiv(n, k): return n // k + (n % k != 0) def ispal(s): for i in range(len(s) // 2 + 1): if s[i] != s[-i - 1]: return False return True for _ in range(ii()): print(math.ceil(ii() / 2))
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
import sys input = sys.stdin.readline ans = [] for i in range(int(input())): n = int(input()) ans.append(n//2 + n %2) print(*ans,sep='\n')
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
import sys input = lambda :sys.stdin.readline().rstrip() for _ in range(int(input())): n=int(input()) print(n//2 + (1 if n%2 else 0))
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
def solve(): print((int(input()) + 1) // 2) for i in range(int(input())): solve()
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
import sys # from collections import deque # from collections import Counter # from math import sqrt # from math import log from math import ceil # from bisect import bisect_left, bisect_right # alpha=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # mod=10**9+7 # mod=998244353 # def BinarySearch(a,x): # i=bisect_left(a,x) # if(i!=len(a) and a[i]==x): # return i # else: # return -1 # def sieve(n): # prime=[True for i in range(n+1)] # p=2 # while(p*p<=n): # if (prime[p]==True): # for i in range(p*p,n+1,p): # prime[i]=False # p+=1 # prime[0]=False # prime[1]=False # s=set() # for i in range(len(prime)): # if(prime[i]): # s.add(i) # return s # def gcd(a, b): # if(a==0): # return b # return gcd(b%a,a) fast_reader=sys.stdin.readline fast_writer=sys.stdout.write def input(): return fast_reader().strip() def print(*argv): fast_writer(' '.join((str(i)) for i in argv)) fast_writer('\n') #____________________________________________________________________________________________________________________________________ for _ in range(int(input())): n=int(input()) print(ceil(n/2))
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
t = int(input()) for _ in range(t): a = int(input()) print((a + 1) // 2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
#!usr/bin/env python3 from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): t = I() for _ in range(t): n = I() print((n+1)>>1) return #Solve def __starting_point(): solve() __starting_point()
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
import sys input = sys.stdin.readline T = int(input()) for t in range(T): N = int(input()) print((N+1)//2)
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
import sys INF = 10**20 MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) from math import gcd from math import ceil from collections import defaultdict as dd, Counter from bisect import bisect_left as bl, bisect_right as br """ Facts and Data representation Constructive? Top bottom up down """ def solve(): n, = I() if n % 2: print(1 + n // 2) else: print(n // 2) t, = I() while t: t -= 1 solve()
A penguin Rocher has $n$ sticks. He has exactly one stick with length $i$ for all $1 \le i \le n$. He can connect some sticks. If he connects two sticks that have lengths $a$ and $b$, he gets one stick with length $a + b$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections. He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $n$ ($1 \le n \le 10^{9}$). -----Output----- For each test case, print a single integer  — the answer to the problem. -----Example----- Input 4 1 2 3 4 Output 1 1 2 2 -----Note----- In the third case, he can connect two sticks with lengths $1$ and $2$ and he will get one stick with length $3$. So, he will have two sticks with lengths $3$. In the fourth case, he can connect two sticks with lengths $1$ and $3$ and he will get one stick with length $4$. After that, he will have three sticks with lengths $\{2, 4, 4\}$, so two sticks have the same length, and one stick has the other length.
t=int(input()) for i in range(t): n=int(input()) print((n+1)//2)
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
for _ in range(int(input())): n, k = map(int, input().split()) mat = [[0] * n for _ in range(n)] for i in range(n): b = False for j in range(n): if i*n+j == k: b = True break mat[(i+j)%n][j] = 1 if b: break if k%n == 0: print(0) else: print(2) for i in range(n): for j in range(n): print(mat[i][j], end="") print()
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
q = int(input()) for _ in range(q): n, k = list(map(int,input().split())) odp = [[0] * n for i in range(n)] cur = [0,0] zap = 0 while True: if zap >= k: break odp[cur[0]][cur[1]] = 1 zap += 1 cur[0] = (cur[0]+1)%n cur[1] = (cur[1]+1)%n if cur[0] == 0: cur[1] = zap//n if k%n == 0: print(0) else: print(2) for i in range(n): print("".join(list(map(str,odp[i]))))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
for _ in range(int(input())): n,k=list(map(int,input().split())) ans=[["0" for j in range(n)] for i in range(n)] posx=0 posy=0 count=k while count: ans[posx][posy]="1" count-=1 if (k-count)%n!=0: posx=(posx+1)%n posy=(posy+1)%n else: posx=(posx+1)%n posy=(posy+2)%n res=0 R=[sum(int(ans[i][j]) for j in range(n)) for i in range(n)] C=[sum(int(ans[i][j]) for i in range(n)) for j in range(n)] res=(max(R)-min(R))**2+(max(C)-min(C))**2 print(res) for i in range(n): print("".join(ans[i]))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline for _ in range(int(input())): N, K = list(map(int, input().split())) if K % N == 0: print(0) else: print(2) ans = [[0] * N for _ in range(N)] cnt = 0 flg = 0 for p in range(N): if flg: break for i in range(N): if cnt == K: flg = 1 break ans[i][(i+p)%N] = 1 cnt += 1 for i in range(N): print("".join(map(str, ans[i]))) def __starting_point(): main() __starting_point()
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
import sys lines = sys.stdin.readlines() T = int(lines[0].strip()) # (N, K) = map(int, lines[0].strip().split(" ")) for t in range(T): (a, b) = map(int, lines[t+1].strip().split(" ")) res = [[0 for _ in range(a)] for _ in range(a)] rema = b % a deno = b // a if rema == 0: val = 0 else: val = 2 for i in range(a): if i < rema: for j in range(deno+1): res[i][(i+j)%a] = 1 else: for j in range(deno): res[i][(i+j)%a] = 1 print(val) for i in range(a): print(''.join(map(str, res[i])))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
import sys input = sys.stdin.readline for f in range(int(input())): n,k=list(map(int,input().split())) sm=k//n bg=sm toad=k%n if toad!=0: bg+=1 print(2*(bg-sm)**2) for i in range(n): line="" for j in range(n): x=i+j x%=n if x<=sm: if x<sm or i<toad: line+="1" else: line+="0" else: line+="0" print(line)
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
t=int(input()) for _ in range(t): n,k=map(int,input().split()) a=k//n rem=k%n grid=[] for i in range(n): grid.append([]) for j in range(n): grid[-1].append('0') for i in range(n): for j in range(i,i+a): grid[i][j%n]='1' if i<rem: grid[i][(i+a)%n]='1' ans=0 r=[] for i in range(n): p=0 for j in range(n): if grid[i][j]=='1': p+=1 r.append(p) c=[] for i in range(n): p=0 for j in range(n): if grid[j][i]=='1': p+=1 c.append(p) print((max(r)-min(r))**2+(max(c)-min(c))**2) for i in range(n): ans=''.join(grid[i]) print(ans)
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
import sys input=lambda: sys.stdin.readline().rstrip() t=int(input()) for _ in range(t): n,k=map(int,input().split()) if k%n==0: print(0) else: print(2) for i in range(n): ans="" if i<k%n: ans="1"*(k//n+1)+"0"*(n-(k//n+1)) ans=ans[i:]+ans[:i] else: ans="1"*(k//n)+"0"*(n-(k//n)) ans=ans[i:]+ans[:i] print(ans)
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n,k = map(int,input().split()) if k%n: print(2) else: print(0) ans = [[0 for i in range(n)] for j in range(n)] if k == 0: for i in ans: print(*i,sep="") continue for i in range(n): for j in range(n): ans[j][(i+j)%n] = 1 k -= 1 if k == 0: break else: continue break for i in ans: print(*i,sep="")
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
import sys input=sys.stdin.readline t=int(input()) for i in range(t): n,k=map(int,input().split()) m=k//n r=k%n if r: print(2) s='1'*(m+1)+'0'*(n-m-1) for i in range(r): print(s) s=s[1:]+s[0] i=(m-r)%n s=s[:i]+'0'+s[i+1:] for i in range(n-r): print(s) s=s[1:]+s[0] else: print(0) s='1'*m+'0'*(n-m) for i in range(n): print(s) s=s[1:]+s[0]
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
def solve(): n, m = map(int, input().split()) ans = 2 if m % n else 0 a = [[0] * n for _ in range(n)] for i in range(n): if m <= 0: break for j in range(n): if m <= 0: break a[j][(i + j) % n] = 1 m -= 1 print(ans) for i in a: print(*i, sep='') t = int(input()) for _ in range(t): solve()
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
t=int(input()) for _ in range(t): n,k=map(int,input().split()) board=[[0]*n for _ in range(n)] cnt=0 for i in range(n): for j in range(n): if cnt==k: break board[(j+i)%n][j]=1 cnt+=1 if cnt==k: break if k%n==0: print(0) else: maxs=(k+n-1)//n mins=k//n print(2*((maxs-mins)**2)) for i in range(n): print(''.join(map(str,board[i])))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
import sys input = sys.stdin.readline import math T = int(input()) def f(M): minR = math.inf maxR = -math.inf minC = math.inf maxC = -math.inf for i in range(len(M)): sumRow = sum(M[i]) minR = min(minR, sumRow) maxR = max(maxR, sumRow) sumCol = sum([M[el][i] for el in range(len(M))]) maxC = max(maxC, sumCol) minC = min(minC, sumCol) return (maxR - minR)**2 + (maxC - minC)**2 for t in range(T): N, K = [int(_) for _ in input().split()] M = [[0] * N for i in range(N)] # save = set() for i in range(K): # assert (i%N, (i//N + i)%N) not in save # save.add((i%N, (i//N + i)%N)) M[i%N][(i//N + i)%N] = 1 print(f(M)) for row in M: print(''.join(map(str, row)))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
import sys input = sys.stdin.readline t=int(input()) for tests in range(t): n,k=list(map(int,input().split())) ANS=[[0]*n for i in range(n)] o=k//n m=k-o*n now=0 for i in range(n): if i<m: for j in range(o+1): ANS[i][now]=1 now=(now+1)%n else: for j in range(o): ANS[i][now]=1 now=(now+1)%n if m==0: print(0) else: print(2) for ans in ANS: print("".join(map(str,ans)))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) res = [["0"] * n for _ in range(n)] if k % n: print(2) else: print(0) for d in range(n): for i in range(n): if k == 0: break res[i][(i + d) % n] = "1" k -= 1 for r in res: print("".join(r))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
tt = int(input()) for loop in range(tt): n,k = list(map(int,input().split())) lis = [ [0] * n for i in range(n) ] ns = 0 for si in range(n): if ns == k: break for i in range(n): lis[(si+i)%n][i] = 1 ns += 1 if ns == k: break else: continue break R = [] for i in range(n): now = 0 for j in range(n): now += lis[i][j] R.append(now) C = [] for i in range(n): now = 0 for j in range(n): now += lis[j][i] C.append(now) print((max(R)-min(R))**2 + (max(C)-min(C))**2) for i in lis: print("".join(map(str,i)))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) table = [[0 for i in range(n)] for j in range(n)] if k % n == 0: print(0) else: print(2) i = 0 j = 0 bias = 0 for __ in range(k): table[i][j % n] = 1 i += 1 j += 1 if i >= n: bias += 1 i = 0 j = bias for i in table: print(''.join(map(str, i)))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
for __ in range(int(input())): n, k = list(map(int, input().split())) ans = [[0] * n for i in range(n)] i, j = 0, 0 while k > 0: while i < n and k > 0: ans[i][j] = 1 i += 1 j += 1 k -= 1 j %= n i = 0 j += 1 a1, a2, b1, b2 = 10 ** 9, 0, 10 ** 9, 0 for i in range(n): a1 = min(a1, ans[i].count(1)) a2 = max(a2, ans[i].count(1)) for i in range(n): kek1 = 0 for j in range(n): if ans[j][i] == 1: kek1 += 1 b1 = min(b1, kek1) b2 = max(b2, kek1) print((a2 - a1) ** 2 + (b2 - b1) ** 2) for elem in ans: print(''.join(map(str, elem)))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
def solve(n, k): matrix = [[0 for _ in range(n)] for _ in range(n)] for right_move in range(n): for height in range(n): if k == 0: continue i = height j = (height + right_move) % n matrix[i][j] = 1 k -= 1 return matrix def get_value(matrix): n = len(matrix) max_r = 0 min_r = n max_c = 0 min_c = n for line in matrix: value = sum(line) max_r = max(max_r, value) min_r = min(min_r, value) for j in range(n): value = sum([matrix[i][j] for i in range(n)]) max_c = max(max_c, value) min_c = min(min_c, value) res = (max_r - min_r) ** 2 res += (max_c - min_c) ** 2 return res T = int(input()) for _ in range(T): n, k = list(map(int, input().split())) matrix = solve(n, k) value = get_value(matrix) print(value) for line in matrix: print(''.join(map(str, line)))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
import math import sys t = int(input()) result = [] for cs in range(t): n, k = list(map(int, input().split())) a = [[0] * n for _ in range(n)] result.append('0' if k % n == 0 else '2') for i in range(n): cur = 0 while cur < n and k > 0: a[cur][(i + cur) % n] = 1 k -= 1 cur += 1 for i in range(n): result.append(''.join(map(str, a[i]))) print('\n'.join(result))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
t = int(input()) for test in range(t): n,k = [int(i) for i in input().split()] tab = [["0" for c in range(n)] for r in range(n)] row = 0 col = 0 while k>0: tab[row][col] = "1" row = (row+1)%n col += 1 if col==n: col = 0 row = (row+1)%n k -= 1 if col==0: print(0) else: print(2) for row in range(n): print(''.join(tab[row]))
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers $n,k$. Construct a grid $A$ with size $n \times n$ consisting of integers $0$ and $1$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $k$. In other words, the number of $1$ in the grid is equal to $k$. Let's define: $A_{i,j}$ as the integer in the $i$-th row and the $j$-th column. $R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$ (for all $1 \le i \le n$). $C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$ (for all $1 \le j \le n$). In other words, $R_i$ are row sums and $C_j$ are column sums of the grid $A$. For the grid $A$ let's define the value $f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$ (here for an integer sequence $X$ we define $\max(X)$ as the maximum value in $X$ and $\min(X)$ as the minimum value in $X$). Find any grid $A$, which satisfies the following condition. Among such grids find any, for which the value $f(A)$ is the minimum possible. Among such tables, you can find any. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Next $t$ lines contain descriptions of test cases. For each test case the only line contains two integers $n$, $k$ $(1 \le n \le 300, 0 \le k \le n^2)$. It is guaranteed that the sum of $n^2$ for all test cases does not exceed $10^5$. -----Output----- For each test case, firstly print the minimum possible value of $f(A)$ among all tables, for which the condition is satisfied. After that, print $n$ lines contain $n$ characters each. The $j$-th character in the $i$-th line should be equal to $A_{i,j}$. If there are multiple answers you can print any. -----Example----- Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 -----Note----- In the first test case, the sum of all elements in the grid is equal to $2$, so the condition is satisfied. $R_1 = 1, R_2 = 1$ and $C_1 = 1, C_2 = 1$. Then, $f(A) = (1-1)^2 + (1-1)^2 = 0$, which is the minimum possible value of $f(A)$. In the second test case, the sum of all elements in the grid is equal to $8$, so the condition is satisfied. $R_1 = 3, R_2 = 3, R_3 = 2$ and $C_1 = 3, C_2 = 2, C_3 = 3$. Then, $f(A) = (3-2)^2 + (3-2)^2 = 2$. It can be proven, that it is the minimum possible value of $f(A)$.
import sys input = sys.stdin.readline inputr = lambda: sys.stdin.readline().rstrip('\n') for _ in range(int(input())): n, k = list(map(int, input().split())) e = k // n T = [[0] * n for _ in range(n)] for i in range((k+n-1)//n): for j in range(min(n, k - i * n)): T[j][(i + j)%n] = 1 rows = [sum(R) for R in T] cols = [sum(C) for C in zip(*T)] assert sum(rows) == k assert sum(cols) == k print((max(rows) - min(rows)) ** 2 + (max(cols) - min(cols)) ** 2) for R in T: print(''.join(map(str, R)))
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$. Is it possible to make the length of this array equal to $1$ with these operations? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$)  — the length of the array. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so. -----Example----- Input 4 3 1 2 3 4 3 1 2 4 3 2 3 1 6 2 4 6 1 3 5 Output YES YES NO YES -----Note----- For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): $[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$ $[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$ $[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$
t = int(input()) for case in range(t): n = int(input()) arr = list(map(int, input().split())) if arr[-1] > arr[0]: print("YES") else: print("NO")
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$. Is it possible to make the length of this array equal to $1$ with these operations? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$)  — the length of the array. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so. -----Example----- Input 4 3 1 2 3 4 3 1 2 4 3 2 3 1 6 2 4 6 1 3 5 Output YES YES NO YES -----Note----- For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): $[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$ $[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$ $[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$
for _ in range(int(input())): n = int(input()) a = list(map(int,input().split())) if a[-1]>a[0]: print("YES") else: print("NO")
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$. Is it possible to make the length of this array equal to $1$ with these operations? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$)  — the length of the array. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so. -----Example----- Input 4 3 1 2 3 4 3 1 2 4 3 2 3 1 6 2 4 6 1 3 5 Output YES YES NO YES -----Note----- For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): $[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$ $[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$ $[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$
for _ in range(int(input())): #n, m = map(int, input().split()) n = int(input()) A = list(map(int, input().split())) if A[0] <= A[-1]: print('YES') else: print('NO')
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$. Is it possible to make the length of this array equal to $1$ with these operations? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$)  — the length of the array. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so. -----Example----- Input 4 3 1 2 3 4 3 1 2 4 3 2 3 1 6 2 4 6 1 3 5 Output YES YES NO YES -----Note----- For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): $[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$ $[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$ $[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$
def solve(): n = int(input()) a = list(map(int, input().split())) q = [] for i in a: while len(q) >= 2 and ((q[-2] < q[-1] and q[-1] > i) or (q[-2] > q[-1] and q[-1] < i)): q.pop(-1) q.append(i) for i in range(len(q) - 1): if q[i] > q[i + 1]: print('NO') return print('YES') t = int(input()) for _ in range(t): solve()
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$. Is it possible to make the length of this array equal to $1$ with these operations? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$)  — the length of the array. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so. -----Example----- Input 4 3 1 2 3 4 3 1 2 4 3 2 3 1 6 2 4 6 1 3 5 Output YES YES NO YES -----Note----- For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): $[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$ $[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$ $[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$
import sys input = sys.stdin.readline t=int(input()) for tests in range(t): n=int(input()) A=list(map(int,input().split())) if A[0]<A[-1]: print("YES") else: print("NO")
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$. Is it possible to make the length of this array equal to $1$ with these operations? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$)  — the length of the array. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so. -----Example----- Input 4 3 1 2 3 4 3 1 2 4 3 2 3 1 6 2 4 6 1 3 5 Output YES YES NO YES -----Note----- For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): $[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$ $[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$ $[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$
''' author: Priyank Koul, PES University, Bengaluru''' for _ in range(int(input())): x= int(input()) li= list(map(int, input().strip().split())) fli=[] for i in range(1,x): fli.append(li[i]-li[i-1]) if(sum(fli)<0): print("NO") else: print("YES")
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$. Is it possible to make the length of this array equal to $1$ with these operations? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$)  — the length of the array. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so. -----Example----- Input 4 3 1 2 3 4 3 1 2 4 3 2 3 1 6 2 4 6 1 3 5 Output YES YES NO YES -----Note----- For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): $[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$ $[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$ $[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$
def main(): n = int(input()) a = list(map(int, input().split())) if a[0] < a[-1]: print("YES") else: print("NO") def __starting_point(): t = int(input()) for i in range(t): main() __starting_point()
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$. Is it possible to make the length of this array equal to $1$ with these operations? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$)  — the length of the array. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so. -----Example----- Input 4 3 1 2 3 4 3 1 2 4 3 2 3 1 6 2 4 6 1 3 5 Output YES YES NO YES -----Note----- For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): $[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$ $[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$ $[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$
import sys input = sys.stdin.readline for f in range(int(input())): n=int(input()) p=list(map(int,input().split())) if p[0]>p[-1]: print("NO") else: print("YES")
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$. Is it possible to make the length of this array equal to $1$ with these operations? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$)  — the length of the array. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so. -----Example----- Input 4 3 1 2 3 4 3 1 2 4 3 2 3 1 6 2 4 6 1 3 5 Output YES YES NO YES -----Note----- For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): $[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$ $[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$ $[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$
def solve(): n = int(input()) arr = [int(x) for x in input().split()] if arr[0] < arr[-1]: print('YES') else: print('NO') def __starting_point(): for _ in range(int(input())): solve() __starting_point()
You are given an array $a$ of length $n$, which initially is a permutation of numbers from $1$ to $n$. In one operation, you can choose an index $i$ ($1 \leq i < n$) such that $a_i < a_{i + 1}$, and remove either $a_i$ or $a_{i + 1}$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $[1, 3, 2]$, you can choose $i = 1$ (since $a_1 = 1 < a_2 = 3$), then either remove $a_1$ which gives the new array $[3, 2]$, or remove $a_2$ which gives the new array $[1, 2]$. Is it possible to make the length of this array equal to $1$ with these operations? -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$)  — the length of the array. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$, $a_i$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $3 \cdot 10^5$. -----Output----- For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so. -----Example----- Input 4 3 1 2 3 4 3 1 2 4 3 2 3 1 6 2 4 6 1 3 5 Output YES YES NO YES -----Note----- For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): $[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$ $[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$ $[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$
T = int(input()) for i in range(T): n = int(input()) #n,m = map(int, input().split()) #a,b = map(int, input().split()) a = [int(i) for i in input().split()] #a = list(input()) if a[-1]>a[0]: print('YES') else: print('NO')
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
mem = [[[0 for i in range(51)] for j in range(31)] for k in range(31)] def f(n, m, k): if mem[n][m][k]: return mem[n][m][k] if (n*m == k) or (k == 0): return 0 cost = 10**9 for x in range(1, n//2 + 1): for z in range(k+1): cost = min(cost, m*m + f(n-x, m, k-z) + f(x, m, z)) for y in range(1, m//2 + 1): for z in range(k+1): cost = min(cost, n*n + f(n, m-y, k-z) + f(n, y, z)) mem[n][m][k] = cost return cost t = int(input()) for i in range(t): n, m, k = list(map(int, input().split())) print(f(n, m, k))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
import sys # sys.stdin = open('ivo.in') mem = [] for i in range(32): mem.append([[-1] * 52 for u in range(32)]) def solve(x, y, z): if x > y: mem[x][y][z] = solve(y, x, z) return mem[x][y][z] if x * y == z or z == 0: mem[x][y][z] = 0 return 0 if x * y < z: mem[x][y][z] = -2 return -2 res = -2 for i in range(1, x//2 + 1): for eaten in range(z + 1): t1 = mem[i][y][eaten] if mem[i][y][eaten] != -1 else solve(i, y, eaten) if t1 == -2: continue t2 = mem[x - i][y][z - eaten] if mem[x - i][y][z - eaten] != -1 else solve(x - i, y, z - eaten) if t2 == -2: continue if res == -2 or res > t1 + t2 + y * y: res = t1 + t2 + y * y for j in range(1, y//2 + 1): for eaten in range(z + 1): t1 = mem[x][j][eaten] if mem[x][j][eaten] != -1 else solve(x, j, eaten) if t1 == -2: continue t2 = mem[x][y - j][z - eaten] if mem[x][y - j][z - eaten] != -1 else solve(x, y - j, z - eaten) if t2 == -2: continue if res == -2 or res > t1 + t2 + x * x: res = t1 + t2 + x * x mem[x][y][z] = res return mem[x][y][z] t = int(sys.stdin.readline()) for it in range(t): n, m, k = list(map(int, sys.stdin.readline().split())) print(solve(n, m, k))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
d = [[[0 for i in range(51)] for j in range(31)] for k in range(31)] def rec(n, m, k): nonlocal d if n*m == k or k == 0: return 0 if d[n][m][k] > 0: return d[n][m][k] if n*m<k: return 10**10 cost = 10**10 for i in range(1, n // 2 + 1): for j in range(k+1): cost = min(cost, m*m + rec(n-i, m, k-j) + rec(i, m, j)) for i in range(1, m // 2 + 1): for j in range(k+1): cost = min(cost, n*n + rec(n, m-i, k-j) + rec(n, i, j)) d[n][m][k] = cost return cost p = [] t = int(input()) for i in range(t): n, m, k = list(map(int, input().split())) p.append(rec(n, m, k)) print('\n'.join(str(x) for x in p))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
d = [[[0 for i in range(51)] for j in range(31)] for k in range(31)] def rec(n, m, k): nonlocal d if n*m == k or k == 0: return 0 if d[n][m][k] > 0: return d[n][m][k] if n*m<k: return 10**10 cost = 10**10 for i in range(1, n // 2 + 1): for j in range(k+1): cost = min(cost, m*m + rec(n-i, m, k-j) + rec(i, m, j)) for i in range(1, m // 2 + 1): for j in range(k+1): cost = min(cost, n*n + rec(n, m-i, k-j) + rec(n, i, j)) d[n][m][k] = cost return cost p = [] t = int(input()) for i in range(t): n, m, k = list(map(int, input().split())) #p.append(rec(n, m, k)) print(rec(n, m, k)) #print('\n'.join(str(x) for x in p))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
d = [0] * 49011 def g(n, m, k): t = 1e9 for i in range(1, m // 2 + 1): for j in range(k + 1): t = min(t, f(n, m - i, k - j) + f(n, i, j)) return n * n + t def f(n, m, k): if n > m: n, m = m, n k = min(k, n * m - k) if k == 0: return 0 if k < 0: return 1e9 q = n + 31 * m + 961 * k if d[q] == 0: d[q] = min(g(n, m, k), g(m, n, k)) return d[q] for q in range(int(input())): n, m, k = map(int, input().split()) print(f(n, m, k))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
d = [[[0 for i in range(51)] for j in range(31)] for k in range(31)] def rec(n, m, k): nonlocal d if n*m == k or k == 0: return 0 if d[n][m][k] > 0: return d[n][m][k] if n*m<k: return 10**10 cost = 10**10 for i in range(1, n // 2 + 1): for j in range(k+1): cost = min(cost, m*m + rec(n-i, m, k-j) + rec(i, m, j)) for i in range(1, m // 2 + 1): for j in range(k+1): cost = min(cost, n*n + rec(n, m-i, k-j) + rec(n, i, j)) d[n][m][k] = cost return cost p = [] t = int(input()) for i in range(t): n, m, k = map(int, input().split()) p.append(rec(n, m, k)) print('\n'.join(str(x) for x in p))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
d = [ [ [ 0 for i in range(51) ] for j in range(31) ] for g in range(31)] def rec(n, m ,k): nonlocal d if k == 0 or n*m == k: return 0 if d[n][m][k] > 0 : return d[n][m][k] if n * m < k: return 10 ** 10 cost = 10**10 for i in range(1, n//2 +1): for j in range(k+1): cost = min(cost, m**2 + rec(i, m, j) + rec(n-i, m, k-j)) for i in range(1, m//2 +1): for j in range(0, k+1): cost = min(cost, n**2 + rec(n, i, j) + rec(n, m-i, k-j)) d[n][m][k] = cost return cost t = int(input()) a = [] for c in range(t): n, m ,k = map(int, input().split()) a.append(rec(n,m,k)) print('\n'.join(str(x) for x in a))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
t=int(input()) d=[] for i in range(31): dd=[] for j in range(31): dd.append([0]*51) d.append(dd) d[1][1][1]=0 for i in range(1,31): for j in range(1,31): for k in range(1,min(i*j,50)+1): if k>i*j//2: d[i][j][k]=d[i][j][i*j-k] elif i>j: d[i][j][k]=d[j][i][k] elif (i,j)!=(1,1): k=min(k,i*j-k) kk=i*j-k jj=(i**2*j)*(j**2)*i for l in range(1,i): if k<=l*j: jj=min(jj,d[l][j][k]+j**2) else: k1=k-l*j jj=min(jj,d[i-l][j][k1]+j**2) if kk<=l*j: if kk<=50: jj=min(jj,d[l][j][kk]+j**2) else: kk1=kk-l*j if kk1<=50: jj=min(jj,d[i-l][j][kk1]+j**2) for l in range(1,j): if k<=l*i: jj=min(jj,d[i][l][k]+i**2) else: k1=k-l*i jj=min(jj,d[i][j-l][k1]+i**2) if kk<=l*i: if kk<=50: jj=min(jj,d[i][l][kk]+i**2) else: kk1=kk-l*i if kk1<=50: jj=min(jj,d[i][j-l][kk1]+i**2) d[i][j][k]=jj for i in range(t): n,m,k=list(map(int,input().split())) jj=d[n][m][k] print(jj)
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
mem = [[[0 for i in range(51)] for j in range(31)] for k in range(31)] def f(n, m, k): if mem[n][m][k]: return mem[n][m][k] if (n*m == k) or (k == 0): return 0 cost = 10**9 for x in range(1, n//2 + 1): for z in range(k+1): cost = min(cost, m*m + f(n-x, m, k-z) + f(x, m, z)) for y in range(1, m//2 + 1): for z in range(k+1): cost = min(cost, n*n + f(n, m-y, k-z) + f(n, y, z)) mem[n][m][k] = cost return cost t = int(input()) for i in range(t): n, m, k = map(int, input().split()) print(f(n, m, k))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
t = int(input()) dp = [[[0 for i in range(51)] for j in range(31)] for k in range(31)] def cost(n, m, k): if (dp[n][m][k] or k == 0 or n * m == k): return dp[n][m][k] c = 10**9 for i in range(1, n // 2 + 1): for j in range(k + 1): c = min(c, cost(n - i, m, k - j) + cost(i, m, j) + m * m) for i in range(1, m // 2 + 1): for j in range(k + 1): c = min(c, cost(n, m - i, k - j) + cost(n, i, j) + n * n) dp[n][m][k] = c return c for _ in range(t): n, m, k = list(map(int, input().split())) print(cost(n, m, k)) # mem = [[[0 for i in range(51)] for j in range(31)] for k in range(31)] # def f(n, m, k): # if mem[n][m][k]: # return mem[n][m][k] # if (n*m == k) or (k == 0): # return 0 # cost = 10**9 # for x in range(1, n//2 + 1): # for z in range(k+1): # cost = min(cost, m*m + f(n-x, m, k-z) + f(x, m, z)) # for y in range(1, m//2 + 1): # for z in range(k+1): # cost = min(cost, n*n + f(n, m-y, k-z) + f(n, y, z)) # mem[n][m][k] = cost # return cost # t = int(input()) # for i in range(t): # n, m, k = map(int, input().split()) # print(f(n, m, k))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
t = int(input()) dp = [[[0 for i in range(51)] for j in range(31)] for k in range(31)] def cost(n, m, k): if (dp[n][m][k] or k == 0 or n * m == k): return dp[n][m][k] c = 10**9 for i in range(1, n // 2 + 1): for j in range(k + 1): c = min(c, cost(i, m, j) + cost(n - i, m, k - j) + m * m) for i in range(1, m // 2 + 1): for j in range(k + 1): c = min(c, cost(n, i, j) + cost(n, m - i, k - j) + n * n) dp[n][m][k] = c return c for _ in range(t): n, m, k = list(map(int, input().split())) print(cost(n, m, k))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
D = {} def h(m,n,answ,k): x = answ for i in range(1,(n+2)//2): if k >= i*m: if m**2+ans(m,n-i,k-i*m) < x: x = m**2+ans(m,n-i,k-i*m) if k <= (n-i)*m: if m**2+ans(m,n-i,k) < x: x = m**2+ans(m,n-i,k) if k >= (n-i)*m: if m**2+ans(m,i,k-(n-i)*m) < x: x = m**2+ans(m,i,k-(n-i)*m) if k <= i*m: if m**2+ans(m,i,k) < x : x = m**2+ans(m,i,k) return x def ans(m,n,k): if k == 0: D[(m,n,k)] = 0 D[(n,m,k)] = 0 return 0 if m*n == k: D[(m,n,k)] = 0 D[(n,m,k)] = 0 return 0 elif m == 1: D[(m,n,k)] = 1 D[(n,m,k)] = 1 return 1 elif n == 1: D[(m,n,k)] = 1 D[(n,m,k)] = 1 return 1 elif (m,n,k) in D: return D[(m,n,k)] else: answ = (n**2)*m t = h(m,n,answ,k) if t < answ: answ = t s = h(n,m,answ,k) if s < answ: answ = s D[(m,n,k)] = answ D[(n,m,k)] = answ return answ for i in range(30,0,-1): for j in range(i,0,-1): for k in range(0,min(i*j,50)+1): ans(i,j,k) t = int(input()) for i in range(t): m,n,k = [int(x) for x in input().split()] print(D[(m,n,k)])
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
import sys input=sys.stdin.readline def main(): ans=[] memo=[[[-1 for _ in range(51)] for _ in range(31)] for _ in range(31)] def solve(n, m , k) : if n*m == k or k==0: return 0 if memo[n][m][k] > -1 : return memo[n][m][k] if memo[m][n][k] > -1 : memo[n][m][k]=memo[m][n][k] ; return memo[n][m][k] r=float('inf') for i in range(k+1): for j in range(1,max(m,n)): if m > j : r=min(r,n**2+solve(j,n,i)+solve(m-j,n,k-i)) if n > j : r=min(r,m**2+solve(m,j,i)+solve(m,n-j,k-i)) memo[n][m][k] = r return r for _ in range(int(input())): n,m,k = map(int,input().split()) ans.append(str(solve(n,m,k))) print('\n'.join(ans)) main()
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
t = int(input()) d = [] for i in range(31): dd = [] for j in range(31): dd.append([0] * 51) d.append(dd) d[1][1][1] = 0 for i in range(1, 31): for j in range(1, 31): for k in range(1, min(i * j, 50) + 1): if k > i * j // 2: d[i][j][k] = d[i][j][i * j - k] elif i > j: d[i][j][k] = d[j][i][k] elif (i, j) != (1, 1): k = min(k, i * j - k) kk = i * j - k jj = (i ** 2 * j) * (j ** 2) * i for l in range(1, i): if k <= l * j: jj = min(jj, d[l][j][k] + j ** 2) else: k1 = k - l * j jj = min(jj, d[i - l][j][k1] + j ** 2) # if kk <= l * j: # if kk <= 50: # jj = min(jj, d[l][j][kk] + j ** 2) # else: # kk1 = kk - l * j # if kk1 <= 50: # jj = min(jj, d[i - l][j][kk1] + j ** 2) for l in range(1, j): if k <= l * i: jj = min(jj, d[i][l][k] + i ** 2) else: k1 = k - l * i jj = min(jj, d[i][j - l][k1] + i ** 2) # if kk <= l * i: # if kk <= 50: # jj = min(jj, d[i][l][kk] + i ** 2) # else: # kk1 = kk - l * i # if kk1 <= 50: # jj = min(jj, d[i][j - l][kk1] + i ** 2) d[i][j][k] = jj for i in range(t): n, m, k = list(map(int, input().split())) jj = d[n][m][k] print(jj) # print(d[3][3][2])
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
dp = {} def getDP(n, m, k): if (n, m, k) in dp: return dp[(n, m, k)] elif (m, n, k) in dp: return dp[(m, n, k)] return None def solve(n, m, k): if n == 2 and m == 3 and k == 5: h = 5 if k == m * n or k == 0: dp[(n, m, k)] = 0 elif k % min(n, m) == 0: dp[(n, m, k)] = min(n, m) ** 2 elif k == 1: dp[(n, m, k)] = min(n, m) ** 2 + 1 elif getDP(n, m, k) is not None: return getDP(n, m, k) else: bestAns = float('inf') for i in range(1, n): if k <= i * m: bestAns = min(bestAns, getDP(i, m, k) + m ** 2) else: bestAns = min(bestAns, getDP(n - i, m, k - i * m) + m ** 2) for i in range(1, m): if k <= i * n: bestAns = min(bestAns, getDP(i, n, k) + n ** 2) else: bestAns = min(bestAns, getDP(m - i, n, k - i * n) + n ** 2) dp[(n, m, k)] = bestAns for i in range(1, 31): for j in range(1, 31): for k in range(min(i * j, 50) + 1): solve(i, j, k) toPrint = [] t = int(input()) for i in range(t): n, m, k = [int(x) for x in input().split(" ")] toPrint.append(getDP(n, m, k)) for x in toPrint: print(x)
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
import sys input = sys.stdin.readline d={} testnumber = int(input()) def calc(n, m, k): if k <= 0 or k == m*n: return 0 if k > n*m: return 1000_000_000 nonlocal d if n < m: n, m = m, n if k > (m*n - m): return m*m + 1 if k < m: return m*m + 1 if k % m == 0: return m*m if (n, m, k) in d: return d[ (n, m, k)] d[ (n, m, k) ] = min( calc2(n, m, k), calc2(m, n, k) ) return d[ (n, m, k) ] def calc2(n, m, k): m2 = m*m ans = m2*2 + 1 for i in range(1, n): if i*m >= k: ans = min(ans, m2 + calc(m, i, k) ) else: ans = min(ans, m2 + calc(m, n-i, k - i*m)) return ans for ntest in range(testnumber): n, m, k = map( int, input().split() ) if k == n*m: print(0) continue print( calc(n, m, k) )
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
import sys input = sys.stdin.readline d={} testnumber = int(input()) def calc(n, m, k): if k <= 0 or k == m*n: return 0 if k > n*m: return 1000_000_000 nonlocal d if n < m: n, m = m, n if k > (m*n - m): return m*m + 1 if k < m: return m*m + 1 if k % m == 0: return m*m if (n, m, k) in d: return d[ (n, m, k)] d[ (n, m, k) ] = min( calc2(n, m, k), calc2(m, n, k) ) return d[ (n, m, k) ] def calc2(n, m, k): m2 = m*m ans = m2*2 + 1 for i in range(1, n): if i*m >= k: ans = min(ans, m2 + calc(m, i, k) ) else: ans = min(ans, m2 + calc(m, n-i, k - i*m)) return ans for ntest in range(testnumber): n, m, k = map( int, input().split() ) if k == n*m: print(0) continue print( calc(n, m, k) )
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
d=[[[0 for i in range(51)] for j in range(31)] for k in range(31)] for i in range(31): d.append([]) for j in range(31): d[i].append([]) for k in range(50): d[i][j].append(0) def rec(n,m,k): nonlocal d if n*m==k or k==0: return 0 if d[n][m][k]>0: return d[n][m][k] if n*m<k: return 10**10 cost=10**10 for i in range(1,n//2+1): for j in range(k+1): cost=min(cost,m*m+rec(n-i,m,k-j)+rec(i,m,j)) for i in range(1,m//2+1): for j in range(k+1): cost=min(cost,n*n+rec(n,m-i,k-j)+rec(n,i,j)) d[n][m][k]=cost return cost for i in range(int(input())): a,b,c=list(map(int,input().split())) print(rec(a,b,c))
You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 × 2 bar into two pieces of 2 × 1 (cost is 2^2 = 4), to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
d=[[[0 for i in range(51)] for j in range(31)] for k in range(31)] def rec(n,m,k): nonlocal d if n*m==k or k==0: return 0 if d[n][m][k]>0: return d[n][m][k] if n*m<k: return 10**10 cost=10**10 for i in range(1,n//2+1): for j in range(k+1): cost=min(cost,m*m+rec(n-i,m,k-j)+rec(i,m,j)) for i in range(1,m//2+1): for j in range(k+1): cost=min(cost,n*n+rec(n,m-i,k-j)+rec(n,i,j)) d[n][m][k]=cost return cost for i in range(int(input())): a,b,c=list(map(int,input().split())) print(rec(a,b,c))
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers. Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$. Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$. Dark should choose an integer $k$ so that $m$ is minimized. Can you help him? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$. -----Output----- Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$. Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$. If there is more than one possible $k$, you can print any of them. -----Example----- Input 7 5 -1 10 -1 12 -1 5 -1 40 35 -1 35 6 -1 -1 9 -1 3 -1 2 -1 -1 2 0 -1 4 1 -1 3 -1 7 1 -1 7 5 2 -1 5 Output 1 11 5 35 3 6 0 42 0 0 1 2 3 4 -----Note----- In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$. In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$. So, the maximum difference between any adjacent elements is $3$.
for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) ans,small,big=0,2*10**9,-1 for i in range(len(a)-1): if a[i]==-1 and a[i+1]!=-1: small=min(small, a[i+1]) big=max(big, a[i+1]) if a[i]!=-1 and a[i+1]==-1: small = min(small, a[i]) big = max(big, a[i]) if a[i]!=-1 and a[i+1]!=-1: ans=max(ans, abs(a[i]-a[i+1])) if big==-1:print(ans, 0) else: x=(small+big)//2 ans=max(ans, abs(big-x)) ans=max(ans, abs(x-small)) print(ans, x)
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers. Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$. Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$. Dark should choose an integer $k$ so that $m$ is minimized. Can you help him? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$. -----Output----- Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$. Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$. If there is more than one possible $k$, you can print any of them. -----Example----- Input 7 5 -1 10 -1 12 -1 5 -1 40 35 -1 35 6 -1 -1 9 -1 3 -1 2 -1 -1 2 0 -1 4 1 -1 3 -1 7 1 -1 7 5 2 -1 5 Output 1 11 5 35 3 6 0 42 0 0 1 2 3 4 -----Note----- In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$. In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$. So, the maximum difference between any adjacent elements is $3$.
for _ in range(int(input())): n = int(input()) ans = 0 adj = [] a = tuple(map(int, input().split())) a1 = iter(a) next(a1) for ai, aj in zip(a, a1): if ai > -1 < aj: ans = max(ans, abs(ai - aj)) elif ai != aj: adj.append(ai + aj + 1) min_adj, max_adj = (min(adj), max(adj)) if adj else (0, 0) print(max(ans, (max_adj - min_adj + 1) // 2), (min_adj + max_adj) // 2)
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers. Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$. Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$. Dark should choose an integer $k$ so that $m$ is minimized. Can you help him? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$. -----Output----- Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$. Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$. If there is more than one possible $k$, you can print any of them. -----Example----- Input 7 5 -1 10 -1 12 -1 5 -1 40 35 -1 35 6 -1 -1 9 -1 3 -1 2 -1 -1 2 0 -1 4 1 -1 3 -1 7 1 -1 7 5 2 -1 5 Output 1 11 5 35 3 6 0 42 0 0 1 2 3 4 -----Note----- In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$. In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$. So, the maximum difference between any adjacent elements is $3$.
import sys input = sys.stdin.readline t=int(input()) for test in range(t): n=int(input()) A=list(map(int,input().split())) MAX=0 DIFMIN=10**10 DIFMAX=-100 for i in range(1,n): if A[i-1]==A[i]==-1: continue elif A[i-1]==-1: DIFMIN=min(DIFMIN,A[i]) DIFMAX=max(DIFMAX,A[i]) elif A[i]==-1: DIFMIN=min(DIFMIN,A[i-1]) DIFMAX=max(DIFMAX,A[i-1]) else: MAX=max(MAX,abs(A[i]-A[i-1])) if DIFMIN==10**10: print(0,0) else: #print(DIFMIN,DIFMAX) k=(DIFMAX+DIFMIN)//2 m=max(MAX,DIFMAX-k,k-DIFMIN) print(m,k)
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $a$ of $n$ non-negative integers. Dark created that array $1000$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $k$ ($0 \leq k \leq 10^{9}$) and replaces all missing elements in the array $a$ with $k$. Let $m$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $|a_i - a_{i+1}|$ for all $1 \leq i \leq n - 1$) in the array $a$ after Dark replaces all missing elements with $k$. Dark should choose an integer $k$ so that $m$ is minimized. Can you help him? -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^{5}$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \leq a_i \leq 10 ^ {9}$). If $a_i = -1$, then the $i$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $n$ for all test cases does not exceed $4 \cdot 10 ^ {5}$. -----Output----- Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $m$ and an integer $k$ ($0 \leq k \leq 10^{9}$) that makes the maximum absolute difference between adjacent elements in the array $a$ equal to $m$. Make sure that after replacing all the missing elements with $k$, the maximum absolute difference between adjacent elements becomes $m$. If there is more than one possible $k$, you can print any of them. -----Example----- Input 7 5 -1 10 -1 12 -1 5 -1 40 35 -1 35 6 -1 -1 9 -1 3 -1 2 -1 -1 2 0 -1 4 1 -1 3 -1 7 1 -1 7 5 2 -1 5 Output 1 11 5 35 3 6 0 42 0 0 1 2 3 4 -----Note----- In the first test case after replacing all missing elements with $11$ the array becomes $[11, 10, 11, 12, 11]$. The absolute difference between any adjacent elements is $1$. It is impossible to choose a value of $k$, such that the absolute difference between any adjacent element will be $\leq 0$. So, the answer is $1$. In the third test case after replacing all missing elements with $6$ the array becomes $[6, 6, 9, 6, 3, 6]$. $|a_1 - a_2| = |6 - 6| = 0$; $|a_2 - a_3| = |6 - 9| = 3$; $|a_3 - a_4| = |9 - 6| = 3$; $|a_4 - a_5| = |6 - 3| = 3$; $|a_5 - a_6| = |3 - 6| = 3$. So, the maximum difference between any adjacent elements is $3$.
from math import * zzz = int(input()) for zz in range(zzz): n = int(input()) a = [int(i) for i in input().split()] b = set() for i in range(n): if a[i] == -1: if i > 0: if a[i-1] >= 0: b.add(a[i-1]) if i < n - 1: if a[i+1] >= 0: b.add(a[i+1]) b = list(b) if len(b) == 0: print(0, 0) else: k = (min(b) + max(b)) // 2 m = 0 for i in range(n): if a[i] == -1: a[i] = k for i in range(1, n): m = max(m, abs(a[i-1]- a[i])) print(m, k)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
n = int(input()) for _ in range(n): a, b = list(map(int, input().split())) print(a ^ b)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
for __ in range(int(input())): a, b = list(map(int, input().split())) print(a ^ b)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
import sys input = sys.stdin.readline def main(): a, b = map(int, input().split()) print(a^b) for _ in range(int(input())): main()
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
from math import * from bisect import * from collections import * from random import * from decimal import * from itertools import * import sys input=sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip('\n') def lis(): return list(map(int,input().split())) def ma(): return list(map(int,input().split())) t=inp() while(t): t-=1 a,b=ma() print(a^b)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
read = lambda: map(int, input().split()) t = int(input()) for i in range(t): a, b = read() print(a^b)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
def main(): a, b = list(map(int, input().split())) print(a + b - 2*(a&b)) def __starting_point(): t = int(input()) for i in range(t): main() __starting_point()
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
for _ in range(int(input())): a, b = list(map(int, input().split())) n = a & b print((a^n) + (b^n))
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
import sys input=sys.stdin.readline T=int(input()) for _ in range(T): n,m=list(map(int,input().split())) print(n^m)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop import math from collections import * from functools import reduce,cmp_to_key import sys input = sys.stdin.readline M = mod = 998244353 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip('\n').split()] def st():return input().rstrip('\n') def val():return int(input().rstrip('\n')) def li2():return [i for i in input().rstrip('\n')] def li3():return [int(i) for i in input().rstrip('\n')] for _ in range(val()): a, b = li() ans = 0 for i in range(32): if (a >> i) & 1 == (b >> i) & 1: ans += 0 else: ans += 1 << i print(ans)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
import sys input = sys.stdin.readline for _ in range(int(input())): a, b = map(int, input().split()) print(a + b - (a & b) * 2)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
import sys import math import bisect from sys import stdin, stdout from math import gcd, floor, sqrt, log from collections import defaultdict as dd from bisect import bisect_left as bl, bisect_right as br from collections import Counter #sys.setrecursionlimit(100000000) inp = lambda: int(input()) strng = lambda: input().strip() jn = lambda x, l: x.join(map(str, l)) strl = lambda: list(input().strip()) mul = lambda: map(int, input().strip().split()) mulf = lambda: map(float, input().strip().split()) seq = lambda: list(map(int, input().strip().split())) ceil = lambda x: int(x) if (x == int(x)) else int(x) + 1 ceildiv = lambda x, d: x // d if (x % d == 0) else x // d + 1 flush = lambda: stdout.flush() stdstr = lambda: stdin.readline() stdint = lambda: int(stdin.readline()) stdpr = lambda x: stdout.write(str(x)) stdarr = lambda: map(int, stdstr().split()) mod = 1000000007 for _ in range(stdint()): a,b = stdarr() print(a^b)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
t = int(input()) for _ in range(t): a,b = map(int,input().split()) if a > b: a,b = b,a print(a^b)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
""" Author: Sagar Pandey """ # ---------------------------------------------------Import Libraries--------------------------------------------------- import sys import os from math import sqrt, log, log2, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial from copy import copy, deepcopy from sys import stdin, stdout from collections import Counter, defaultdict, deque from itertools import permutations import heapq from bisect import bisect_left as bl # If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect # If the element is already present in the list, # the right most position where element has to be inserted is r # ---------------------------------------------------Global Variables--------------------------------------------------- # sys.setrecursionlimit(100000000) mod = 1000000007 # ---------------------------------------------------Helper Functions--------------------------------------------------- iinp = lambda: int(sys.stdin.readline()) inp = lambda: sys.stdin.readline().strip() strl = lambda: list(inp().strip().split(" ")) intl = lambda: list(map(int, inp().split(" "))) mint = lambda: list(map(int, inp().split())) flol = lambda: list(map(float, inp().split(" "))) flush = lambda: stdout.flush() def permute(nums): def fun(arr, nums, cur, v): if len(cur) == len(nums): arr.append(cur.copy()) i = 0 while i < len(nums): if v[i]: i += 1 continue else: cur.append(nums[i]) v[i] = 1 fun(arr, nums, cur, v) cur.pop() v[i] = 0 i += 1 # while i<len(nums) and nums[i]==nums[i-1]:i+=1 # Uncomment for unique permutations return arr res = [] nums.sort() v = [0] * len(nums) return fun(res, nums, [], v) def subsets(res, index, arr, cur): res.append(cur.copy()) for i in range(index, len(arr)): cur.append(arr[i]) subsets(res, i + 1, arr, cur) cur.pop() return res def sieve(N): root = int(sqrt(N)) primes = [1] * (N + 1) primes[0], primes[1] = 0, 0 for i in range(2, root + 1): if primes[i]: for j in range(i * i, N + 1, i): primes[j] = 0 return primes def bs(arr, l, r, x): if x < arr[0] or x > arr[len(arr) - 1]: return -1 while l <= r: mid = l + (r - l) // 2 if arr[mid] == x: return mid elif arr[mid] < x: l = mid + 1 else: r = mid - 1 return -1 def isPrime(n): if n <= 1: return False if n <= 3: return True if n % 2 == 0 or n % 3 == 0: return False p = int(sqrt(n)) for i in range(5, p + 1, 6): if n % i == 0 or n % (i + 2) == 0: return False return True # -------------------------------------------------------Functions------------------------------------------------------ def solve(): a,b=mint() print(a^b) # -------------------------------------------------------Main Code------------------------------------------------------ for _ in range(iinp()): solve()
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
t = int(input()) for _ in range(t): a, b = map(int, input().split()) print((a + b) - (a & b) * 2)
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
t = int(input()) for _ in range(t): a,b = list(map(int,input().split())) x = a&b print((a^x) + (b^x))
In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game. Tzuyu gave Sana two integers $a$ and $b$ and a really important quest. In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$). -----Output----- For each testcase, output the smallest possible value of the given expression. -----Example----- Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 -----Note----- For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
t=int(input()) for you in range(t): l=input().split() a=int(l[0]) b=int(l[1]) z=a&b print((a^z)+(b^z))