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.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189 | instruction | 0 | 2,736 | 24 | 5,472 |
Tags: implementation
Correct Solution:
```
def cnt(n):
c=0
while n:
if n%10==9:
c+=1
n//=10
else:
break
return c
def zerocheck(arr,i):
for x in range(i-1,-1,-1):
if arr[x]=="0":
arr[x]="9"
else:
arr[x] = str(int(arr[x]) - 1)
break
return arr
def offer(n,d):
if d==0:
return n
lst=[i for i in str(n)]
ans=[]
# temp=int("9"*(len(str(n))-1))
# while temp:
# if n-temp<=d:
# ans.append(temp)
# break
# else:
# temp//=10
i=len(lst)-1
while i>=1:
if lst[i]!="9" :
if lst[i-1]!="0":
lst[i]="9"
lst[i-1]=str(int(lst[i-1])-1)
else:
lst[i] = "9"
lst[i - 1] = "9"
lst=zerocheck(lst,i-1)
base=int("".join(lst))
if n-base<=d:
ans.append(base)
i-=1
if ans==[]:
return n
return max(ans,key=lambda s:cnt(s))
a,b=map(int,input().strip().split())
print(offer(a,b))
``` | output | 1 | 2,736 | 24 | 5,473 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189 | instruction | 0 | 2,737 | 24 | 5,474 |
Tags: implementation
Correct Solution:
```
a,b=input().strip().split(" ")
a,b=[int(a),int(b)]
k=1
while(True):
nine=k-1
dif=(a-nine)%k
if(dif>b or dif<0):
break
if(k==1000000000000000000):
break
k*=10
k//=10
dif=(a-k+1)%k
ans=a-dif
print(ans)
``` | output | 1 | 2,737 | 24 | 5,475 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189 | instruction | 0 | 2,738 | 24 | 5,476 |
Tags: implementation
Correct Solution:
```
import re
import itertools
from collections import Counter
class Task:
p, d = "", ""
answer = ""
def getData(self):
self.p, self.d = input().split(" ")
def solve(self):
p = self.p
p_minus_d = str(int(self.p) - int(self.d))
p_minus_d = '0' * (len(p) - len(p_minus_d)) + p_minus_d
for i in range(0, len(p)):
if p[i] == p_minus_d[i]:
continue
self.answer = p[0:i]
if p[i + 1:] == '9' * (len(p) - i - 1):
self.answer += p[i]
else:
self.answer += chr(ord(p[i]) - 1)
self.answer += '9' * (len(p) - i - 1)
self.answer = re.sub('^0+', '', self.answer)
return
self.answer = p
def printAnswer(self):
print(self.answer)
task = Task();
task.getData();
task.solve();
task.printAnswer();
``` | output | 1 | 2,738 | 24 | 5,477 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189 | instruction | 0 | 2,739 | 24 | 5,478 |
Tags: implementation
Correct Solution:
```
"""http://codeforces.com/problemset/problem/219/B"""
if __name__ == '__main__':
p, d = map(int, input().split())
lo = p - d
res = p
for i in range(1, 18):
num = 10 ** i
t = p // num * num + (num - 1)
t = t if t <= p else t - num
if p - t <= d:
res = t
else:
break
print(res)
``` | output | 1 | 2,739 | 24 | 5,479 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189 | instruction | 0 | 2,740 | 24 | 5,480 |
Tags: implementation
Correct Solution:
```
p, d = input().split()
p = int(p); d = int(d)
ans = p = p + 1
i = 10
while i <= 1000000000000000000:
if p % i <= d:
ans = p - p % i
i *= 10
print(ans - 1)
``` | output | 1 | 2,740 | 24 | 5,481 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189 | instruction | 0 | 2,741 | 24 | 5,482 |
Tags: implementation
Correct Solution:
```
import sys
p, d = map(int, sys.stdin.readline().split())
k = 10
n = p
while p>=k and p%k+1<=d:
if p%k < k-1:
n = p-p%k-1
k*=10
print (n)
# Made By Mostafa_Khaled
``` | output | 1 | 2,741 | 24 | 5,483 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189
Submitted Solution:
```
from math import *
n,d=map(int,input().split())
ln=0
temp=n
ta=0
num=n
k=1
while(1):
if(temp==0):
break
ans=num
val=0
if(temp%10!=9):
val=1
temp//=10
ln+=1
temp-=val
ta*=10
ta+=9
num=temp*(10**ln)+ta
if(n-num>d):
print(ans)
k=0
break
if(k):
print(ta)
``` | instruction | 0 | 2,742 | 24 | 5,484 |
Yes | output | 1 | 2,742 | 24 | 5,485 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189
Submitted Solution:
```
import sys
import math
import collections
import heapq
import decimal
input=sys.stdin.readline
p,d=(int(i) for i in input().split())
prod=10
ans=-1
for i in range(20):
if(p-(p%prod)!=0):
k=p-(p%prod)-1
if(p-k<=d):
ans=k
prod*=10
if(ans==-1):
ans=p
s1=str(ans)
s2=str(p)
c1,c2=0,0
for i in range(len(s1)-1,-1,-1):
if(s1[i]!='9'):
break
else:
c1+=1
for i in range(len(s2)-1,-1,-1):
if(s2[i]!='9'):
break
else:
c2+=1
if(c2>=c1):
print(p)
else:
print(ans)
``` | instruction | 0 | 2,743 | 24 | 5,486 |
Yes | output | 1 | 2,743 | 24 | 5,487 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189
Submitted Solution:
```
p, d = map(int, input().split())
digit = 0
while d >= 0:
last_digit = (p // (10 ** (digit))) % 10
sub = ((last_digit + 1) % 10) * (10 ** digit)
d -= sub
if d >= 0:
p -= sub
else:
break
s = str(p)
if s.count("9") == len(s):
break
digit += 1
print(p)
``` | instruction | 0 | 2,744 | 24 | 5,488 |
Yes | output | 1 | 2,744 | 24 | 5,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189
Submitted Solution:
```
def solve(p, d):
diff = 0
place = 1
save = diff
psave = p
while p > 0 and diff <= d:
save = diff
if p % 10 != 9:
t = p%10 + 10 - 9
p -= t
diff += place*t
p //= 10
place *= 10
if(diff <= d):
save = diff
return psave - save
p, d = [int(x) for x in input().split()]
print(solve(p, d))
``` | instruction | 0 | 2,745 | 24 | 5,490 |
Yes | output | 1 | 2,745 | 24 | 5,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189
Submitted Solution:
```
p,d = map(int,input().split())
k = p-d
# print(k)
count = 0
if str(p)[-1] == '9':
for i in str(p)[::-1]:
if i == '9':
count+=1
else:
break
x = len(str(p))
y = len(str(k))
# print(x,y)
if x>y:
if int(str(p)[0]) > 1:
first = str(int(str(p)[0])-1)
first+='9'*(x-1)
if x-1>count:
print(first)
else:
print(p)
else:
print('9'*(x-1))
else:
if int(str(p)[0])>int(str(k)[0]):
first = str(int(str(p)[0])-1)
first+='9'*(x-1)
if x-1>count:
print(first)
else:
print(p)
else:
if int(str(p)[0])==int(str(k)[0]):
i = 0
j = 0
ans = ''
# print(k)
while i<x and j<x:
if int(str(p)[i])!=int(str(k)[j]):
ans+=str(int(str(p)[i])-1)
ans+='9'*(x - (i+1))
if x - (i+1)>count:
print(ans)
else:
print(p)
break
else:
ans+=str(int(str(p)[i]))
i+=1
j+=1
``` | instruction | 0 | 2,746 | 24 | 5,492 |
No | output | 1 | 2,746 | 24 | 5,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
n,d=value()
n=str(n)
i=len(n)-1
while(i>0):
if(n[i]!='9'):
#print(n,i)
new=str(int(n[:i])-1)+'9'+n[i+1:]
#print(new)
if(int(n)-int(new)<=d):
d-=int(n)-int(new)
n=str(new)
i=len(n)-1
else:
i-=1
else:
i-=1
print(n)
``` | instruction | 0 | 2,747 | 24 | 5,494 |
No | output | 1 | 2,747 | 24 | 5,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189
Submitted Solution:
```
p, d = map(int, input().split())
best = p
for i in range(len(str(p))):
new_price = p - (p % 10**i) - 10**i + (0 if i == 0 else int('9' * i))
if p - new_price <= d:
best = new_price
else:
break
print(best)
``` | instruction | 0 | 2,748 | 24 | 5,496 |
No | output | 1 | 2,748 | 24 | 5,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price.
Input
The first line contains two integers p and d (1 β€ p β€ 1018; 0 β€ d < p) β the initial price of scissors and the maximum possible price reduction.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print the required price β the maximum price that ends with the largest number of nines and that is less than p by no more than d.
The required number shouldn't have leading zeroes.
Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189
Submitted Solution:
```
a,b=map(int,input().split())
#print(len(str(a)))
t=a
s=a
a=str(s)
p=t
for i in range(len(a)-1,0,-1):
if a[i]=='9':
continue
else:
x=9-int(a[i])
s=s-10**(len(a)-i)+x*10**(len(a)-i-1)
#print(s,t-s)
if t-s>b:
print(p)
exit()
p=a
a=str(s)
#break
print(s)
``` | instruction | 0 | 2,749 | 24 | 5,498 |
No | output | 1 | 2,749 | 24 | 5,499 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO | instruction | 0 | 3,597 | 24 | 7,194 |
Tags: brute force, dp, math
Correct Solution:
```
# stdin = open('testdata.txt')
# def input():
# return stdin.readline().strip()
def solve(n):
if (n < 2020):
print("NO")
return
if n%2020==0 or n%2021==0:
print("YES")
return
k = n
count = 0
while k%2020 != 0:
k = k-1
count += 1
if (count*2021 <= n):
print("YES")
return
print('NO')
return
t = int(input())
for _ in range(t):
n = int(input())
solve(n)
``` | output | 1 | 3,597 | 24 | 7,195 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO | instruction | 0 | 3,598 | 24 | 7,196 |
Tags: brute force, dp, math
Correct Solution:
```
for tests in range(int(input())):
n=int(input())
if n%2020==0:
print('YES')
elif n%2021==0:
print('YES')
else:
x=n//2020 +1
sum=0
flag='NO'
for i in range(x):
n-=2020
if n%2021==0:
flag='YES'
break
print(flag)
``` | output | 1 | 3,598 | 24 | 7,197 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO | instruction | 0 | 3,599 | 24 | 7,198 |
Tags: brute force, dp, math
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a = n // 2020
b = n % 2020
if b > a:
print('NO')
else:
print('YES')
``` | output | 1 | 3,599 | 24 | 7,199 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO | instruction | 0 | 3,600 | 24 | 7,200 |
Tags: brute force, dp, math
Correct Solution:
```
n = int(input())
while n>0:
k = int(input())
for i in range(k//2020+1):
rem = k - i*2020
if rem%2021==0:
print("YES")
break
else:
print("NO")
n-=1
``` | output | 1 | 3,600 | 24 | 7,201 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO | instruction | 0 | 3,601 | 24 | 7,202 |
Tags: brute force, dp, math
Correct Solution:
```
times = int(input())
def check(n):
remainder = n % 2020
quotient = n // 2020
if remainder <= quotient:
return "YES"
else:
return "NO"
arr = []
for i in range(times):
arr.append(check(int(input())))
for i in range(times):
print(arr[i])
``` | output | 1 | 3,601 | 24 | 7,203 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO | instruction | 0 | 3,602 | 24 | 7,204 |
Tags: brute force, dp, math
Correct Solution:
```
'''
Aaditya Upadhyay
ud$$$**$$$$$$bc.
u@**" 4$$$$$$Nu
J ""#$$$$$r
@ $$$b
.F ^*3$$$
:% 4 J$$N
$ :F :$$$$$
4F 9 J$$$$$$$
4$ k 4$$$bed$$$$$$$$$
$r 'F $$$$$$$$$$$$$$$$r
$$$ b. $$$$$$$$$$$$$$$$N
$$$$k 3eeed$b $$Euec."$$$$$$$$$
.@$**N. $$$$$" $$$$$F'L $$$$$$$$$$$ $$$$$$$
:$L 'L $$$$$ 4$$$$$$ * $$$$$$$$$F $$$$$F edNc
@$$$N ^k $$$$$ 3$$$$*% F4$$$$$$$ $$$$$" d" zN
$$$$$$ ^k '$$$" #$$F .$ $$$$c.u@$$$ J" @$$$r
$$$$$$b *u ^L $$ $$$$$$$$$$$u@ $$ d$$$$$$
^$$$$$$. "NL "N. z@* $$$ $$$$$$$$$$$$P P d$$$$$$$
^"*$$$b '*L 9E 4$$$ d$$$$$$$$$$$" d* J$$$$r
^$$$u '$. $$L "#" d$$$$$$".@$$ .@$" z$$$$*"
^$$$$. ^N.3$$$ 4u$$$$$$$ 4$$$ u$*" z$$$"
'*$$$$$$$$ *b J$$$$$$b u$P $" d$P
#$$$$$$ 4$ 3*$"$*$ $"$'c@@$$$$ .u@$$P
"$$$$ ""F~$ uNr$$$^&J$$$F $$$$#
"$$ "$$bd$.W$$$$$$$F $$"
?k ?$$$$$$$$$$F'*
9$bL z$$$$$$$$$$F
$$$$ $$$$$$$$$$$$$
'#$c '$$$$$$$$$"
.@"#$$$$$$$$$$$b
z* $$$$$$$$$$$N.
e" z$$" #$$k '*$$.
.u* u@P" '#$c "$c
u@$*""" d$$" "$$u ^*$b.
:F JP" ^$$c '"$$$$$bL
d$$ .. @$# #$b '#$
9$$$$$b 4$$ ^$k '$
"$""b u$$ '$ d$$$$P
'F $$$$$" ^b ^$$$b$
'W$$$$" 'b@$$$$"
^$$$*
'''
from sys import stdin, stdout
from collections import *
from math import gcd, floor, ceil
def st(): return list(stdin.readline().strip())
def li(): return list(map(int, stdin.readline().split()))
def mp(): return map(int, stdin.readline().split())
def inp(): return int(stdin.readline())
def pr(n): return stdout.write(str(n)+"\n")
mod = 1000000007
INF = float('inf')
def solve():
n = inp()
if n % 2020 <= n//2020:
pr('YES')
else:
pr('NO')
for _ in range(inp()):
solve()
``` | output | 1 | 3,602 | 24 | 7,205 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO | instruction | 0 | 3,603 | 24 | 7,206 |
Tags: brute force, dp, math
Correct Solution:
```
t=int(input())
for _ in range(t):
n = int(input())
if n<2020:
print('NO')
else:
m = n//2020
if n in range(2020*m,2021*m+1):
print('YES')
else:
print('NO')
``` | output | 1 | 3,603 | 24 | 7,207 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO | instruction | 0 | 3,604 | 24 | 7,208 |
Tags: brute force, dp, math
Correct Solution:
```
t=int(input())
for T in range(t):
n=int(input())
x=n//2020
if (n-x*2020)<=x:
print('YES')
else:
print('NO')
``` | output | 1 | 3,604 | 24 | 7,209 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO
Submitted Solution:
```
t = int(input())
while t:
t -= 1
n = int(input())
if n < 2020:
print("NO")
continue
"""
2020*a + 2021*b = n
2020*a + 2020*b + b = n
2020*(a+b) + b = n
2020*(a+b) = (n-b)
8079 => 6060 + 2019 => b:2019, (a+b) = 3
"""
b = n%2020
aPb = n//2020
a = aPb-b
if a < 0:
print("NO")
continue
if 2020*a + 2021*b == n:
ans = "YES"
else:
ans = "NO"
print(ans)
``` | instruction | 0 | 3,605 | 24 | 7,210 |
Yes | output | 1 | 3,605 | 24 | 7,211 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO
Submitted Solution:
```
def check(x):
if (x%2020) < (x//2020) or x%2021==0 :
return True
return False
#if x < 2020:
#return False
# else:
# return check(x-2020) or check(x-2021)
t = int(input())
for i in range(t):
n = int(input())
if check(n):
print("YES")
else:
print("NO")
``` | instruction | 0 | 3,606 | 24 | 7,212 |
Yes | output | 1 | 3,606 | 24 | 7,213 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO
Submitted Solution:
```
tests = int(input())
for t in range(tests):
a = int(input())
if ((a // 2020) >= (a % 2020)):
print("YES")
else:
print("NO")
``` | instruction | 0 | 3,607 | 24 | 7,214 |
Yes | output | 1 | 3,607 | 24 | 7,215 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO
Submitted Solution:
```
def solve(a, b, n):
i = 0
while i * a <= n:
if (n - (i * a)) % b == 0:
print('YES')
return 0
i = i + 1
print("NO")
a = 2020
b = 2021
t = int(input())
for i in range(t):
n = int(input())
solve(a, b, n)
``` | instruction | 0 | 3,608 | 24 | 7,216 |
Yes | output | 1 | 3,608 | 24 | 7,217 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO
Submitted Solution:
```
t = int(input())
a = [0]*t
for i in range(t):
c = int(input())
if c < 2020: a[i] = "NO"
elif c%2020 == 0: a[i] = "YES"
elif c%2021 == 0: a[i] = "YES"
else:
if abs(int(10*c/2021) - int(10*c/2020)) > 0: a[i] = "YES"
else: a[i] = "NO"
for i in a: print(i)
``` | instruction | 0 | 3,609 | 24 | 7,218 |
No | output | 1 | 3,609 | 24 | 7,219 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO
Submitted Solution:
```
for i in range(int(input())):
n=int(input())
if(n<2020):
print("no")
elif((n%2021)%2020==0 or (n%2020)%2021==1 ):
print("yes")
else:
print("NO")
``` | instruction | 0 | 3,610 | 24 | 7,220 |
No | output | 1 | 3,610 | 24 | 7,221 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO
Submitted Solution:
```
t=int(input())
for i in range(t):
p=int(input())
if p%2021==0 or p%2020==0:
print("YES")
elif p%4041==0:
print("YES")
else:
print("NO")
``` | instruction | 0 | 3,611 | 24 | 7,222 |
No | output | 1 | 3,611 | 24 | 7,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
* n=4041, then the number n can be represented as the sum 2020 + 2021;
* n=4042, then the number n can be represented as the sum 2021 + 2021;
* n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021;
* n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (1 β€ n β€ 10^6) β the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
* "YES" if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
5
1
4041
4042
8081
8079
Output
NO
YES
YES
YES
NO
Submitted Solution:
```
t = int(input())
while t:
n=int(input())
n2 = n
count = 0
while n>2020:
n-=2021
if(n==0 or n==2020 or n==2021):
count = 1
break
while n2>2021:
n2-=2020
if(n2==0 or n2==2020 or n2==2021):
count = 1
break
if(count==1):
print("YES")
else:
print("NO")
t-=1
``` | instruction | 0 | 3,612 | 24 | 7,224 |
No | output | 1 | 3,612 | 24 | 7,225 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are three horses living in a horse land: one gray, one white and one gray-and-white. The horses are really amusing animals, which is why they adore special cards. Each of those cards must contain two integers, the first one on top, the second one in the bottom of the card. Let's denote a card with a on the top and b in the bottom as (a, b).
Each of the three horses can paint the special cards. If you show an (a, b) card to the gray horse, then the horse can paint a new (a + 1, b + 1) card. If you show an (a, b) card, such that a and b are even integers, to the white horse, then the horse can paint a new <image> card. If you show two cards (a, b) and (b, c) to the gray-and-white horse, then he can paint a new (a, c) card.
Polycarpus really wants to get n special cards (1, a1), (1, a2), ..., (1, an). For that he is going to the horse land. He can take exactly one (x, y) card to the horse land, such that 1 β€ x < y β€ m. How many ways are there to choose the card so that he can perform some actions in the horse land and get the required cards?
Polycarpus can get cards from the horses only as a result of the actions that are described above. Polycarpus is allowed to get additional cards besides the cards that he requires.
Input
The first line contains two integers n, m (1 β€ n β€ 105, 2 β€ m β€ 109). The second line contains the sequence of integers a1, a2, ..., an (2 β€ ai β€ 109). Note, that the numbers in the sequence can coincide.
The numbers in the lines are separated by single spaces.
Output
Print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
1 6
2
Output
11
Input
1 6
7
Output
14
Input
2 10
13 7
Output
36 | instruction | 0 | 3,676 | 24 | 7,352 |
Tags: constructive algorithms, math, number theory
Correct Solution:
```
# written with help of editorial
n, m = map(int, input().split())
a = list(map(int, input().split()))
def gcd(x, y):
while y:
x, y = y, x % y
return x
g = 0
for x in a:
g = gcd(g, x - 1)
answer = 0
def process(x):
global answer
if x % 2 == 0:
return 0
for i in range(30):
v = 2 ** i * x
if v > m:
break
answer += m - v
for i in range(1, g + 1):
if i * i > g:
break
if g % i:
continue
process(i)
if i * i != g:
process(g // i)
print(answer)
``` | output | 1 | 3,676 | 24 | 7,353 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three horses living in a horse land: one gray, one white and one gray-and-white. The horses are really amusing animals, which is why they adore special cards. Each of those cards must contain two integers, the first one on top, the second one in the bottom of the card. Let's denote a card with a on the top and b in the bottom as (a, b).
Each of the three horses can paint the special cards. If you show an (a, b) card to the gray horse, then the horse can paint a new (a + 1, b + 1) card. If you show an (a, b) card, such that a and b are even integers, to the white horse, then the horse can paint a new <image> card. If you show two cards (a, b) and (b, c) to the gray-and-white horse, then he can paint a new (a, c) card.
Polycarpus really wants to get n special cards (1, a1), (1, a2), ..., (1, an). For that he is going to the horse land. He can take exactly one (x, y) card to the horse land, such that 1 β€ x < y β€ m. How many ways are there to choose the card so that he can perform some actions in the horse land and get the required cards?
Polycarpus can get cards from the horses only as a result of the actions that are described above. Polycarpus is allowed to get additional cards besides the cards that he requires.
Input
The first line contains two integers n, m (1 β€ n β€ 105, 2 β€ m β€ 109). The second line contains the sequence of integers a1, a2, ..., an (2 β€ ai β€ 109). Note, that the numbers in the sequence can coincide.
The numbers in the lines are separated by single spaces.
Output
Print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
1 6
2
Output
11
Input
1 6
7
Output
14
Input
2 10
13 7
Output
36
Submitted Solution:
```
n, m = map(int, input().split())
a = list(map(int, input().split()))
def is_special(x):
return (x % 4 == 2 and (x // 4) % 4 <= 1) or (x % 4 == 0 and (x // 4) % 4 > 1)
def series(y):
ret = []
while y <= 10 ** 9:
ret.append(y - 1)
y = 2 * y - 1
return ret
has_special = False
generators = set()
for number in a:
x = number - 1
while x % 2 == 0:
x //= 2
x += 1
# print(number, x)
generators.add(x)
if not is_special(x):
x -= 1
while x % 3 == 0:
x //= 3
generators.add(x + 1)
else:
has_special = True
if sum(map(lambda t: int(is_special(t)), generators)) > 1:
print(0)
else:
differencies = set()
rr = set()
had = False
if has_special:
for x in sorted(generators):
if is_special(x):
differencies.update(set(series(x)))
else:
for x in sorted(generators):
if is_special(x):
differencies.update(set(series(x)))
else:
if not had:
had = True
differencies.update(set(series(x)))
else:
pass
print(x, series(x))
# print(sorted(generators))
# print(sorted(differencies))
answer = 0
for d in differencies:
answer += max(0, m - d)
print(answer)
``` | instruction | 0 | 3,677 | 24 | 7,354 |
No | output | 1 | 3,677 | 24 | 7,355 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three horses living in a horse land: one gray, one white and one gray-and-white. The horses are really amusing animals, which is why they adore special cards. Each of those cards must contain two integers, the first one on top, the second one in the bottom of the card. Let's denote a card with a on the top and b in the bottom as (a, b).
Each of the three horses can paint the special cards. If you show an (a, b) card to the gray horse, then the horse can paint a new (a + 1, b + 1) card. If you show an (a, b) card, such that a and b are even integers, to the white horse, then the horse can paint a new <image> card. If you show two cards (a, b) and (b, c) to the gray-and-white horse, then he can paint a new (a, c) card.
Polycarpus really wants to get n special cards (1, a1), (1, a2), ..., (1, an). For that he is going to the horse land. He can take exactly one (x, y) card to the horse land, such that 1 β€ x < y β€ m. How many ways are there to choose the card so that he can perform some actions in the horse land and get the required cards?
Polycarpus can get cards from the horses only as a result of the actions that are described above. Polycarpus is allowed to get additional cards besides the cards that he requires.
Input
The first line contains two integers n, m (1 β€ n β€ 105, 2 β€ m β€ 109). The second line contains the sequence of integers a1, a2, ..., an (2 β€ ai β€ 109). Note, that the numbers in the sequence can coincide.
The numbers in the lines are separated by single spaces.
Output
Print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
1 6
2
Output
11
Input
1 6
7
Output
14
Input
2 10
13 7
Output
36
Submitted Solution:
```
n, m = map(int, input().split())
a = list(map(int, input().split()))
def is_special(x):
return (x % 4 == 2 and (x // 4) % 4 <= 1) or (x % 4 == 0 and (x // 4) % 4 > 1)
def series(y):
ret = []
while y <= 10 ** 9:
ret.append(y - 1)
y = 2 * y - 1
return ret
has_special = False
generators = set()
for number in a:
x = number - 1
while x % 2 == 0:
x //= 2
x += 1
# print(number, x)
generators.add(x)
if not is_special(x):
x -= 1
while x % 3 == 0:
x //= 3
generators.add(x + 1)
else:
has_special = True
if sum(map(lambda t: int(is_special(t)), generators)) > 1:
print(0)
else:
differencies = set()
rr = set()
had = False
if has_special:
for x in sorted(generators):
if is_special(x):
differencies.update(set(series(x)))
else:
for x in sorted(generators):
if is_special(x):
differencies.update(set(series(x)))
else:
if not had:
had = True
differencies.update(set(series(x)))
else:
pass
# print(x, series(x))
# print(sorted(generators))
# print(sorted(differencies))
answer = 0
for d in differencies:
answer += max(0, m - d)
print(answer)
``` | instruction | 0 | 3,678 | 24 | 7,356 |
No | output | 1 | 3,678 | 24 | 7,357 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three horses living in a horse land: one gray, one white and one gray-and-white. The horses are really amusing animals, which is why they adore special cards. Each of those cards must contain two integers, the first one on top, the second one in the bottom of the card. Let's denote a card with a on the top and b in the bottom as (a, b).
Each of the three horses can paint the special cards. If you show an (a, b) card to the gray horse, then the horse can paint a new (a + 1, b + 1) card. If you show an (a, b) card, such that a and b are even integers, to the white horse, then the horse can paint a new <image> card. If you show two cards (a, b) and (b, c) to the gray-and-white horse, then he can paint a new (a, c) card.
Polycarpus really wants to get n special cards (1, a1), (1, a2), ..., (1, an). For that he is going to the horse land. He can take exactly one (x, y) card to the horse land, such that 1 β€ x < y β€ m. How many ways are there to choose the card so that he can perform some actions in the horse land and get the required cards?
Polycarpus can get cards from the horses only as a result of the actions that are described above. Polycarpus is allowed to get additional cards besides the cards that he requires.
Input
The first line contains two integers n, m (1 β€ n β€ 105, 2 β€ m β€ 109). The second line contains the sequence of integers a1, a2, ..., an (2 β€ ai β€ 109). Note, that the numbers in the sequence can coincide.
The numbers in the lines are separated by single spaces.
Output
Print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
1 6
2
Output
11
Input
1 6
7
Output
14
Input
2 10
13 7
Output
36
Submitted Solution:
```
n, m = map(int, input().split())
a = list(map(int, input().split()))
def is_special(x):
return not(x % 6 == 4)
def series(y):
ret = []
while y <= 10 ** 9:
ret.append(y - 1)
y = 2 * y - 1
return ret
has_special = False
generators = set()
for number in a:
x = number - 1
while x % 2 == 0:
x //= 2
x += 1
# print(number, x)
generators.add(x)
if not is_special(x):
x -= 1
while x % 3 == 0:
x //= 3
generators.add(x + 1)
else:
has_special = True
if sum(map(lambda t: int(is_special(t)), generators)) > 1:
print(0)
else:
differencies = set()
rr = set()
had = False
if has_special:
for x in sorted(generators):
if is_special(x):
differencies.update(set(series(x)))
else:
for x in sorted(generators):
if is_special(x):
differencies.update(set(series(x)))
else:
if not had:
had = True
rr = set(series(x))
else:
continue
rr = rr.intersection(set(series(x)))
# print(x, series(x))
differencies.update(rr)
# print(sorted(generators))
# print(sorted(differencies))
answer = 0
for d in differencies:
answer += max(0, m - d)
print(answer)
``` | instruction | 0 | 3,679 | 24 | 7,358 |
No | output | 1 | 3,679 | 24 | 7,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same. | instruction | 0 | 6,000 | 24 | 12,000 |
Tags: greedy, math, number theory
Correct Solution:
```
import sys
import math,bisect,operator
inf,m = float('inf'),10**9+7
sys.setrecursionlimit(10 ** 5)
from itertools import groupby,accumulate
from heapq import heapify,heappop,heappush
from collections import deque,Counter,defaultdict
I = lambda : int(sys.stdin.readline())
neo = lambda : map(int, sys.stdin.readline().split())
Neo = lambda : list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
n = I()
A = Neo()
def check(t):
i = 0
k = 0
while i < n:
k += A[i]
if k == t:
k = 0
elif k > t:
return 0
i += 1
if k == t or k == 0:
return 1
return 0
t = sum(A)
div = set()
for i in range(1,int(t**.5)+1):
if t%i == 0:
div.add(i)
div.add(t//i)
# print(div)
for i in sorted(div):
if check(i):
print(n-t//i)
break
``` | output | 1 | 6,000 | 24 | 12,001 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same. | instruction | 0 | 6,001 | 24 | 12,002 |
Tags: greedy, math, number theory
Correct Solution:
```
t = int(input())
INF = 1 << 60
def solve():
n = int(input())
a = list(map(int, input().split()))
ans = INF
s = 0
for i in range(n):
s += a[i]
cnt = 0
tmpsum = 0
for j in range(n):
if tmpsum < s:
tmpsum += a[j]
continue
elif tmpsum == s:
cnt += 1
tmpsum = a[j]
continue
else:
cnt = -INF
break
if tmpsum == s:
cnt += 1
else:
cnt = -INF
ans = min(ans, n - cnt)
print(ans)
for i in range(t):
solve()
``` | output | 1 | 6,001 | 24 | 12,003 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same. | instruction | 0 | 6,002 | 24 | 12,004 |
Tags: greedy, math, number theory
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
arr = list(map(int,input().split()))
if arr.count(arr[0])==len(arr):print("0")
else:
sub_arr_sum,ans = 0,-1
for k in range(1,n):
if sum(arr)%(n-k)==0:
sub_arr_sum_limit,part_cnt,sub_arr_sum = sum(arr)//(n-k),0,0
for i in range(n):
if sub_arr_sum != sub_arr_sum_limit:
sub_arr_sum += arr[i]
else:
sub_arr_sum = arr[i]
part_cnt += 1
if sub_arr_sum == sub_arr_sum_limit:part_cnt += 1
#print(sub_arr_sum,part_cnt)
if part_cnt==n-k:
ans = k
break
else:continue
if ans!=-1:break
if ans == -1:print(n-1)
else: print(ans)
``` | output | 1 | 6,002 | 24 | 12,005 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same. | instruction | 0 | 6,003 | 24 | 12,006 |
Tags: greedy, math, number theory
Correct Solution:
```
from sys import stdin,stdout
from math import gcd,sqrt,factorial,pi,inf
from collections import deque,defaultdict
from bisect import bisect,bisect_left
input=stdin.readline
R=lambda:map(int,input().split())
I=lambda:int(input())
S=lambda:input().rstrip('\n')
L=lambda:list(R())
P=lambda x:stdout.write(str(x)+'\n')
nCr=lambda x,y:(f[x]*inv((f[y]*f[x-y])%N))%N
inv=lambda x:pow(x,N-2,N)
lcm=lambda x,y:(x*y)//gcd(x,y)
hg=lambda x,y:((y+x-1)//x)*x
pw=lambda x:1 if x==1 else 1+pw(x//2)
chk=lambda x:chk(x//2) if not x%2 else True if x==1 else False
sm=lambda x:(x**2+x)//2
N=10**9+7
for _ in range(I()):
n=I()
*a,=R()
for i in range(1,n):
a[i]+=a[i-1]
for i in range(n):
if a[-1]%a[i]==0:
p=0
for j in range(i,n):
if a[j]%a[i]==0:p+=1
if a[-1]//a[i]==p:ans=n-p;break
print(ans)
``` | output | 1 | 6,003 | 24 | 12,007 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same. | instruction | 0 | 6,004 | 24 | 12,008 |
Tags: greedy, math, number theory
Correct Solution:
```
"""
Author: Enivar
Date: Tue 22 Dec 2020 13:30:33 EAT
"""
from sys import exit, stderr, stdout
from math import sqrt
def debug(*args):
for i in args:
stderr.write(str(i)+' ')
stderr.write('\n')
class Input:
def __init__(self):
self.sum = 0
self.mx = 0
def inp(self, x):
self.sum+=x
self.mx = max(self.mx, x)
return x
def factors(N):
lim = N//2+1
ret = []
for i in range(obj.mx,lim):
if N%i==0: ret.append(i)
return ret
for _ in range(int(input())):
n = int(input())
obj = Input()
a = [obj.inp(int(x)) for x in input().split()]
divisors = factors(obj.sum)
D = len(divisors)
if n==1:
print(0)
continue
if D==0:
print(n-1)
continue
j, tmp, fg = 0, 0, False
for i in range(n):
tmp+=a[i]
while tmp>divisors[j]:
if j+1<D:
if tmp<divisors[j+1]: break
j+=1
else: break
if tmp in divisors[j:]:
tp, k = 0, i+1
ans = 0
if i>0: ans = i
while True:
if k>=n:
fg = True
break
tp+=a[k]
if a[k]!=tmp: ans+=1
if tp==tmp:
tp = 0
if ans>0 and a[k]!=tmp: ans-=1
elif tp>tmp: break
k+=1
if fg: break
if fg: print(ans)
else: print(n-1)
``` | output | 1 | 6,004 | 24 | 12,009 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same. | instruction | 0 | 6,005 | 24 | 12,010 |
Tags: greedy, math, number theory
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
sum_ = sum(a)
for j in range(n):
if sum_ % (n - j) == 0:
elem = sum_ // (n - j)
i = 1; curr = a[0]; co = 0
while i < n:
if curr < elem:
curr += a[i]
co += 1; i += 1
elif curr == elem:
curr = a[i]; i += 1
else:
break
if co == j and curr == elem: print(j); break
``` | output | 1 | 6,005 | 24 | 12,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same. | instruction | 0 | 6,006 | 24 | 12,012 |
Tags: greedy, math, number theory
Correct Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
min_ans=0
while True:
chk=True
avg_ans=s/(n-min_ans)
curr_sum=0
for i in range(n):
if(curr_sum<avg_ans):
curr_sum+=a[i]
if(curr_sum==avg_ans):
curr_sum=0
elif(curr_sum>avg_ans):
chk=False
min_ans+=1
break
if(chk==True):
print(min_ans)
break
``` | output | 1 | 6,006 | 24 | 12,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same. | instruction | 0 | 6,007 | 24 | 12,014 |
Tags: greedy, math, number theory
Correct Solution:
```
t = int(input())
while t > 0:
t -= 1
n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
for k in range(s):
if s%(n-k) == 0:
ps = s//(n-k)
tmpSum = 0
for i in a:
if tmpSum > ps:
break
elif tmpSum < ps:
tmpSum += i
else:
tmpSum = i
if tmpSum == ps:
print(k)
break
``` | output | 1 | 6,007 | 24 | 12,015 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
for i in range(int(input())):
length=int(input())
ls=list(map(int,input().split()))
if len(ls)==1 or ls.count(ls[0])==len(ls):
print(0)
else:
res=len(ls)-1
s=sum(ls)
for i in range(length-1,1,-1):
if s%i==0:
cur=0
need=s//i
ans = 0
flag = 1
for j in range(len(ls)):
cur+=ls[j]
if cur>need:
flag=0
break
elif cur==need:
cur=0
if flag==1:
res=length-i
break
print(res)
``` | instruction | 0 | 6,008 | 24 | 12,016 |
Yes | output | 1 | 6,008 | 24 | 12,017 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
# You miss 100% of the shots you don't take. Wayne Gretzky
# by : Blue Edge - Create some chaos
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
f=n
b=[0]
for x in a:
b+=x+b[-1],
while f:
if s%f==0:
p=s//f
for i in range(1,f):
if p*i not in b:
break
else:
ans = 0
sm = 0
c = -1
for x in a:
sm+=x
c+=1
if sm==p:
ans+=c
c=-1
sm=0
print(ans)
break
f-=1
``` | instruction | 0 | 6,009 | 24 | 12,018 |
Yes | output | 1 | 6,009 | 24 | 12,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
#New Imports
from collections import defaultdict
def solution():
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
i = 0
j = n-1
sleft = 0
sright = 0
sleftarr = []
srightarr = []
for i in range(n):
sleft += a[i]
sleftarr.append(sleft)
for i in reversed(range(n)):
sright += a[i]
srightarr.append(sright)
srightarr.reverse()
for i in range(n):
for j in range(i+1,n):
sleft = sleftarr[i]
sright = srightarr[j]
if sleft == sright and (s-(2*sleft))%sleft == 0:
x = i+1
f = 0
sval = 0
while x < j:
sval += a[x]
if sval == sleft:
sval = 0
if x == j-1:
f = 1
elif sval > sleft:
break
x += 1
if f == 1:
count = s//sleft
print(n-count)
return
if sleft == sright and i+1 == j:
print(n-2)
return
print(n-1)
return
def main():
testcases = 1
testcases = int(input())
for _ in range(testcases):
solution()
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | instruction | 0 | 6,010 | 24 | 12,020 |
Yes | output | 1 | 6,010 | 24 | 12,021 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
A = list(map(int, input().split()))
s = sum(A)
for i in range(n):
if s%(n-i) != 0:
continue
p = s//(n-i)
#print(p)
cur = 0
flag = True
for j in range(n):
if cur+A[j]>p:
flag=False
break
elif cur+A[j] == p:
cur = 0
else:
cur += A[j]
if flag:
ans = i
break
print(ans)
``` | instruction | 0 | 6,011 | 24 | 12,022 |
Yes | output | 1 | 6,011 | 24 | 12,023 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
from collections import defaultdict
import heapq
def solve(n, arr):
deleted = [False] * n
ans = 0
while True:
min_i, max_i = -1, -1
for i, x in enumerate(arr):
if deleted[i]: continue
if min_i == -1 or (arr[min_i] > arr[i]):
min_i = i
if max_i == -1 or (arr[max_i] < arr[i]):
max_i = i
if min_i == -1 or arr[min_i] == arr[max_i]:
return ans
i = min_i - 1
while i >= 0 and deleted[i]: i -= 1
l = arr[i] if i >= 0 else float('inf')
j = min_i + 1
while j < n and deleted[j]: j += 1
r = arr[j] if j < n else float('inf')
if l == r:
return ans
if l <= r:
arr[i] += arr[min_i]
else:
arr[j] += arr[min_i]
deleted[min_i] = True
ans += 1
# def print_arr(arr):
# return ' '.join(map(str, arr))
#
# if __name__ == '__main__':
# print(solve(7, [1, 2, 1, 2, 3, 3, 6]))
T = int(input())
for _ in range(T):
n = int(input())
arr = [*map(int, input().split())]
print(solve(n, arr))
# print(print_arr(res))
#print('YES' if res else 'NO')
``` | instruction | 0 | 6,012 | 24 | 12,024 |
No | output | 1 | 6,012 | 24 | 12,025 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
b = []
m = sum(a)
for i in range(n, 0, -1):
if m % i == 0:
b.append(m // i)
for i in b:
x = 0
f = 0
for j in range(n):
x += a[j]
if x == i:
x = 0
elif x > i:
x = 0
f = 1
break
if x:
if x > i:
f = 1
if f == 0:
break
print(n - (m//i))
``` | instruction | 0 | 6,013 | 24 | 12,026 |
No | output | 1 | 6,013 | 24 | 12,027 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
T=int(input())
for _ in range(T):
n=int(input())
arr=list(map(int,input().split()))
if set(arr)=={arr[0]}:print(0)
else:
ans=0
while len(set(arr))!=1:
m=min(arr);idx=arr.index(m)
ans+=1
if idx-1==-1:arr[idx+1]+=arr[idx]
elif idx+1==len(arr):arr[idx-1]+=arr[idx]
else:
if arr[idx-1]<arr[idx+1]:arr[idx-1]+=arr[idx]
else:arr[idx+1]+=arr[idx]
arr.pop(idx)
print(ans)
``` | instruction | 0 | 6,014 | 24 | 12,028 |
No | output | 1 | 6,014 | 24 | 12,029 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp was given an array of a[1 ... n] of n integers. He can perform the following operation with the array a no more than n times:
* Polycarp selects the index i and adds the value a_i to one of his choice of its neighbors. More formally, Polycarp adds the value of a_i to a_{i-1} or to a_{i+1} (if such a neighbor does not exist, then it is impossible to add to it).
* After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.
For example, if Polycarp has an array a = [3, 1, 6, 6, 2], then it can perform the following sequence of operations with it:
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [4, 6, 6, 2].
* Polycarp selects i = 1 and adds the value a_i to (i+1)-th element: a = [10, 6, 2].
* Polycarp selects i = 3 and adds the value a_i to (i-1)-th element: a = [10, 8].
* Polycarp selects i = 2 and adds the value a_i to (i-1)-th element: a = [18].
Note that Polycarp could stop performing operations at any time.
Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all a_i are equal to each other).
Input
The first line contains a single integer t (1 β€ t β€ 3000) β the number of test cases in the test. Then t test cases follow.
The first line of each test case contains a single integer n (1 β€ n β€ 3000) β the length of the array. The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β array a.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Output
For each test case, output a single number β the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).
Example
Input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
Output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):
[3, 1, 6, 6, 2] \xrightarrow[]{i=4,~add~to~left} [3, 1, 12, 2] \xrightarrow[]{i=2,~add~to~right} [3, 13, 2] \xrightarrow[]{i=1,~add~to~right} [16, 2] \xrightarrow[]{i=2,~add~to~left} [18]. All elements of the array [18] are the same.
In the second test case of the example, the answer can be constructed like this (just one way among other ways):
[1, 2, 2, 1] \xrightarrow[]{i=1,~add~to~right} [3, 2, 1] \xrightarrow[]{i=3,~add~to~left} [3, 3]. All elements of the array [3, 3] are the same.
In the third test case of the example, Polycarp doesn't need to perform any operations since [2, 2, 2] contains equal (same) elements only.
In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):
[6, 3, 2, 1] \xrightarrow[]{i=3,~add~to~right} [6, 3, 3] \xrightarrow[]{i=3,~add~to~left} [6, 6]. All elements of the array [6, 6] are the same.
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a = [int(i) for i in input().split()]
s = 0
rs = n - 1
for i in range(n):
s += a[i]
s1 = 0
cnt = i
ok = 1
for j in range(i + 1, n):
s1 += a[j]
if s1 > s:
ok = 0
break
if s1 == s:
s1 = 0
else:
cnt += 1
if ok == 1:
rs = min(rs, cnt)
print(rs)
``` | instruction | 0 | 6,015 | 24 | 12,030 |
No | output | 1 | 6,015 | 24 | 12,031 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.