text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Tags: greedy, math, number theory
Correct Solution:
```
import sys
import math,bisect,operator
inf,m = float('inf'),10**9+7
sys.setrecursionlimit(10 ** 5)
from itertools import groupby,accumulate
from heapq import heapify,heappop,heappush
from collections import deque,Counter,defaultdict
I = lambda : int(sys.stdin.readline())
neo = lambda : map(int, sys.stdin.readline().split())
Neo = lambda : list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
n = I()
A = Neo()
def check(t):
i = 0
k = 0
while i < n:
k += A[i]
if k == t:
k = 0
elif k > t:
return 0
i += 1
if k == t or k == 0:
return 1
return 0
t = sum(A)
div = set()
for i in range(1,int(t**.5)+1):
if t%i == 0:
div.add(i)
div.add(t//i)
# print(div)
for i in sorted(div):
if check(i):
print(n-t//i)
break
```
| 6,000 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Tags: greedy, math, number theory
Correct Solution:
```
t = int(input())
INF = 1 << 60
def solve():
n = int(input())
a = list(map(int, input().split()))
ans = INF
s = 0
for i in range(n):
s += a[i]
cnt = 0
tmpsum = 0
for j in range(n):
if tmpsum < s:
tmpsum += a[j]
continue
elif tmpsum == s:
cnt += 1
tmpsum = a[j]
continue
else:
cnt = -INF
break
if tmpsum == s:
cnt += 1
else:
cnt = -INF
ans = min(ans, n - cnt)
print(ans)
for i in range(t):
solve()
```
| 6,001 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Tags: greedy, math, number theory
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
arr = list(map(int,input().split()))
if arr.count(arr[0])==len(arr):print("0")
else:
sub_arr_sum,ans = 0,-1
for k in range(1,n):
if sum(arr)%(n-k)==0:
sub_arr_sum_limit,part_cnt,sub_arr_sum = sum(arr)//(n-k),0,0
for i in range(n):
if sub_arr_sum != sub_arr_sum_limit:
sub_arr_sum += arr[i]
else:
sub_arr_sum = arr[i]
part_cnt += 1
if sub_arr_sum == sub_arr_sum_limit:part_cnt += 1
#print(sub_arr_sum,part_cnt)
if part_cnt==n-k:
ans = k
break
else:continue
if ans!=-1:break
if ans == -1:print(n-1)
else: print(ans)
```
| 6,002 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Tags: greedy, math, number theory
Correct Solution:
```
from sys import stdin,stdout
from math import gcd,sqrt,factorial,pi,inf
from collections import deque,defaultdict
from bisect import bisect,bisect_left
input=stdin.readline
R=lambda:map(int,input().split())
I=lambda:int(input())
S=lambda:input().rstrip('\n')
L=lambda:list(R())
P=lambda x:stdout.write(str(x)+'\n')
nCr=lambda x,y:(f[x]*inv((f[y]*f[x-y])%N))%N
inv=lambda x:pow(x,N-2,N)
lcm=lambda x,y:(x*y)//gcd(x,y)
hg=lambda x,y:((y+x-1)//x)*x
pw=lambda x:1 if x==1 else 1+pw(x//2)
chk=lambda x:chk(x//2) if not x%2 else True if x==1 else False
sm=lambda x:(x**2+x)//2
N=10**9+7
for _ in range(I()):
n=I()
*a,=R()
for i in range(1,n):
a[i]+=a[i-1]
for i in range(n):
if a[-1]%a[i]==0:
p=0
for j in range(i,n):
if a[j]%a[i]==0:p+=1
if a[-1]//a[i]==p:ans=n-p;break
print(ans)
```
| 6,003 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Tags: greedy, math, number theory
Correct Solution:
```
"""
Author: Enivar
Date: Tue 22 Dec 2020 13:30:33 EAT
"""
from sys import exit, stderr, stdout
from math import sqrt
def debug(*args):
for i in args:
stderr.write(str(i)+' ')
stderr.write('\n')
class Input:
def __init__(self):
self.sum = 0
self.mx = 0
def inp(self, x):
self.sum+=x
self.mx = max(self.mx, x)
return x
def factors(N):
lim = N//2+1
ret = []
for i in range(obj.mx,lim):
if N%i==0: ret.append(i)
return ret
for _ in range(int(input())):
n = int(input())
obj = Input()
a = [obj.inp(int(x)) for x in input().split()]
divisors = factors(obj.sum)
D = len(divisors)
if n==1:
print(0)
continue
if D==0:
print(n-1)
continue
j, tmp, fg = 0, 0, False
for i in range(n):
tmp+=a[i]
while tmp>divisors[j]:
if j+1<D:
if tmp<divisors[j+1]: break
j+=1
else: break
if tmp in divisors[j:]:
tp, k = 0, i+1
ans = 0
if i>0: ans = i
while True:
if k>=n:
fg = True
break
tp+=a[k]
if a[k]!=tmp: ans+=1
if tp==tmp:
tp = 0
if ans>0 and a[k]!=tmp: ans-=1
elif tp>tmp: break
k+=1
if fg: break
if fg: print(ans)
else: print(n-1)
```
| 6,004 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Tags: greedy, math, number theory
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
sum_ = sum(a)
for j in range(n):
if sum_ % (n - j) == 0:
elem = sum_ // (n - j)
i = 1; curr = a[0]; co = 0
while i < n:
if curr < elem:
curr += a[i]
co += 1; i += 1
elif curr == elem:
curr = a[i]; i += 1
else:
break
if co == j and curr == elem: print(j); break
```
| 6,005 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Tags: greedy, math, number theory
Correct Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
min_ans=0
while True:
chk=True
avg_ans=s/(n-min_ans)
curr_sum=0
for i in range(n):
if(curr_sum<avg_ans):
curr_sum+=a[i]
if(curr_sum==avg_ans):
curr_sum=0
elif(curr_sum>avg_ans):
chk=False
min_ans+=1
break
if(chk==True):
print(min_ans)
break
```
| 6,006 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Tags: greedy, math, number theory
Correct Solution:
```
t = int(input())
while t > 0:
t -= 1
n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
for k in range(s):
if s%(n-k) == 0:
ps = s//(n-k)
tmpSum = 0
for i in a:
if tmpSum > ps:
break
elif tmpSum < ps:
tmpSum += i
else:
tmpSum = i
if tmpSum == ps:
print(k)
break
```
| 6,007 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
for i in range(int(input())):
length=int(input())
ls=list(map(int,input().split()))
if len(ls)==1 or ls.count(ls[0])==len(ls):
print(0)
else:
res=len(ls)-1
s=sum(ls)
for i in range(length-1,1,-1):
if s%i==0:
cur=0
need=s//i
ans = 0
flag = 1
for j in range(len(ls)):
cur+=ls[j]
if cur>need:
flag=0
break
elif cur==need:
cur=0
if flag==1:
res=length-i
break
print(res)
```
Yes
| 6,008 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
# You miss 100% of the shots you don't take. Wayne Gretzky
# by : Blue Edge - Create some chaos
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
f=n
b=[0]
for x in a:
b+=x+b[-1],
while f:
if s%f==0:
p=s//f
for i in range(1,f):
if p*i not in b:
break
else:
ans = 0
sm = 0
c = -1
for x in a:
sm+=x
c+=1
if sm==p:
ans+=c
c=-1
sm=0
print(ans)
break
f-=1
```
Yes
| 6,009 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
#New Imports
from collections import defaultdict
def solution():
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
i = 0
j = n-1
sleft = 0
sright = 0
sleftarr = []
srightarr = []
for i in range(n):
sleft += a[i]
sleftarr.append(sleft)
for i in reversed(range(n)):
sright += a[i]
srightarr.append(sright)
srightarr.reverse()
for i in range(n):
for j in range(i+1,n):
sleft = sleftarr[i]
sright = srightarr[j]
if sleft == sright and (s-(2*sleft))%sleft == 0:
x = i+1
f = 0
sval = 0
while x < j:
sval += a[x]
if sval == sleft:
sval = 0
if x == j-1:
f = 1
elif sval > sleft:
break
x += 1
if f == 1:
count = s//sleft
print(n-count)
return
if sleft == sright and i+1 == j:
print(n-2)
return
print(n-1)
return
def main():
testcases = 1
testcases = int(input())
for _ in range(testcases):
solution()
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
```
Yes
| 6,010 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
A = list(map(int, input().split()))
s = sum(A)
for i in range(n):
if s%(n-i) != 0:
continue
p = s//(n-i)
#print(p)
cur = 0
flag = True
for j in range(n):
if cur+A[j]>p:
flag=False
break
elif cur+A[j] == p:
cur = 0
else:
cur += A[j]
if flag:
ans = i
break
print(ans)
```
Yes
| 6,011 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
from collections import defaultdict
import heapq
def solve(n, arr):
deleted = [False] * n
ans = 0
while True:
min_i, max_i = -1, -1
for i, x in enumerate(arr):
if deleted[i]: continue
if min_i == -1 or (arr[min_i] > arr[i]):
min_i = i
if max_i == -1 or (arr[max_i] < arr[i]):
max_i = i
if min_i == -1 or arr[min_i] == arr[max_i]:
return ans
i = min_i - 1
while i >= 0 and deleted[i]: i -= 1
l = arr[i] if i >= 0 else float('inf')
j = min_i + 1
while j < n and deleted[j]: j += 1
r = arr[j] if j < n else float('inf')
if l == r:
return ans
if l <= r:
arr[i] += arr[min_i]
else:
arr[j] += arr[min_i]
deleted[min_i] = True
ans += 1
# def print_arr(arr):
# return ' '.join(map(str, arr))
#
# if __name__ == '__main__':
# print(solve(7, [1, 2, 1, 2, 3, 3, 6]))
T = int(input())
for _ in range(T):
n = int(input())
arr = [*map(int, input().split())]
print(solve(n, arr))
# print(print_arr(res))
#print('YES' if res else 'NO')
```
No
| 6,012 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
b = []
m = sum(a)
for i in range(n, 0, -1):
if m % i == 0:
b.append(m // i)
for i in b:
x = 0
f = 0
for j in range(n):
x += a[j]
if x == i:
x = 0
elif x > i:
x = 0
f = 1
break
if x:
if x > i:
f = 1
if f == 0:
break
print(n - (m//i))
```
No
| 6,013 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
T=int(input())
for _ in range(T):
n=int(input())
arr=list(map(int,input().split()))
if set(arr)=={arr[0]}:print(0)
else:
ans=0
while len(set(arr))!=1:
m=min(arr);idx=arr.index(m)
ans+=1
if idx-1==-1:arr[idx+1]+=arr[idx]
elif idx+1==len(arr):arr[idx-1]+=arr[idx]
else:
if arr[idx-1]<arr[idx+1]:arr[idx-1]+=arr[idx]
else:arr[idx+1]+=arr[idx]
arr.pop(idx)
print(ans)
```
No
| 6,014 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a = [int(i) for i in input().split()]
s = 0
rs = n - 1
for i in range(n):
s += a[i]
s1 = 0
cnt = i
ok = 1
for j in range(i + 1, n):
s1 += a[j]
if s1 > s:
ok = 0
break
if s1 == s:
s1 = 0
else:
cnt += 1
if ok == 1:
rs = min(rs, cnt)
print(rs)
```
No
| 6,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Tags: brute force, data structures, graphs, greedy, implementation, sortings, two pointers
Correct Solution:
```
import sys
from collections import defaultdict
from io import BytesIO, IOBase
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
self.BUFSIZE = 4096
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def get_int():
return int(input())
def get_ints():
return list(map(int, input().split(' ')))
def solve_e():
n = get_ints()
costs = [get_ints() for course in range(4)]
costs = [[(x, j) for j, x in enumerate(cost)] for cost in costs]
m = []
mismatch_graph = [defaultdict(set) for _ in range(3)]
for course_trans in range(3):
m.append(get_int())
for mismatch in range(m[-1]):
a, b = get_ints()
mismatch_graph[course_trans][b - 1].add(a - 1)
dp = [sorted(costs[0])]
for j in range(3):
tmp = []
for l in range(n[j + 1]):
k = 0
while k < n[j]:
if costs[j + 1][l][-1] in mismatch_graph[j] and dp[-1][k][-1] in mismatch_graph[j][costs[j + 1][l][-1]]:
k += 1
else:
break
if k == n[j]:
tmp.append((float('inf'), costs[j + 1][l][-1]))
else:
tmp.append((costs[j + 1][l][0] + dp[-1][k][0], costs[j + 1][l][-1]))
dp.append(sorted(tmp))
if min(dp[-1])[0] == float('inf'):
return - 1
else:
return min(dp[-1])[0]
print(solve_e())
```
| 6,016 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Tags: brute force, data structures, graphs, greedy, implementation, sortings, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
def read_int():
return int(input())
def read_ints():
return map(int, input().split(' '))
INF = int(1e9)
n = list(read_ints())
a = [[] for _ in range(4)]
a[0] = list(read_ints())
a[1] = list(read_ints())
a[2] = list(read_ints())
a[3] = list(read_ints())
dp = [(a[0][i], i) for i in range(n[0])]
dp.sort()
for k in range(3):
m = read_int()
banned = [set() for _ in range(n[k + 1])]
for _ in range(m):
u, v = read_ints()
banned[v - 1].add(u - 1)
f = [INF] * n[k + 1]
for i in range(n[k + 1]):
for j in range(n[k]):
if dp[j][1] not in banned[i]:
f[i] = dp[j][0] + a[k + 1][i]
break
dp = [(f[i], i) for i in range(n[k + 1])]
dp.sort()
print(dp[0][0] if dp[0][0] < INF else -1)
```
| 6,017 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Tags: brute force, data structures, graphs, greedy, implementation, sortings, two pointers
Correct Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO,IOBase
from math import inf,isinf
def some_random_function():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am writing
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def main():
n = list(map(int,input().split()))
cost = [list(map(int,input().split())) for _ in range(4)]
op = [[] for _ in range(3)]
for i in range(3):
for _ in range(int(input())):
op[i].append(tuple(map(int,input().split())))
for i in range(2,-1,-1):
band = [[] for _ in range(n[i+1]+1)]
for j in op[i]:
band[j[1]].append(j[0])
ke = sorted(range(n[i+1]),key=lambda xx:cost[i+1][xx])
z = min(cost[i+1])
cost1 = cost[i]
for k in range(len(cost1)):
cost1[k] += z
addi = [0]*n[i]
for ind,k in enumerate(ke):
for z in band[k+1]:
if addi[z-1] == ind and not isinf(cost1[z-1]):
cost1[z-1] -= cost[i+1][k]
cost1[z-1] += inf if ind==len(ke)-1 else cost[i+1][ke[ind+1]]
addi[z-1] = ind+1
mini = min(cost[0])
if isinf(mini):
print(-1)
else:
print(mini)
# Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self,file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd,max(os.fstat(self._fd).st_size,BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0,2),self.buffer.write(b),self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines==0:
b = os.read(self._fd,max(os.fstat(self._fd).st_size,BUFSIZE))
self.newlines = b.count(b"\n")+(not b)
ptr = self.buffer.tell()
self.buffer.seek(0,2),self.buffer.write(b),self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd,self.buffer.getvalue())
self.buffer.truncate(0),self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self,file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s:self.buffer.write(s.encode("ascii"))
self.read = lambda:self.buffer.read().decode("ascii")
self.readline = lambda:self.buffer.readline().decode("ascii")
sys.stdin,sys.stdout = IOWrapper(sys.stdin),IOWrapper(sys.stdout)
input = lambda:sys.stdin.readline().rstrip("\r\n")
def some_random_function1():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am writing
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def some_random_function2():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am writing
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def some_random_function3():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am writing
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
def some_random_function4():
"""due to the fast IO template, my code gets caught in
plag check for no reason. That is why, I am writing
random functions"""
x = 10
x *= 100
i_dont_know = x
why_am_i_writing_this = x*x
print(i_dont_know)
print(why_am_i_writing_this)
if __name__=='__main__':
main()
```
| 6,018 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Tags: brute force, data structures, graphs, greedy, implementation, sortings, two pointers
Correct Solution:
```
import sys
from sys import stdin
n = list(map(int,stdin.readline().split()))
a = [list(map(int,stdin.readline().split())) for i in range(4)]
v = [set([]) for i in range(3)]
for i in range(3):
m = int(stdin.readline())
for j in range(m):
x,y = map(int,stdin.readline().split())
x -= 1
y -= 1
v[i].add( (x,y) )
dp = [[float("inf")] * len(a[i]) for i in range(4)]
dp[0] = a[0]
for i in range(1,4):
lai = [ (dp[i-1][j],j) for j in range(len(a[i-1])) ]
lai.sort()
remR = [i for i in range(len(a[i]))]
for cost,lv in lai:
nexR = []
for rv in remR:
if (lv,rv) not in v[i-1]:
#print (dp[i][rv] , cost , a[i][rv])
dp[i][rv] = cost + a[i][rv]
else:
nexR.append(rv)
remR = nexR
if len(remR) == 0:
break
ans = (min(dp[-1]))
if ans == float("inf"):
print (-1)
else:
print (ans)
```
| 6,019 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Tags: brute force, data structures, graphs, greedy, implementation, sortings, two pointers
Correct Solution:
```
from sys import stdin, stdout
n1, n2, n3, n4 = [int(x) for x in stdin.readline().split()]
a = [int(x) for x in stdin.readline().split()]
b = [int(x) for x in stdin.readline().split()]
c = [int(x) for x in stdin.readline().split()]
d = [int(x) for x in stdin.readline().split()]
a = [[i, a[i]] for i in range(n1)]
a.sort(key=lambda x:x[1])
m1 = int(stdin.readline())
bad_match = [set() for _ in range(n2)]
for _ in range(m1):
X, Y = [int(x) for x in stdin.readline().split()]
bad_match[Y-1].add(X-1)
B = [[i, 400000001] for i in range(n2)]
for Y in range(n2):
for candidate in a:
if not (candidate[0] in bad_match[Y]):
B[Y][1] = candidate[1] + b[Y]
break
B.sort(key=lambda x:x[1])
m2 = int(stdin.readline())
bad_match = [set() for _ in range(n3)]
for _ in range(m2):
X, Y = [int(x) for x in stdin.readline().split()]
bad_match[Y-1].add(X-1)
C = [[i, 400000001] for i in range(n3)]
for Y in range(n3):
for candidate in B:
if not (candidate[0] in bad_match[Y]):
C[Y][1] = candidate[1] + c[Y]
break
C.sort(key=lambda x:x[1])
m3 = int(stdin.readline())
bad_match = [set() for _ in range(n4)]
for _ in range(m3):
X, Y = [int(x) for x in stdin.readline().split()]
bad_match[Y-1].add(X-1)
D = [400000001]*n4
for Y in range(n4):
for candidate in C:
if not (candidate[0] in bad_match[Y]):
D[Y] = candidate[1] + d[Y]
break
minimum = min(D)
if minimum >= 400000001:
stdout.write(str(-1)+'\n')
else:
stdout.write(str(minimum)+'\n')
```
| 6,020 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Tags: brute force, data structures, graphs, greedy, implementation, sortings, two pointers
Correct Solution:
```
import sys
import bisect
from collections import defaultdict
from io import BytesIO, IOBase
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
self.BUFSIZE = 8192
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def get_int():
return int(input())
def get_ints():
return list(map(int, input().split(' ')))
def get_int_grid(n):
return [get_ints() for _ in range(n)]
def get_str():
return input().split(' ')
def yes_no(b):
if b:
return "YES"
else:
return "NO"
def prefix_sums(a):
p = [0]
for x in a:
p.append(p[-1] + x)
return p
def solve_a():
n = get_int()
a = get_ints()
return n - a.count(min(a))
def solve_b():
n, k = get_ints()
if n % 2 == 0:
return (k - 1) % n + 1
else:
return ((k + (k - 1) // (n // 2)) - 1) % n + 1
def solve_c():
n = get_int()
ans = []
if n % 2 == 1:
for team in range(n):
ans.extend([(-1) ** i for i in range(n - team - 1)])
else:
for team in range(n):
ans.extend([(-1) ** i - int(i == 0 and team % 2 == 0) for i in range(n - team - 1)])
return ans
def solve_d():
c_vals = [5]
i = 5
while c_vals[-1] < 10 ** 9 + 1:
c_vals.append((i ** 2 - 1) // 2 + 1)
i += 2
t = get_int()
r = []
for _ in range(t):
n = get_int()
r.append(bisect.bisect_right(c_vals, n))
return r
def solve_e():
n = get_ints()
costs = [get_ints() for course in range(4)]
costs = [[(x, j) for j, x in enumerate(cost)] for cost in costs]
m = []
mismatch_graph = [defaultdict(set) for _ in range(3)]
for course_trans in range(3):
m.append(get_int())
for mismatch in range(m[-1]):
a, b = get_ints()
mismatch_graph[course_trans][b - 1].add(a - 1)
dp = [sorted(costs[0])]
for j in range(3):
tmp = []
for l in range(n[j + 1]):
k = 0
while k < n[j]:
if costs[j + 1][l][-1] in mismatch_graph[j] and dp[-1][k][-1] in mismatch_graph[j][costs[j + 1][l][-1]]:
k += 1
else:
break
if k == n[j]:
tmp.append((float('inf'), costs[j + 1][l][-1]))
else:
tmp.append((costs[j + 1][l][0] + dp[-1][k][0], costs[j + 1][l][-1]))
dp.append(sorted(tmp))
if min(dp[-1])[0] == float('inf'):
return - 1
else:
return min(dp[-1])[0]
print(solve_e())
```
| 6,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Tags: brute force, data structures, graphs, greedy, implementation, sortings, two pointers
Correct Solution:
```
from sys import stdin
def read_int():
return int(stdin.readline())
def read_ints():
return map(int, stdin.readline().split(' '))
INF = int(1e9)
n = list(read_ints())
a = [list(read_ints()) for _ in range(4)]
dp = [(a[0][i], i) for i in range(n[0])]
dp.sort()
#print(a)
for k in range(3):
m = read_int()
banned = [set() for _ in range(n[k + 1])]
for _ in range(m):
u, v = read_ints()
banned[v - 1].add(u - 1)
f = [INF] * n[k + 1]
for i in range(n[k + 1]):
for j in range(n[k]):
if dp[j][1] not in banned[i]:
f[i] = dp[j][0] + a[k + 1][i]
break
dp = [(f[i], i) for i in range(n[k + 1])]
#print(dp)
dp.sort()
print(dp[0][0] if dp[0][0] < INF else -1)
```
| 6,022 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Tags: brute force, data structures, graphs, greedy, implementation, sortings, two pointers
Correct Solution:
```
#!/usr/bin/python3.6
import sys
input = sys.stdin.readline
def encode(x, y):
return (x - 1 << 18) + y
n1, n2, n3, n4 = map(int, input().split())
a = [(int(item), i) for i, item in enumerate(input().split())]
b = [(int(item), i) for i, item in enumerate(input().split())]
c = [(int(item), i) for i, item in enumerate(input().split())]
d = [(int(item), i) for i, item in enumerate(input().split())]
a.sort()
b.sort()
c.sort()
d.sort()
m1 = int(input())
m1s = set()
for _ in range(m1):
x, y = map(int, input().split())
x -= 1; y -= 1
m1s.add(encode(x, y))
m2 = int(input())
m2s = set()
for _ in range(m2):
x, y = map(int, input().split())
x -= 1; y -= 1
m2s.add(encode(x, y))
m3 = int(input())
m3s = set()
for _ in range(m3):
x, y = map(int, input().split())
x -= 1; y -= 1
m3s.add(encode(x, y))
ab = []
for bb, ib in b:
for aa, ia in a:
if encode(ia, ib) in m1s:
continue
ab.append((aa + bb, ib))
break
if not ab:
print(-1); exit()
ab.sort()
bc = []
for cc, ic in c:
for aabb, ib in ab:
if encode(ib, ic) in m2s:
continue
bc.append((aabb + cc, ic))
break
if not bc:
print(-1); exit()
bc.sort()
cd = []
ans = 10**10
for dd, idd in d:
for bbcc, ic in bc:
if encode(ic, idd) in m3s:
continue
if bbcc + dd < ans:
ans = bbcc + dd
break
if ans == 10**10:
print(-1)
else:
print(ans)
```
| 6,023 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Submitted Solution:
```
mod = 1000000007
eps = 10**-9
inf = 10**15
def main():
import sys
input = sys.stdin.buffer.readline
def F(L, R, bad):
NL = len(L)
NR = len(R)
ret = [inf] * NR
L_sorted = [(l, i) for i, l in enumerate(L)]
L_sorted.sort(key=lambda x: x[0])
for i, r in enumerate(R):
for l, k in L_sorted:
if k * (NR+1) + i in bad:
continue
else:
ret[i] = l + r
break
return ret
N1, N2, N3, N4 = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
D = list(map(int, input().split()))
M1 = int(input())
if M1 == N1 * N2:
print(-1)
exit()
bad1 = set()
for _ in range(M1):
x, y = map(int, input().split())
x -= 1
y -= 1
bad1.add(x * (N2+1) + y)
M2 = int(input())
if M2 == N2 * N3:
print(-1)
exit()
bad2 = set()
for _ in range(M2):
x, y = map(int, input().split())
x -= 1
y -= 1
bad2.add(x * (N3 + 1) + y)
M3 = int(input())
if M3 == N3 * N4:
print(-1)
exit()
bad3 = set()
for _ in range(M3):
x, y = map(int, input().split())
x -= 1
y -= 1
bad3.add(y * (N3 + 1) + x)
L = F(A, B, bad1)
R = F(D, C, bad3)
ans = min(F(L, R, bad2))
if ans < inf:
print(ans)
else:
print(-1)
if __name__ == '__main__':
main()
```
Yes
| 6,024 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Submitted Solution:
```
import sys
input = sys.stdin.readline
input()
INF = 10 ** 18
prev = list(map(int, input().split()))
As = [list(map(int, input().split())) for _ in range(3)]
for nex in As:
order = sorted(range(len(prev)), key=prev.__getitem__)
nadj = [set() for _ in range(len(nex))]
for _ in range(int(input())):
x, y = map(lambda s: int(s)-1, input().split())
nadj[y].add(x)
for i in range(len(nex)):
j = 0
while j < len(order) and order[j] in nadj[i]: j += 1
nex[i] = min(INF, nex[i] + (prev[order[j]] if j < len(order) else INF))
prev = nex
r = min(prev)
print(-1 if r == INF else r)
```
Yes
| 6,025 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Submitted Solution:
```
import sys
input = sys.stdin.readline
from operator import itemgetter
n1,n2,n3,n4=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
D=list(map(int,input().split()))
AX=[(a,i) for i,a in enumerate(A)]
AX.sort(key=itemgetter(0))
DPB=[1<<60]*n2
m2=int(input())
BAN=[set() for i in range(n2)]
for i in range(m2):
x,y=map(int,input().split())
BAN[y-1].add(x-1)
for j in range(n2):
for a,i in AX:
if i in BAN[j]:
continue
else:
DPB[j]=a+B[j]
break
DPC=[1<<60]*n3
m3=int(input())
BAN=[set() for i in range(n3)]
for i in range(m3):
x,y=map(int,input().split())
BAN[y-1].add(x-1)
BX=[(a,i) for i,a in enumerate(DPB)]
BX.sort(key=itemgetter(0))
for j in range(n3):
for a,i in BX:
if i in BAN[j]:
continue
else:
DPC[j]=a+C[j]
break
DPD=[1<<60]*n4
m4=int(input())
BAN=[set() for i in range(n4)]
for i in range(m4):
x,y=map(int,input().split())
BAN[y-1].add(x-1)
CX=[(a,i) for i,a in enumerate(DPC)]
CX.sort(key=itemgetter(0))
for j in range(n4):
for a,i in CX:
if i in BAN[j]:
continue
else:
DPD[j]=a+D[j]
break
ANS=min(DPD)
if ANS>=1<<60:
print(-1)
else:
print(ANS)
```
Yes
| 6,026 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Submitted Solution:
```
import sys
import math
import heapq
import bisect
from collections import Counter
from collections import defaultdict
from io import BytesIO, IOBase
import string
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
self.BUFSIZE = 8192
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def get_int():
return int(input())
def get_ints():
return list(map(int, input().split(' ')))
def get_int_grid(n):
return [get_ints() for _ in range(n)]
def get_str():
return input().split(' ')
def yes_no(b):
if b:
return "YES"
else:
return "NO"
def prefix_sums(a):
p = [0]
for x in a:
p.append(p[-1] + x)
return p
def solve_a():
n = get_int()
a = get_ints()
return n - a.count(min(a))
def solve_b():
n, k = get_ints()
if n % 2 == 0:
return (k - 1) % n + 1
else:
return ((k + (k - 1) // (n // 2)) - 1) % n + 1
def solve_c():
n = get_int()
ans = []
if n % 2 == 1:
for team in range(n):
ans.extend([(-1) ** i for i in range(n - team - 1)])
else:
for team in range(n):
ans.extend([(-1) ** i - int(i == 0 and team % 2 == 0) for i in range(n - team - 1)])
return ans
d_sols = [5]
i = 5
while d_sols[-1] < 10 ** 9 + 1:
d_sols.append((i ** 2 - 1) // 2 + 1)
i += 2
def solve_d():
n = get_int()
ans = bisect.bisect_right(d_sols, n)
return ans
def solve_e():
n = get_ints()
costs = [get_ints() for course in range(4)]
costs = [sorted([(x, j) for j, x in enumerate(cost)]) for cost in costs]
m = []
mismatch_graph = [defaultdict(set) for _ in range(3)]
for course_trans in range(3):
m.append(get_int())
for mismatch in range(m[-1]):
a, b = get_ints()
mismatch_graph[course_trans][b - 1].add(a - 1)
dp = [costs[0]]
for j in range(3):
tmp = []
for l in range(n[j + 1]):
k = 0
while k < n[j]:
if costs[j + 1][l][-1] in mismatch_graph[j] and dp[-1][k][-1] in mismatch_graph[j][costs[j + 1][l][-1]]:
k += 1
else:
break
if k == n[j]:
tmp.append((float('inf'), costs[j + 1][l][-1]))
else:
tmp.append((costs[j + 1][l][0] + dp[-1][k][0], costs[j + 1][l][-1]))
tmp.sort()
dp.append(tmp)
if min(dp[-1])[0] == float('inf'):
return - 1
else:
return min(dp[-1])[0]
print(solve_e())
```
Yes
| 6,027 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Submitted Solution:
```
#i'm n..not ba.. b..baka > <
import sys
n = [int(i) for i in input().split()]
a = [[], [], [], [], ]
new_pos = []
for i in range(4):
tmp_list = [int(x) for x in input().split()]
new_pos.append([0, ] * n[i])
for j in range(n[i]):
a[i].append([tmp_list[j], j])
def upd_pos(i):
a[i].sort()
for j in range(n[i]):
new_pos[i][a[i][j][1]] = j
m = []
conf = [[], [], [], ]
conf_num = [[], [], [], ]
rev_conf_num = [[-1] * n[0], [-1] * n[1], [-1] * n[2], ]
def upd_conf(i):
tmp_list = []
for j in range(m[i]):
tmp_list.append([conf[i][j][0], new_pos[i + 1][conf[i][j][1]]])
conf[i] = []
tmp_list.sort()
tmp_list.append([96969696, 0])
r = 0
for j in range(0, m[i] + 1):
if(j != m[i] and rev_conf_num[i][tmp_list[j][0]] != -1):
continue
if (j == 0 or tmp_list[j][0] != tmp_list[j-1][0]):
if tmp_list[j][1] != 0:
continue
if j > 0 and len(conf_num[i]):
rev_conf_num[i][conf_num[i][-1]] = len(conf[i])
conf[i].append(r)
r = 0
conf_num[i].append(tmp_list[j][0])
else:
if(tmp_list[j][1] != tmp_list[j-1][1] + 1):
rev_conf_num[i][tmp_list[j][0]] = len(conf[i])
else:
r += 1
def upd_val(i):
for j in range(n[i]):
try:
if(rev_conf_num[i][j] != -1 and conf[i][rev_conf_num[i][j]] == n[i + 1] - 1) :
print("oh shit")
except IndexError:
print("oh shit\n"*10)
if (rev_conf_num[i][j] == -1):
val = a[i + 1][0][0]
elif conf[i][rev_conf_num[i][j]] == n[i + 1] - 1:
val = 999999999999
print("oh shit")
print("oh shit")
print("oh shit")
print("oh shit")
print("oh shit")
print("oh shit")
print("oh shit")
print("oh shit")
print("oh shit")
print("oh shit")
print("oh shit")
print("oh shit")
else:
val = a[i + 1][conf[i][rev_conf_num[i][j]] + 1][0]
a[i][j][0] += val
for i in range(3):
m.append(int(input()))
for j in range(m[i]):
conf[i].append([int(x) - 1 for x in input().split()])
for i in range(2,-1,-1):
upd_pos(i + 1)
upd_conf(i)
upd_val(i)
upd_pos(0)
print(a[0][0][0] if a[0][0][0] < 999999999999 else -1)
```
No
| 6,028 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Submitted Solution:
```
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
INF = 10**18
def main():
n1, n2, n3, n4 = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
D = list(map(int, input().split()))
A = [(e, i) for i, e in enumerate(A)]
B = [(e, i) for i, e in enumerate(B)]
C = [(e, i) for i, e in enumerate(C)]
D = [(e, i) for i, e in enumerate(D)]
A.sort()
B.sort()
C.sort()
D.sort()
m1 = int(input())
XY1 = []
for i in range(m1):
x, y = map(int, input().split())
x, y = x-1, y-1
XY1.append((x,y))
m2 = int(input())
XY2 = []
for i in range(m2):
x, y = map(int, input().split())
x, y = x-1, y-1
XY2.append((x,y))
m3 = int(input())
XY3 = []
for i in range(m3):
x, y = map(int, input().split())
x, y = x-1, y-1
XY3.append((x,y))
N = n1+n2+n3+n4+2
g = [[] for i in range(N)]
for a, i in A:
g[N-2].append((a, i))
# 1-2
S1 = set(range(n1))
XY1 = set(XY1)
for b, i in B:
used = set()
for j in S1:
if (j, i) in XY1:
continue
g[j].append((b, i+n1))
used.add(j)
for j in used:
S1.remove(j)
if not S1:
break
S2 = set(range(n2))
for a, i in A:
used = set()
for j in S2:
if (i, j) in XY1:
continue
g[i].append((B[j][0], j+n1))
used.add(j)
for j in used:
S2.remove(j)
# 2-3
S2 = set(range(n2))
XY2 = set(XY2)
for c, i in C:
used = set()
for j in S2:
if (j, i) in XY2:
continue
g[j+n1].append((c, i+n1+n2))
used.add(j)
for j in used:
S2.remove(j)
S3 = set(range(n3))
for b, i in B:
used = set()
for j in S3:
if (i, j) in XY2:
continue
g[i+n1].append((C[j][0], j+n1+n2))
used.add(j)
for j in used:
S3.remove(j)
# 3-4
S3 = set(range(n3))
XY3 = set(XY3)
for d, i in D:
used = set()
for j in S3:
if (j, i) in XY3:
continue
g[j+n1+n2].append((d, i+n1+n2+n3))
used.add(j)
for j in used:
S3.remove(j)
S4 = set(range(n4))
XY3 = set(XY3)
for c, i in C:
used = set()
for j in S4:
if (i, j) in XY3:
continue
g[i+n1+n2].append((D[j][0], j+n1+n2+n3))
used.add(j)
for j in used:
S4.remove(j)
for i in range(n4):
g[i+n1+n2+n3].append((0, N-1))
import heapq
def dijkstra_heap(s, edge):
n = len(edge)
d = [INF] * n
used = [True] * n #True: not used
d[s] = 0
used[s] = False
edgelist = []
for e in edge[s]:
heapq.heappush(edgelist,e)
while len(edgelist):
minedge = heapq.heappop(edgelist)
if not used[minedge[1]]:
continue
v = minedge[1]
d[v] = minedge[0]
used[v] = False
for e in edge[v]:
if used[e[1]]:
heapq.heappush(edgelist,(e[0]+d[v],e[1]))
return d
d = dijkstra_heap(N-2, g)
#print(d)
if d[N-1] != INF:
print(d[N-1])
else:
print(-1)
if __name__ == '__main__':
main()
```
No
| 6,029 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Submitted Solution:
```
"""
Author - Satwik Tiwari .
7th Feb , 2021 - Sunday
"""
#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
# from itertools import *
from heapq import *
from math import gcd, factorial,floor,ceil,sqrt,log2
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
#==============================================================================================
#fast I/O region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
#===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType
def iterative(f, stack=[]):
def wrapped_func(*args, **kwargs):
if stack: return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
continue
stack.pop()
if not stack: break
to = stack[-1].send(to)
return to
return wrapped_func
#### END ITERATE RECURSION ####
#===============================================================================================
#some shortcuts
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def testcase(t):
for pp in range(t):
solve(pp)
def google(p):
print('Case #'+str(p)+': ',end='')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(x, y, p) :
y%=(p-1) #not so sure about this. used when y>p-1. if p is prime.
res = 1 # Initialize result
x = x % p # Update x if it is more , than or equal to p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) : # If y is odd, multiply, x with result
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def ncr(n,r): return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))
def isPrime(n) :
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
inf = pow(10,20)
mod = 10**9+7
#===============================================================================================
# code here ;))
def bucketsort(order, seq):
buckets = [0] * (max(seq) + 1)
for x in seq:
buckets[x] += 1
for i in range(len(buckets) - 1):
buckets[i + 1] += buckets[i]
new_order = [-1] * len(seq)
for i in reversed(order):
x = seq[i]
idx = buckets[x] = buckets[x] - 1
new_order[idx] = i
return new_order
def ordersort(order, seq, reverse=False):
bit = max(seq).bit_length() >> 1
mask = (1 << bit) - 1
order = bucketsort(order, [x & mask for x in seq])
order = bucketsort(order, [x >> bit for x in seq])
if reverse:
order.reverse()
return order
def long_ordersort(order, seq):
order = ordersort(order, [int(i & 0x7fffffff) for i in seq])
return ordersort(order, [int(i >> 31) for i in seq])
def multikey_ordersort(order, *seqs, sort=ordersort):
for i in reversed(range(len(seqs))):
order = sort(order, seqs[i])
return order
def do(a,b,map):
ans = [-1]*len(b)
ind = [i for i in range(len(a))]
order = multikey_ordersort(range(len(a)),a,ind)
new = []
for i in order:
new.append((a[i],ind[i]))
for i in range(len(b)):
for j in range(len(a)):
if((new[j][1],i) in map): continue
ans[i] = b[i] + new[j][0]
break
return ans
def solve(case):
n1,n2,n3,n4 = sep()
a = lis()
b = lis()
c = lis()
d = lis()
map1 = {}
for i in range(int(inp())):
x,y = sep()
x-=1;y-=1
map1[(x,y)] = 1
map2 = {}
for i in range(int(inp())):
x,y = sep()
x-=1;y-=1
map2[(x,y)] = 1
map3 = {}
for i in range(int(inp())):
x,y = sep()
x-=1;y-=1
map3[(x,y)] = 1
dp = a
dp = do(dp,b,map1)
if(min(dp) <= 0):
print(-1)
return
dp = do(dp,c,map2)
if(min(dp) <= 0):
print(-1)
return
dp = do(dp,d,map3)
ans = min(dp)
if(ans <= 0): print(-1)
else: print(ans)
testcase(1)
# testcase(int(inp()))
```
No
| 6,030 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).
Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
Input
The first line contains four integers n_1, n_2, n_3 and n_4 (1 β€ n_i β€ 150000) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains n_1 integers a_1, a_2, ..., a_{n_1} (1 β€ a_i β€ 10^8), where a_i is the cost of the i-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1 β€ b_i, c_i, d_i β€ 10^8).
The next line contains one integer m_1 (0 β€ m_1 β€ 200000) β the number of pairs of first and second courses that don't go well with each other. Each of the next m_1 lines contains two integers x_i and y_i (1 β€ x_i β€ n_1; 1 β€ y_i β€ n_2) denoting that the first course number x_i doesn't go well with the second course number y_i. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0 β€ m_2, m_3 β€ 200000).
Output
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print -1. Otherwise, print one integer β the minimum total cost of the dinner.
Examples
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
Note
The best option in the first example is to take the first course 2, the second course 1, the drink 2 and the dessert 1.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
Submitted Solution:
```
import io
import os
from collections import defaultdict
# https://raw.githubusercontent.com/cheran-senthil/PyRival/master/pyrival/data_structures/SegmentTree.py
class SegmentTree:
def __init__(self, data, default=0, func=max):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size : _size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
"""func of data[start, stop)"""
start += self._size
stop += self._size
res_left = res_right = self._default
while start < stop:
if start & 1:
res_left = self._func(res_left, self.data[start])
start += 1
if stop & 1:
stop -= 1
res_right = self._func(self.data[stop], res_right)
start >>= 1
stop >>= 1
return self._func(res_left, res_right)
def __repr__(self):
return "SegmentTree({0})".format(self.data)
inf = float("inf")
def solve(A, B, C, D, M1, M2, M3):
def groupBadPrev(M):
d = defaultdict(list)
for x, y in M:
x -= 1
y -= 1
d[y].append(x)
return d
dp = list(A)
courses = [B, C, D]
badPrevs = [
groupBadPrev(M1),
groupBadPrev(M2),
groupBadPrev(M3),
]
for course, badPrev in zip(courses, badPrevs):
prevDp = dp
segTree = SegmentTree(prevDp, inf, min)
dp = [inf] * len(course)
bestA = segTree.query(0, len(segTree))
for b in range(len(course)):
if b in badPrev:
# Temporarily set unreachable prev to inf weight
for a in badPrev[b]:
segTree[a] = inf
# Extend off lowest
dp[b] = segTree.query(0, len(segTree)) + course[b]
# Undo
for a in badPrev[b]:
segTree[a] = prevDp[a]
else:
dp[b] = bestA + course[b]
ans = max(dp)
if ans == inf:
return -1
return ans
if __name__ == "__main__":
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = 1
for tc in range(1, TC + 1):
N1, N2, N3, N4 = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
C = [int(x) for x in input().split()]
D = [int(x) for x in input().split()]
(M,) = [int(x) for x in input().split()]
M1 = [[int(x) for x in input().split()] for i in range(M)]
(M,) = [int(x) for x in input().split()]
M2 = [[int(x) for x in input().split()] for i in range(M)]
(M,) = [int(x) for x in input().split()]
M3 = [[int(x) for x in input().split()] for i in range(M)]
ans = solve(A, B, C, D, M1, M2, M3)
print(ans)
```
No
| 6,031 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Tags: constructive algorithms, math, number theory
Correct Solution:
```
for t in range(int(input())):
a, b, c = map(int, input().split())
print("1" + "0" * (a - 1), "1" * (b - c + 1) + "0" * (c - 1))
```
| 6,032 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Tags: constructive algorithms, math, number theory
Correct Solution:
```
for _ in range(int(input())):
a, b, c = map(int, input().split())
g = 7
while len(str(g)) < c: g *= 7
assert len(str(g)) == c
x = g
y = g
while len(str(x)) < a: x *= 2
while len(str(y)) < b: y *= 3
assert len(str(x)) == a
assert len(str(y)) == b
print(x, y)
```
| 6,033 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Tags: constructive algorithms, math, number theory
Correct Solution:
```
T=int(input())
for t in range(T):
a,b,c=[int(i) for i in input().split()]
gcd=10**(c-1)
x=gcd
lim=10**(a-1)
while x<lim or x<gcd:
x*=2
y=gcd
lim=10**(b-1)
while y<lim or y<gcd:
y*=3
print(x,y)
```
| 6,034 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Tags: constructive algorithms, math, number theory
Correct Solution:
```
import time
#start_time = time.time()
#def TIME_(): print(time.time()-start_time)
import os, sys
from io import BytesIO, IOBase
from types import GeneratorType
from bisect import bisect_left, bisect_right
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string, heapq as h
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def getInt(): return int(input())
def getStrs(): return input().split()
def getInts(): return list(map(int,input().split()))
def getStr(): return input()
def listStr(): return list(input())
def getMat(n): return [getInts() for _ in range(n)]
def getBin(): return list(map(int,list(input())))
def isInt(s): return '0' <= s[0] <= '9'
def ceil_(a,b): return a//b + (a%b > 0)
MOD = 10**9 + 7
"""
Start with number Z of C digits
"""
def solve():
A,B,C = getInts()
Z = 10**(C-1)
X = Z
Y = Z
while not len(str(X)) == A:
X *= 7
while not len(str(Y)) == B:
Y *= 3
print(X,Y)
return
for _ in range(getInt()):
#print(solve())
solve()
#TIME_()
```
| 6,035 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Tags: constructive algorithms, math, number theory
Correct Solution:
```
from sys import maxsize, stdout, stdin,stderr
mod = int(1e9 + 7)
def I(): return int(stdin.readline())
def lint(): return [int(x) for x in stdin.readline().split()]
def S(): return input().strip()
def grid(r, c): return [lint() for i in range(r)]
from collections import defaultdict
import math
l = [2,16,128,1024,16384,131072,1048576,16777216,134217728]
t = I()
for _ in range(t):
a,b,c= lint()
c-=1
c=l[c]
a1 = l[a-1]
b-=1
b1=c
while(b1/10**b<1):
b1*=3
print(a1,b1)
```
| 6,036 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Tags: constructive algorithms, math, number theory
Correct Solution:
```
t = int(input())
for _ in range(t):
a,b,c = map(int, input().split())
a = a-1
b = b-1
c = c-1
x = 10**a
y = (10**b) + (10**c)
print(x,y)
```
| 6,037 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Tags: constructive algorithms, math, number theory
Correct Solution:
```
import os
from io import BytesIO, IOBase
import sys
import math
def main():
for i in range(int(input())):
a,b,c=map(int,input().split())
a1=1
b1=1
c1=1
while len(str(c1))<c:
c1=c1*2
a1=a1*2
b1=b1*2
while len(str(b1))<b:
b1=b1*5
while len(str(a1))<a:
a1=a1*3
print(a1,b1)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
```
| 6,038 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Tags: constructive algorithms, math, number theory
Correct Solution:
```
#rOkY
#FuCk
############################### kOpAl ##################################
t=int(input())
while(t>0):
a,b,c=map(int,input().split())
x=1
y=1
while(len(str(x))!=c):
if(len(str(x))==c):
break
x*=2
y*=2
while(len(str(x))!=a):
if(len(str(x))==a):
break
x*=3
while(len(str(y))!=b):
if(len(str(y))==b):
break
y*=5
print(x,y)
t-=1
```
| 6,039 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Submitted Solution:
```
from math import gcd
def solve():
a, b, c = map(int, input().split())
print(*res[a - 1][b - 1][c - 1])
if __name__ == '__main__':
res = [[[[2, 2], [11, 11], [101, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[2, 10], [11, 11], [101, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[2, 100], [11, 110], [101, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[2, 1000], [11, 1001], [101, 1010], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[2, 10000], [11, 10010], [101, 10100], [1009, 10090], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[2, 100000], [11, 100001], [101, 100091], [1009, 100900], [10007, 100070], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[2, 1000000], [11, 1000010], [101, 1000001], [1009, 1000928], [10007, 1000700], [100003, 1000030], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[2, 10000000], [11, 10000001], [101, 10000010], [1009, 10000199], [10007, 10007000], [100003, 10000300], [1000033, 10000330], [10000019, 10000019], [100000007, 100000007]], [[2, 100000000], [11, 100000010], [101, 100000100], [1009, 100000981], [10007, 100009958], [100003, 100003000], [1000033, 100003300], [10000019, 100000190], [100000007, 100000007]]], [[[10, 2], [11, 11], [101, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10, 12], [11, 11], [101, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10, 102], [11, 110], [101, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10, 1002], [11, 1001], [101, 1010], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10, 10002], [11, 10010], [101, 10100], [1009, 10090], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10, 100002], [11, 100001], [101, 100091], [1009, 100900], [10007, 100070], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10, 1000002], [11, 1000010], [101, 1000001], [1009, 1000928], [10007, 1000700], [100003, 1000030], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10, 10000002], [11, 10000001], [101, 10000010], [1009, 10000199], [10007, 10007000], [100003, 10000300], [1000033, 10000330], [10000019, 10000019], [100000007, 100000007]], [[10, 100000002], [11, 100000010], [101, 100000100], [1009, 100000981], [10007, 100009958], [100003, 100003000], [1000033, 100003300], [10000019, 100000190], [100000007, 100000007]]], [[[100, 2], [110, 11], [101, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100, 14], [110, 11], [101, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100, 102], [110, 121], [101, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100, 1002], [110, 1001], [101, 1010], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100, 10002], [110, 10021], [101, 10100], [1009, 10090], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100, 100002], [110, 100001], [101, 100091], [1009, 100900], [10007, 100070], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100, 1000002], [110, 1000021], [101, 1000001], [1009, 1000928], [10007, 1000700], [100003, 1000030], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100, 10000002], [110, 10000001], [101, 10000010], [1009, 10000199], [10007, 10007000], [100003, 10000300], [1000033, 10000330], [10000019, 10000019], [100000007, 100000007]], [[100, 100000002], [110, 100000021], [101, 100000100], [1009, 100000981], [10007, 100009958], [100003, 100003000], [1000033, 100003300], [10000019, 100000190], [100000007, 100000007]]], [[[1000, 2], [1001, 11], [1010, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000, 14], [1001, 11], [1010, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000, 102], [1001, 110], [1010, 101], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000, 1002], [1001, 1012], [1010, 1111], [1009, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000, 10002], [1001, 10021], [1010, 10201], [1009, 10090], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000, 100002], [1001, 100001], [1010, 100091], [1009, 100900], [10007, 100070], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000, 1000002], [1001, 1000010], [1010, 1000001], [1009, 1000928], [10007, 1000700], [100003, 1000030], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000, 10000002], [1001, 10000001], [1010, 10000111], [1009, 10000199], [10007, 10007000], [100003, 10000300], [1000033, 10000330], [10000019, 10000019], [100000007, 100000007]], [[1000, 100000002], [1001, 100000010], [1010, 100000201], [1009, 100000981], [10007, 100009958], [100003, 100003000], [1000033, 100003300], [10000019, 100000190], [100000007, 100000007]]], [[[10000, 2], [10010, 11], [10100, 101], [10090, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000, 14], [10010, 11], [10100, 101], [10090, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000, 102], [10010, 121], [10100, 101], [10090, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000, 1002], [10010, 1023], [10100, 1111], [10090, 1009], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000, 10002], [10010, 10021], [10100, 10201], [10090, 11099], [10007, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000, 100002], [10010, 100001], [10100, 100091], [10090, 101909], [10007, 100070], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000, 1000002], [10010, 1000021], [10100, 1000001], [10090, 1001937], [10007, 1000700], [100003, 1000030], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000, 10000002], [10010, 10000001], [10100, 10000111], [10090, 10000199], [10007, 10007000], [100003, 10000300], [1000033, 10000330], [10000019, 10000019], [100000007, 100000007]], [[10000, 100000002], [10010, 100000021], [10100, 100000201], [10090, 100000981], [10007, 100009958], [100003, 100003000], [1000033, 100003300], [10000019, 100000190], [100000007, 100000007]]], [[[100000, 2], [100001, 11], [100091, 101], [100900, 1009], [100070, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100000, 14], [100001, 11], [100091, 101], [100900, 1009], [100070, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100000, 102], [100001, 110], [100091, 101], [100900, 1009], [100070, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100000, 1002], [100001, 1001], [100091, 1010], [100900, 1009], [100070, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100000, 10002], [100001, 10010], [100091, 10100], [100900, 11099], [100070, 10007], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100000, 100002], [100001, 100012], [100091, 100192], [100900, 101909], [100070, 110077], [100003, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100000, 1000002], [100001, 1000021], [100091, 1000001], [100900, 1001937], [100070, 1010707], [100003, 1000030], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[100000, 10000002], [100001, 10000001], [100091, 10000010], [100900, 10000199], [100070, 10017007], [100003, 10000300], [1000033, 10000330], [10000019, 10000019], [100000007, 100000007]], [[100000, 100000002], [100001, 100000010], [100091, 100000100], [100900, 100000981], [100070, 100039979], [100003, 100003000], [1000033, 100003300], [10000019, 100000190], [100000007, 100000007]]], [[[1000000, 2], [1000010, 11], [1000001, 101], [1000928, 1009], [1000700, 10007], [1000030, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000000, 14], [1000010, 11], [1000001, 101], [1000928, 1009], [1000700, 10007], [1000030, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000000, 102], [1000010, 121], [1000001, 101], [1000928, 1009], [1000700, 10007], [1000030, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000000, 1002], [1000010, 1001], [1000001, 1010], [1000928, 1009], [1000700, 10007], [1000030, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000000, 10002], [1000010, 10021], [1000001, 10100], [1000928, 11099], [1000700, 10007], [1000030, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000000, 100002], [1000010, 100023], [1000001, 100091], [1000928, 101909], [1000700, 110077], [1000030, 100003], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000000, 1000002], [1000010, 1000021], [1000001, 1000102], [1000928, 1001937], [1000700, 1010707], [1000030, 1100033], [1000033, 1000033], [10000019, 10000019], [100000007, 100000007]], [[1000000, 10000002], [1000010, 10000001], [1000001, 10000111], [1000928, 10000199], [1000700, 10017007], [1000030, 10100303], [1000033, 10000330], [10000019, 10000019], [100000007, 100000007]], [[1000000, 100000002], [1000010, 100000021], [1000001, 100000201], [1000928, 100000981], [1000700, 100039979], [1000030, 100103003], [1000033, 100003300], [10000019, 100000190], [100000007, 100000007]]], [[[10000000, 2], [10000001, 11], [10000010, 101], [10000199, 1009], [10007000, 10007], [10000300, 100003], [10000330, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000000, 14], [10000001, 11], [10000010, 101], [10000199, 1009], [10007000, 10007], [10000300, 100003], [10000330, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000000, 102], [10000001, 110], [10000010, 101], [10000199, 1009], [10007000, 10007], [10000300, 100003], [10000330, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000000, 1002], [10000001, 1001], [10000010, 1111], [10000199, 1009], [10007000, 10007], [10000300, 100003], [10000330, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000000, 10002], [10000001, 10010], [10000010, 10201], [10000199, 10090], [10007000, 10007], [10000300, 100003], [10000330, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000000, 100002], [10000001, 100001], [10000010, 100091], [10000199, 100900], [10007000, 110077], [10000300, 100003], [10000330, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000000, 1000002], [10000001, 1000010], [10000010, 1000203], [10000199, 1000928], [10007000, 1010707], [10000300, 1100033], [10000330, 1000033], [10000019, 10000019], [100000007, 100000007]], [[10000000, 10000002], [10000001, 10000012], [10000010, 10000111], [10000199, 10001208], [10007000, 10017007], [10000300, 10100303], [10000330, 11000363], [10000019, 10000019], [100000007, 100000007]], [[10000000, 100000002], [10000001, 100000021], [10000010, 100000201], [10000199, 100000981], [10007000, 100039979], [10000300, 100103003], [10000330, 101003333], [10000019, 100000190], [100000007, 100000007]]], [[[100000000, 2], [100000010, 11], [100000100, 101], [100000981, 1009], [100009958, 10007], [100003000, 100003], [100003300, 1000033], [100000190, 10000019], [100000007, 100000007]], [[100000000, 14], [100000010, 11], [100000100, 101], [100000981, 1009], [100009958, 10007], [100003000, 100003], [100003300, 1000033], [100000190, 10000019], [100000007, 100000007]], [[100000000, 102], [100000010, 121], [100000100, 101], [100000981, 1009], [100009958, 10007], [100003000, 100003], [100003300, 1000033], [100000190, 10000019], [100000007, 100000007]], [[100000000, 1002], [100000010, 1001], [100000100, 1111], [100000981, 1009], [100009958, 10007], [100003000, 100003], [100003300, 1000033], [100000190, 10000019], [100000007, 100000007]], [[100000000, 10002], [100000010, 10021], [100000100, 10201], [100000981, 10090], [100009958, 10007], [100003000, 100003], [100003300, 1000033], [100000190, 10000019], [100000007, 100000007]], [[100000000, 100002], [100000010, 100001], [100000100, 100091], [100000981, 100900], [100009958, 110077], [100003000, 100003], [100003300, 1000033], [100000190, 10000019], [100000007, 100000007]], [[100000000, 1000002], [100000010, 1000021], [100000100, 1000203], [100000981, 1000928], [100009958, 1010707], [100003000, 1100033], [100003300, 1000033], [100000190, 10000019], [100000007, 100000007]], [[100000000, 10000002], [100000010, 10000023], [100000100, 10000111], [100000981, 10000199], [100009958, 10017007], [100003000, 10100303], [100003300, 11000363], [100000190, 10000019], [100000007, 100000007]], [[100000000, 100000002], [100000010, 100000021], [100000100, 100000201], [100000981, 100001990], [100009958, 100019965], [100003000, 100103003], [100003300, 101003333], [100000190, 110000209], [100000007, 100000007]]]]
t = int(input())
for _ in range(t):
solve()
```
Yes
| 6,040 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Submitted Solution:
```
#print("/sys/devices/pci0000:00/0000:00:01.0/0000:02:00.0/drm/card0/card0-LVDS-1/radeon_bl0$ sudo nano brightness")
for _ in range(int(input())):
a,b,c=map(int,input().split())
print(10**(a-1)+10**(c-1),10**(b-1))
```
Yes
| 6,041 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Submitted Solution:
```
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())
t = I()
for _ in range(t):
a,b,c = MI()
if a >= b:
x = 10**(a-1)+10**(c-1)
y = 10**(b-1)
else:
x = 10**(a-1)
y = 10**(b-1)+10**(c-1)
print(x,y)
```
Yes
| 6,042 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Submitted Solution:
```
from math import gcd as g
t = int(input())
for i in range(t):
a, b, c = list(map(int, input().split()))
am = 10 ** a - 1
bm = 10 ** b - 1
cm = 10 ** c - 1
ax = 10 ** (a - 1)
bx = 10 ** (b - 1)
cx = 10 ** (c - 1)
m = 0
if a == b and b == c: print(am, bm)
elif c == 1:
if a > b: print(bm*(10 ** (a - b)) + 1, bm)
elif b > a: print(am, am * (10 ** (b -a)) + 1)
else: print (am, am - 1)
else:
if a > b:
if b != c: print(ax, bx + cx)
else: print(ax, bx)
else:
if a != c: print(ax + cx, bx)
else: print(ax, bx)
```
Yes
| 6,043 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Submitted Solution:
```
t = int(input())
for _ in range(t):
a,b,c = list(map(int,input().strip().split()))
x,y,z = 0,0,0
c -= 1
z = 10**c
primes = {1:[3,5],2:[11,13],3:[151,163],4:[1064,1069],5:[10009,10039],6:[947711,947263],7:[1777771,5164541],8:[12815137,12830327],9:[10000019,10000169]}
x = primes[(a-c)][0]*z
y = primes[(b-c)][1]*z
print(x,y)
1 - 0,1
2 - 2,3
3 - 4,5
4 - 6,7
6 - 8,9
```
No
| 6,044 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Submitted Solution:
```
import math as m
for _ in range(int(input())):
a,b,c=map(int,input().split())
x=(10**(a-c-1)+1)*(10**c)
y=(10**(b-c-1)+2)*(10**c)
print(x,y)
```
No
| 6,045 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Submitted Solution:
```
t = int(input())
for __ in range(t):
a,b,c = map(int,input().split())
aa = [1]*a
ab = [1]*b
ans = 1
aa = aa[:len(aa)-c+1] + [0]*(c-1)
ab = ab[:len(ab)-c+1] + [0]*(c-1)
for j in range(len(aa)-1,-1,-1):
if aa[j]==1:
aa[j] = ans
ans+=1
ans = 2
for j in range(len(ab)-1,-1,-1):
if ab[j]==1:
ab[j] = ans
ans+=1
print(*aa," ",*ab,sep="")
```
No
| 6,046 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers a, b and c.
Find two positive integers x and y (x > 0, y > 0) such that:
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y.
Output x and y. If there are multiple answers, output any of them.
Input
The first line contains a single integer t (1 β€ t β€ 285) β the number of testcases.
Each of the next t lines contains three integers a, b and c (1 β€ a, b β€ 9, 1 β€ c β€ min(a, b)) β the required lengths of the numbers.
It can be shown that the answer exists for all testcases under the given constraints.
Additional constraint on the input: all testcases are different.
Output
For each testcase print two positive integers β x and y (x > 0, y > 0) such that
* the decimal representation of x without leading zeroes consists of a digits;
* the decimal representation of y without leading zeroes consists of b digits;
* the decimal representation of gcd(x, y) without leading zeroes consists of c digits.
Example
Input
4
2 3 1
2 2 2
6 6 2
1 1 1
Output
11 492
13 26
140133 160776
1 1
Note
In the example:
1. gcd(11, 492) = 1
2. gcd(13, 26) = 13
3. gcd(140133, 160776) = 21
4. gcd(1, 1) = 1
Submitted Solution:
```
M=\
{(1, 1, 1): (3,3),
(1, 2, 1): (3,11),
(1, 3, 1): (3,101),
(1, 4, 1): (3,1009),
(1, 5, 1): (3,10007),
(1, 6, 1): (3,100003),
(1, 7, 1): (3,1000003),
(1, 8, 1): (3,10000019),
(1, 9, 1): (3,100000007),
(2, 1, 1): (11,3),
(2, 2, 1): (11,11),
(2, 2, 2): (22,11),
(2, 3, 1): (11,101),
(2, 3, 2): (22,297),
(2, 4, 1): (11,1009),
(2, 4, 2): (22,2673),
(2, 5, 1): (11,10007),
(2, 5, 2): (22,24057),
(2, 6, 1): (11,100003),
(2, 6, 2): (22,216513),
(2, 7, 1): (11,1000003),
(2, 7, 2): (22,1948617),
(2, 8, 1): (11,10000019),
(2, 8, 2): (22,17537553),
(2, 9, 1): (11,100000007),
(2, 9, 2): (22,157837977),
(3, 1, 1): (101,3),
(3, 2, 1): (101,11),
(3, 2, 2): (220,11),
(3, 3, 1): (101,101),
(3, 3, 2): (220,297),
(3, 3, 3): (202,101),
(3, 4, 1): (101,1009),
(3, 4, 2): (220,2673),
(3, 4, 3): (202,2727),
(3, 5, 1): (101,10007),
(3, 5, 2): (220,24057),
(3, 5, 3): (202,24543),
(3, 6, 1): (101,100003),
(3, 6, 2): (220,216513),
(3, 6, 3): (202,220887),
(3, 7, 1): (101,1000003),
(3, 7, 2): (220,1948617),
(3, 7, 3): (202,1987983),
(3, 8, 1): (101,10000019),
(3, 8, 2): (220,17537553),
(3, 8, 3): (202,17891847),
(3, 9, 1): (101,100000007),
(3, 9, 2): (220,157837977),
(3, 9, 3): (202,161026623),
(4, 1, 1): (1009,3),
(4, 2, 1): (1009,11),
(4, 2, 2): (2200,11),
(4, 3, 1): (1009,101),
(4, 3, 2): (2200,297),
(4, 3, 3): (2020,101),
(4, 4, 1): (1009,1009),
(4, 4, 2): (2200,2673),
(4, 4, 3): (2020,2727),
(4, 4, 4): (2018,1009),
(4, 5, 1): (1009,10007),
(4, 5, 2): (2200,24057),
(4, 5, 3): (2020,24543),
(4, 5, 4): (2018,27243),
(4, 6, 1): (1009,100003),
(4, 6, 2): (2200,216513),
(4, 6, 3): (2020,220887),
(4, 6, 4): (2018,245187),
(4, 7, 1): (1009,1000003),
(4, 7, 2): (2200,1948617),
(4, 7, 3): (2020,1987983),
(4, 7, 4): (2018,2206683),
(4, 8, 1): (1009,10000019),
(4, 8, 2): (2200,17537553),
(4, 8, 3): (2020,17891847),
(4, 8, 4): (2018,19860147),
(4, 9, 1): (1009,100000007),
(4, 9, 2): (2200,157837977),
(4, 9, 3): (2020,161026623),
(4, 9, 4): (2018,178741323),
(5, 1, 1): (10007,3),
(5, 2, 1): (10007,11),
(5, 2, 2): (22000,11),
(5, 3, 1): (10007,101),
(5, 3, 2): (22000,297),
(5, 3, 3): (20200,101),
(5, 4, 1): (10007,1009),
(5, 4, 2): (22000,2673),
(5, 4, 3): (20200,2727),
(5, 4, 4): (20180,1009),
(5, 5, 1): (10007,10007),
(5, 5, 2): (22000,24057),
(5, 5, 3): (20200,24543),
(5, 5, 4): (20180,27243),
(5, 5, 5): (20014,10007),
(5, 6, 1): (10007,100003),
(5, 6, 2): (22000,216513),
(5, 6, 3): (20200,220887),
(5, 6, 4): (20180,245187),
(5, 6, 5): (20014,270189),
(5, 7, 1): (10007,1000003),
(5, 7, 2): (22000,1948617),
(5, 7, 3): (20200,1987983),
(5, 7, 4): (20180,2206683),
(5, 7, 5): (20014,2431701),
(5, 8, 1): (10007,10000019),
(5, 8, 2): (22000,17537553),
(5, 8, 3): (20200,17891847),
(5, 8, 4): (20180,19860147),
(5, 8, 5): (20014,21885309),
(5, 9, 1): (10007,100000007),
(5, 9, 2): (22000,157837977),
(5, 9, 3): (20200,161026623),
(5, 9, 4): (20180,178741323),
(5, 9, 5): (20014,196967781),
(6, 1, 1): (100003,3),
(6, 2, 1): (100003,11),
(6, 2, 2): (220000,11),
(6, 3, 1): (100003,101),
(6, 3, 2): (220000,297),
(6, 3, 3): (202000,101),
(6, 4, 1): (100003,1009),
(6, 4, 2): (220000,2673),
(6, 4, 3): (202000,2727),
(6, 4, 4): (201800,1009),
(6, 5, 1): (100003,10007),
(6, 5, 2): (220000,24057),
(6, 5, 3): (202000,24543),
(6, 5, 4): (201800,27243),
(6, 5, 5): (200140,10007),
(6, 6, 1): (100003,100003),
(6, 6, 2): (220000,216513),
(6, 6, 3): (202000,220887),
(6, 6, 4): (201800,245187),
(6, 6, 5): (200140,270189),
(6, 6, 6): (200006,100003),
(6, 7, 1): (100003,1000003),
(6, 7, 2): (220000,1948617),
(6, 7, 3): (202000,1987983),
(6, 7, 4): (201800,2206683),
(6, 7, 5): (200140,2431701),
(6, 7, 6): (200006,2700081),
(6, 8, 1): (100003,10000019),
(6, 8, 2): (220000,17537553),
(6, 8, 3): (202000,17891847),
(6, 8, 4): (201800,19860147),
(6, 8, 5): (200140,21885309),
(6, 8, 6): (200006,24300729),
(6, 9, 1): (100003,100000007),
(6, 9, 2): (220000,157837977),
(6, 9, 3): (202000,161026623),
(6, 9, 4): (201800,178741323),
(6, 9, 5): (200140,196967781),
(6, 9, 6): (200006,218706561),
(7, 1, 1): (1000003,3),
(7, 2, 1): (1000003,11),
(7, 2, 2): (2200000,11),
(7, 3, 1): (1000003,101),
(7, 3, 2): (2200000,297),
(7, 3, 3): (2020000,101),
(7, 4, 1): (1000003,1009),
(7, 4, 2): (2200000,2673),
(7, 4, 3): (2020000,2727),
(7, 4, 4): (2018000,1009),
(7, 5, 1): (1000003,10007),
(7, 5, 2): (2200000,24057),
(7, 5, 3): (2020000,24543),
(7, 5, 4): (2018000,27243),
(7, 5, 5): (2001400,10007),
(7, 6, 1): (1000003,100003),
(7, 6, 2): (2200000,216513),
(7, 6, 3): (2020000,220887),
(7, 6, 4): (2018000,245187),
(7, 6, 5): (2001400,270189),
(7, 6, 6): (2000060,100003),
(7, 7, 1): (1000003,1000003),
(7, 7, 2): (2200000,1948617),
(7, 7, 3): (2020000,1987983),
(7, 7, 4): (2018000,2206683),
(7, 7, 5): (2001400,2431701),
(7, 7, 6): (2000060,2700081),
(7, 7, 7): (2000006,1000003),
(7, 8, 1): (1000003,10000019),
(7, 8, 2): (2200000,17537553),
(7, 8, 3): (2020000,17891847),
(7, 8, 4): (2018000,19860147),
(7, 8, 5): (2001400,21885309),
(7, 8, 6): (2000060,24300729),
(7, 8, 7): (2000006,27000081),
(7, 9, 1): (1000003,100000007),
(7, 9, 2): (2200000,157837977),
(7, 9, 3): (2020000,161026623),
(7, 9, 4): (2018000,178741323),
(7, 9, 5): (2001400,196967781),
(7, 9, 6): (2000060,218706561),
(7, 9, 7): (2000006,243000729),
(8, 1, 1): (10000019,3),
(8, 2, 1): (10000019,11),
(8, 2, 2): (22000000,11),
(8, 3, 1): (10000019,101),
(8, 3, 2): (22000000,297),
(8, 3, 3): (20200000,101),
(8, 4, 1): (10000019,1009),
(8, 4, 2): (22000000,2673),
(8, 4, 3): (20200000,2727),
(8, 4, 4): (20180000,1009),
(8, 5, 1): (10000019,10007),
(8, 5, 2): (22000000,24057),
(8, 5, 3): (20200000,24543),
(8, 5, 4): (20180000,27243),
(8, 5, 5): (20014000,10007),
(8, 6, 1): (10000019,100003),
(8, 6, 2): (22000000,216513),
(8, 6, 3): (20200000,220887),
(8, 6, 4): (20180000,245187),
(8, 6, 5): (20014000,270189),
(8, 6, 6): (20000600,100003),
(8, 7, 1): (10000019,1000003),
(8, 7, 2): (22000000,1948617),
(8, 7, 3): (20200000,1987983),
(8, 7, 4): (20180000,2206683),
(8, 7, 5): (20014000,2431701),
(8, 7, 6): (20000600,2700081),
(8, 7, 7): (20000060,1000003),
(8, 8, 1): (10000019,10000019),
(8, 8, 2): (22000000,17537553),
(8, 8, 3): (20200000,17891847),
(8, 8, 4): (20180000,19860147),
(8, 8, 5): (20014000,21885309),
(8, 8, 6): (20000600,24300729),
(8, 8, 7): (20000060,27000081),
(8, 8, 8): (20000038,10000019),
(8, 9, 1): (10000019,100000007),
(8, 9, 2): (22000000,157837977),
(8, 9, 3): (20200000,161026623),
(8, 9, 4): (20180000,178741323),
(8, 9, 5): (20014000,196967781),
(8, 9, 6): (20000600,218706561),
(8, 9, 7): (20000060,243000729),
(8, 9, 8): (20000038,270000513),
(9, 1, 1): (100000007,3),
(9, 2, 1): (100000007,11),
(9, 2, 2): (220000000,11),
(9, 3, 1): (100000007,101),
(9, 3, 2): (220000000,297),
(9, 3, 3): (202000000,101),
(9, 4, 1): (100000007,1009),
(9, 4, 2): (220000000,2673),
(9, 4, 3): (202000000,2727),
(9, 4, 4): (201800000,1009),
(9, 5, 1): (100000007,10007),
(9, 5, 2): (220000000,24057),
(9, 5, 3): (202000000,24543),
(9, 5, 4): (201800000,27243),
(9, 5, 5): (200140000,10007),
(9, 6, 1): (100000007,100003),
(9, 6, 2): (220000000,216513),
(9, 6, 3): (202000000,220887),
(9, 6, 4): (201800000,245187),
(9, 6, 5): (200140000,270189),
(9, 6, 6): (200006000,100003),
(9, 7, 1): (100000007,1000003),
(9, 7, 2): (220000000,1948617),
(9, 7, 3): (202000000,1987983),
(9, 7, 4): (201800000,2206683),
(9, 7, 5): (200140000,2431701),
(9, 7, 6): (200006000,2700081),
(9, 7, 7): (200000600,1000003),
(9, 8, 1): (100000007,10000019),
(9, 8, 2): (220000000,17537553),
(9, 8, 3): (202000000,17891847),
(9, 8, 4): (201800000,19860147),
(9, 8, 5): (200140000,21885309),
(9, 8, 6): (200006000,24300729),
(9, 8, 7): (200000600,27000081),
(9, 8, 8): (200000380,10000019),
(9, 9, 1): (100000007,100000007),
(9, 9, 2): (220000000,157837977),
(9, 9, 3): (202000000,161026623),
(9, 9, 4): (201800000,178741323),
(9, 9, 5): (200140000,196967781),
(9, 9, 6): (200006000,218706561),
(9, 9, 7): (200000600,243000729),
(9, 9, 8): (200000380,270000513),
(9, 9, 9): (200000014,100000007)}
T=int(input())
for _ in range(T):
a,b,c=map(int,input().split())
print(*M[(a,b,c)])
```
No
| 6,047 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Tags: binary search, greedy, math, ternary search
Correct Solution:
```
for _ in range(int(input())):
x,y,a,b=list(map(int,input().split()))
if x>y:
x,y=y,x
if a>b:
a,b=b,a
d,d1,v=y-x,b-a,0
if d>d1:
q=10**18
if d1!=0:
q=d//d1
v1=min(q,x//a,y//b)
v+=v1
x,y=x-a*v1,y-b*v1
val1,val2=2*min(x//(a+b),y//(a+b)),0
q1=(x-a)//(a+b)
q2=(y-b)//(a+b)
if min(x//a,y//b)!=0:
val2=1+2*min(q1,q2)
print(v+max(val1,val2))
```
| 6,048 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Tags: binary search, greedy, math, ternary search
Correct Solution:
```
t = int(input())
for _ in range(t):
x, y, a, b = map(int, input().split())
d = 0
if a > b:
a, b = b, a
if x > y:
x, y = y, x
ans = min(x//a, y//b)
if a != b:
d = (y * b - x * a)//(b * b - a * a)
for k in range(d, d + 2):
A = x - k * a
B = y - k * b
if A >= 0 and B >= 0:
ans = max(ans, k + min(A//b, B//a))
print(ans)
```
| 6,049 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Tags: binary search, greedy, math, ternary search
Correct Solution:
```
import sys
input = sys.stdin.readline
def solve():
x, y, a, b = map(int, input().split())
w = min(x//a, y//b)
xx = x - a*w
yy = y - b*w
r = w + min(xx//b, yy//a)
w = min(x//b, y//a)
xx = x - b*w
yy = y - a*w
r = max(r, w + min(xx//a, yy//b))
if b*b - a*a != 0:
z = (y*b-x*a)//(b*b-a*a)
for zz in range(z-1,z+2):
if zz < 0:
continue
#print('zz', zz)
x1 = x - a*zz
y1 = y - b*zz
if x1 < 0 or y1 < 0:
continue
w = min(x1//a, y1//b)
xx = x1 - a*w
yy = y1 - b*w
r = max(r, zz + w + min(xx//b, yy//a))
w = min(x1//b, y1//a)
xx = x1 - b*w
yy = y1 - a*w
#print('zz', zz, w, min(xx//a, yy//b),zz + w + min(xx//a, yy//b))
r = max(r, zz + w + min(xx//a, yy//b))
print(r)
for i in range(int(input())):
solve()
```
| 6,050 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Tags: binary search, greedy, math, ternary search
Correct Solution:
```
for _ in range(int(input())):
x, y, a, b = map(int,input().split())
# print(x,y,a,b)
if x>y:
x,y=y,x
if a>b:
a,b=b,a
# print(a,b)
if y-x<=b-a:
base1 = x//(a+b)
base2 = min((x-base1*(a+b))//a,(y-base1*(a+b))//b)
# print(a+b)
print(base1*2+base2)
else:
if b==a:
print(min(x//a,y//b))
continue
# print("YES")
# base1 = (y-x)//(b-a)
base1 = (y - x) // (b - a)
if x//a < base1:
print(x//a)
# print("YES")
continue
x -= base1 * a
y -= base1 * b
base2 = x // (a + b)
x -= base2 * (a+b)
y -= base2 * (a+b)
# print(base1,base2)
ans=(base1+base2*2)
if x >= a and y >= b:
ans += 1
print(ans)
```
| 6,051 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Tags: binary search, greedy, math, ternary search
Correct Solution:
```
# Author : nitish420 --------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
from math import ceil,floor
def main():
for _ in range(int(input())):
x,y,a,b=map(int,input().split())
if x>y:
x,y=y,x
if a>b:
a,b=b,a
if a==b:
print(x//a)
continue
l=0
r=y//a+1
while r-l>1:
# print(l,r)
n=(l+r)//2
mx,mm=min(floor((x-a*n)/(b-a)),n),max(0,ceil((y-b*n)/(a-b)))
# print(n,mx,mm)
if mx>=mm:
l=n
else:
r=n
print(l)
'''
1
10 12 2 5
'''
#----------------------------------------------------------------------------------------
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = 'x' in file.mode or 'r' not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b'\n') + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# endregion
if __name__ == '__main__':
main()
```
| 6,052 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Tags: binary search, greedy, math, ternary search
Correct Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
x, y, a, b = map(int, input().split())
if a<b:a,b=b,a
if x<y:x,y=y,x
take = min(x//a, y//b)
x -= a*take
y -= b*take
diff = a-b
# print(x, y, take)
if not take:print(0);continue
"""
transfer from y to x such that we can take a from x and y from b maximum times
"""
ans = take
if diff:
can_take = (x+y)//(a+b)
# print(can_take)
target_for_x = can_take*a
need_to_add = target_for_x-x
transfer = need_to_add//diff
x += transfer*diff
y -= transfer*diff
# print(x, y, x+y)
if need_to_add>=0:
if y>=0:
ans = max(ans, take+min(x//a, y//b))
if y-diff>=0:
ans = max(ans, take+min((x+diff)//a, (y-diff)//b))
print(ans)
```
| 6,053 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Tags: binary search, greedy, math, ternary search
Correct Solution:
```
def add_solve(x, y, a, b):
res = 0
alpha, beta = x // (a + b), y // (a + b)
res += 2 * min(alpha, beta)
x = x - min(alpha, beta) * (a + b)
y = y - min(alpha, beta) * (a + b)
if x < y:
x, y = y, x
if y < a:
return res
elif x < b:
return res
elif x >= b and y >= a:
return res + 1
def solve():
x, y, a, b = [int(i) for i in input().split()]
if a == b:
print(min(x // a, y // a))
return
if a > b:
a, b = b, a
if x > y:
x, y = y, x
# a < b, x <= y
if (y < b or x < a):
print(0)
return
res1 = (y - x) // (b - a)
res2 = y // b
res3 = x // a
result = 0
tmp = min(res1, res2, res3)
result += tmp
y -= b * tmp
x -= a * tmp
result += add_solve(x, y, a, b)
print(result)
return
for t in range(int(input())):
solve()
```
| 6,054 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Tags: binary search, greedy, math, ternary search
Correct Solution:
```
import sys
input = lambda : sys.stdin.readline().rstrip()
"""
say i make n of type 1 and m of type 2
type 1 = a of red, b of blue
type 2 = b of red, a of blue
n*a + m*b <= x
n*b + m*a <= y
n+m should be maximum
c1 c2
n :{0,min(x/a,y/b)}
"""
def f(n):
x1,y1=x - n*a,y-n*b
return n+min(x1/b,y1/a)
def ternary_search():
l,r = 0,int(min(x/a,y/b))
while (r - l > 3):
m1 = int(l + (r - l) / 3);
m2 = int(r - (r - l) / 3);
if (f(m1) < f(m2)):
l = m1;
else:
r = m2;
ans1 = 0
for i in range(l,r+1):
ans1 = max(ans1,int(f(i)))
return ans1;
for _ in range(int(input())):
x,y,a,b=map(int,input().split())
mx = min(x//a,y//b)
ans = mx
if a==b:
print(min(x,y)//a)
continue
print(ternary_search())
```
| 6,055 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Submitted Solution:
```
for _ in range(int(input())):
x,y,a,b=map(int,input().split())
x,y=min(x,y), max(x,y)
a,b=min(a,b), max(a,b)
# print(x,y,a,b)
if a==b:
print(x//a)
else:
c=b-a
z=y-x
ans=0
k=min(z//c, x//a)
ans+=k
x-=a*k
y-=b*k
# print(ans,x,y)
k=min(x,y)//(a+b)
ans+=2*k
x-=(a+b)*k
y-=(b+a)*k
# print(ans,x,y)
if(max(x,y)>=b and min(x,y)>=a):
ans+=1
print(ans)
```
Yes
| 6,056 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Submitted Solution:
```
for _ in range(int(input())):
x,y,a,b=map(int,input().split())
if(a==b):
print(min(x,y)//a)
continue
if(a<b):b,a=a,b
l,r,ans=0,(x+y)//(a+b),-1
while l<=r:
m=(l+r)//2
i=max(0,(a*m-y+a-b-1)//(a-b))
j=min(m,(x-b*m)//(a-b))
if(i<=j):l,ans=m+1,m
else: r=m-1
print(ans)
```
Yes
| 6,057 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Submitted Solution:
```
import sys,os,io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range (int(input())):
x,y,a,b = [int(i) for i in input().split()]
x,y = min(x,y), max(x,y)
a,b = min(a,b), max(a,b)
lo = 0
hi = 10**9
if b==a:
print(x//a)
continue
while(lo<=hi):
mid = (lo + hi)//2
if x < a*mid:
hi = mid-1
continue
maxi = (x - a*mid) // (b-a)
maxi = min(maxi, mid)
req = mid - (y - a*mid) // (b-a)
if maxi >= req:
ans = mid
lo = mid+1
else:
hi = mid-1
print(ans)
```
Yes
| 6,058 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Submitted Solution:
```
t = int(input())
for _ in range(t):
x, y, a, b = map(int, input().split())
ans = max(min(x//a, y//b), min(x//b, y//a))
d = 0
if a != b:
d = max(x * a - y * b, x * b - y * a)//abs(b * b - a * a)
for k in range(max(d-1, 0), d + 2):
A = x - k * a
B = y - k * b
if A >= 0 and B >= 0:
ans = max(ans, k + min(A//b, B//a))
for k in range(max(d-1, 0), d + 2):
A = y - k * a
B = x - k * b
if A >= 0 and B >= 0:
ans = max(ans, k + min(A//b, B//a))
print(ans)
```
Yes
| 6,059 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Submitted Solution:
```
def check(x,y,a,b,k):
if a==b:
return True
n=(x-a*k)//(b-a)
if b>a and n>=0:
m=max(0,k-n)
n=k-m
if m>=0 and n*a+m*b<=y:
return True
else:
n=(y-b*k)//(a-b)
if n>=0:
m=max(0,k-n)
n=k-m
if m>=0 and m*a+n*b<=x:
return True
return False
def check1(x,y,a,b,k):
if a==b:
return True
m=(x-b*k)//(a-b)
if a>b and m>=0:
n=max(0,k-m)
m=k-n
if n>=0 and n*a+m*b<=y:
return True
else:
m=(y-a*k)//(b-a)
if m>=0:
n=max(0,k-m)
m=k-n
if n>=0 and m*a+n*b<=x:
return True
return False
t=int(input())
t1=1
while t>0:
vals=list(map(int,input().split()))
x=vals[0]
y=vals[1]
a=vals[2]
b=vals[3]
if t1==1:
print(x,y,a,b,sep=(""))
p_sol=(x+y)//(a+b)
l=0
ans=0
while l<=p_sol:
k=(l+p_sol)//2
if check(x,y,a,b,k) or check1(x,y,a,b,k):
ans=k
l=k+1
else:
p_sol=k-1
print(ans)
t1+=1
t-=1
```
No
| 6,060 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Submitted Solution:
```
from collections import defaultdict, deque, Counter
from heapq import heapify, heappop, heappush
import math
from copy import deepcopy
from itertools import combinations, permutations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
import sys
def input():
return sys.stdin.readline().rstrip()
def getN():
return int(input())
def getNM():
return map(int, input().split())
def getList():
return list(map(int, input().split()))
def getArray(intn):
return [int(input()) for i in range(intn)]
mod = 10 ** 9 + 7
MOD = 998244353
INF = float('inf')
eps = 10 ** (-10)
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]
#############
# Main Code #
#############
def f(i):
j = min((x - a * i) // b, (y - b * i) // a)
if j < 0 or i < 0:
return -float('inf')
else:
return i + j
T = getN()
for _ in range(T):
x, y, a, b = getNM()
if x > y:
x, y = y, x
if a > b:
a, b = b, a
l = 0
r = 10 ** 18
while abs(r - l) > 1000:
mid = (l + r) // 2
# γΎγ ε’γγγ
if 0 <= f(mid) <= f(mid + 1):
l = mid
else:
r = mid
res = 0
for i in range(max(0, l), r):
res = max(res, f(i))
print(res)
```
No
| 6,061 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Submitted Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO,IOBase
import __pypy__
from types import GeneratorType
# from array import array
# 2D list [[0]*large_index for _ in range(small_index)]
# switch from integers to floats if all integers are β€ 2^52 and > 32 bit int
def bootstrap(f,stack=[]):
def wrappedfunc(*args,**kwargs):
if stack:
return f(*args,**kwargs)
else:
to = f(*args,**kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
int_add = __pypy__.intop.int_add
int_sub = __pypy__.intop.int_sub
int_mul = __pypy__.intop.int_mul
def make_mod_mul(mod=10**9+7):
fmod_inv = 1.0 / mod
def mod_mul(a, b, c=0):
res = int_sub(int_add(int_mul(a, b), c), int_mul(mod,int(fmod_inv * a * b + fmod_inv * c)))
if res >= mod:
return res - mod
elif res < 0:
return res + mod
else:
return res
return mod_mul
mod_mul = make_mod_mul()
def mod_pow(x,y):
if y == 0:
return 1
res = 1
while y > 1:
if y & 1 == 1:
res = mod_mul(res, x)
x = mod_mul(x, x)
y >>= 1
return mod_mul(res, x)
least_bit = lambda xx: xx & -xx
def main():
for _ in range(int(input())):
x,y,a,b = map(int,input().split())
if a < b:
a,b = b,a
if x < y:
x,y = y,x
ans = max(min(x//a,y//b),min(x//b,y//a))
if a == b:
print(ans)
continue
t = (b*x-a*y)//(b*b-a*a)
ans = max(ans,t+min((x-a*t)//b,(y-b*t)//a))
t += 1
if x >= a*t and y >= b*t:
ans = max(ans,t+min((x-a*t)//b,(y-b*t)//a))
print(ans)
#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == '__main__':
main()
```
No
| 6,062 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has x of red and y of blue candies. Using them, he wants to make gift sets. Each gift set contains either a red candies and b blue candies, or a blue candies and b red candies. Any candy can belong to at most one gift set.
Help Polycarp to find the largest number of gift sets he can create.
For example, if x = 10, y = 12, a = 5, and b = 2, then Polycarp can make three gift sets:
* In the first set there will be 5 red candies and 2 blue candies;
* In the second set there will be 5 blue candies and 2 red candies;
* In the third set will be 5 blue candies and 2 red candies.
Note that in this example there is one red candy that Polycarp does not use in any gift set.
Input
The first line contains an integer t (1 β€ t β€ 10^4). Then t test cases follow.
Each test case consists of a single string containing four integers x, y, a, and b (1 β€ x, y, a, b β€ 10^9).
Output
For each test case, output one number β the maximum number of gift sets that Polycarp can make.
Example
Input
9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3
Output
3
0
4
1000000000
1
1
1
5
0
Submitted Solution:
```
# J A I S H R E E R A M #
import math, sys, collections, functools, time, itertools;
# sys.setrecursionlimit(10**6)
def Read_Ints() : return map(int, input().strip().split())
def Read_Array() : return list(Read_Ints())
def Read_Strings() : return list(input().strip().split())
def printxsp(*args) : return print(*args, end="")
def printsp(*args) : return print(*args, end=" ")
d4i = [-1, +0, +1, +0]; d8i = [-1, -1, +0, +1, +1, +1, +0, -1];
d4j = [+0, +1, +0, -1]; d8j = [+0, +1, +1, +1, +0, -1, -1, -1];
# >>>>>>--->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def solveEachTest(_TestCase):
# printsp("Case #{}: ".format(_TestCase))
x, y, a, b = Read_Ints()
beta_01 = (x * b - y * a);
beta_02 = (b * b - a * a);
if (a == b) :
zeke = (min(x, y) // a);
print(zeke);
return;
beta = 0;
l = 0; r = min(x//b, y//a);
while (l <= r):
mid = (l+r) // 1;
if (mid * beta_02 <= beta_01):
beta = mid;
l = mid + 1;
else:
r = mid - 1;
alpha = (x - (beta * b)) // a;
alpha = min(alpha, (y - (beta * a)) // b);
print(alpha + beta);
_T0T4 = 1;
_T0T4 = int(input())
for _TestCase in range(1, _T0T4 + 1):
solveEachTest(_TestCase)
# Udit "luctivud" Gupta
# linkedin : https://www.linkedin.com/in/udit-gupta-1b7863135/
```
No
| 6,063 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Tags: binary search, implementation
Correct Solution:
```
def sum_series (v, k):
power = 0
res = 0
while v // (k ** power) > 1.0e-6:
res += v // (k ** power)
power += 1
return res
n, k = list(map(int, input().split()))
low = 1
high = n
while low <= high:
v = (low + high) // 2
if sum_series(v, k) >= n:
high = v - 1
else:
low = v + 1
print(low)
```
| 6,064 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Tags: binary search, implementation
Correct Solution:
```
n, k = (int(x) for x in input().split())
def is_possible(v, n):
i = 0
ans = 0
while int(v/(k**i)) != 0:
ans += int(v/(k**i))
i += 1
if ans >= n:
return True
return ans >= n
l = 0
r = n
while l < r:
med = int((l + r) / 2)
res = is_possible(med, n)
if not res:
l = med + 1
else:
r = med
if is_possible(l, n):
print(l)
else:
print(r)
```
| 6,065 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Tags: binary search, implementation
Correct Solution:
```
n,m=map(int,input().split())
l,r=0,n
mins=999999999999
while abs(l-r)!=1:
mid=(l+r)//2
cnt=0
mid_copy=mid
while mid>=1:
cnt+=mid
mid//=m
if n<=cnt:
mins=min(mins,mid_copy)
r=mid_copy
continue
if n==cnt:
print(min_copy)
break
if cnt>n:
r=mid_copy
else:
l=mid_copy
else:
if m>n:
print(n)
else:
mid=(l+r)//2
cnt=0
mid_copy=mid
while mid>=1:
cnt+=mid
mid//=m
if n<=cnt:
mins=min(mins,mid_copy)
r=mid_copy
if n==cnt:
print(mid_copy)
else:
print(mins)
```
| 6,066 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Tags: binary search, implementation
Correct Solution:
```
n, k = [int(i) for i in input().split()]
def f(x):
sum = x
i = 1
while k ** i <= x:
sum += int(x / k ** i)
i += 1
return sum
l = 0
r = 10 ** 30
while (r - l > 1):
m = (r + l) // 2
if f(m) >= n:
r = m
else:
l = m
print(r)
```
| 6,067 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Tags: binary search, implementation
Correct Solution:
```
import math
n,k = map(int,input().split())
l=1
r=n
while l!= r:
middle= math.floor(l+(r-l)/2)
tempAdder = middle
sum = 0
while tempAdder!= 0:
sum+=tempAdder
tempAdder=math.floor(tempAdder/k)
if sum>=n:
r=middle
else:
l=middle+1
print(l)
```
| 6,068 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Tags: binary search, implementation
Correct Solution:
```
n, k = map(int, input().split())
start, end = 1, n
while start != end:
org = mid = (start + end) >> 1
s = 0
while mid != 0:
s += mid
mid //= k
if s >= n:
end = org
else:
start = org + 1
print(start)
```
| 6,069 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Tags: binary search, implementation
Correct Solution:
```
def checker(v, k, n):
sum = 0
i = 0
lines = v
while sum < n and lines > 0:
sum += lines
i += 1
lines = int(v/(k**i))
return sum >= n
n, k = input().split(' ')
n = int(n)
k = int(k)
lb = int(n * (1 - (1/k)))
while not checker(lb, k, n):
lb += 1
print(lb)
```
| 6,070 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Tags: binary search, implementation
Correct Solution:
```
import sys
def get_lines(v, k):
lines = 0
q = v
while q != 0:
lines += q
q = q // k
return int(lines)
def get_min_v(n, k):
val = n if n % 2 == 0 else n + 1
curr_lines = get_lines(val, k)
# print("before while loop")
# print("val is ", val)
# print("curr lines is ", curr_lines)
while curr_lines >= n:
val = val / 2
curr_lines = get_lines(val, k)
# print("new val is ", val)
# print("new curr_lines is ", curr_lines)
# return int(val * 2)
low = int(val)
high = n
# print("low is ", low)
# print("high is ", high)
while low < high:
# print("low is ", low)
# print("high is ", high)
if high - low == 1:
return int(high)
mid = int(low + (high - low) / 2)
# print("mid is ", mid)
# print("lines is ", get_lines(mid, k))
if get_lines(mid, k) < n:
low = mid
else:
high = mid
f = sys.stdin
n, k = [int(x) for x in f.readline().strip().split(" ")]
print(get_min_v(n, k))
```
| 6,071 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Submitted Solution:
```
def check(start, k, n):
pages = 0
while(start > 0):
pages += start
start = int(start / k)
return pages >= n
p = list(map(int, input().split(" ")))
n = p[0]
k = p[1]
low = 0
high = n + 1
while(low + 1 < high):
mid = int((low + high) / 2)
if(check(mid, k, n)):
high = mid
else:
low = mid
print(low + 1)
```
Yes
| 6,072 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Submitted Solution:
```
import sys
n,k=map(int,input().split())
if n <= k :
print(n)
else:
start = k ; end = n
while end > start :
mid = (start+end)//2
y=1 ; ans = mid
while mid >= (k**y):
ans += mid//(k**y)
y+=1
if ans == n :
print(mid)
sys.exit(0)
elif ans > n :
rr = mid
end = mid
else:
start = mid+1
print(rr)
```
Yes
| 6,073 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Submitted Solution:
```
from math import log, floor
def binarysearch(n, k, p):
left = 0
right = n+1
while left < right:
middle = (left + right) // 2
if sum([middle//k**i for i in range(p+1)]) < n:
left = middle + 1
else:
right = middle
return left
n, k = [int(i) for i in input().split()]
p = floor(log(n, k))
y = binarysearch(n, k, p)
print(y)
```
Yes
| 6,074 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Submitted Solution:
```
l = input().split(' ')
n, k = int(l[0]), int(l[1])
left, right = 1, n
while left != right:
x = middle = (left + right) // 2
sum = 0
while x != 0:
sum += x
x //= k
if sum >= n:
right = middle
else:
left = middle + 1
print(left)
```
Yes
| 6,075 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Submitted Solution:
```
n, k = map(int, input().split())
l, r = 1, int(10e6 + 1)
m = (l + r) // 2
sm = 0
it = 1000
while l < r - 1 and it > 0:
it -= 1
f = 0
m = (l + r) // 2
sm = 0
i = 0
v = int(m / pow(k, i))
while v:
sm += v
i += 1
v = int(m / pow(k, i))
if sm >= n:
r = m
else:
l = m
print(m)
```
No
| 6,076 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Submitted Solution:
```
n,m=map(int,input().split())
l,r=0,n
mins=999999999999
while abs(l-r)!=1:
mid=(l+r)//2
cnt=0
mid_copy=mid
while mid>=1:
cnt+=mid
mid//=m
if n<=cnt:
mins=min(mins,mid_copy)
r=mid_copy
continue
if n==cnt:
print(min_copy)
break
if cnt>n:
r=mid_copy
else:
l=mid_copy
else:
if n==1:
print(1)
else:
mid=(l+r)//2
cnt=0
mid_copy=mid
while mid>=1:
cnt+=mid
mid//=m
if n<=cnt:
mins=min(mins,mid_copy)
r=mid_copy
if n==cnt:
print(mid_copy)
else:
print(mins)
```
No
| 6,077 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Submitted Solution:
```
import math
def check(n,k,maxi,v):
best=v
v=v+1
while(True):
sumi=0
for i in range(maxi):
sumi+= v/ k**i
if(int(sumi)>=n):
return v
def proB(n,k):
maxi=0
while(k**maxi<=n):
maxi+=1
sumi=0
for i in range(maxi):
sumi+= 1/ k**i
ans=n/sumi
if(int(ans)*sumi==n):
return int(ans)
return check(n,k,maxi,int(ans))
arr1=list(map(int,input().split()))
n,m=arr1[0],arr1[1]
print(proB(n,m))
```
No
| 6,078 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as <image> lines, drinks another cup of tea, then he writes <image> lines and so on: <image>, <image>, <image>, ...
The expression <image> is regarded as the integral part from dividing number a by number b.
The moment the current value <image> equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
Input
The input consists of two integers n and k, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1 β€ n β€ 109, 2 β€ k β€ 10.
Output
Print the only integer β the minimum value of v that lets Vasya write the program in one night.
Examples
Input
7 2
Output
4
Input
59 9
Output
54
Note
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
Submitted Solution:
```
from sys import stdin
def works(n, v, k):
d = 1
t = 0
while v >= d and t < n:
t += v // d
d *= k
return t >= n
line = stdin.readline().strip().split(" ")
n = int(line[0])
k = int(line[1])
lo = 1
hi = 10 ** 9
while lo + 1 < hi:
mid = (lo + hi) // 2
if works(n, mid, k):
hi = mid
else:
lo = mid
print(hi)
```
No
| 6,079 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Tags: math
Correct Solution:
```
def gh(a,b):
t=1
while b>0:
if b&1:t=(t*a)%1000000007
b=b>>1
a=(a*a)%1000000007
return t
a=int(input())
if a==0:print(1);exit()
print((((gh(2,2*a-1)+gh(2,a-1))%1000000007)))
```
| 6,080 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Tags: math
Correct Solution:
```
def modularExponentiation(x,n,M):
res=1
while n>0:
if n%2==1:
res=(res*x)%M
x=(x*x)%M
n=n//2
return res
n=int(input())
s=modularExponentiation(2,n,10**9+7)
print(s*(s+1)//2%(10**9+7))
```
| 6,081 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Tags: math
Correct Solution:
```
MOD = 10**9 + 7
def mat_mul(a, b):
result = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
tmp = 0
for k in range(2):
tmp = (tmp + a[i][k] * b[k][j]) % MOD
result[i][j] = tmp
return result
def mat_pow(mat, exp):
if exp == 0:
return [[1, 0], [0, 1]]
elif exp % 2 == 0:
tmp = mat_pow(mat, exp // 2)
return mat_mul(tmp, tmp)
else:
return mat_mul(mat, mat_pow(mat, exp - 1))
n = int(input())
transform = mat_pow([[3, 1], [1, 3]], n)
print(transform[0][0])
```
| 6,082 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Tags: math
Correct Solution:
```
sa=int(input())
mod=10**9+7
if sa==0:
print(1)
else:
print((pow(2, 2*sa-1, mod)+pow(2, sa-1, mod))%mod)
```
| 6,083 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Tags: math
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
from collections import Counter
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
mod=int(1e9)+7
def lcm(a, b):
return (a * b) / gcd(a, b)
def power(k, n):
if n == 0:
return 1
if n % 2:
return (power(k, n - 1) * k) % mod
t = power(k, n // 2)
return (t * t) % mod
def main():
# for n in range(1, 385599125):
# mod=int(1e9)+7
# #n=int(input())
# dp=[[0,0] for i in range(n+1)]
# dp[0][0]=1 # up facing
# dp[0][1]=0 # down facing
# for i in range(1, n+1):
# dp[i][0]=(3*dp[i-1][0]+dp[i-1][1])%mod
# dp[i][1]=(dp[i-1][0]+3*dp[i-1][1])%mod
# print((dp[n][0]+dp[n][1]),(dp[n][0])%mod)
#
n = int(input())
if n==0:
print(1)
return
m = power(4, n-1)
#print(m / 2)
ans=(2*m + power(2, n - 1)) % mod
print(int(ans))
return
#
# # ans= (4^n)/2+(4^n-1)
if __name__ == "__main__":
main()
```
| 6,084 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Tags: math
Correct Solution:
```
n= int(input())
m= 10**9 + 7
if n==0:
print(1)
else:
print((pow(2,2*n-1,m)+pow(2,n-1,m))%m)
```
| 6,085 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Tags: math
Correct Solution:
```
N = int(input())
result = (pow(2, N, int(1e9 + 7)) * (pow(2, N, int(1e9 + 7)) + 1)) % int(1e9+7)
result = (result * pow(2, int(1e9+5), int(1e9+7))) % int(1e9+7)
print(result)
```
| 6,086 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Tags: math
Correct Solution:
```
n=int(input())
e=10**9+7
print(1if n==0else(pow(2,n-1,e)+pow(2,n*2-1,e))%e)
# Made By Mostafa_Khaled
```
| 6,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Submitted Solution:
```
n=int(input())
m=pow(10,9)+7
if n==0:
print(1)
else:
print((pow(2,2*n-1,m)+pow(2,n-1,m))%m)
```
Yes
| 6,088 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Submitted Solution:
```
def power(a, b, mod, c):
if b == 0:
return c
if b % 2 == 1:
return power(a, b - 1, mod, c) * a % mod
else:
return power(a, b // 2, mod, c) ** 2 % mod
return c
n = int(input())
n = power(2, n, 1000000007, 1)
print((n * (n + 1)) // 2 % 1000000007)
```
Yes
| 6,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
from collections import Counter
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
mod=int(1e9)+7
def lcm(a, b):
return (a * b) / gcd(a, b)
def power(k, n):
if n == 0:
return 1
if n % 2:
return (power(k, n - 1) * k) % mod
t = power(k, n // 2)
return (t * t) % mod
def main():
# for n in range(1, 10):
# mod=int(1e9)+7
# #n=int(input())
# dp=[[0,0] for i in range(n+1)]
# dp[0][0]=1 # up facing
# dp[0][1]=0 # down facing
# for i in range(1, n+1):
# dp[i][0]=(3*dp[i-1][0]+dp[i-1][1])%mod
# dp[i][1]=(dp[i-1][0]+3*dp[i-1][1])%mod
# print((dp[n][0]+dp[n][1]),(dp[n][0])%mod)
#
n = int(input())
m = power(4, n)
#print(m / 2)
ans=(m / 2 + power(2, n - 1)) % mod
print(int(ans))
return
# ans= (4^n)/2+(4^n-1)
return
if __name__ == "__main__":
main()
```
No
| 6,090 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
from collections import Counter
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
mod=int(1e9)+7
def lcm(a, b):
return (a * b) / gcd(a, b)
def power(k, n):
if n == 0:
return 1
if n % 2:
return (power(k, n - 1) * k) % mod
t = power(k, n // 2)
return (t * t) % mod
def main():
# for n in range(1, 10):
# mod=int(1e9)+7
# #n=int(input())
# dp=[[0,0] for i in range(n+1)]
# dp[0][0]=1 # up facing
# dp[0][1]=0 # down facing
# for i in range(1, n+1):
# dp[i][0]=(3*dp[i-1][0]+dp[i-1][1])%mod
# dp[i][1]=(dp[i-1][0]+3*dp[i-1][1])%mod
# print((dp[n][0]+dp[n][1]),(dp[n][0])%mod)
#
n = int(input())
m = power(4, n)
#print(m / 2)
print((m / 2 + power(2, n - 1)) % mod)
return
# ans= (4^n)/2+(4^n-1)
return
if __name__ == "__main__":
main()
```
No
| 6,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Submitted Solution:
```
N = int(input())
print(int(pow(2, N, 1000000007) * (pow(2, N, 1000000007) + 1) / 2 % 1000000007))
```
No
| 6,092 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
<image>
Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.
Input
The first line contains a single integer n (0 β€ n β€ 1018) β the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
Submitted Solution:
```
sa=int(input())
mod=10**9+7
print(pow(2, 2*sa-1, mod)+pow(2, sa-1, mod))
```
No
| 6,093 |
Provide a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem:
You've got some training set of documents. For each document you know its subject. The subject in this problem is an integer from 1 to 3. Each of these numbers has a physical meaning. For instance, all documents with subject 3 are about trade.
You can download the training set of documents at the following link: http://download4.abbyy.com/a2/X2RZ2ZWXBG5VYWAL61H76ZQM/train.zip. The archive contains three directories with names "1", "2", "3". Directory named "1" contains documents on the 1-st subject, directory "2" contains documents on the 2-nd subject, and directory "3" contains documents on the 3-rd subject. Each document corresponds to exactly one file from some directory.
All documents have the following format: the first line contains the document identifier, the second line contains the name of the document, all subsequent lines contain the text of the document. The document identifier is used to make installing the problem more convenient and has no useful information for the participants.
You need to write a program that should indicate the subject for a given document. It is guaranteed that all documents given as input to your program correspond to one of the three subjects of the training set.
Input
The first line contains integer id (0 β€ id β€ 106) β the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes.
The tests for this problem are divided into 10 groups. Documents of groups 1 and 2 are taken from the training set, but their identifiers will not match the identifiers specified in the training set. Groups from the 3-rd to the 10-th are roughly sorted by the author in ascending order of difficulty (these groups contain documents which aren't present in the training set).
Output
Print an integer from 1 to 3, inclusive β the number of the subject the given document corresponds to.
Examples
"Correct Solution:
```
print('2')
```
| 6,094 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the Ox axis, equals w, the length of the side that is parallel to the Oy axis, equals h. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle Ξ±.
<image>
Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.
Input
The first line contains three integers w, h, Ξ± (1 β€ w, h β€ 106; 0 β€ Ξ± β€ 180). Angle Ξ± is given in degrees.
Output
In a single line print a real number β the area of the region which belongs to both given rectangles.
The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
Input
1 1 45
Output
0.828427125
Input
6 4 30
Output
19.668384925
Note
The second sample has been drawn on the picture above.
Tags: geometry
Correct Solution:
```
#!/usr/bin/python3
from math import sin, cos, pi, atan2
w, h, a = tuple(map(int, input().split()))
if a in [0, 180]:
print(w * h)
elif a == 90:
print(min(w, h)**2)
else:
a *= pi / 180
w /= 2
h /= 2
base = [(w, h), (-w, h), (-w, -h), (w, -h)]
rot = [(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a)) for x, y in base]
pol = []
for i, j in zip(range(-1, 3), range(4)):
t = (w - rot[i][0]) / (rot[j][0] - rot[i][0])
if 0 <= t <= 1:
pol.append((w, rot[i][1] + (rot[j][1] - rot[i][1]) * t))
t = (-w - rot[i][0]) / (rot[j][0] - rot[i][0])
if 0 <= t <= 1:
pol.append((-w, rot[i][1] + (rot[j][1] - rot[i][1]) * t))
t = (h - rot[i][1]) / (rot[j][1] - rot[i][1])
if 0 <= t <= 1:
pol.append((rot[i][0] + (rot[j][0] - rot[i][0]) * t, h))
t = (-h - rot[i][1]) / (rot[j][1] - rot[i][1])
if 0 <= t <= 1:
pol.append((rot[i][0] + (rot[j][0] - rot[i][0]) * t, -h))
pol.sort(key=lambda x: atan2(*x))
print('%0.9f' % (sum([abs(pol[i][0] * pol[j][1] - pol[i][1] * pol[j][0]) for i, j in zip(range(-1, len(pol) - 1), range(len(pol)))]) / 2))
```
| 6,095 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the Ox axis, equals w, the length of the side that is parallel to the Oy axis, equals h. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle Ξ±.
<image>
Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.
Input
The first line contains three integers w, h, Ξ± (1 β€ w, h β€ 106; 0 β€ Ξ± β€ 180). Angle Ξ± is given in degrees.
Output
In a single line print a real number β the area of the region which belongs to both given rectangles.
The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
Input
1 1 45
Output
0.828427125
Input
6 4 30
Output
19.668384925
Note
The second sample has been drawn on the picture above.
Tags: geometry
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
import math
def main():
pass
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def binary(n):
return (bin(n).replace("0b", ""))
def decimal(s):
return (int(s, 2))
def pow2(n):
p = 0
while (n > 1):
n //= 2
p += 1
return (p)
def primeFactors(n):
l=[]
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
l.append(i)
n = n / i
if n > 2:
l.append(int(n))
return (l)
def isPrime(n):
if (n == 1):
return (False)
else:
root = int(n ** 0.5)
root += 1
for i in range(2, root):
if (n % i == 0):
return (False)
return (True)
def maxPrimeFactors(n):
maxPrime = -1
while n % 2 == 0:
maxPrime = 2
n >>= 1
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
maxPrime = i
n = n / i
if n > 2:
maxPrime = n
return int(maxPrime)
w,h,a=map(int,input().split())
if(a==0 or a==180):
print(w*h)
else:
w,h=max(w,h),min(w,h)
if(a>90):
a=180-a
a=(a*math.pi)/180
#print(h*(1+math.cos(a)),w*math.sin(a))
if(h*(1+math.cos(a))< w*math.sin(a)):
print(pow(h,2)/math.sin(a))
else:
#print("HEY")
#print(math.sin(a))
x=h-w*((math.cos(a)+1)/math.sin(a))
t=math.tan(a)-((math.cos(a)+1)*(math.cos(a)+1)/(math.cos(a)*math.sin(a)))
x=x/t
x=abs(x)
b2=w-x-(x/math.cos(a))
b2=abs(b2)
h2=b2/math.tan(a)
h2=abs(h2)
h1=h-h2-(h2/math.cos(a))
h1=abs(h1)
tbm=((x*h1)+(b2*h2))
#print(x,h1,h2,b2)
print((h*w)-tbm)
```
| 6,096 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the Ox axis, equals w, the length of the side that is parallel to the Oy axis, equals h. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle Ξ±.
<image>
Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.
Input
The first line contains three integers w, h, Ξ± (1 β€ w, h β€ 106; 0 β€ Ξ± β€ 180). Angle Ξ± is given in degrees.
Output
In a single line print a real number β the area of the region which belongs to both given rectangles.
The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
Input
1 1 45
Output
0.828427125
Input
6 4 30
Output
19.668384925
Note
The second sample has been drawn on the picture above.
Tags: geometry
Correct Solution:
```
from math import sin, cos, tan, atan, pi
def main():
w, h, a = map(int, input().split())
a = min(a, 180 - a) * pi / 180
if h > w:
h, w = w, h
if h * (1 + cos(a)) < w * sin(a):
res = h * h / sin(a)
else:
res = h * w - ((w - h * tan(a / 2)) ** 2 * tan(a) + (h - w * tan(a / 2)) ** 2 * tan(a)) / 4
print('{:.9f}'.format(res))
if __name__ == '__main__':
main()
# Made By Mostafa_Khaled
```
| 6,097 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the Ox axis, equals w, the length of the side that is parallel to the Oy axis, equals h. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle Ξ±.
<image>
Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.
Input
The first line contains three integers w, h, Ξ± (1 β€ w, h β€ 106; 0 β€ Ξ± β€ 180). Angle Ξ± is given in degrees.
Output
In a single line print a real number β the area of the region which belongs to both given rectangles.
The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
Input
1 1 45
Output
0.828427125
Input
6 4 30
Output
19.668384925
Note
The second sample has been drawn on the picture above.
Tags: geometry
Correct Solution:
```
import math
def rot_by_a(p, a):
return (p[0] * math.cos(a) - p[1] * math.sin(a),
p[0] * math.sin(a) + p[1] * math.cos(a))
h, w, alpha = map(int, input().split())
if h > w:
h, w = w, h
if alpha > 90:
alpha = 180 - alpha
ang = math.radians(alpha)
b = 2 * math.atan(h / w) # why not * 2 ?
#print(math.degrees(b))
if alpha == 0:
# print("one")
print(h * w)
elif alpha == 90:
# print("two")
print(h * h)
elif ang > b:
# print("three")
side = h / math.sin(ang)
print(side * h)
else:
# print("four")
left_top = (w / 2, h / 2)
rot_left_top = rot_by_a(left_top, ang)
tri_h = rot_left_top[1] - left_top[1]
tri_base = tri_h / math.tan(ang) + tri_h / math.tan(math.pi / 2 - ang)
longer_cathetus = tri_base * math.cos(ang)
smaller_tri_smaller_cathetus = w - tri_base - longer_cathetus
smaller_tri_longer_cathetus = smaller_tri_smaller_cathetus / math.tan(ang)
print(w * h - tri_base * tri_h - smaller_tri_smaller_cathetus * smaller_tri_longer_cathetus)
```
| 6,098 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the Ox axis, equals w, the length of the side that is parallel to the Oy axis, equals h. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle Ξ±.
<image>
Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.
Input
The first line contains three integers w, h, Ξ± (1 β€ w, h β€ 106; 0 β€ Ξ± β€ 180). Angle Ξ± is given in degrees.
Output
In a single line print a real number β the area of the region which belongs to both given rectangles.
The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
Input
1 1 45
Output
0.828427125
Input
6 4 30
Output
19.668384925
Note
The second sample has been drawn on the picture above.
Submitted Solution:
```
import math
def rot_by_a(p, a):
return (p[0] * math.cos(a) - p[1] * math.sin(a),
p[0] * math.sin(a) + p[1] * math.cos(a))
h, w, alpha = map(int, input().split())
if h > w:
h, w = w, h
if alpha > 90:
alpha = 180 - alpha
ang = math.radians(alpha)
b = math.tan(h / w) # why not * 2 ?
if alpha == 0:
print(h * w)
#elif alpha == 90:
# print(h * h)
elif ang > b:
side = h / math.sin(ang)
print(side * h)
else:
left_top = (w / 2, h / 2)
rot_left_top = rot_by_a(left_top, ang)
tri_h = rot_left_top[1] - left_top[1]
tri_base = tri_h * math.tan(ang) + tri_h / math.tan(math.pi / 2 - ang)
longer_cathetus = tri_base * math.cos(ang)
smaller_tri_smaller_cathetus = w - tri_base - longer_cathetus
smaller_tri_longer_cathetus = smaller_tri_smaller_cathetus / math.tan(ang)
print(w * h - tri_base * tri_h - smaller_tri_smaller_cathetus * smaller_tri_longer_cathetus)
```
No
| 6,099 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.