inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...
Lee thinks a regular $n$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $OX$-axis and at least one of its edges is parallel to the $OY$-axis at the same time.
Recall that a regular $n$-sided polygon is a convex polygon with $n$ vertices such that all the edges and angles are equal.
Now he is shopping: the market has $t$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β the number of polygons in the market.
Each of the next $t$ lines contains a single integer $n_i$ ($3 \le n_i \le 10^9$): it means that the $i$-th polygon is a regular $n_i$-sided polygon.
-----Output-----
For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).
-----Example-----
Input
4
3
4
12
1000000000
Output
NO
YES
YES
YES
-----Note-----
In the example, there are $4$ polygons in the market. It's easy to see that an equilateral triangle (a regular $3$-sided polygon) is not beautiful, a square (a regular $4$-sided polygon) is beautiful and a regular $12$-sided polygon (is shown below) is beautiful as well. [Image]
|
import sys
def second(ele):
return(ele[1])
for _ in range(int(input())):
n = int(input())
if(n%4==0):
print("YES")
else:
print("NO")
|
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...
Lee thinks a regular $n$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $OX$-axis and at least one of its edges is parallel to the $OY$-axis at the same time.
Recall that a regular $n$-sided polygon is a convex polygon with $n$ vertices such that all the edges and angles are equal.
Now he is shopping: the market has $t$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β the number of polygons in the market.
Each of the next $t$ lines contains a single integer $n_i$ ($3 \le n_i \le 10^9$): it means that the $i$-th polygon is a regular $n_i$-sided polygon.
-----Output-----
For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).
-----Example-----
Input
4
3
4
12
1000000000
Output
NO
YES
YES
YES
-----Note-----
In the example, there are $4$ polygons in the market. It's easy to see that an equilateral triangle (a regular $3$-sided polygon) is not beautiful, a square (a regular $4$-sided polygon) is beautiful and a regular $12$-sided polygon (is shown below) is beautiful as well. [Image]
|
def read_int():
return int(input())
def read_ints():
return list(map(int, input().split(' ')))
t = read_int()
for case_num in range(t):
n = read_int()
print('YES' if n % 4 == 0 else 'NO')
|
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...
Lee thinks a regular $n$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $OX$-axis and at least one of its edges is parallel to the $OY$-axis at the same time.
Recall that a regular $n$-sided polygon is a convex polygon with $n$ vertices such that all the edges and angles are equal.
Now he is shopping: the market has $t$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β the number of polygons in the market.
Each of the next $t$ lines contains a single integer $n_i$ ($3 \le n_i \le 10^9$): it means that the $i$-th polygon is a regular $n_i$-sided polygon.
-----Output-----
For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).
-----Example-----
Input
4
3
4
12
1000000000
Output
NO
YES
YES
YES
-----Note-----
In the example, there are $4$ polygons in the market. It's easy to see that an equilateral triangle (a regular $3$-sided polygon) is not beautiful, a square (a regular $4$-sided polygon) is beautiful and a regular $12$-sided polygon (is shown below) is beautiful as well. [Image]
|
for _ in range(int(input())):
n = int(input())
if(n%4==0):
print("YES")
else:
print("NO")
|
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...
Lee thinks a regular $n$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $OX$-axis and at least one of its edges is parallel to the $OY$-axis at the same time.
Recall that a regular $n$-sided polygon is a convex polygon with $n$ vertices such that all the edges and angles are equal.
Now he is shopping: the market has $t$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β the number of polygons in the market.
Each of the next $t$ lines contains a single integer $n_i$ ($3 \le n_i \le 10^9$): it means that the $i$-th polygon is a regular $n_i$-sided polygon.
-----Output-----
For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).
-----Example-----
Input
4
3
4
12
1000000000
Output
NO
YES
YES
YES
-----Note-----
In the example, there are $4$ polygons in the market. It's easy to see that an equilateral triangle (a regular $3$-sided polygon) is not beautiful, a square (a regular $4$-sided polygon) is beautiful and a regular $12$-sided polygon (is shown below) is beautiful as well. [Image]
|
import sys
input = sys.stdin.readline
t=int(input())
for tests in range(t):
n=int(input())
if n%4==0:
print("YES")
else:
print("NO")
|
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...
Lee thinks a regular $n$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $OX$-axis and at least one of its edges is parallel to the $OY$-axis at the same time.
Recall that a regular $n$-sided polygon is a convex polygon with $n$ vertices such that all the edges and angles are equal.
Now he is shopping: the market has $t$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β the number of polygons in the market.
Each of the next $t$ lines contains a single integer $n_i$ ($3 \le n_i \le 10^9$): it means that the $i$-th polygon is a regular $n_i$-sided polygon.
-----Output-----
For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).
-----Example-----
Input
4
3
4
12
1000000000
Output
NO
YES
YES
YES
-----Note-----
In the example, there are $4$ polygons in the market. It's easy to see that an equilateral triangle (a regular $3$-sided polygon) is not beautiful, a square (a regular $4$-sided polygon) is beautiful and a regular $12$-sided polygon (is shown below) is beautiful as well. [Image]
|
from sys import stdin, stdout
import math
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
print("YES" if n%4==0 else "NO")
|
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...
Lee thinks a regular $n$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $OX$-axis and at least one of its edges is parallel to the $OY$-axis at the same time.
Recall that a regular $n$-sided polygon is a convex polygon with $n$ vertices such that all the edges and angles are equal.
Now he is shopping: the market has $t$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β the number of polygons in the market.
Each of the next $t$ lines contains a single integer $n_i$ ($3 \le n_i \le 10^9$): it means that the $i$-th polygon is a regular $n_i$-sided polygon.
-----Output-----
For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).
-----Example-----
Input
4
3
4
12
1000000000
Output
NO
YES
YES
YES
-----Note-----
In the example, there are $4$ polygons in the market. It's easy to see that an equilateral triangle (a regular $3$-sided polygon) is not beautiful, a square (a regular $4$-sided polygon) is beautiful and a regular $12$-sided polygon (is shown below) is beautiful as well. [Image]
|
t=int(input())
for i in range(t):
n=int(input())
if n%4==0:
print("YES")
else:
print("NO")
|
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...
Lee thinks a regular $n$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $OX$-axis and at least one of its edges is parallel to the $OY$-axis at the same time.
Recall that a regular $n$-sided polygon is a convex polygon with $n$ vertices such that all the edges and angles are equal.
Now he is shopping: the market has $t$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β the number of polygons in the market.
Each of the next $t$ lines contains a single integer $n_i$ ($3 \le n_i \le 10^9$): it means that the $i$-th polygon is a regular $n_i$-sided polygon.
-----Output-----
For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).
-----Example-----
Input
4
3
4
12
1000000000
Output
NO
YES
YES
YES
-----Note-----
In the example, there are $4$ polygons in the market. It's easy to see that an equilateral triangle (a regular $3$-sided polygon) is not beautiful, a square (a regular $4$-sided polygon) is beautiful and a regular $12$-sided polygon (is shown below) is beautiful as well. [Image]
|
for _ in range(int(input())):
a=int(input())
if a%4==0: print("YES")
else: print("NO")
|
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...
Lee thinks a regular $n$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $OX$-axis and at least one of its edges is parallel to the $OY$-axis at the same time.
Recall that a regular $n$-sided polygon is a convex polygon with $n$ vertices such that all the edges and angles are equal.
Now he is shopping: the market has $t$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β the number of polygons in the market.
Each of the next $t$ lines contains a single integer $n_i$ ($3 \le n_i \le 10^9$): it means that the $i$-th polygon is a regular $n_i$-sided polygon.
-----Output-----
For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).
-----Example-----
Input
4
3
4
12
1000000000
Output
NO
YES
YES
YES
-----Note-----
In the example, there are $4$ polygons in the market. It's easy to see that an equilateral triangle (a regular $3$-sided polygon) is not beautiful, a square (a regular $4$-sided polygon) is beautiful and a regular $12$-sided polygon (is shown below) is beautiful as well. [Image]
|
def solve():
n = int(input())
if n % 4 == 0:
print("YES")
else:
print("NO")
for i in range(int(input())):
solve()
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
3
import math
import os
import sys
DEBUG = 'DEBUG' in os.environ
def inp():
return sys.stdin.readline().rstrip()
def dprint(*value, sep=' ', end='\n'):
if DEBUG:
print(*value, sep=sep, end=end)
INF = 10 ** 20
def solve(N, A, B):
dp = {A[0]: 0, A[0] + 1: B[0], A[0] + 2: B[0] * 2}
for i in range(1, N):
ndp = {}
h = A[i]
for ph, c in dp.items():
for inc in range(3):
nh = h + inc
if ph == nh:
continue
if nh not in ndp:
ndp[nh] = INF
ndp[nh] = min(ndp[nh], c + B[i] * inc)
dp = ndp
return min(dp.values())
def main():
Q = int(inp())
for _ in range(Q):
N = int(inp())
A = []
B = []
for _ in range(N):
a, b = [int(e) for e in inp().split()]
A.append(a)
B.append(b)
print(solve(N, A, B))
def __starting_point():
main()
__starting_point()
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
import sys
input = sys.stdin.readline
q=int(input())
for testcases in range(q):
n=int(input())
f=[tuple(map(int,input().split())) for i in range(n)]
DP0=[0]*n
DP1=[0]*n
DP2=[0]*n
DP1[0]=f[0][1]
DP2[0]=f[0][1]*2
for i in range(1,n):
x=f[i-1][0]
y=f[i][0]
if y==x:
DP0[i]=min(DP1[i-1],DP2[i-1])
DP1[i]=f[i][1]+min(DP0[i-1],DP2[i-1])
DP2[i]=f[i][1]*2+min(DP0[i-1],DP1[i-1])
elif y==x+1:
DP0[i]=min(DP0[i-1],DP2[i-1])
DP1[i]=f[i][1]+min(DP0[i-1],DP1[i-1])
DP2[i]=f[i][1]*2+min(DP0[i-1],DP1[i-1],DP2[i-1])
elif y==x+2:
DP0[i]=min(DP0[i-1],DP1[i-1])
DP1[i]=f[i][1]+min(DP0[i-1],DP1[i-1],DP2[i-1])
DP2[i]=f[i][1]*2+min(DP0[i-1],DP1[i-1],DP2[i-1])
elif y==x-1:
DP0[i]=min(DP0[i-1],DP1[i-1],DP2[i-1])
DP1[i]=f[i][1]+min(DP1[i-1],DP2[i-1])
DP2[i]=f[i][1]*2+min(DP0[i-1],DP2[i-1])
elif y==x-2:
DP0[i]=min(DP0[i-1],DP1[i-1],DP2[i-1])
DP1[i]=f[i][1]+min(DP0[i-1],DP1[i-1],DP2[i-1])
DP2[i]=f[i][1]*2+min(DP1[i-1],DP2[i-1])
else:
DP0[i]=min(DP0[i-1],DP1[i-1],DP2[i-1])
DP1[i]=f[i][1]+min(DP0[i-1],DP1[i-1],DP2[i-1])
DP2[i]=f[i][1]*2+min(DP0[i-1],DP1[i-1],DP2[i-1])
print(min(DP0[n-1],DP1[n-1],DP2[n-1]))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
import sys
input = sys.stdin.readline
Q = int(input())
for _ in range(Q):
N = int(input())
X = []
ne = [0] * 3
for i in range(N):
a, b = list(map(int, input().split()))
X.append((a, b))
if i == 0:
ne = [0, b, b*2]
continue
Y = ne
ne = [1<<100] * 3
if X[i][0] != X[i-1][0]: ne[0] = min(ne[0], Y[0])
if X[i][0] != X[i-1][0]+1: ne[0] = min(ne[0], Y[1])
if X[i][0] != X[i-1][0]+2: ne[0] = min(ne[0], Y[2])
if X[i][0]+1 != X[i-1][0]: ne[1] = min(ne[1], Y[0] + b)
if X[i][0]+1 != X[i-1][0]+1: ne[1] = min(ne[1], Y[1] + b)
if X[i][0]+1 != X[i-1][0]+2: ne[1] = min(ne[1], Y[2] + b)
if X[i][0]+2 != X[i-1][0]: ne[2] = min(ne[2], Y[0] + b * 2)
if X[i][0]+2 != X[i-1][0]+1: ne[2] = min(ne[2], Y[1] + b * 2)
if X[i][0]+2 != X[i-1][0]+2: ne[2] = min(ne[2], Y[2] + b * 2)
print(min(ne))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
from sys import stdin
input = stdin.readline
q = int(input())
for rwe in range(q):
n = int(input())
a = [0] * n
b = [0] * n
for i in range(n):
x, y = map(int, input().split())
a[i] = x
b[i] = y
dp = [[0,0,0] for i in range(n)]
dp[0][0] = 0
dp[0][1] = b[0]
dp[0][2] = 2 * b[0]
for i in range(1, n):
for pod in range(3):
x = (dp[i-1][0] if a[i-1] != a[i] + pod else 100000000000000000000000000)
y = (dp[i-1][1] if a[i-1] + 1 != a[i] + pod else 10000000000000000000000000000)
z = (dp[i-1][2] if a[i-1] + 2 != a[i] + pod else 10000000000000000000000000000)
dp[i][pod] = min(x, y, z) + pod*b[i]
print(min(dp[n-1]))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
''' CODED WITH LOVE BY SATYAM KUMAR '''
from sys import stdin, stdout
import heapq
import cProfile, math
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
import sys
sys.setrecursionlimit(10 ** 6) # max depth of recursion
threading.stack_size(2 ** 27) # new thread will get stack of such size
fac_warm_up = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7
class MergeFind:
def __init__(self, n):
self.parent = list(range(n))
self.size = [1] * n
self.num_sets = n
self.lista = [[_] for _ in range(n)]
def find(self, a):
to_update = []
while a != self.parent[a]:
to_update.append(a)
a = self.parent[a]
for b in to_update:
self.parent[b] = a
return self.parent[a]
def merge(self, a, b):
a = self.find(a)
b = self.find(b)
if a == b:
return
if self.size[a] < self.size[b]:
a, b = b, a
self.num_sets -= 1
self.parent[b] = a
self.size[a] += self.size[b]
self.lista[a] += self.lista[b]
def set_size(self, a):
return self.size[self.find(a)]
def __len__(self):
return self.num_sets
def display(string_to_print):
stdout.write(str(string_to_print) + "\n")
def prime_factors(n): # n**0.5 complex
factors = dict()
for i in range(2, math.ceil(math.sqrt(n)) + 1):
while n % i == 0:
if i in factors:
factors[i] += 1
else:
factors[i] = 1
n = n // i
if n > 2:
factors[n] = 1
return (factors)
def all_factors(n):
return set(reduce(list.__add__,
([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
def fibonacci_modP(n, MOD):
if n < 2: return 1
return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(
fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD
def factorial_modP_Wilson(n, p):
if (p <= n):
return 0
res = (p - 1)
for i in range(n + 1, p):
res = (res * cached_fn(InverseEuler, i, p)) % p
return res
def binary(n, digits=20):
b = bin(n)[2:]
b = '0' * (digits - len(b)) + b
return b
def is_prime(n):
"""Returns True if n is prime."""
if n < 4:
return True
if n % 2 == 0:
return False
if n % 3 == 0:
return False
i = 5
w = 2
while i * i <= n:
if n % i == 0:
return False
i += w
w = 6 - w
return True
def generate_primes(n):
prime = [True for i in range(n + 1)]
p = 2
while p * p <= n:
if prime[p]:
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
return prime
factorial_modP = []
def warm_up_fac(MOD):
nonlocal factorial_modP, fac_warm_up
if fac_warm_up: return
factorial_modP = [1 for _ in range(fac_warm_up_size + 1)]
for i in range(2, fac_warm_up_size):
factorial_modP[i] = (factorial_modP[i - 1] * i) % MOD
fac_warm_up = True
def InverseEuler(n, MOD):
return pow(n, MOD - 2, MOD)
def nCr(n, r, MOD):
nonlocal fac_warm_up, factorial_modP
if not fac_warm_up:
warm_up_fac(MOD)
fac_warm_up = True
return (factorial_modP[n] * (
(pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD)) % MOD)) % MOD
def test_print(*args):
if testingMode:
print(args)
def display_list(list1, sep=" "):
stdout.write(sep.join(map(str, list1)) + "\n")
def display_2D_list(li):
for i in li:
print(i)
def prefix_sum(li):
sm = 0
res = []
for i in li:
sm += i
res.append(sm)
return res
def get_int():
return int(stdin.readline().strip())
def get_tuple():
return list(map(int, stdin.readline().split()))
def get_list():
return list(map(int, stdin.readline().split()))
memory = dict()
def clear_cache():
nonlocal memory
memory = dict()
def cached_fn(fn, *args):
nonlocal memory
if args in memory:
return memory[args]
else:
result = fn(*args)
memory[args] = result
return result
def ncr(n, r):
return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))
def binary_search(i, li):
fn = lambda x: li[x] - x // i
x = -1
b = len(li)
while b >= 1:
while b + x < len(li) and fn(b + x) > 0: # Change this condition 2 to whatever you like
x += b
b = b // 2
return x
# -------------------------------------------------------------- MAIN PROGRAM
TestCases = True
fac_warm_up_size = 10 ** 5 + 100
optimise_for_recursion = False # Can not be used clubbed with TestCases WHen using recursive functions, use Python 3
def main():
n = get_int()
lengths = []
costs = []
for _ in range(n):
a, b = get_tuple()
lengths.append(a)
costs.append(b)
dp = [[0, costs[0], 2*costs[0]]]
for i in range(1, n):
kt = [10 ** 20] * 3
for k in range(3):
for j, ele in enumerate(dp[-1]):
if lengths[i-1]+j!=lengths[i]+k:
kt[k] = min(kt[k], ele+costs[i]*k)
dp.append(kt)
#print(dp)
print(min(dp[-1]))
# --------------------------------------------------------------------- END=
if TestCases:
for i in range(get_int()):
main()
else:
main() if not optimise_for_recursion else threading.Thread(target=main).start()
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
import sys
input = sys.stdin.readline
q = int(input())
for i in range(q):
n = int(input())
dp = [[0] * n for _ in range(3)]
prev = 0
for i in range(n):
l, c = [int(item) for item in input().split()]
if i == 0:
dp[0][0] = 0
dp[1][0] = c
dp[2][0] = c * 2
prev = l
continue
prev_min = min(dp[0][i-1], dp[1][i-1], dp[2][i-1])
if l > prev + 2:
dp[0][i] = prev_min
dp[1][i] = prev_min + c
dp[2][i] = prev_min + c * 2
elif l == prev + 2:
dp[0][i] = min(dp[0][i-1], dp[1][i-1])
dp[1][i] = prev_min + c
dp[2][i] = prev_min + c * 2
elif l == prev + 1:
dp[0][i] = min(dp[0][i-1], dp[2][i-1])
dp[1][i] = min(dp[0][i-1], dp[1][i-1]) + c
dp[2][i] = prev_min + c * 2
elif l == prev:
dp[0][i] = min(dp[1][i-1], dp[2][i-1])
dp[1][i] = min(dp[0][i-1], dp[2][i-1]) + c
dp[2][i] = min(dp[0][i-1], dp[1][i-1]) + 2 * c
elif l == prev - 1:
dp[0][i] = prev_min
dp[1][i] = min(dp[1][i-1], dp[2][i-1]) + c
dp[2][i] = min(dp[0][i-1], dp[2][i-1]) + 2 * c
elif l == prev - 2:
dp[0][i] = prev_min
dp[1][i] = prev_min + c
dp[2][i] = min(dp[1][i-1], dp[2][i-1]) + 2 * c
elif l < prev - 2:
dp[0][i] = prev_min
dp[1][i] = prev_min + c
dp[2][i] = prev_min + c * 2
prev = l
print(min(dp[0][n-1], dp[1][n-1], dp[2][n-1]))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
n = int(input())
d = [[-100, 0]] + [list(map(int, input().split())) for _ in range(n)]
dp = [[100 for j in range(3)] for i in range(n+1)]
dp[0] = [0, 0, 0, 0]
for i in range(n):
if d[i+1][0] == d[i][0]:
dp[i+1][0] = min(dp[i][1], dp[i][2])
dp[i+1][1] = min(dp[i][0], dp[i][2]) + d[i+1][1]
dp[i+1][2] = min(dp[i][0], dp[i][1]) + d[i+1][1] * 2
elif d[i+1][0] + 1 == d[i][0]:
dp[i+1][0] = min(dp[i])
dp[i+1][1] = min(dp[i][1], dp[i][2]) + d[i+1][1]
dp[i+1][2] = min(dp[i][0], dp[i][1]) + d[i+1][1] * 2
elif d[i+1][0] + 2 == d[i][0]:
dp[i+1][0] = min(dp[i])
dp[i+1][1] = min(dp[i]) + d[i+1][1]
dp[i+1][2] = min(dp[i][1], dp[i][2]) + d[i+1][1] * 2
elif d[i+1][0] - 1 == d[i][0]:
dp[i+1][0] = min(dp[i][0], dp[i][2])
dp[i+1][1] = min(dp[i][1], dp[i][0]) + d[i+1][1]
dp[i+1][2] = min(dp[i]) + d[i+1][1] * 2
elif d[i+1][0] - 2 == d[i][0]:
dp[i+1][0] = min(dp[i][0], dp[i][1])
dp[i+1][1] = min(dp[i]) + d[i+1][1]
dp[i+1][2] = min(dp[i]) + d[i+1][1] * 2
else:
dp[i+1][0] = min(dp[i])
dp[i+1][1] = min(dp[i]) + d[i+1][1]
dp[i+1][2] = min(dp[i]) + d[i+1][1] * 2
print(min(dp[n]))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
import sys
input=sys.stdin.readline
q=int(input())
for _ in range(q):
n=int(input())
ans=0
a,b=list(map(int,input().split()))
cur=a
A=[0,b,2*b]
for i in range(n-1):
a=cur
na,nb=list(map(int,input().split()))
cur=na
a0,a1,a2=A
if na==a:
A[0]=min(a1,a2)
A[1]=nb+min(a0,a2)
A[2]=2*nb+min(a0,a1)
elif na==a-1:
A[0]=min(a0,a1,a2)
A[1]=nb+min(a1,a2)
A[2]=2*nb+min(a0,a2)
elif na==a-2:
A[0]=min(a0,a1,a2)
A[1]=nb+min(a0,a1,a2)
A[2]=2*nb+min(a1,a2)
elif na==a+1:
A[0]=min(a0,a2)
A[1]=nb+min(a0,a1)
A[2]=2*nb+min(a0,a1,a2)
elif na==a+2:
A[0]=min(a0,a1)
A[1]=nb+min(a0,a1,a2)
A[2]=2*nb+min(a0,a1,a2)
else:
A[0]=min(a0,a1,a2)
A[1]=nb+min(a0,a1,a2)
A[2]=2*nb+min(a0,a1,a2)
print(min(A))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
import sys
input = sys.stdin.readline
Q = int(input())
data = []
for _ in range(Q):
N = int(input())
A = []
B = []
for _ in range(N):
a, b = list(map(int, input().split()))
A.append(a)
B.append(b)
data.append([N, A, B])
for N, A, B in data:
dp = [[0, 0, 0] for _ in range(N)]
dp[0][1] = B[0]
dp[0][2] = 2*B[0]
for i in range(1, N):
if A[i] == A[i-1]:
dp[i][0] = min(dp[i-1][1], dp[i-1][2])
elif A[i] == A[i-1]+1:
dp[i][0] = min(dp[i-1][0], dp[i-1][2])
elif A[i] == A[i-1]+2:
dp[i][0] = min(dp[i-1][0], dp[i-1][1])
else:
dp[i][0] = min([dp[i-1][0], dp[i-1][1], dp[i-1][2]])
if A[i] == A[i-1]+1:
dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + B[i]
elif A[i] == A[i-1]:
dp[i][1] = min(dp[i-1][0], dp[i-1][2]) + B[i]
elif A[i] == A[i-1]-1:
dp[i][1] = min(dp[i-1][1], dp[i-1][2]) + B[i]
else:
dp[i][1] = min([dp[i-1][0], dp[i-1][1], dp[i-1][2]]) + B[i]
if A[i] == A[i-1]:
dp[i][2] = min(dp[i-1][0], dp[i-1][1]) + 2*B[i]
elif A[i] == A[i-1]-1:
dp[i][2] = min(dp[i-1][0], dp[i-1][2]) + 2*B[i]
elif A[i] == A[i-1]-2:
dp[i][2] = min(dp[i-1][1], dp[i-1][2]) + 2*B[i]
else:
dp[i][2] = min([dp[i-1][0], dp[i-1][1], dp[i-1][2]]) + 2*B[i]
ans = min(dp[N-1])
print(ans)
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
'''input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
'''
from sys import stdin
from math import ceil, log
# main starts
q = int(stdin.readline().strip())
for _ in range(q):
n = int(stdin.readline().strip())
h = []
c = []
dp = dict()
for i in range(n):
a, b = list(map(int, stdin.readline().split()))
h.append(a)
c.append(b)
dp = [[0 for x in range(3)] for y in range(n)]
dp[0][0] = 0
dp[0][1] = c[0]
dp[0][2] = 2*c[0]
for i in range(1, n):
if h[i] == h[i - 1]:
dp[i][0] = min(dp[i - 1][1], dp[i- 1][2])
dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + c[i]
dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + 2 * c[i]
elif h[i] + 1 == h[i - 1]:
dp[i][0] = min(dp[i - 1])
dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) + c[i]
dp[i][2] = min(dp[i - 1][0], dp[i - 1][2]) + 2 * c[i]
elif h[i] + 2 == h[i - 1]:
dp[i][0] = min(dp[i - 1])
dp[i][1] = min(dp[i - 1])+ c[i]
dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + 2 * c[i]
elif h[i] == h[i - 1] + 1:
dp[i][0] = min(dp[i - 1][0], dp[i - 1][2])
dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + c[i]
dp[i][2] = min(dp[i - 1]) + 2 * c[i]
elif h[i] == h[i - 1] + 2:
dp[i][0] = min(dp[i - 1][:2])
dp[i][1] = min(dp[i - 1])+ c[i]
dp[i][2] = min(dp[i - 1]) + 2 * c[i]
else:
dp[i][0] = min(dp[i - 1])
dp[i][1] = min(dp[i - 1])+ c[i]
dp[i][2] = min(dp[i - 1]) + 2 * c[i]
print(min(dp[-1]))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
import sys
from math import inf as inf
for _ in range(int(input())):
n=int(sys.stdin.readline())
dp=[[inf,inf,inf] for i in range(n+1)]
a=[]
for i in range(n):
a.append(list(map(int,sys.stdin.readline().split())))
dp[0][0]=0
dp[0][1]=a[0][1]
dp[0][2]=2*a[0][1]
for i in range(1,n):
for j in range(3):
for k in range(3):
if a[i][0] + j != a[i-1][0] + k:
dp[i][j]=min(dp[i][j],dp[i-1][k] + j*a[i][1])
# print(dp)
print(min(dp[n-1]))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
from sys import stdin
test = stdin.readlines()
inf = float('infinity')
q = int(test[0])
k = 1
for _ in range(q):
n = int(test[k])
h, c = [], []
for j in range(k+1, k+n+1):
x, y = list(map(int, test[j].split()))
h.append(x)
c.append(y)
dp = [[0, c[0], 2 * c[0]]] + [[0, 0, 0] for i in range(n-1)]
for i in range(1, n):
for j in range(3):
x = (dp[i - 1][0] if h[i - 1] != h[i] + j else inf)
y = (dp[i - 1][1] if h[i - 1] + 1 != h[i] + j else inf)
z = (dp[i - 1][2] if h[i - 1] + 2 != h[i] + j else inf)
dp[i][j] = min(x, y, z) + j * c[i]
print(min(dp[n - 1]))
k += n + 1
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
from sys import stdin
test = stdin.readlines()
inf = float('infinity')
q = int(test[0])
k = 1
for _ in range(q):
n = int(test[k])
h, c = [], []
for j in range(k+1, k+n+1):
x, y = list(map(int, test[j].split()))
h.append(x)
c.append(y)
dp = [[0, c[0], 2 * c[0]]]
dp += [[0, 0, 0] for i in range(n-1)]
for i in range(1, n):
for j in range(3):
x = (dp[i - 1][0] if h[i - 1] != h[i] + j else inf)
y = (dp[i - 1][1] if h[i - 1] + 1 != h[i] + j else inf)
z = (dp[i - 1][2] if h[i - 1] + 2 != h[i] + j else inf)
dp[i][j] = min(x, y, z) + j * c[i]
print(min(dp[n - 1]))
k += n + 1
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
from sys import stdin
test = stdin.readlines()
inf = float('infinity')
q = int(test[0])
k = 1
for _ in range(q):
n = int(test[k])
h, c = [], []
for j in range(k+1, k+n+1):
x, y = list(map(int, test[j].split()))
h.append(x)
c.append(y)
dp = [[0, 0, 0] for i in range(n)]
dp[0] = [0, c[0], 2 * c[0]]
for i in range(1, n):
for j in range(3):
x = (dp[i - 1][0] if h[i - 1] != h[i] + j else inf)
y = (dp[i - 1][1] if h[i - 1] + 1 != h[i] + j else inf)
z = (dp[i - 1][2] if h[i - 1] + 2 != h[i] + j else inf)
dp[i][j] = min(x, y, z) + j * c[i]
print(min(dp[n - 1]))
k += n + 1
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
from sys import stdin
test = stdin.readlines()
inf = float('infinity')
q = int(test[0])
k = 1
for _ in range(q):
n = int(test[k])
h, c = [0]*n, [0]*n
for j in range(k+1, k+n+1):
h[j-k-1], c[j-k-1] = list(map(int, test[j].split()))
dp = [[0, c[0], 2 * c[0]]] + [[0, 0, 0] for i in range(n-1)]
for i in range(1, n):
for j in range(3):
x = (dp[i - 1][0] if h[i - 1] != h[i] + j else inf)
y = (dp[i - 1][1] if h[i - 1] + 1 != h[i] + j else inf)
z = (dp[i - 1][2] if h[i - 1] + 2 != h[i] + j else inf)
dp[i][j] = min(x, y, z) + j * c[i]
print(min(dp[n - 1]))
k += n + 1
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
import sys
input = sys.stdin.readline
t=int(input())
for rainbow in range(t):
n=int(input())
f=[tuple(map(int,input().split())) for i in range(n)]
dp=[0]*n;dp1=[0]*n;dp2=[0]*n
dp1[0]=f[0][1];dp2[0]=f[0][1]*2
for i in range(1,n):
a=f[i-1][0]
b=f[i][0]
if b==a:
dp[i]=min(dp1[i-1],dp2[i-1])
dp1[i]=f[i][1]+min(dp[i-1],dp2[i-1])
dp2[i]=f[i][1]*2+min(dp[i-1],dp1[i-1])
elif b==a+1:
dp[i]=min(dp[i-1],dp2[i-1])
dp1[i]=f[i][1]+min(dp[i-1],dp1[i-1])
dp2[i]=f[i][1]*2+min(dp[i-1],dp1[i-1],dp2[i-1])
elif b==a+2:
dp[i]=min(dp[i-1],dp1[i-1])
dp1[i]=f[i][1]+min(dp[i-1],dp1[i-1],dp2[i-1])
dp2[i]=f[i][1]*2+min(dp[i-1],dp1[i-1],dp2[i-1])
elif b==a-1:
dp[i]=min(dp[i-1],dp1[i-1],dp2[i-1])
dp1[i]=f[i][1]+min(dp1[i-1],dp2[i-1])
dp2[i]=f[i][1]*2+min(dp[i-1],dp2[i-1])
elif b==a-2:
dp[i]=min(dp[i-1],dp1[i-1],dp2[i-1])
dp1[i]=f[i][1]+min(dp[i-1],dp1[i-1],dp2[i-1])
dp2[i]=f[i][1]*2+min(dp1[i-1],dp2[i-1])
else:
dp[i]=min(dp[i-1],dp1[i-1],dp2[i-1])
dp1[i]=f[i][1]+min(dp[i-1],dp1[i-1],dp2[i-1])
dp2[i]=f[i][1]*2+min(dp[i-1],dp1[i-1],dp2[i-1])
print(min(dp[n-1],dp1[n-1],dp2[n-1]))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
"""
NTC here
"""
from sys import setcheckinterval, stdin, setrecursionlimit
setcheckinterval(1000)
setrecursionlimit(10**7)
# print("Case #{}: {} {}".format(i, n + m, n * m))
def iin(): return int(stdin.readline())
def lin(): return list(map(int, stdin.readline().split()))
for _ in range(iin()):
n=iin()
fence=[lin() for i in range(n)]
dp=[[0,j,2*j] for i,j in fence]
for i in range(1,n):
for j in range(3):
dp[i][j]+= min([dp[i-1][k] for k in range(3) if fence[i-1][0]+k!=fence[i][0]+j])
#print(*dp)
print(min(dp[-1]))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
# import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
sys.setrecursionlimit(100000)
input = sys.stdin.readline
def read_int():
return int(input())
def read_int_n():
return list(map(int, input().split()))
def read_float():
return float(input())
def read_float_n():
return list(map(float, input().split()))
def read_str():
return input().strip()
def read_str_n():
return list(map(str, input().split()))
def error_print(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.time()
ret = f(*args, **kwargs)
e = time.time()
error_print(e - s, 'sec')
return ret
return wrap
# @mt
def slv(N, AB):
memo = [0, AB[0][1], AB[0][1]*2]
for i in range(1, N):
a, b = AB[i]
a1, _ = AB[i-1]
memo2 = [0] * 3
for j in range(3):
tmp = 1e+1000
for k in range(3):
if a + j != a1 + k:
tmp = min(tmp, memo[k])
memo2[j] = tmp + j * b
memo = memo2
return min(memo)
def main():
Q = read_int()
for _ in range(Q):
N = read_int()
AB = [read_int_n() for _ in range(N)]
print(slv(N, AB))
# N = 100
# AB = [[1000000000, 1000000000] for _ in range(N)]
# print(slv(N, AB))
def __starting_point():
main()
__starting_point()
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
import sys
for q in range(int(input())):
n = int(sys.stdin.readline())
data = []
for i in range(n):
data.append([int(j) for j in sys.stdin.readline().split()])
dp = [[0, data[0][1], 2 * data[0][1]]]
for i in range(1, n):
a, b = data[i]
lasta = data[i-1][0]
l = dp[-1]
vals = [(lasta + j) for j in range(3)]
ans = [0]*3
for j in range(3):
w = a + j
x = [ l[k] for k in range(3) if lasta + k != w ]
# print(w, dp[-1], x)
ans[j] = j*b + min(x)
dp.append(ans)
print(min(dp[-1]))
# print(dp)
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
from math import inf as inf
import sys
for _ in range(int(input())):
n=int(sys.stdin.readline())
dp = [[inf,inf,inf] for _ in range(n+1)]
arr = []
for i in range(n):
arr.append(list(map(int,sys.stdin.readline().split())))
dp[0] = [0,arr[0][1],arr[0][1]*2]
for i in range(1,n):
for j in range(3):
for k in range(3):
if arr[i][0]+j!=arr[i-1][0]+k:
dp[i][j] = min(dp[i][j],dp[i-1][k]+j*arr[i][1])
print(min(dp[n-1]))
|
You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.
Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).
Calculate the minimum number of rubles you have to spend to make the fence great again!
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) β the number of boards in the fence.
The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) β the length of the $i$-th board and the price for increasing it by $1$, respectively.
It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.
It is guaranteed that answer to each query will not exceed $10^{18}$.
-----Output-----
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
Output
2
9
0
-----Note-----
In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.
In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.
In the third query the fence is great initially, so you don't need to spend rubles.
|
import sys, math
INF = float("inf")
def main():
def input():
return sys.stdin.readline()[:-1]
q = int(input())
for _ in range(q):
n = int(input())
w = [list(map(int,input().split())) for k in range(n)]
dp = [[INF,INF,INF] for k in range(n)]
dp[0] = [0,w[0][1],2*w[0][1]]
for k in range(1,n):
for l in range(3):
for m in range(3):
if w[k-1][0]+l != w[k][0]+m:
dp[k][m] = min(dp[k][m], dp[k-1][l] + w[k][1]*m)
print(min(dp[n-1]))
def __starting_point():
main()
__starting_point()
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
import sys
q = int(sys.stdin.readline().strip())
for t in range(0, q):
n, m = list(map(int, sys.stdin.readline().strip().split()))
L = []
R = [0] * n
C = [0] * m
for i in range (0, n):
L.append(sys.stdin.readline().strip())
for j in range (0, m):
if L[i][j] != "*":
R[i] = R[i] + 1
C[j] = C[j] + 1
ans = n + m - 1
for i in range (0, n):
for j in range (0, m):
x = 0
if L[i][j] != "*":
x = -1
ans = min([ans, R[i]+C[j]+x])
print(ans)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
import sys
Q = int(input())
for _ in range(Q):
N, M = map(int, sys.stdin.readline().split())
G = [[1 if s == '*' else 0 for s in sys.stdin.readline().strip()] for _ in range(N)]
ans = 10**9+7
G1 = [sum(g) for g in G]
G = list(map(list, zip(*G)))
G2 = [sum(g) for g in G]
G = list(map(list, zip(*G)))
K = N + M - 1
for i in range(N):
for j in range(M):
if G[i][j]:
ans = min(ans, K - (G1[i] + G2[j] - 1))
else:
ans = min(ans, K - (G1[i] + G2[j]))
sys.stdout.write('{}\n'.format(ans))
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
import sys
import math
input = sys.stdin.readline
q=int(input())
for i in range(q):
n,m=list(map(int,input().split()))
r=[0]*n
c=[0]*m
arr=[]
for i in range(n):
arr.append(input())
for i in range(n):
for j in range(m):
if arr[i][j]==".":
r[i]+=1
c[j]+=1
minn=1000000000
for i in range(n):
for j in range(m):
if arr[i][j]==".":
minn=min(minn,r[i]+c[j]-1)
else:
minn=min(minn,r[i]+c[j])
print(minn)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
q = int(input())
for query in range(q):
n, m = list(map(int, input().split()))
matrix = [input() for i in range(n)]
row = [0] * n
col = [0] * m
for i in range(n):
suma = 0
for j in range(m):
if matrix[i][j] == '*':
suma += 1
row[i] = suma
for j in range(m):
suma = 0
for i in range(n):
if matrix[i][j] == '*':
suma += 1
col[j] = suma
wynik = 100000000000
for i in range(n):
for j in range(m):
pom = m + n - 1 - (row[i] + col[j] - (1 if matrix[i][j] == '*' else 0))
wynik = min(wynik, pom)
print(wynik)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
from sys import stdin
c=int(stdin.readline().strip())
for i in range(c):
n,m=list(map(int,stdin.readline().strip().split()))
s=[stdin.readline().strip() for j in range(n)]
ms=[]
ns=[]
for j in range(n):
ns.append(s[j].count("."))
for j in range(m):
ms.append(0)
for k in range(n):
if s[k][j]==".":
ms[-1]+=1
ans=10**15
for j in range(n):
for k in range(m):
x=ns[j]+ms[k]
if s[j][k]==".":
x-=1
ans=min(ans,x)
print(ans)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
t = int(input())
for i in range(t):
e,s = list(map(int, input().split()))
f = []
for h in range(e):
f.append(input())
now = 0
mi = 0
mi2 = []
g = []
for y in range(e):
now = 0
g = []
for x in range(s):
if(f[y][x]=="*"):
now+=1
else:
g.append(x)
if(now>mi):
mi = now
mi2 = []
for k in range(len(g)):
mi2.append(g[k])
elif(now==mi):
for k in range(len(g)):
mi2.append(g[k])
now = 0
ma = 0
d = True
for y in range(s):
now = 0
for x in range(e):
if(f[x][y]=="*"):
now+=1
if(now>ma):
ma = now
if(y in mi2):
d = False
else:
d = True
elif(now==ma):
if(y in mi2):
d = False
if(d):
print(e-mi+s-ma)
else:
print(e-mi+s-ma-1)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
import sys
input = sys.stdin.readline
Q=int(input())
for testcases in range(Q):
n,m=list(map(int,input().split()))
MAP=[list(input().strip()) for i in range(n)]
R=[MAP[i].count("*") for i in range(n)]
C=[]
for j in range(m):
M=[MAP[i][j] for i in range(n)]
C.append(M.count("*"))
ANS=float("inf")
for i in range(n):
for j in range(m):
if MAP[i][j]=="*":
ANS=min(ANS,n+m-R[i]-C[j])
else:
ANS=min(ANS,n+m-R[i]-C[j]-1)
print(ANS)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
n=int(input())
for i in range(n):
x,y=list(map(int,input().split()))
stolb=[0]*y
stroki=[0]*x
al=[]
for j in range(x):
st=input()
al.append(st)
for j2 in range(y):
if st[j2]=='.':
stroki[j]+=1
stolb[j2]+=1
mi=1000000000000000
for j in range(x):
for j2 in range(y):
if al[j][j2]=='.':
if stroki[j]+stolb[j2]-1<mi:
mi=stroki[j]+stolb[j2]-1
else:
if stroki[j]+stolb[j2]<mi:
mi=stroki[j]+stolb[j2]
print(mi)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
q = int(input())
for _ in range(q):
n, m = list(map(int, input().split()))
d = [list(input()) for _ in range(n)]
count_x = list([x.count('*') for x in d])
count_y = list([x.count('*') for x in zip(*d)])
#dt = map(list, zip(*d))
rls = n+m-1
for i in range(n):
for j in range(m):
a = count_x[i]
b = count_y[j]
if d[i][j] == '*':
rls = min(rls, n+m-a-b)
else:
rls = min(rls, n+m-a-b-1)
print(rls)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
import io, sys, atexit, os
import math as ma
from decimal import Decimal as dec
from itertools import permutations
from itertools import combinations
def li ():
return list (map (int, input ().split ()))
def num ():
return map (int, input ().split ())
def nu ():
return int (input ())
def find_gcd ( x, y ):
while (y):
x, y = y, x % y
return x
def lcm ( x, y ):
gg = find_gcd (x, y)
return (x * y // gg)
mm = 1000000007
yp = 0
def solve ():
t =nu()
for tt in range (t):
n,m=num()
a=[0]*n
x=[0]*n
y=[0]*m
for i in range(n):
p=input()
a[i]=[0]*m
for j in range(m):
if(p[j]=="*"):
a[i][j]=1
x[i]=sum(a[i])
for i in range(m):
cc=0
for j in range(n):
cc+=a[j][i]
y[i]=cc
mn=9999999999
for i in range(n):
for j in range(m):
pp=0
pp = (n - x [ i ]) + (m - y [ j ])
if(a[i][j]==0):
pp-=1
mn=min(mn,pp)
print(mn)
def __starting_point():
solve ()
__starting_point()
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
from fractions import gcd
import math
def inverse(a):
return pow(a, mod - 2, mod)
def usearch(x, a):
lft = 0
rgt = len(a) + 1
while rgt - lft > 1:
mid = (rgt + lft) // 2
if a[mid] <= x:
lft = mid
else:
rgt = mid
return lft
def main():
q = int(input())
for i in range(q):
n,m = list(map(int, input().split()))
a = [input() for i in range(n)]
h = 0
h_a = []
for i in range(m):
tmp = 0
for j in range(n):
if a[j][i] == '*':
tmp += 1
if h < tmp:
h = tmp
h_a = [i]
if h == tmp:
h_a.append(i)
w = 0
w_a = []
for i in range(n):
tmp = len([i for i in a[i] if i == '*'])
if w < tmp:
w = tmp
w_a = [i]
if w == tmp:
w_a.append(i)
flag = 0
for i in h_a:
for j in w_a:
if a[j][i] == '.':
flag = 1
break
print(n+m-h-w-flag)
main()
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
for _ in range(int(input())):
m,n=list(map(int,input().strip().split(' ')))
grid=[]
for i in range(m):
grid+=[input()]
row=[]
col=[]
MIN_row=10**10
MIN_col=10**10
for i in range(m):
count=0
for j in range(n):
if grid[i][j]==".":
count+=1
row+=[count]
MIN_row=min(MIN_row,count)
for j in range(n):
count=0
for i in range(m):
if grid[i][j]==".":
count+=1
col+=[count]
MIN_col=min(MIN_col,count)
want_row=set([])
for i in range(len(row)):
if row[i]==MIN_row:
want_row.add(i)
want_col=set([])
for i in range(len(col)):
if col[i]==MIN_col:
want_col.add(i)
flag=0
for i in range(len(row)):
if flag==1:
break
for j in range(len(col)):
if grid[i][j]==".":
if i in want_row and j in want_col:
flag=1
break
print(MIN_row+MIN_col-flag)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
from sys import stdin, stdout
import collections
import math
T = int(input())
#A,H,Q = [int(x) for x in stdin.readline().split()]
for t in range(T):
N,M = [int(x) for x in stdin.readline().split()]
#print(N,M)
data = []
row = [M]*N
col = [N]*M
for i in range(N):
s = input()
data.append(list(s))
for j in range(M):
if s[j]=='*':
row[i] -= 1
col[j] -= 1
min_row = min(row)
min_col = min(col)
if min_row==0 or min_col==0:
print(min_row+min_col)
else:
r = []
c = []
#res = 9999999
for i in range(N):
if row[i]==min_row:
r.append(i)
for j in range(M):
if col[j]==min_col:
c.append(j)
flag = 0
for x in r:
for y in c:
if data[x][y]=='.' and flag==0:
print(min_row+min_col-1)
flag = 1
if flag==1:
break
if flag==0:
print(min_row+min_col)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
q = int(input())
for i in range(q):
n, m = list(map(int,input().split()))
field = [input() for j in range(n)]
raw = [0] * n
col = [0] * m
num = 0
for j in range(n):
for z in range(m):
if field[j][z] == "*":
raw[j] += 1
col[z] += 1
for z in range(m):
for j in range(n):
temp = raw[j] + col[z] - int(field[j][z] == "*")
if num < temp:
num = temp
print(n + m - num - 1)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
def gns():
return list(map(int,input().split()))
t=int(input())
def one():
n, m = gns()
mp = []
ns = [0] * n
ms = [0] * m
for i in range(n):
mp.append(input())
for j in range(m):
if mp[-1][j] == '*':
ns[i] += 1
ms[j] += 1
min_n = max(ns)
min_m = max(ms)
min_ns = set([i for i in range(n) if ns[i] == min_n])
min_ms = set([i for i in range(m) if ms[i] == min_m])
ans = n - min_n + m - min_m
for i in min_ns:
for j in min_ms:
if mp[i][j] == '.':
print(ans - 1)
return
print(ans)
for i in range(t):
one()
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
for TT in range(1, int(input()) + 1):
n, m = map(int, input().split())
mat = [[1 if c == '*' else 0 for c in input()] for _ in range(n)]
h = [sum(l) for l in mat]
v = [0] * m
for i in range(n):
for j in range(m):
v[j] += mat[i][j]
res = float('inf')
for i in range(n):
for j in range(m):
val = (n - h[i]) + (m - v[j])
if mat[i][j] == 0: val -= 1
res = min(res, val)
print(res)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
import sys
input = lambda: sys.stdin.readline().strip()
print = lambda x: sys.stdout.write(f"{x}\n")
for t in range(int(input())):
r, c = map(int, input().split())
arr = [input() for i in range(r)]
rarr = [0] * r
carr = [0] * c
for i in range(r):
for j in range(c):
if arr[i][j] == '.':
rarr[i] += 1
carr[j] += 1
ans = float('inf')
for i in range(r):
for j in range(c):
s = rarr[i] + carr[j]
if arr[i][j] == '.':
ans = min(ans, s - 1)
else:
ans = min(ans, s)
print(ans)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
def __starting_point():
T = int(input())
#fout = open('1194B.out', 'w')
for _ in range(T):
s = input().rstrip().split()
n = int(s[0])
m = int(s[1])
cell = [[0 for j in range(m)] for i in range(n)]
r = [0 for i in range(n)]
c = [0 for j in range(m)]
for i in range(n):
s = input()
for j in range(len(s)):
cell[i][j] = s[j]
if s[j] == '*':
r[i] += 1
c[j] += 1
nmax = 0
for i in range(n):
for j in range(m):
if r[i] + c[j] + (cell[i][j] == '.') > nmax:
nmax = r[i] + c[j] + (cell[i][j] == '.')
print(str(m + n - nmax) + '\n')
__starting_point()
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
q = int(input())
for _ in range(q):
ans = 10000000000
n, m = list(map(int, input().split()))
picture = []
dotcount_w = []
dotcount_h = []
for _ in range(n):
picture.append(input())
dotcount_w.append(picture[-1].count("."))
for i in range(m):
count = 0
for j in range(n):
if picture[j][i] == ".":
count += 1
dotcount_h.append(count)
for i in range(m):
for j in range(n):
if picture[j][i] == ".":
ans = min(dotcount_h[i]+dotcount_w[j]-1, ans)
else:
ans = min(dotcount_h[i]+dotcount_w[j], ans)
print(ans)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
q = int(input())
for i in range(q):
n,m = list(map(int,input().split()))
a = [input() for _ in range(n)]
a_t = list(zip(*a))
mx_1 = mx_2 = 0
idx_1 = []
idx_2 = []
for j,i in enumerate(a):
temp = i.count('*')
if temp > mx_1:
mx_1 = temp
idx_1 = [j]
elif temp == mx_1:
idx_1.append(j)
for j,i in enumerate(a_t):
temp = i.count('*')
if temp > mx_2:
mx_2 = temp
idx_2 = [j]
elif temp == mx_2:
idx_2.append(j)
ans = 0
for i in idx_1:
for j in idx_2:
if a[i][j] == '.':
ans = -1
print(n+m-mx_1-mx_2+ans)
|
You are given a picture consisting of $n$ rows and $m$ columns. Rows are numbered from $1$ to $n$ from the top to the bottom, columns are numbered from $1$ to $m$ from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers $x$ and $y$, where $1 \le x \le n$ and $1 \le y \le m$, such that all cells in row $x$ and all cells in column $y$ are painted black.
For examples, each of these pictures contain crosses:
[Image]
The fourth picture contains 4 crosses: at $(1, 3)$, $(1, 5)$, $(3, 3)$ and $(3, 5)$.
Following images don't contain crosses:
[Image]
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 5 \cdot 10^4$) β the number of queries.
The first line of each query contains two integers $n$ and $m$ ($1 \le n, m \le 5 \cdot 10^4$, $n \cdot m \le 4 \cdot 10^5$) β the number of rows and the number of columns in the picture.
Each of the next $n$ lines contains $m$ characters β '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that $\sum n \le 5 \cdot 10^4$ and $\sum n \cdot m \le 4 \cdot 10^5$.
-----Output-----
Print $q$ lines, the $i$-th line should contain a single integer β the answer to the $i$-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
-----Example-----
Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
0
0
0
0
0
4
1
1
2
-----Note-----
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint $(1, 3)$, $(3, 1)$, $(5, 3)$ and $(3, 5)$ on the $6$-th picture to get a cross in $(3, 3)$. That'll take you $4$ minutes.
You can paint $(1, 2)$ on the $7$-th picture to get a cross in $(4, 2)$.
You can paint $(2, 2)$ on the $8$-th picture to get a cross in $(2, 2)$. You can, for example, paint $(1, 3)$, $(3, 1)$ and $(3, 3)$ to get a cross in $(3, 3)$ but that will take you $3$ minutes instead of $1$.
There are 9 possible crosses you can get in minimum time on the $9$-th picture. One of them is in $(1, 1)$: paint $(1, 2)$ and $(2, 1)$.
|
M = 10**9 + 7
R = lambda: map(int, input().split())
n = int(input())
for i in range(n):
n,m = R()
L = [[0 for i in range(m)] for j in range(n)]
a = []
for i in range(n):
a.append(input())
for i in range(n):
c = 0
for j in range(m):
if a[i][j] == '.':c += 1
for j in range(m):
L[i][j] = c
for i in range(m):
c = 0
for j in range(n):
if a[j][i] == '.':c += 1
for j in range(n):
L[j][i] += c
mi = 10**9
k = []
for i in range(n):
for j in range(m):
mi = min(mi,L[i][j])
for i in range(n):
for j in range(m):
if L[i][j] == mi and a[i][j] == '.':
mi -= 1
print(mi)
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
from math import ceil
t = int(input())
for _ in range(t):
n = int(input())
pf = []
for i in range(2, ceil(n**0.5)+1):
while n % i == 0:
pf.append(i)
n //= i
if n > 1:
pf.append(n)
if len(pf) == 2 and pf[0] != pf[1]:
print(pf[0], pf[1], pf[0]*pf[1])
print(1)
else:
pg = []
fac = []
nfac = []
while len(pf) > 0:
p = pf[-1]
mul = 0
while len(pf) > 0 and pf[-1] == p:
pf.pop()
mul += 1
pg.append([mul, p])
pg.sort()
pg = pg[::-1]
# print(pg)
cur = 0
if pg[0][0] == 1:
a = pg[0][1]
b = pg[1][1]
c = pg[2][1]
fac = [a, a*b*c, a*b, b, b*c, c, a*c]
cur = 3
else:
fac = [pg[0][1]**i for i in range(1, pg[0][0]+1)]
cur = 1
while cur < len(pg):
mul = pg[cur][0]
p = pg[cur][1]
nfac = []
for i in range(len(fac)):
if i == 0:
nfac += [fac[i]*(p**j) for j in range(mul, -1, -1)]
else:
nfac += [fac[i]*(p**j) for j in range(mul+1)]
nfac += [p**i for i in range(1, mul+1)]
fac = nfac
cur += 1
print(" ".join([str(i) for i in fac]))
print(0)
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
def gcd(a, b):
while b: a, b = b, a % b
return a
def isPrimeMR(n):
d = n - 1
d = d // (d & -d)
L = [2, 7, 61] if n < 1<<32 else [2, 3, 5, 7, 11, 13, 17] if n < 1<<48 else [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
for a in L:
t = d
y = pow(a, t, n)
if y == 1: continue
while y != n - 1:
y = y * y % n
if y == 1 or t == n - 1: return 0
t <<= 1
return 1
def findFactorRho(n):
m = 1 << n.bit_length() // 8
for c in range(1, 99):
f = lambda x: (x * x + c) % n
y, r, q, g = 2, 1, 1, 1
while g == 1:
x = y
for i in range(r):
y = f(y)
k = 0
while k < r and g == 1:
ys = y
for i in range(min(m, r - k)):
y = f(y)
q = q * abs(x - y) % n
g = gcd(q, n)
k += m
r <<= 1
if g == n:
g = 1
while g == 1:
ys = f(ys)
g = gcd(abs(x - ys), n)
if g < n:
if isPrimeMR(g): return g
elif isPrimeMR(n // g): return n // g
return findFactorRho(g)
def primeFactor(n):
i = 2
ret = {}
rhoFlg = 0
while i * i <= n:
k = 0
while n % i == 0:
n //= i
k += 1
if k: ret[i] = k
i += i % 2 + (3 if i % 3 == 1 else 1)
if i == 101 and n >= 2 ** 20:
while n > 1:
if isPrimeMR(n):
ret[n], n = 1, 1
else:
rhoFlg = 1
j = findFactorRho(n)
k = 0
while n % j == 0:
n //= j
k += 1
ret[j] = k
if n > 1: ret[n] = 1
if rhoFlg: ret = {x: ret[x] for x in sorted(ret)}
return ret
def divisors(pf):
ret = [1]
for p in pf:
ret_prev = ret
ret = []
for i in range(pf[p]+1):
for r in ret_prev:
ret.append(r * (p ** i))
return sorted(ret)
T = int(input())
for _ in range(T):
N = int(input())
pf = primeFactor(N)
dv = divisors(pf)
if len(pf) == 2 and len(dv) == 4:
print(*dv[1:])
print(1)
continue
if len(pf) == 1:
print(*dv[1:])
print(0)
continue
lpf = list(pf)
# print("lpf =", lpf)
X = [[] for _ in range(len(pf))]
S = {1}
if len(lpf) == 2:
X[0].append(lpf[0] * lpf[1])
X[1].append(N)
S.add(lpf[0] * lpf[1])
S.add(N)
for i, p in enumerate(lpf):
for j in range(1, pf[p] + 1):
X[i].append(p ** j)
S.add(p ** j)
else:
for i, p in enumerate(lpf):
# print("i, p, pf[p] =", i, p, pf[p])
X[i].append(lpf[i-1] * p)
S.add(lpf[i-1] * p)
for j in range(1, pf[p] + 1):
X[i].append(p ** j)
S.add(p ** j)
for a in dv:
if a not in S:
for i, p in enumerate(lpf):
if a % p == 0:
X[i].append(a)
break
# print("X =", X)
ANS = []
for x in X:
for y in x:
ANS.append(y)
print(*ANS)
print(0)
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
t = int(input())
for _ in range(t):
works = 0
n = int(input())
ps = []
for i in range(2,4*10**4):
c = 0
while n % i == 0:
c += 1
n //= i
if c:
ps.append((i,c))
if n > 1:
ps.append((n,1))
if len(ps) >= 3:
base = [0] * (2 * len(ps))
lists = [[] for i in range(2 * len(ps))]
for i in range(len(ps)):
base[2*i] = ps[i][0]
base[2*i+1] = ps[i][0] * ps[(i + 1)%len(ps)][0]
factors = [1]
for p,pp in ps:
mult = [pow(p,i) for i in range(pp+1)]
new = []
for m in mult:
for f in factors:
new.append(m*f)
factors = new
for v in factors:
if v in base:
lists[base.index(v)] += [v]
else:
for u in range(2*len(ps)):
if v % base[u] ==0:
lists[u] += [v]
break
out=sum(lists,[])
elif len(ps) == 2:
p, q = ps
if p[1] < q[1]:
p,q=q,p
p, pp = p
q, qq = q
if pp == 1 and qq == 1:
works = 1
out = [p,p*q,q]
else:
base = [p,p*q,q,p*p*q]
lists = [[],[],[],[]]
for i in range(pp + 1):
for j in range(qq + 1):
v = pow(p,i) * pow(q,j)
if v in base:
lists[base.index(v)] += [v]
else:
for u in range(4):
if v % base[u]==0:
lists[u] += [v]
break
out=sum(lists,[])
else:
out = []
for i in range(1, ps[0][1]+1):
out.append(pow(ps[0][0],i))
print(' '.join(map(str,out)))
print(works)
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
import itertools
y=lambda:int(input())
p=[1]*32000
for i in range(180):
if p[i]:
for j in range(2*i+2,len(p),i+2):p[j]=0
q=[i+2 for i in range(len(p))if p[i]]
for _ in range(y()):
n=y();d=[];e=set()
for i in q:
if n%i<1:
n//=i;d.append([i,1])
while n%i<1:n//=i;d[-1][1]+=1
if n>1:d.append([n,1])
l=len(d)
for i in itertools.product(*(range(i[1]+1)for i in d)):
p=1
for j in range(l):p*=d[j][0]**i[j]
e.add(p)
e.remove(1)
b=l==2 and d[0][1]+d[1][1]==2
if l<2 or b:f=list(e)
elif l<3:
s=d[1][1]>1
v=d[s][0]*d[1-s][0]
f=[v]
e.remove(v)
k=set()
for i in e:
if i%d[1-s][0]<1:k.add(i);f.append(i)
v=(d[s][0]**2)*d[1-s][0]
f.remove(v)
f.append(v)
e-=k
for i in e:f.append(i)
else:
v=d[0][0]*d[-1][0]
f=[v]
e.remove(v)
for i in range(l):
v=d[i][0]*d[i-1][0]
f.remove(v)
f.append(v)
k=set()
for j in e:
if j%d[i][0]<1:k.add(j);f.append(j)
e-=k
print(' '.join(map(str,f)))
print(int(b))
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
import sys
input=sys.stdin.readline
t=int(input())
for you in range(t):
n=int(input())
z=n
primes=[]
i=2
while(i*i<=z):
if(z%i==0):
primes.append(i)
while(z%i==0):
z=z//i
i+=1
if(z!=1):
primes.append(z)
hashi=dict()
for i in primes:
hashi[i]=[]
hashinew=dict()
new=[]
k=len(primes)
hasho=dict()
if(k>2):
for i in range(k):
new.append(primes[i]*primes[(i+1)%k])
hasho[primes[i]*primes[(i+1)%k]]=1
if(k==2):
hasho[primes[0]*primes[1]]=1
i=2
while(i*i<=n):
if(n%i==0):
num1=i
num2=n//i
if(num1 not in hasho):
for j in primes:
if(num1%j==0):
break
hashi[j].append(num1)
if(num2!=num1 and num2 not in hasho):
for j in primes:
if(num2%j==0):
break
hashi[j].append(num2)
i+=1
for j in primes:
if(n%j==0):
break
hashi[j].append(n)
done=dict()
if(len(primes)==1):
for i in hashi[primes[0]]:
print(i,end=" ")
print()
print(0)
continue
if(len(primes)==2):
if(primes[0]*primes[1]==n):
print(primes[0],primes[1],n)
print(1)
else:
for i in hashi[primes[0]]:
print(i,end=" ")
for i in hashi[primes[1]]:
print(i,end=" ")
print(primes[0]*primes[1],end=" ")
print()
print(0)
continue
for i in range(k):
for j in hashi[primes[i]]:
print(j,end=" ")
ko=primes[i]*primes[(i+1)%k]
print(ko,end=" ")
print()
print(0)
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
import sys
input=lambda:sys.stdin.readline().rstrip()
def gcd(a, b):
while b: a, b = b, a % b
return a
def isPrimeMR(n):
d = n - 1
d = d // (d & -d)
L = [2, 3, 61]
for a in L:
t = d
y = pow(a, t, n)
if y == 1: continue
while y != n - 1:
y = (y * y) % n
if y == 1 or t == n - 1: return 0
t <<= 1
return 1
def findFactorRho(n):
m = 1 << n.bit_length() // 8
for c in range(1, 99):
f = lambda x: (x * x + c) % n
y, r, q, g = 2, 1, 1, 1
while g == 1:
x = y
for i in range(r):
y = f(y)
k = 0
while k < r and g == 1:
ys = y
for i in range(min(m, r - k)):
y = f(y)
q = q * abs(x - y) % n
g = gcd(q, n)
k += m
r <<= 1
if g == n:
g = 1
while g == 1:
ys = f(ys)
g = gcd(abs(x - ys), n)
if g < n:
if isPrimeMR(g): return g
elif isPrimeMR(n // g): return n // g
return findFactorRho(g)
def primeFactor(n):
i = 2
ret = {}
rhoFlg = 0
while i*i <= n:
k = 0
while n % i == 0:
n //= i
k += 1
if k: ret[i] = k
i += 1 + i % 2
if i == 101 and n >= 2 ** 20:
while n > 1:
if isPrimeMR(n):
ret[n], n = 1, 1
else:
rhoFlg = 1
j = findFactorRho(n)
k = 0
while n % j == 0:
n //= j
k += 1
ret[j] = k
if n > 1: ret[n] = 1
if rhoFlg: ret = {x: ret[x] for x in sorted(ret)}
return ret
for _ in range(int(input())):
n=int(input())
ret=primeFactor(n)
m=len(ret)
s=1
a=[]
for i in ret:
a.append(i)
s*=ret[i]+1
ans=[]
for i in range(m):
s//=ret[a[i]]+1
for j in range(1,ret[a[i]]+1):
for k in range(s):
x=a[i]**j
for l in range(i+1,m):
k,t=divmod(k,ret[a[l]]+1)
x*=a[l]**t
ans.append(x)
if gcd(ans[0],ans[-1])==1:
del ans[ans.index(ans[0]*ans[-1])]
ans.append(ans[0]*ans[-1])
anss=0
for i in range(len(ans)-1):
if gcd(ans[i],ans[i+1])==1:anss+=1
print(*ans)
print(anss)
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
from functools import lru_cache
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')]
def isprime(n):
for j in range(2, int(n ** 0.5) + 1):
if n % j == 0:return 0
return 1
for _ in range(val()):
n = val()
l1 = factors(n)[1:]
l = []
for j in l1:
if isprime(j):l.append(j)
l1 = set(l1)
l1 -= set(l)
# print(l, l1)
d = defaultdict(set)
for j in range(len(l)):
for i in sorted(list(l1)):
if i % l[j] == 0 and i % l[j - 1] == 0:
d[tuple(sorted([l[j], l[j - 1]]))].add(i)
l1.remove(i)
break
# print(l, l1)
for j in range(len(l)):
for i in sorted(list(l1)):
if i % l[j] == 0 and i % l[j - 1] == 0:
d[tuple(sorted([l[j], l[j - 1]]))].add(i)
l1.remove(i)
# print(l, l1, d)
only = defaultdict(list)
for j in range(len(l)):
for i in sorted(list(l1)):
if i % l[j] == 0:
only[l[j]].append(i)
l1.remove(i)
fin = []
if len(l) == 2:
fin.append(l[0])
for j in only[l[0]]:fin.append(j)
for i in range(len(l)):
for j in list(d[tuple(sorted([l[i], l[(i + 1) % len(l)]]))]):
fin.append(j)
d[tuple(sorted([l[i], l[(i + 1) % len(l)]]))].remove(j)
if i != len(l) - 1:break
if i != len(l) - 1:
fin.append(l[i + 1])
for j in only[l[i + 1]]:
fin.append(j)
else:
fin.append(l[0])
for j in only[l[0]]:fin.append(j)
for i in range(len(l)):
for j in d[tuple(sorted([l[i], l[(i + 1) % len(l)]]))]:
fin.append(j)
if i != len(l) - 1:
fin.append(l[i + 1])
for j in only[l[i + 1]]:
fin.append(j)
ans = 0
for i in range(len(fin)):
if math.gcd(fin[i], fin[i - 1]) == 1:ans += 1
print(*fin)
print(ans)
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
from sys import stdin
input = stdin.readline
q = int(input())
for _ in range(q):
n = int(input())
pier = []
i = 2
nn = n
while True:
if nn%i == 0:
pier.append(i)
nn//=i
else:
i+= 1
if i**2 > n:
break
if nn != 1:
pier.append(nn)
pier_unique = list(set(pier))
dzielniki = [1]
for p in pier_unique:
pot = p
addition = []
while n%pot == 0:
addition1 = [d*pot for d in dzielniki]
addition += addition1
pot *= p
dzielniki += addition
dzielniki = dzielniki[1:]
k = len(pier_unique)
if k == 1:
print(*dzielniki)
print(0)
elif k >= 3:
dzielniki = set(dzielniki)
odp = []
for i in range(k):
dzielniki.remove(pier_unique[i-1]*pier_unique[i])
for i in range(k):
odp.append(pier_unique[i-1]*pier_unique[i])
to_rem = []
for dz in dzielniki:
if dz%pier_unique[i] == 0:
to_rem.append(dz)
odp.append(dz)
for to in to_rem:
dzielniki.remove(to)
print(*odp)
print(0)
else:
p = pier_unique[0]
q = pier_unique[1]
if n == p*q:
print(p,q,p*q)
print(1)
else:
test = p**2
if n%test != 0:
p,q= q,p
#p^2 dzieli n
dzielniki = set(dzielniki)
dzielniki.remove(p*q)
dzielniki.remove(p*p*q)
odp = [p*q]
to_rem = []
for dzu in dzielniki:
if dzu%p == 0:
to_rem.append(dzu)
odp.append(dzu)
for tu in to_rem:
dzielniki.remove(tu)
odp.append(p*p*q)
for dzu in dzielniki:
odp.append(dzu)
print(*odp)
print(0)
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
from sys import stdin, stdout
import math
import bisect
def gcd(a,b):
while b > 0:
a, b = b, a % b
return a
def solve(n):
dv = [n]
x = 2
while x*x <= n:
if n%x == 0:
dv.append(x)
if x != n//x:
dv.append(n//x)
x += 1
dv = sorted(dv)
ans = [0]*len(dv)
ans[0], ans[-1] = dv[0], dv[-1]
seen = {dv[0], dv[-1]}
cur_prime = dv[0]
min_prime = dv[0]
while len(seen) < len(dv):
for x in dv:
if x in seen: continue
if min_prime == -1:
min_prime = x
if cur_prime == -1:
if ans[len(seen)-2]%x == 0:
cur_prime = x
ans[len(seen)-1] = x
seen.add(x)
else:
if x%cur_prime == 0:
ans[len(seen)-1] = x
seen.add(x)
if cur_prime == -1:
cur_prime = min_prime
else:
cur_prime = -1
min_prime = -1
cnt = 0
for i in range(1, len(ans)):
if gcd(ans[i], ans[i-1]) == 1:
cnt += 1
print(" ".join(map(str, ans)))
print(cnt)
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
solve(n)
#for i in range(2, 50):
# solve(i)
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
# prime power always 0
# two prime powers?
#
# == 1 prime factor
# trivial
# == 2 prime factors
# p^a q^b
# if a + b > 2: then fine
# p*q
# (remaining with p)
# p*p*q
# (remaining with q)
# >= 3 prime factors is fine
# what ordering?
# p*q*r
# (all left with p)
# p*q
# (all left with q)
# q*r
# (all left with r)
from collections import defaultdict as dd, deque
def factor(n):
factors = dd(int)
d = 2
while d*d <= n:
while n%d == 0:
factors[d] += 1
n //= d
d += 1
if n != 1:
factors[n] += 1
return factors
def divisors(n) :
i = 1
factors = []
while i*i <= n:
if n % i == 0:
# If divisors are equal, print only one
if n//i == i:
factors.append(i)
else :
factors.append(i)
factors.append(n//i)
i += 1
return factors
t = int(input())
for _ in range(t):
n = int(input())
F = factor(n)
D = set(divisors(n))
D.remove(1)
if len(F) == 1:
print(*list(D))
print(0)
continue
if len(F) == 2:
p,q = list(F)
exp = sum(F.values())
if exp > 2:
res = []
D.remove(p*q)
D.remove(p*p*q)
divP = {d for d in D if d%p == 0}
divQ = D - divP
print(p*q, *divP, p*p*q, *divQ)
print(0)
else:
print(p, p*q, q)
print(1)
continue
first = 1
for prime in F:
first *= prime
D.remove(first)
Flist = list(F)
res = [first]
for i in range(len(Flist)-1):
p,q = Flist[i-1],Flist[i]
D.remove(p*q)
for i in range(len(Flist)-1):
p,q = Flist[i-1],Flist[i]
div = {d for d in D if d%p == 0}
D -= div
res.extend(div)
res.append(p*q)
res.extend(D)
print(*res)
print(0)
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
from itertools import product
def p_factorization_t(n):
if n == 1: return []
pf_cnt = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i == 0:
cnt = 0
while temp%i == 0:
cnt += 1
temp //= i
pf_cnt.append((i,cnt))
if temp != 1: pf_cnt.append((temp,1))
return pf_cnt
def main():
ansl = []
for _ in range(int(input())):
n = int(input())
facs = p_factorization_t(n)
# print(facs)
if len(facs) == 1:
p,cnt = facs[0]
al = []
for i in range(1,cnt+1):
al.append(pow(p,i))
print(*al)
print(0)
ff = []
pd = {}
ps = []
for p,cnt in facs:
row = []
for i in range(0,cnt+1):
row.append(pow(p,i))
ff.append(row)
pd[p] = []
ps.append(p)
vals = [1]
for row in ff:
new_vals = []
for v in vals:
for p in row:
new_vals.append(p*v)
if p != 1:
pd[row[1]].append(v*p)
vals = new_vals[:]
if len(facs) >= 3:
al = []
for i in range(len(ps)):
cval = -1
if i > 0:
cval = (ps[i]*ps[i-1])
al.append(cval)
else:
cval = (ps[i]*ps[-1])
for v in pd[ps[i]]:
if v != cval:
al.append(v)
print(*al)
print(0)
elif len(facs) == 2:
al = []
for i in range(len(ps)):
cval = -1
if i > 0:
cval = (ps[i]*ps[i-1])
al.append(cval)
else:
cval = (ps[i]*ps[-1])
for v in pd[ps[i]]:
if v != cval:
al.append(v)
print(*al)
if facs[0][1] == 1 and facs[1][1] == 1:
print(1)
else:
print(0)
# elif len(facs) == 2:
def __starting_point():
main()
__starting_point()
|
An agent called Cypher is decrypting a message, that contains a composite number $n$. All divisors of $n$, which are greater than $1$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.
In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.
A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.
Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
-----Input-----
The first line contains an integer $t$ $(1 \le t \le 100)$Β β the number of test cases. Next $t$ lines describe each test case.
In a single line of each test case description, there is a single composite number $n$ $(4 \le n \le 10^9)$Β β the number from the message.
It's guaranteed that the total number of divisors of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case in the first line output the initial order of divisors, which are greater than $1$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message.
If there are different possible orders with a correct answer, print any of them.
-----Example-----
Input
3
6
4
30
Output
2 3 6
1
2 4
0
2 30 6 3 15 5 10
0
-----Note-----
In the first test case $6$ has three divisors, which are greater than $1$: $2, 3, 6$. Regardless of the initial order, numbers $2$ and $3$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $2, 6, 3, 6$, and every two adjacent numbers are not coprime.
In the second test case $4$ has two divisors greater than $1$: $2, 4$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.
In the third test case all divisors of $30$ greater than $1$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
def gen(i, cur):
nonlocal dvs, used
if i == len(kk):
if (ohne != 1 or cur != 1) and (ok or not used[cur * ohne]):
dvs.append(cur * ohne)
return
gen(i + 1, cur)
for j in range(kk[i]):
cur *= pp[i]
gen(i + 1, cur)
gans = []
for _ in range(int(input())):
n = int(input())
pp = []
kk = []
i = 2
cnt = []
while i * i <= n:
if n % i == 0:
pp.append(i)
kk.append(0)
while n % i == 0:
kk[-1] += 1
n //= i
i += 1
if n != 1:
pp.append(n)
kk.append(1)
dvs = []
ohne = 1
ok = True
gen(0, 1)
if len(pp) == 1:
gans.append(' '.join(map(str, dvs)))
gans.append(str(0))
elif len(pp) == 2 and kk[0] == kk[1] == 1:
gans.append(' '.join(map(str, dvs)))
gans.append(str(1))
elif len(pp) == 2:
used = dict()
for i in range(len(dvs)):
used[dvs[i]] = False
ans = []
ok = False
used[pp[0] * pp[1]] = True
aaa = [pp[0] * pp[1]]
if kk[0] > 1:
used[pp[0] * pp[0] * pp[1]] = True
aaa.append(pp[0] * pp[0] * pp[1])
else:
used[pp[0] * pp[1] * pp[1]] = True
aaa.append(pp[0] * pp[1] * pp[1])
for i in range(len(pp)):
dvs = []
ans.append(aaa[i])
kk[i] -= 1
ohne = pp[i]
gen(0, 1)
for j in range(len(dvs)):
used[dvs[j]] = True
ans.append(dvs[j])
gans.append(' '.join(map(str, ans)))
gans.append(str(0))
else:
used = dict()
for i in range(len(dvs)):
used[dvs[i]] = False
ans = []
ok = False
for i in range(len(pp)):
used[pp[i - 1] * pp[i]] = True
for i in range(len(pp)):
dvs = []
ans.append(pp[i - 1] * pp[i])
kk[i] -= 1
ohne = pp[i]
gen(0, 1)
for j in range(len(dvs)):
used[dvs[j]] = True
ans.append(dvs[j])
gans.append(' '.join(map(str, ans)))
gans.append(str(0))
print('\n'.join(gans))
|
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute $1 + 3$ using the calculator, he gets $2$ instead of $4$. But when he tries computing $1 + 4$, he gets the correct answer, $5$. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!
So, when he tries to compute the sum $a + b$ using the calculator, he instead gets the xorsum $a \oplus b$ (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).
As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers $l$ and $r$, how many pairs of integers $(a, b)$ satisfy the following conditions: $$a + b = a \oplus b$$ $$l \leq a \leq r$$ $$l \leq b \leq r$$
However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of testcases.
Then, $t$ lines follow, each containing two space-separated integers $l$ and $r$ ($0 \le l \le r \le 10^9$).
-----Output-----
Print $t$ integers, the $i$-th integer should be the answer to the $i$-th testcase.
-----Example-----
Input
3
1 4
323 323
1 1000000
Output
8
0
3439863766
-----Note-----
$a \oplus b$ denotes the bitwise XOR of $a$ and $b$.
For the first testcase, the pairs are: $(1, 2)$, $(1, 4)$, $(2, 1)$, $(2, 4)$, $(3, 4)$, $(4, 1)$, $(4, 2)$, and $(4, 3)$.
|
def solve(L, R):
res = 0
for i in range(32):
for j in range(32):
l = (L >> i) << i
r = (R >> j) << j
#print(l, r)
if l>>i&1==0 or r>>j&1==0:
continue
l -= 1<<i
r -= 1<<j
if l & r:
continue
lr = l ^ r
ma = max(i, j)
mi = min(i, j)
mask = (1<<ma)-1
p = bin(lr&mask).count("1")
ip = ma - mi - p
res += 3**mi * 2**ip
#print(l, r, mi, ip, 3**mi * 2**ip)
return res
T = int(input())
for _ in range(T):
l, r = list(map(int, input().split()))
print(solve(r+1, r+1) + solve(l, l) - solve(l, r+1) * 2)
|
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute $1 + 3$ using the calculator, he gets $2$ instead of $4$. But when he tries computing $1 + 4$, he gets the correct answer, $5$. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!
So, when he tries to compute the sum $a + b$ using the calculator, he instead gets the xorsum $a \oplus b$ (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).
As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers $l$ and $r$, how many pairs of integers $(a, b)$ satisfy the following conditions: $$a + b = a \oplus b$$ $$l \leq a \leq r$$ $$l \leq b \leq r$$
However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of testcases.
Then, $t$ lines follow, each containing two space-separated integers $l$ and $r$ ($0 \le l \le r \le 10^9$).
-----Output-----
Print $t$ integers, the $i$-th integer should be the answer to the $i$-th testcase.
-----Example-----
Input
3
1 4
323 323
1 1000000
Output
8
0
3439863766
-----Note-----
$a \oplus b$ denotes the bitwise XOR of $a$ and $b$.
For the first testcase, the pairs are: $(1, 2)$, $(1, 4)$, $(2, 1)$, $(2, 4)$, $(3, 4)$, $(4, 1)$, $(4, 2)$, and $(4, 3)$.
|
def g( a , b ):
cur = 1
res = 0
ze = 0
while cur <= b:
if b & cur:
b ^= cur
if a & b == 0:
res += ( 1 << ze )
if a & cur == 0:
ze = ze + 1
cur <<= 1
return res
def f( a , b ):
res = 0
if a == b:
return 0
if a == 0:
return 2 * b - 1 + f( 1 , b )
if a & 1:
res = res + 2 * ( g( a , b ) - g( a , a ) )
a = a + 1
if b & 1:
res = res + 2 * ( g( b - 1 , b ) - g( b - 1 , a ) )
return 3 * f( a >> 1 , b >> 1 ) + res
t = int(input())
while t > 0:
t = t - 1
l , r = map(int , input().split())
print( f( l , r + 1 ) )
|
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute $1 + 3$ using the calculator, he gets $2$ instead of $4$. But when he tries computing $1 + 4$, he gets the correct answer, $5$. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!
So, when he tries to compute the sum $a + b$ using the calculator, he instead gets the xorsum $a \oplus b$ (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).
As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers $l$ and $r$, how many pairs of integers $(a, b)$ satisfy the following conditions: $$a + b = a \oplus b$$ $$l \leq a \leq r$$ $$l \leq b \leq r$$
However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of testcases.
Then, $t$ lines follow, each containing two space-separated integers $l$ and $r$ ($0 \le l \le r \le 10^9$).
-----Output-----
Print $t$ integers, the $i$-th integer should be the answer to the $i$-th testcase.
-----Example-----
Input
3
1 4
323 323
1 1000000
Output
8
0
3439863766
-----Note-----
$a \oplus b$ denotes the bitwise XOR of $a$ and $b$.
For the first testcase, the pairs are: $(1, 2)$, $(1, 4)$, $(2, 1)$, $(2, 4)$, $(3, 4)$, $(4, 1)$, $(4, 2)$, and $(4, 3)$.
|
def g( a , b ):
cur = 1
res = 0
ze = 0
while cur <= b:
if b & cur:
b ^= cur
if a & b == 0:
res += ( 1 << ze )
if a & cur == 0:
ze = ze + 1
cur <<= 1
return res
def f( a , b ):
res = 0
if a == b:
return 0
if a == 0:
return 2 * b - 1 + f( 1 , b )
if a & 1:
res = res + 2 * ( g( a , b ) - g( a , a ) )
a = a + 1
if b & 1:
res = res + 2 * ( g( b - 1 , b ) - g( b - 1 , a ) )
return 3 * f( a >> 1 , b >> 1 ) + res
t = int(input())
while t > 0:
t = t - 1
l , r = map(int , input().split())
print( f( l , r + 1 ) )
|
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute $1 + 3$ using the calculator, he gets $2$ instead of $4$. But when he tries computing $1 + 4$, he gets the correct answer, $5$. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!
So, when he tries to compute the sum $a + b$ using the calculator, he instead gets the xorsum $a \oplus b$ (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).
As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers $l$ and $r$, how many pairs of integers $(a, b)$ satisfy the following conditions: $$a + b = a \oplus b$$ $$l \leq a \leq r$$ $$l \leq b \leq r$$
However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of testcases.
Then, $t$ lines follow, each containing two space-separated integers $l$ and $r$ ($0 \le l \le r \le 10^9$).
-----Output-----
Print $t$ integers, the $i$-th integer should be the answer to the $i$-th testcase.
-----Example-----
Input
3
1 4
323 323
1 1000000
Output
8
0
3439863766
-----Note-----
$a \oplus b$ denotes the bitwise XOR of $a$ and $b$.
For the first testcase, the pairs are: $(1, 2)$, $(1, 4)$, $(2, 1)$, $(2, 4)$, $(3, 4)$, $(4, 1)$, $(4, 2)$, and $(4, 3)$.
|
def get_bin (a):
nums=[]
for i in range (32):
if ((1<<i)&a):
nums.append(1)
else:
nums.append(0)
while(len(nums)>0 and nums[-1]==0):
nums.pop()
return nums
dp={}
def get_num (a, b):
nonlocal dp
if ((a,b) in dp):
return dp[(a,b)]
if (a < 0 or b < 0):
return 0
if (a == 0 and b == 0):
return 1
a_bin = get_bin(a)
b_bin = get_bin(b)
if(b>a):
a_bin,b_bin=b_bin,a_bin
a,b=b,a
if (len(a_bin)>len(b_bin)):
big_bit = 1 << (len(a_bin) - 1)
to_ret=((get_num(big_bit-1,b) + get_num(a-big_bit, b)))
dp[(a,b)]=to_ret
return to_ret
if(sum(a_bin)==len(a_bin) and sum(b_bin)==len(b_bin)):
to_ret = pow(3, len(a_bin))
dp[(a,b)]=to_ret
return to_ret
big_bit = 1 << (len(a_bin) - 1)
to_ret=(get_num(big_bit-1, b-big_bit) + get_num(a, big_bit-1))
dp[(a,b)]=to_ret
return to_ret
tc = int(input(""))
for i in range (int(tc)):
nums = input("").split(' ')
l = int(nums[0])
r = int(nums[1])
ans = get_num(r, r) - 2 * get_num(r, l - 1) + get_num(l - 1, l - 1)
print(ans)
|
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute $1 + 3$ using the calculator, he gets $2$ instead of $4$. But when he tries computing $1 + 4$, he gets the correct answer, $5$. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!
So, when he tries to compute the sum $a + b$ using the calculator, he instead gets the xorsum $a \oplus b$ (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).
As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers $l$ and $r$, how many pairs of integers $(a, b)$ satisfy the following conditions: $$a + b = a \oplus b$$ $$l \leq a \leq r$$ $$l \leq b \leq r$$
However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of testcases.
Then, $t$ lines follow, each containing two space-separated integers $l$ and $r$ ($0 \le l \le r \le 10^9$).
-----Output-----
Print $t$ integers, the $i$-th integer should be the answer to the $i$-th testcase.
-----Example-----
Input
3
1 4
323 323
1 1000000
Output
8
0
3439863766
-----Note-----
$a \oplus b$ denotes the bitwise XOR of $a$ and $b$.
For the first testcase, the pairs are: $(1, 2)$, $(1, 4)$, $(2, 1)$, $(2, 4)$, $(3, 4)$, $(4, 1)$, $(4, 2)$, and $(4, 3)$.
|
def g( a , b ):
cur = 1
res = 0
ze = 0
while cur <= b:
if b & cur:
b ^= cur
if a & b == 0:
res += ( 1 << ze )
if a & cur == 0:
ze = ze + 1
cur <<= 1
return res
def f( a , b ):
res = 0
if a == b:
return 0
if a == 0:
return 2 * b - 1 + f( 1 , b )
if a & 1:
res = res + 2 * ( g( a , b ) - g( a , a ) )
a = a + 1
if b & 1:
res = res + 2 * ( g( b - 1 , b ) - g( b - 1 , a ) )
return 3 * f( a >> 1 , b >> 1 ) + res
t = int(input())
while t > 0:
t = t - 1
l , r = map(int , input().split())
print( f( l , r + 1 ) )
|
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute $1 + 3$ using the calculator, he gets $2$ instead of $4$. But when he tries computing $1 + 4$, he gets the correct answer, $5$. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!
So, when he tries to compute the sum $a + b$ using the calculator, he instead gets the xorsum $a \oplus b$ (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).
As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers $l$ and $r$, how many pairs of integers $(a, b)$ satisfy the following conditions: $$a + b = a \oplus b$$ $$l \leq a \leq r$$ $$l \leq b \leq r$$
However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of testcases.
Then, $t$ lines follow, each containing two space-separated integers $l$ and $r$ ($0 \le l \le r \le 10^9$).
-----Output-----
Print $t$ integers, the $i$-th integer should be the answer to the $i$-th testcase.
-----Example-----
Input
3
1 4
323 323
1 1000000
Output
8
0
3439863766
-----Note-----
$a \oplus b$ denotes the bitwise XOR of $a$ and $b$.
For the first testcase, the pairs are: $(1, 2)$, $(1, 4)$, $(2, 1)$, $(2, 4)$, $(3, 4)$, $(4, 1)$, $(4, 2)$, and $(4, 3)$.
|
def g( a , b ):
cur = 1
res = 0
ze = 0
while cur <= b:
if b & cur:
b ^= cur
if a & b == 0:
res += ( 1 << ze )
if a & cur == 0:
ze = ze + 1
cur <<= 1
return res
def f( a , b ):
res = 0
if a == b:
return 0
if a == 0:
return 2 * b - 1 + f( 1 , b )
if a & 1:
res = res + 2 * ( g( a , b ) - g( a , a ) )
a = a + 1
if b & 1:
res = res + 2 * ( g( b - 1 , b ) - g( b - 1 , a ) )
return 3 * f( a >> 1 , b >> 1 ) + res
t = int(input())
while t > 0:
t = t - 1
l , r = list(map(int , input().split()))
print( f( l , r + 1 ) )
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
for _ in range(int(input())):
a, b, c = input(), input(), input()
n = len(a)
ok = True
for i in range(n):
if c[i] not in [a[i], b[i]]:
ok = False
print('YES' if ok else 'NO')
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
for _ in range(int(input())):
a=input()
b=input()
c=input()
ans="YES"
for i in range(len(a)):
if a[i]!=c[i] and b[i]!=c[i]:ans="NO"
print(ans)
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
t = int(input())
def test(a,b,c):
for x,y,z in zip(a,b,c):
if not (x==z or y==z):
return False
return True
for _ in range(t):
a = input()
b = input()
c = input()
if test(a,b,c):
print("YES")
else:
print("NO")
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
for _ in range(int(input())):
for a, b, c in zip(input(), input(), input()):
if c not in (a, b):
print("NO")
break
else:
print("YES")
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
q = int(input())
for rwere in range(q):
a = input()
b = input()
c = input()
n = len(a)
dasie = True
for i in range(n):
if c[i] == a[i] or c[i] == b[i]:
continue
else:
dasie = False
if dasie:
print("YES")
else:
print("NO")
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
for _ in range(int(input())):
a = input()
b = input()
c = input()
n = len(a)
flag = 0
for i in range(n):
if a[i]==c[i] or b[i]==c[i]:
continue
flag = 1
break
if flag==0:
print("YES")
else:
print("NO")
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
T = int(input())
for _ in range(T):
A = input()
B = input()
C = input()
N = len(A)
for i in range(N):
if not (A[i] == C[i] or B[i] == C[i]):
print("NO")
break
else:
print("YES")
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
from math import *
zzz = int(input())
for zz in range(zzz):
a = input()
b = input()
c = input()
ha = True
for i in range (len(a)):
if not b[i] == c[i] and not a[i] == c[i]:
ha= False
break
if not ha:
print('NO')
else:
print('YES')
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
def mi():
return map(int, input().split())
for _ in range(int(input())):
a = list(input())
b = list(input())
c = list(input())
np = False
for i in range(len(a)):
if a[i]==c[i] or b[i]==c[i]:
continue
np = True
break
if np:
print ('NO')
else:
print ('YES')
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
t = int(input())
def test():
a = input().strip()
b = input().strip()
c= input().strip()
for x,y,z in zip(a,b,c):
if z != x and z != y:
print("NO"); return
print("YES")
for _ in range(t):
test()
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
t = int(input())
for _ in range(t):
a = input()
b = input()
c = input()
n = len(a)
flag = True
for i in range(n):
if a[i] == c[i] or b[i] == c[i]:
continue
flag = False
if flag:
print("YES")
else:
print("NO")
|
You are given three strings $a$, $b$ and $c$ of the same length $n$. The strings consist of lowercase English letters only. The $i$-th letter of $a$ is $a_i$, the $i$-th letter of $b$ is $b_i$, the $i$-th letter of $c$ is $c_i$.
For every $i$ ($1 \leq i \leq n$) you must swap (i.e. exchange) $c_i$ with either $a_i$ or $b_i$. So in total you'll perform exactly $n$ swap operations, each of them either $c_i \leftrightarrow a_i$ or $c_i \leftrightarrow b_i$ ($i$ iterates over all integers between $1$ and $n$, inclusive).
For example, if $a$ is "code", $b$ is "true", and $c$ is "help", you can make $c$ equal to "crue" taking the $1$-st and the $4$-th letters from $a$ and the others from $b$. In this way $a$ becomes "hodp" and $b$ becomes "tele".
Is it possible that after these swaps the string $a$ becomes exactly the same as the string $b$?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters $a$.
The second line of each test case contains a string of lowercase English letters $b$.
The third line of each test case contains a string of lowercase English letters $c$.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $100$.
-----Output-----
Print $t$ lines with answers for all test cases. For each test case:
If it is possible to make string $a$ equal to string $b$ print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
-----Example-----
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
-----Note-----
In the first test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
In the second test case, you should swap $c_i$ with $a_i$ for all possible $i$. After the swaps $a$ becomes "bca", $b$ becomes "bca" and $c$ becomes "abc". Here the strings $a$ and $b$ are equal.
In the third test case, you should swap $c_1$ with $a_1$, $c_2$ with $b_2$, $c_3$ with $b_3$ and $c_4$ with $a_4$. Then string $a$ becomes "baba", string $b$ becomes "baba" and string $c$ becomes "abab". Here the strings $a$ and $b$ are equal.
In the fourth test case, it is impossible to do the swaps so that string $a$ becomes exactly the same as string $b$.
|
for _ in range(int(input())):
a = input()
b = input()
c = input()
l = len(a)
flag = 0
for i in range(l):
if c[i]==a[i] or c[i]==b[i]:
continue
else:
flag = 1
break
if flag:
print("NO")
else:
print("YES")
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for __ in range(int(input())):
n = int(input())
ar = list(map(int, input().split()))
ar.reverse()
print(*ar)
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for _ in range(int(input())):
am = int(input())
arr = list(map(int,input().split()))
print(*list(reversed(arr)))
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
print(*a[::-1])
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for _ in range(int(input())):
n=int(input())
print(*[*map(int,input().split())][::-1])
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
import sys
ii = lambda: sys.stdin.readline().strip()
idata = lambda: [int(x) for x in ii().split()]
def solve():
n = int(ii())
data = idata()
print(*data[::-1])
return
for t in range(int(ii())):
solve()
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
print(*a[::-1])
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
import sys
import random
from fractions import Fraction
from math import *
def input():
return sys.stdin.readline().strip()
def iinput():
return int(input())
def finput():
return float(input())
def tinput():
return input().split()
def linput():
return list(input())
def rinput():
return list(map(int, tinput()))
def fiinput():
return list(map(float, tinput()))
def flinput():
return list(fiinput())
def rlinput():
return list(map(int, input().split()))
def trinput():
return tuple(rinput())
def srlinput():
return sorted(list(map(int, input().split())))
def NOYES(fl):
if fl:
print("NO")
else:
print("YES")
def YESNO(fl):
if fl:
print("YES")
else:
print("NO")
def main():
n = iinput()
q = rlinput()
q.reverse()
print(*q)
for TESTING in range(iinput()):
main()
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
print(' '.join(list(map(str,l[::-1]))))
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
#!/usr/bin/env pypy3
T = int(input())
for t in range(T):
input()
P = input().split()
print(*(P[::-1]))
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int,input().split()))
l.reverse()
print(*l)
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
T = int(input())
#lets = 'abcdefghijklmnopqrstuvwxyz'
#key = {lets[i]:i for i in range(26)}
for t in range(T):
n = int(input())
#n,k = map(int,input().split())
#a = list(map(int,input().split()))
a = input().split()
d = False
a.reverse()
print(' '.join(a))
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
for i in range(n-1,-1,-1):
print(a[i],end=' ')
print()
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
print(*a[::-1])
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
t=int(input())
for _ in range(t):
n=int(input())
arr=list(map(int,input().split()))
for i in range(len(arr)-1,-1,-1):
print(arr[i],end=" ")
print()
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for _ in range (int(input())):
n=int(input())
a=list(map(int,input().split()))
a=a[::-1]
print(*a)
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for i in range(int(input())):
input()
print(*[int(i) for i in input().split()][::-1])
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
t=int(input())
for t in range(t):
n=int(input())
a=[int(x) for x in input().split(' ')]
a.reverse()
print(*a)
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
#!/usr/local/bin/python3
t = int(input())
for _ in range(t):
n = int(input())
arr=list(map(int,input().split()))
result=arr[::-1]
print(' '.join(map(str,result)))
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
t=int(input())
while t:
t-=1
n=int(input())
a=[int(i) for i in input().split()]
a.reverse()
print(*a,sep=" ")
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) Β β the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ β a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
import sys
# import math
# from collections import deque
# import heapq
# from math import inf
# from math import gcd
# print(help(deque))
# 26
pprint = lambda s: print(' '.join(map(lambda x: str(x), s)))
input = lambda: sys.stdin.readline().strip()
ipnut = input
mod = 1000000007
for i in range(int(input())):
# n, k = map(int, input().split())
n = int(input())
p = list(map(int,input().split()))
pprint(reversed(p))
# c = list(map(lambda x: int(x)-1, input().split()))
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.