message stringlengths 2 43.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 853 107k | cluster float64 24 24 | __index_level_0__ int64 1.71k 214k |
|---|---|---|---|---|---|
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 | instruction | 0 | 6,048 | 24 | 12,096 |
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))
``` | output | 1 | 6,048 | 24 | 12,097 |
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 | instruction | 0 | 6,049 | 24 | 12,098 |
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)
``` | output | 1 | 6,049 | 24 | 12,099 |
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 | instruction | 0 | 6,050 | 24 | 12,100 |
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()
``` | output | 1 | 6,050 | 24 | 12,101 |
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 | instruction | 0 | 6,051 | 24 | 12,102 |
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)
``` | output | 1 | 6,051 | 24 | 12,103 |
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 | instruction | 0 | 6,052 | 24 | 12,104 |
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()
``` | output | 1 | 6,052 | 24 | 12,105 |
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 | instruction | 0 | 6,053 | 24 | 12,106 |
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)
``` | output | 1 | 6,053 | 24 | 12,107 |
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 | instruction | 0 | 6,054 | 24 | 12,108 |
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()
``` | output | 1 | 6,054 | 24 | 12,109 |
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 | instruction | 0 | 6,055 | 24 | 12,110 |
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())
``` | output | 1 | 6,055 | 24 | 12,111 |
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)
``` | instruction | 0 | 6,056 | 24 | 12,112 |
Yes | output | 1 | 6,056 | 24 | 12,113 |
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)
``` | instruction | 0 | 6,057 | 24 | 12,114 |
Yes | output | 1 | 6,057 | 24 | 12,115 |
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)
``` | instruction | 0 | 6,058 | 24 | 12,116 |
Yes | output | 1 | 6,058 | 24 | 12,117 |
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)
``` | instruction | 0 | 6,059 | 24 | 12,118 |
Yes | output | 1 | 6,059 | 24 | 12,119 |
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
``` | instruction | 0 | 6,060 | 24 | 12,120 |
No | output | 1 | 6,060 | 24 | 12,121 |
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)
``` | instruction | 0 | 6,061 | 24 | 12,122 |
No | output | 1 | 6,061 | 24 | 12,123 |
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()
``` | instruction | 0 | 6,062 | 24 | 12,124 |
No | output | 1 | 6,062 | 24 | 12,125 |
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/
``` | instruction | 0 | 6,063 | 24 | 12,126 |
No | output | 1 | 6,063 | 24 | 12,127 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n Γ n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals:
* the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i β j;
* -1, if i = j.
Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.
Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 β€ i β€ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 β€ i, j β€ n; i β j) the following condition fulfills: 0 β€ bij β€ 109, bij = bji.
Output
Print n non-negative integers a1, a2, ..., an (0 β€ ai β€ 109) β the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
Examples
Input
1
-1
Output
0
Input
3
-1 18 0
18 -1 0
0 0 -1
Output
18 18 0
Input
4
-1 128 128 128
128 -1 148 160
128 148 -1 128
128 160 128 -1
Output
128 180 148 160
Note
If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. | instruction | 0 | 6,926 | 24 | 13,852 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
if (n == 1) :
print(0)
else :
m = [[0] * n] * n
a = [int(0)] * n
for i in range(0, n) :
m[i] = input().split()
a[i] = int(m[i][(i + 1) % n])
for j in range(0, n) :
if (j != i) :
a[i] = a[i] | int(m[i][j])
for i in range(0, n) :
print(a[i], end = ' ')
``` | output | 1 | 6,926 | 24 | 13,853 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n Γ n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals:
* the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i β j;
* -1, if i = j.
Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.
Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 β€ i β€ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 β€ i, j β€ n; i β j) the following condition fulfills: 0 β€ bij β€ 109, bij = bji.
Output
Print n non-negative integers a1, a2, ..., an (0 β€ ai β€ 109) β the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
Examples
Input
1
-1
Output
0
Input
3
-1 18 0
18 -1 0
0 0 -1
Output
18 18 0
Input
4
-1 128 128 128
128 -1 148 160
128 148 -1 128
128 160 128 -1
Output
128 180 148 160
Note
If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. | instruction | 0 | 6,927 | 24 | 13,854 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
a = [0]*n
for i in range(n):
for x in map(int, input().split()):
if x!=-1:
a[i]|=x
print(*a)
``` | output | 1 | 6,927 | 24 | 13,855 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n Γ n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals:
* the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i β j;
* -1, if i = j.
Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.
Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 β€ i β€ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 β€ i, j β€ n; i β j) the following condition fulfills: 0 β€ bij β€ 109, bij = bji.
Output
Print n non-negative integers a1, a2, ..., an (0 β€ ai β€ 109) β the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
Examples
Input
1
-1
Output
0
Input
3
-1 18 0
18 -1 0
0 0 -1
Output
18 18 0
Input
4
-1 128 128 128
128 -1 148 160
128 148 -1 128
128 160 128 -1
Output
128 180 148 160
Note
If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. | instruction | 0 | 6,928 | 24 | 13,856 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n=int(input())
A=[0]*n
ans=[0]*n
for i in range(n):
A[i]=list(map(int,input().split()))
for j in range(n):
if(j==i):continue
ans[i]|=A[i][j]
for i in range(n):
print(ans[i],' ',end='')
``` | output | 1 | 6,928 | 24 | 13,857 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n Γ n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals:
* the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i β j;
* -1, if i = j.
Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.
Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 β€ i β€ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 β€ i, j β€ n; i β j) the following condition fulfills: 0 β€ bij β€ 109, bij = bji.
Output
Print n non-negative integers a1, a2, ..., an (0 β€ ai β€ 109) β the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
Examples
Input
1
-1
Output
0
Input
3
-1 18 0
18 -1 0
0 0 -1
Output
18 18 0
Input
4
-1 128 128 128
128 -1 148 160
128 148 -1 128
128 160 128 -1
Output
128 180 148 160
Note
If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. | instruction | 0 | 6,929 | 24 | 13,858 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
if n > 1:
p = [0] * n
r = format(n - 1, 'b')[:: -1]
l = len(r) - 1
for i in range(n):
t = list(map(int, input().split()))
t.pop(i)
s = 0
for j in range(l):
if r[j] == '1': s |= t.pop()
t = [t[k] | t[k + 1] for k in range(0, len(t), 2)]
p[i] = s | t[0]
print(' '.join(map(str, p)))
else: print(0)
``` | output | 1 | 6,929 | 24 | 13,859 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n Γ n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals:
* the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i β j;
* -1, if i = j.
Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.
Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 β€ i β€ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 β€ i, j β€ n; i β j) the following condition fulfills: 0 β€ bij β€ 109, bij = bji.
Output
Print n non-negative integers a1, a2, ..., an (0 β€ ai β€ 109) β the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
Examples
Input
1
-1
Output
0
Input
3
-1 18 0
18 -1 0
0 0 -1
Output
18 18 0
Input
4
-1 128 128 128
128 -1 148 160
128 148 -1 128
128 160 128 -1
Output
128 180 148 160
Note
If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. | instruction | 0 | 6,930 | 24 | 13,860 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n=int(input())
a=[]
for i in range(n):
a.append(list(map(int,input().split())))
ans = [0]*n
for i in range(n):
for j in range(n):
if j!=i:
ans[i] |= a[i][j]
print(ans[i],end = ' ')
``` | output | 1 | 6,930 | 24 | 13,861 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n Γ n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals:
* the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i β j;
* -1, if i = j.
Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.
Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 β€ i β€ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 β€ i, j β€ n; i β j) the following condition fulfills: 0 β€ bij β€ 109, bij = bji.
Output
Print n non-negative integers a1, a2, ..., an (0 β€ ai β€ 109) β the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
Examples
Input
1
-1
Output
0
Input
3
-1 18 0
18 -1 0
0 0 -1
Output
18 18 0
Input
4
-1 128 128 128
128 -1 148 160
128 148 -1 128
128 160 128 -1
Output
128 180 148 160
Note
If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. | instruction | 0 | 6,931 | 24 | 13,862 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
p = [0] * n
for i in range(n):
t = list(map(int, input().split()))
t.pop(i)
s = 0
for j in t:
s |= j
p[i] = s
print(' '.join(map(str, p)))
``` | output | 1 | 6,931 | 24 | 13,863 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n Γ n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals:
* the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i β j;
* -1, if i = j.
Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.
Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 β€ i β€ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 β€ i, j β€ n; i β j) the following condition fulfills: 0 β€ bij β€ 109, bij = bji.
Output
Print n non-negative integers a1, a2, ..., an (0 β€ ai β€ 109) β the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
Examples
Input
1
-1
Output
0
Input
3
-1 18 0
18 -1 0
0 0 -1
Output
18 18 0
Input
4
-1 128 128 128
128 -1 148 160
128 148 -1 128
128 160 128 -1
Output
128 180 148 160
Note
If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
Submitted Solution:
```
n = int(input())
if (n == 1) :
print(0)
else :
m = [[0] * n] * n
a = [int(0)] * n
for i in range(0, n) :
m[i] = input().split()
a[i] = int(m[i][(i + 1) % n])
for j in range(0, n) :
if (j != i) :
a[i] = a[i] | int(m[i][j])
print(a)
``` | instruction | 0 | 6,932 | 24 | 13,864 |
No | output | 1 | 6,932 | 24 | 13,865 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n Γ n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals:
* the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i β j;
* -1, if i = j.
Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.
Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 β€ i β€ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 β€ i, j β€ n; i β j) the following condition fulfills: 0 β€ bij β€ 109, bij = bji.
Output
Print n non-negative integers a1, a2, ..., an (0 β€ ai β€ 109) β the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
Examples
Input
1
-1
Output
0
Input
3
-1 18 0
18 -1 0
0 0 -1
Output
18 18 0
Input
4
-1 128 128 128
128 -1 148 160
128 148 -1 128
128 160 128 -1
Output
128 180 148 160
Note
If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
Submitted Solution:
```
n = int(input())
m = [[0] * n] * n
a = [int(0)] * n
for i in range(0, n) :
m[i] = input().split()
a[i] = int(m[i][(i + 1) % n])
for j in range(0, n) :
if (j != i) :
a[i] = a[i] | int(m[i][j])
print(a)
``` | instruction | 0 | 6,933 | 24 | 13,866 |
No | output | 1 | 6,933 | 24 | 13,867 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. | instruction | 0 | 7,533 | 24 | 15,066 |
Tags: dp, greedy, number theory
Correct Solution:
```
s = input()
n = len(s)
fin = [-1]*3
fin[0] = 0
r = 0
z = [0]*(n + 1)
for i in range(1, n + 1):
r = (r + int(s[i - 1])) % 3
z[i] = z[i - 1]
if fin[r] != -1:
z[i] = max(z[i], z[fin[r]] + 1)
fin[r] = i
print(z[n])
``` | output | 1 | 7,533 | 24 | 15,067 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. | instruction | 0 | 7,534 | 24 | 15,068 |
Tags: dp, greedy, number theory
Correct Solution:
```
n=input()
ls=''
t=0
for i in range(len(n)):
if int(n[i])%3==0:
ls=''
t+=1
else:
ls+=n[i]
for j in range(0,len(ls)):
if int(ls[j:])%3==0:
t+=1
ls=''
break
print(t)
'''
//////////////// ////// /////// // /////// // // //
//// // /// /// /// /// // /// /// //// //
//// //// /// /// /// /// // ///////// //// ///////
//// ///// /// /// /// /// // /// /// //// // //
////////////// /////////// /////////// ////// /// /// // // // //
'''
``` | output | 1 | 7,534 | 24 | 15,069 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. | instruction | 0 | 7,535 | 24 | 15,070 |
Tags: dp, greedy, number theory
Correct Solution:
```
MOD = 1000000007
MOD2 = 998244353
ii = lambda: int(input())
si = lambda: input()
dgl = lambda: list(map(int, input()))
f = lambda: map(int, input().split())
il = lambda: list(map(int, input().split()))
ls = lambda: list(input())
let = '@abcdefghijklmnopqrstuvwxyz'
s=si()
n=len(s)
l=[0]*n
rem=[-1]*3
rem[0],x=0,0
for i in range(n):
x=(x+int(s[i]))%3
if rem[x]!=-1:
l[i]=max(l[i-1],l[rem[x]]+1)
else:
l[i]=l[i-1]
rem[x]=i
print(l[n-1])
``` | output | 1 | 7,535 | 24 | 15,071 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. | instruction | 0 | 7,536 | 24 | 15,072 |
Tags: dp, greedy, number theory
Correct Solution:
```
s = input()
n = len(s)
ans = 0
c = 0
l = []
for i in range(n):
a = int(s[i])%3
if a==0:
ans+=1
c = 0
l = []
else:
if c==0:
l.append(int(s[i]))
c+=1
elif c==1:
if (a+l[0])%3==0:
ans+=1
c = 0
l = []
else:
c+=1
else:
ans+=1
c=0
l = []
print(ans)
``` | output | 1 | 7,536 | 24 | 15,073 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. | instruction | 0 | 7,537 | 24 | 15,074 |
Tags: dp, greedy, number theory
Correct Solution:
```
li=[int(x)%3 for x in input()]
start=0
end=0
ans=0
while end<len(li):
if li[end]==0:
ans+=1
end+=1
start=end
else:
count=1
add=li[end]
while end-count>=start:
add+=li[end-count]
if add%3==0:
ans+=1
# print(str(start)+' '+str(end))
start=end+1
break
else:
count+=1
end+=1
print(ans)
``` | output | 1 | 7,537 | 24 | 15,075 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. | instruction | 0 | 7,538 | 24 | 15,076 |
Tags: dp, greedy, number theory
Correct Solution:
```
m=1
r=s=0
for c in input():
s+=int(c);b=1<<s%3
if m&b:m=0;r+=1
m|=b
print(r)
``` | output | 1 | 7,538 | 24 | 15,077 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. | instruction | 0 | 7,539 | 24 | 15,078 |
Tags: dp, greedy, number theory
Correct Solution:
```
s=input()
lastCut=0
cpt=0
for i in range(1,len(s)+1):
for k in range(i-lastCut):
#print(lastCut+k,i,"chaine: ",s[lastCut+k:i],"cpt : ",cpt)
#print(i!=lastCut,int(s[lastCut+k:i])%3==0,int(s[lastCut+k])==0,len(s[lastCut+k:i])==1)
if(i!=lastCut and int(s[lastCut+k:i])%3==0 and not(int(s[lastCut+k])==0 and len(s[lastCut+k:i])!=1)):
lastCut=i
cpt+=1
print(cpt)
``` | output | 1 | 7,539 | 24 | 15,079 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3. | instruction | 0 | 7,540 | 24 | 15,080 |
Tags: dp, greedy, number theory
Correct Solution:
```
from sys import stdin, stdout
nmbr = lambda: int(input())
lst = lambda: list(map(int, input().split()))
from functools import lru_cache
@lru_cache(None)
def fn(pos, rem):
if pos==n:return 0
cur_rem=int(s[pos])%3
ans=fn(pos+1, 0)
if (rem+cur_rem)%3==0 or cur_rem==0:ans=max(1+fn(pos+1, 0), ans)
ans=max(ans, fn(pos+1, (rem+cur_rem)%3))
return ans
for _ in range(1):#nmbr()):
# n=nmbr()
# n,k=lst()
s=input()
n=len(s)
# print(fn(0,0))
NI=float('-inf')
dp=[[NI for i in range(3)] for i in range(n+1)]
dp[0][0]=0
for i in range(n):
for rem in range(3):
cur_rem=int(s[i])%3
dp[i+1][0]=max(dp[i+1][0], dp[i][rem])
if (rem + cur_rem) % 3 == 0 or cur_rem == 0: dp[i+1][0] = max(1 + dp[i][rem], dp[i+1][0])
dp[i+1][(rem+cur_rem)%3]=max(dp[i+1][(rem+cur_rem)%3], dp[i][rem])
# print(*dp, sep='\n')
print(dp[n][0])
``` | output | 1 | 7,540 | 24 | 15,081 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3.
Submitted Solution:
```
S = input()
n, c = 0, len(S)
for i in reversed(range(len(S))):
for k in range(i, c):
if int(S[i:k+1])%3 == 0:
n += 1
c = i
break
print(n)
``` | instruction | 0 | 7,541 | 24 | 15,082 |
Yes | output | 1 | 7,541 | 24 | 15,083 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3.
Submitted Solution:
```
s = input()
f = [0]*len(s)
cnt = 0
for i in range(len(s)):
a = int(s[i])
if a % 3 == 0:
cnt += 1
f[i] = 1
for i in range(len(s)-1):
if f[i] == 0 and f[i+1] == 0:
a = int(s[i])*10+int(s[i+1])
if a % 3 == 0:
cnt += 1
f[i] = f[i+1] = 1
elif i < len(s)-2 and f[i] == 0 and f[i+1] == 0 and f[i+2] == 0:
a = int(s[i]) * 100 + int(s[i + 1]) * 10 + int(s[i + 2])
if a % 3 == 0:
cnt += 1
f[i] = f[i + 1] = f[i + 2] = 1
for i in range(len(s) - 2):
if f[i] == 0 and f[i+1] == 0 and f[i+2] == 0:
a = int(s[i])*100 + int(s[i+1])*10 + int(s[i+2])
if a % 3 == 0:
cnt += 1
f[i] = f[i+1] = f[i+2] = 1
for i in range(len(s) - 3):
if f[i] == 0 and f[i+1] == 0 and f[i+2] == 0 and f[i+3] == 0:
a = int(s[i])*1000 + int(s[i+1])*100 + int(s[i+2])*10+ int(s[i+3])
if a % 3 == 0:
cnt += 1
print(cnt)
``` | instruction | 0 | 7,542 | 24 | 15,084 |
Yes | output | 1 | 7,542 | 24 | 15,085 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3.
Submitted Solution:
```
a = input()
count = 0
lenofsnake = 0
snaketotal = 0
for i in range(len(a)):
if int(a[i])%3 == 0:
count = count+1
lenofsnake = 0
snaketotal = 0
elif lenofsnake == 2 or (snaketotal+int(a[i]))%3 == 0:
count = count + 1
lenofsnake = 0
snaketotal = 0
else:
lenofsnake = lenofsnake + 1
snaketotal = snaketotal + int(a[i])
print(count)
``` | instruction | 0 | 7,543 | 24 | 15,086 |
Yes | output | 1 | 7,543 | 24 | 15,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3.
Submitted Solution:
```
s = input(); l = len(s); c = 0; k = 0; z = 0
for i in range(l):
c += int(s[i])
z += 1
if c%3 == 0 or int(s[i])%3 == 0 or z%3 == 0:
c = 0
z = 0
k += 1
print (k)
``` | instruction | 0 | 7,544 | 24 | 15,088 |
Yes | output | 1 | 7,544 | 24 | 15,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3.
Submitted Solution:
```
s = input()
sum = 0
ans = 0
for i in s:
sum += int(i)
if sum % 3 == 0 or i in '0369':
ans += 1
sum = 0
print(ans)
``` | instruction | 0 | 7,545 | 24 | 15,090 |
No | output | 1 | 7,545 | 24 | 15,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3.
Submitted Solution:
```
S = list(map(int, input()))
SHORTEST = [0 for _ in S]
for i in range(len(S)):
SUM = 0
for j in range(i, len(S)):
SUM += S[j]
if S[i] == 0 or SUM % 3 == 0:
SHORTEST[i] = j-i+1
break
I = 0
ANSWER = 0
while I < len(S):
N = SHORTEST[I]
if N == 0:
I += 1
else:
ANSWER += 1
I += N
print(ANSWER)
``` | instruction | 0 | 7,546 | 24 | 15,092 |
No | output | 1 | 7,546 | 24 | 15,093 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3.
Submitted Solution:
```
def main():
n = input()
dp = []
s = ''
if len(n) > 50:
print(112135)
else:
if int(n[0]) % 3 == 0:
dp.append(1)
else:
dp.append(0)
for i in range(1, len(n)):
dp.append(0)
for j in range(1, i + 1):
if n[i - j + 1:i - j + 2] == '0' and int(n[i - j + 1:i + 1]) != 0:
continue
else:
if int(n[i - j + 1:i + 1]) % 3 == 0:
k = 1
else:
k = 0
dp[i] = max(dp[i], dp[i - j] + k)
if int(n) % 3 == 0 and dp[-1] == 0:
print(1)
else:
print(dp[-1])
main()
# subscribe to Matskevich Play
``` | instruction | 0 | 7,547 | 24 | 15,094 |
No | output | 1 | 7,547 | 24 | 15,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes numbers that are divisible by 3.
He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.
For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 3 that Polycarp can obtain?
Input
The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2β
10^5, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s.
Examples
Input
3121
Output
2
Input
6
Output
1
Input
1000000000000000000000000000000000
Output
33
Input
201920181
Output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 3.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 33 digits 0. Each of the 33 digits 0 forms a number that is divisible by 3.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 0, 9, 201 and 81 are divisible by 3.
Submitted Solution:
```
print(''.join([str(int(i) % 3) for i in input()]).replace('12', '0').replace('21', '0').replace('111', '0').count('0'))
``` | instruction | 0 | 7,548 | 24 | 15,096 |
No | output | 1 | 7,548 | 24 | 15,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp invented a new way to encode strings. Let's assume that we have string T, consisting of lowercase English letters. Let's choose several pairs of letters of the English alphabet in such a way that each letter occurs in at most one pair. Then let's replace each letter in T with its pair letter if there is a pair letter for it. For example, if you chose pairs (l, r), (p, q) and (a, o), then word "parallelogram" according to the given encoding principle transforms to word "qolorreraglom".
Polycarpus already has two strings, S and T. He suspects that string T was obtained after applying the given encoding method from some substring of string S. Find all positions mi in S (1 β€ mi β€ |S| - |T| + 1), such that T can be obtained fro substring SmiSmi + 1... Smi + |T| - 1 by applying the described encoding operation by using some set of pairs of English alphabet letters
Input
The first line of the input contains two integers, |S| and |T| (1 β€ |T| β€ |S| β€ 2Β·105) β the lengths of string S and string T, respectively.
The second and third line of the input contain strings S and T, respectively. Both strings consist only of lowercase English letters.
Output
Print number k β the number of suitable positions in string S.
In the next line print k integers m1, m2, ..., mk β the numbers of the suitable positions in the increasing order.
Examples
Input
11 5
abacabadaba
acaba
Output
3
1 3 7
Input
21 13
paraparallelogramgram
qolorreraglom
Output
1
5
Submitted Solution:
```
def kmp(s):
k = [0] * len(s)
j = 0
for i in range(1, len(s)):
j = k[i - 1]
while (True):
if (s[i] == s[j] or (s[j] == 0 and s[i] != -1)):
k[i] = j + 1
break
if (j == 0):
k[i] = 0
break
j = k[j - 1]
return k
def mns(s):
d = dict()
a = []
for i in range(len(s)):
e = s[i]
if e in d.keys():
a.append(i - d[e])
else:
a.append(0)
d[e] = i
return a
def check(s, s2, j):
q = set()
for i in range(len(s2)):
a = s[i+j]
b = s2[i]
q.add((min(a, b), max(a, b)))
q = list(q)
d = set()
for p in q:
a, b = p
if a == b:
if a in d:
return False
d.add(a)
else:
if a in d:
return False
if b in d:
return False
d.add(a)
d.add(b)
return True
if __name__ == '__main__':
n, m = [int(e) for e in input().split()]
s = input()
s1 = input()
a = mns(s)
a1 = mns(s1)
q = a1 + [-1] + a
kmp_res = kmp(q)
ans = []
for i in range(len(kmp_res)):
e = kmp_res[i]
if e == m:
ans.append(i-m-1-m+1)
res = []
for an in ans:
if check(s, s1, an):
res.append(an)
print(len(res))
print(' '.join([str(e+1) for e in res]))
``` | instruction | 0 | 7,939 | 24 | 15,878 |
No | output | 1 | 7,939 | 24 | 15,879 |
Provide tags and a correct Python 3 solution for this coding contest problem.
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:
* at first, he visits booth number 1;
* if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
* then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
Input
The first line contains two integers n and T (1 β€ n β€ 2 β
10^5, 1 β€ T β€ 10^{18}) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the price of the single candy at booth number i.
Output
Print a single integer β the total number of candies Polycarp will buy.
Examples
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
Note
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
1. Booth 1, buys candy for 5, T = 33;
2. Booth 2, buys candy for 2, T = 31;
3. Booth 3, buys candy for 5, T = 26;
4. Booth 1, buys candy for 5, T = 21;
5. Booth 2, buys candy for 2, T = 19;
6. Booth 3, buys candy for 5, T = 14;
7. Booth 1, buys candy for 5, T = 9;
8. Booth 2, buys candy for 2, T = 7;
9. Booth 3, buys candy for 5, T = 2;
10. Booth 1, buys no candy, not enough money;
11. Booth 2, buys candy for 2, T = 0.
No candy can be bought later. The total number of candies bought is 10.
In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount. | instruction | 0 | 9,102 | 24 | 18,204 |
Tags: binary search, brute force, data structures, greedy
Correct Solution:
```
n,t=map(int,input().split())
a=list(map(int,input().split()))
b=[]
for i in a:
if(i<=t):
b.append(i)
c=0
n=len(b)
s=sum(b)
while(len(b)>0 and t>=min(b)):
if(s<=t):
k=t//s
t-=(s*k)
c+=(n*k)
else:
for i in b:
if(i<=t):
t-=i
c+=1
d=b
z=[]
for i in d:
if(i<=t):
z.append(i)
b=z
n=len(b)
s=sum(b)
if(n==0):
break
print(c)
``` | output | 1 | 9,102 | 24 | 18,205 |
Provide tags and a correct Python 3 solution for this coding contest problem.
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:
* at first, he visits booth number 1;
* if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
* then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
Input
The first line contains two integers n and T (1 β€ n β€ 2 β
10^5, 1 β€ T β€ 10^{18}) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the price of the single candy at booth number i.
Output
Print a single integer β the total number of candies Polycarp will buy.
Examples
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
Note
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
1. Booth 1, buys candy for 5, T = 33;
2. Booth 2, buys candy for 2, T = 31;
3. Booth 3, buys candy for 5, T = 26;
4. Booth 1, buys candy for 5, T = 21;
5. Booth 2, buys candy for 2, T = 19;
6. Booth 3, buys candy for 5, T = 14;
7. Booth 1, buys candy for 5, T = 9;
8. Booth 2, buys candy for 2, T = 7;
9. Booth 3, buys candy for 5, T = 2;
10. Booth 1, buys no candy, not enough money;
11. Booth 2, buys candy for 2, T = 0.
No candy can be bought later. The total number of candies bought is 10.
In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount. | instruction | 0 | 9,103 | 24 | 18,206 |
Tags: binary search, brute force, data structures, greedy
Correct Solution:
```
n,amount = map(int,input().split())
fair = []
fair = list(map(int,input().split()))
minP = min(fair)
candies = 0
while(amount >= minP):
price = 0
count = 0
copieAmount = amount
for i in range(n):
if(copieAmount >= fair[i]):
copieAmount-= fair[i]
price += fair[i]
count+=1
candies += (count) * (amount//price)
amount = amount%price
print(candies)
``` | output | 1 | 9,103 | 24 | 18,207 |
Provide tags and a correct Python 3 solution for this coding contest problem.
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:
* at first, he visits booth number 1;
* if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
* then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
Input
The first line contains two integers n and T (1 β€ n β€ 2 β
10^5, 1 β€ T β€ 10^{18}) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the price of the single candy at booth number i.
Output
Print a single integer β the total number of candies Polycarp will buy.
Examples
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
Note
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
1. Booth 1, buys candy for 5, T = 33;
2. Booth 2, buys candy for 2, T = 31;
3. Booth 3, buys candy for 5, T = 26;
4. Booth 1, buys candy for 5, T = 21;
5. Booth 2, buys candy for 2, T = 19;
6. Booth 3, buys candy for 5, T = 14;
7. Booth 1, buys candy for 5, T = 9;
8. Booth 2, buys candy for 2, T = 7;
9. Booth 3, buys candy for 5, T = 2;
10. Booth 1, buys no candy, not enough money;
11. Booth 2, buys candy for 2, T = 0.
No candy can be bought later. The total number of candies bought is 10.
In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount. | instruction | 0 | 9,104 | 24 | 18,208 |
Tags: binary search, brute force, data structures, greedy
Correct Solution:
```
def main():
n, t = map(int, input().split())
a = [int(i) for i in input().split()]
x = min(a)
ans = 0
while t >= x:
cnt = 0
s = 0
tau = t
for i in range(n):
if tau >= a[i]:
tau -= a[i]
s += a[i]
cnt += 1
ans += cnt * (t // s)
t %= s
print(ans)
main()
``` | output | 1 | 9,104 | 24 | 18,209 |
Provide tags and a correct Python 3 solution for this coding contest problem.
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:
* at first, he visits booth number 1;
* if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
* then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
Input
The first line contains two integers n and T (1 β€ n β€ 2 β
10^5, 1 β€ T β€ 10^{18}) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the price of the single candy at booth number i.
Output
Print a single integer β the total number of candies Polycarp will buy.
Examples
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
Note
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
1. Booth 1, buys candy for 5, T = 33;
2. Booth 2, buys candy for 2, T = 31;
3. Booth 3, buys candy for 5, T = 26;
4. Booth 1, buys candy for 5, T = 21;
5. Booth 2, buys candy for 2, T = 19;
6. Booth 3, buys candy for 5, T = 14;
7. Booth 1, buys candy for 5, T = 9;
8. Booth 2, buys candy for 2, T = 7;
9. Booth 3, buys candy for 5, T = 2;
10. Booth 1, buys no candy, not enough money;
11. Booth 2, buys candy for 2, T = 0.
No candy can be bought later. The total number of candies bought is 10.
In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount. | instruction | 0 | 9,105 | 24 | 18,210 |
Tags: binary search, brute force, data structures, greedy
Correct Solution:
```
def main():
N, money = list(map(int, input().split()))
candies = list(map(int, input().split()))
number_of_candies_bought = 0
current_money = money
while True:
current_number_of_candies_bought = 0
total_price = 0
for candy in candies:
#if Polycarp has enough money
if total_price + candy <= current_money:
current_number_of_candies_bought += 1
total_price += candy
if current_number_of_candies_bought == 0:
break
number_of_iteration = current_money // total_price
number_of_candies_bought += number_of_iteration * current_number_of_candies_bought
current_money -= number_of_iteration * total_price
print(number_of_candies_bought)
if __name__ == '__main__':
main()
``` | output | 1 | 9,105 | 24 | 18,211 |
Provide tags and a correct Python 3 solution for this coding contest problem.
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:
* at first, he visits booth number 1;
* if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
* then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
Input
The first line contains two integers n and T (1 β€ n β€ 2 β
10^5, 1 β€ T β€ 10^{18}) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the price of the single candy at booth number i.
Output
Print a single integer β the total number of candies Polycarp will buy.
Examples
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
Note
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
1. Booth 1, buys candy for 5, T = 33;
2. Booth 2, buys candy for 2, T = 31;
3. Booth 3, buys candy for 5, T = 26;
4. Booth 1, buys candy for 5, T = 21;
5. Booth 2, buys candy for 2, T = 19;
6. Booth 3, buys candy for 5, T = 14;
7. Booth 1, buys candy for 5, T = 9;
8. Booth 2, buys candy for 2, T = 7;
9. Booth 3, buys candy for 5, T = 2;
10. Booth 1, buys no candy, not enough money;
11. Booth 2, buys candy for 2, T = 0.
No candy can be bought later. The total number of candies bought is 10.
In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount. | instruction | 0 | 9,106 | 24 | 18,212 |
Tags: binary search, brute force, data structures, greedy
Correct Solution:
```
import sys
from collections import defaultdict,deque
import heapq
n,t=map(int,sys.stdin.readline().split())
arr=list(map(int,sys.stdin.readline().split()))
pre=[]
s=0
for i in range(n):
s+=arr[i]
pre.append(s)
#print(pre,'pre')
ans=0
x=min(arr)
while t>=x:
cost,cnt=0,0
rem=t
for i in range(n):
if rem>=arr[i]:
rem-=arr[i]
cnt+=1
delta=t-rem
#print(delta,'delta',t,'t',cnt,'cnt')
sweet=t//(delta)*(cnt)
t=t%delta
ans+=sweet
print(ans)
``` | output | 1 | 9,106 | 24 | 18,213 |
Provide tags and a correct Python 3 solution for this coding contest problem.
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:
* at first, he visits booth number 1;
* if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
* then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
Input
The first line contains two integers n and T (1 β€ n β€ 2 β
10^5, 1 β€ T β€ 10^{18}) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the price of the single candy at booth number i.
Output
Print a single integer β the total number of candies Polycarp will buy.
Examples
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
Note
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
1. Booth 1, buys candy for 5, T = 33;
2. Booth 2, buys candy for 2, T = 31;
3. Booth 3, buys candy for 5, T = 26;
4. Booth 1, buys candy for 5, T = 21;
5. Booth 2, buys candy for 2, T = 19;
6. Booth 3, buys candy for 5, T = 14;
7. Booth 1, buys candy for 5, T = 9;
8. Booth 2, buys candy for 2, T = 7;
9. Booth 3, buys candy for 5, T = 2;
10. Booth 1, buys no candy, not enough money;
11. Booth 2, buys candy for 2, T = 0.
No candy can be bought later. The total number of candies bought is 10.
In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount. | instruction | 0 | 9,107 | 24 | 18,214 |
Tags: binary search, brute force, data structures, greedy
Correct Solution:
```
n,T = map(int, input().split())
L = list(map(int, input().split()))
res = 0
while len(L) > 0:
s = sum(L)
l = len(L)
r = T//s
res += r*l
T -= r*s
a = []
for i in L:
if T >= i:
T -= i
res += 1
a.append(i)
L = a
print(res)
``` | output | 1 | 9,107 | 24 | 18,215 |
Provide tags and a correct Python 3 solution for this coding contest problem.
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:
* at first, he visits booth number 1;
* if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
* then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
Input
The first line contains two integers n and T (1 β€ n β€ 2 β
10^5, 1 β€ T β€ 10^{18}) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the price of the single candy at booth number i.
Output
Print a single integer β the total number of candies Polycarp will buy.
Examples
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
Note
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
1. Booth 1, buys candy for 5, T = 33;
2. Booth 2, buys candy for 2, T = 31;
3. Booth 3, buys candy for 5, T = 26;
4. Booth 1, buys candy for 5, T = 21;
5. Booth 2, buys candy for 2, T = 19;
6. Booth 3, buys candy for 5, T = 14;
7. Booth 1, buys candy for 5, T = 9;
8. Booth 2, buys candy for 2, T = 7;
9. Booth 3, buys candy for 5, T = 2;
10. Booth 1, buys no candy, not enough money;
11. Booth 2, buys candy for 2, T = 0.
No candy can be bought later. The total number of candies bought is 10.
In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount. | instruction | 0 | 9,108 | 24 | 18,216 |
Tags: binary search, brute force, data structures, greedy
Correct Solution:
```
def func(n, t, a):
count = 0
s = sum(a)
z = []
if t >= s:
count+=t//s * n
t%=s
for i in a:
if t >= i:
z.append(i)
count+=1
t-=i
if t >= min(a):
count+=func(len(z), t, z)
return count
n, t = map(int, input().split())
a = list(map(int, input().split()))
print(func(n, t, a))
``` | output | 1 | 9,108 | 24 | 18,217 |
Provide tags and a correct Python 3 solution for this coding contest problem.
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:
* at first, he visits booth number 1;
* if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
* then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
Input
The first line contains two integers n and T (1 β€ n β€ 2 β
10^5, 1 β€ T β€ 10^{18}) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the price of the single candy at booth number i.
Output
Print a single integer β the total number of candies Polycarp will buy.
Examples
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
Note
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
1. Booth 1, buys candy for 5, T = 33;
2. Booth 2, buys candy for 2, T = 31;
3. Booth 3, buys candy for 5, T = 26;
4. Booth 1, buys candy for 5, T = 21;
5. Booth 2, buys candy for 2, T = 19;
6. Booth 3, buys candy for 5, T = 14;
7. Booth 1, buys candy for 5, T = 9;
8. Booth 2, buys candy for 2, T = 7;
9. Booth 3, buys candy for 5, T = 2;
10. Booth 1, buys no candy, not enough money;
11. Booth 2, buys candy for 2, T = 0.
No candy can be bought later. The total number of candies bought is 10.
In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount. | instruction | 0 | 9,109 | 24 | 18,218 |
Tags: binary search, brute force, data structures, greedy
Correct Solution:
```
n, T = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
candy = 0
while True:
endCycle = True
roundMoney = 0
roundCandy = 0
for x in a:
if T >= x:
endCycle = False
T -= x
roundMoney += x
roundCandy += 1
if roundMoney:
candy += roundCandy * ((T // roundMoney) + 1)
T %= roundMoney
if endCycle or T == 0:
print(candy)
break
``` | output | 1 | 9,109 | 24 | 18,219 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:
* at first, he visits booth number 1;
* if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
* then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
Input
The first line contains two integers n and T (1 β€ n β€ 2 β
10^5, 1 β€ T β€ 10^{18}) β the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the price of the single candy at booth number i.
Output
Print a single integer β the total number of candies Polycarp will buy.
Examples
Input
3 38
5 2 5
Output
10
Input
5 21
2 4 100 2 6
Output
6
Note
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
1. Booth 1, buys candy for 5, T = 33;
2. Booth 2, buys candy for 2, T = 31;
3. Booth 3, buys candy for 5, T = 26;
4. Booth 1, buys candy for 5, T = 21;
5. Booth 2, buys candy for 2, T = 19;
6. Booth 3, buys candy for 5, T = 14;
7. Booth 1, buys candy for 5, T = 9;
8. Booth 2, buys candy for 2, T = 7;
9. Booth 3, buys candy for 5, T = 2;
10. Booth 1, buys no candy, not enough money;
11. Booth 2, buys candy for 2, T = 0.
No candy can be bought later. The total number of candies bought is 10.
In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount.
Submitted Solution:
```
def splitInput():
return list(map(int,input().split()))
nT = splitInput()
money = nT[1]
kioski = list(filter(lambda x: x <= money , splitInput()))
konfet = 0
if len(kioski)>0:
minKioskiPrice = min(kioski)
while money>=minKioskiPrice:
sumKioski = sum(kioski)
countCycles = money//sumKioski
if countCycles>0:
konfet += countCycles*len(kioski)
money -= sumKioski*countCycles
for k in kioski:
if k<=money:
money -= k
konfet += 1
if money<minKioskiPrice:
break
kioski = list(filter(lambda x: x <= money , kioski))
print(konfet)
``` | instruction | 0 | 9,110 | 24 | 18,220 |
Yes | output | 1 | 9,110 | 24 | 18,221 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.