text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
Tags: brute force, dp, implementation, math, two pointers
Correct Solution:
```
###### ### ####### ####### ## # ##### ### #####
# # # # # # # # # # # # # ###
# # # # # # # # # # # # # ###
###### ######### # # # # # # ######### #
###### ######### # # # # # # ######### #
# # # # # # # # # # #### # # #
# # # # # # # ## # # # # #
###### # # ####### ####### # # ##### # # # #
from __future__ import print_function # for PyPy2
# from itertools import permutations
# from functools import cmp_to_key # for adding custom comparator
# from fractions import Fraction
# from collections import *
from sys import stdin
# from bisect import *
# from heapq import *
# from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1e9)+7
inf = float("inf")
n, = gil()
a, b = gil(), gil()
ans = 0
base = 0
# cal ans for odd len
for ci in range(n):
lo, ro, le, re, anso, anse = ci-1, ci+1, ci, ci+1, 0, 0
base += a[ci]*b[ci]
while (le >= 0 and re < n) or (lo >= 0 and ro < n):
if (le >= 0 and re < n):anse += (a[le]-a[re])*b[re] + (a[re]- a[le])*b[le] ;ans = max(anse, ans)
if (lo >= 0 and ro < n):anso += (a[lo]- a[ro])*b[ro] + (a[ro]- a[lo])*b[lo] ;ans = max(anso, ans)
le -= 1;re += 1;lo -= 1;ro += 1
print(ans+base)
```
| 105,500 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
Tags: brute force, dp, implementation, math, two pointers
Correct Solution:
```
n=int(input())
array1=list(map(int,input().split()))
array2=list(map(int,input().split()))
totalSum=sum([array2[i]*array1[i] for i in range(n)])
maxSum=totalSum
"""
for i in range(n):
for j in range(i,i+2):
start=i
end=j
curSum=totalSum
while start>=0 and end<n:
#curSum=curSum-array1[start]*array2[start]-array1[end]*array2[end]
#curSum=curSum+array1[start]*array2[end]+array1[end]*array2[start]
curSum+=(array1[start]-array1[end])*(array2[end]-array2[start])
start-=1
end+=1
maxSum=max(maxSum,curSum)
"""
for i in range(n):
#for j in range(i,i+2):
start=i
end=i
curSum=totalSum
while start>=0 and end<n:
curSum+=(array1[start]-array1[end])*(array2[end]-array2[start])
start-=1
end+=1
maxSum=max(maxSum,curSum)
for i in range(n):
start=i
end=i+1
curSum=totalSum
while start>=0 and end<n:
curSum+=(array1[start]-array1[end])*(array2[end]-array2[start])
start-=1
end+=1
maxSum=max(maxSum,curSum)
print(maxSum)
```
| 105,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
Submitted Solution:
```
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
base_sum = sum([a * b for a, b in zip(a, b)])
max_possible_sum = base_sum
for num in range(1, 2 * n - 2):
if num % 2 == 0:
left = num // 2 - 1
right = num // 2 + 1
else:
left = (num - 1) // 2
right = (num + 1) // 2
local_max_possible_sum = base_sum
while left >= 0 and right < n:
local_max_possible_sum -= (a[left] - a[right]) * (b[left] - b[right])
max_possible_sum = max(max_possible_sum, local_max_possible_sum)
left -= 1
right += 1
print(max_possible_sum)
```
Yes
| 105,502 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = 0
for i in range(n):
s+=(a[i]*b[i])
ans = s
k = s
for c in range(1, n-1):
l = c-1
r = c+1
res = k
while l>=0 and r<n:
res+=((a[l]-a[r])*(b[r]-b[l]))
ans = max(ans, res)
l-=1
r+=1
for c in range(n-1):
res = k
l = c
r = c+1
while l>=0 and r<n:
res+=((a[l]-a[r])*(b[r]-b[l]))
ans = max(ans, res)
l-=1
r+=1
print(ans)
```
Yes
| 105,503 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
Submitted Solution:
```
def func(l, r, d):
global ans
while l >= 0 and r < n:
d -= (a[l] - a[r]) * (b[l] - b[r])
ans = max(ans, d)
l -= 1
r += 1
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
p = 0
for i in range(n):
ans += a[i] * b[i]
p += a[i] * b[i]
for i in range(n):
func(i - 1, i + 1, p)
func(i, i + 1, p)
print(ans)
```
Yes
| 105,504 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
Submitted Solution:
```
n=int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
resi = 0
for j in range(n):
resi+=a[j]*b[j]
ans=resi+0
for j in range(n):
tmp=resi+0
l=j
r=j+1
while(l>=0 and r<n):
tmp+=(a[l]-a[r])*(b[r]-b[l])
ans=max(tmp,ans)
l-=1
r+=1
for j in range(n):
tmp=resi+0
l=j-1
r=j+1
while(l>=0 and r<n):
tmp+=(a[l]-a[r])*(b[r]-b[l])
ans=max(tmp,ans)
r+=1
l-=1
print(ans)
```
Yes
| 105,505 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
init = 0
for i in range(n):
init += a[i] * b[i]
mx = init
for i in range(2*n-1):
now = init
if i % 2 == 0:
for j in range(min(i//2, (2*n-i)//2-1)):
now += a[i//2-j] * b[i//2+j] + a[i//2+j] * b[i//2-j]
now -= a[i//2-j] * b[i//2-j] + a[i//2+j] * b[i//2+j]
mx = max(mx, now)
else:
for j in range(min(i//2+1, (2*n-i)//2)):
now += a[i//2-j] * b[i//2+j+1] + a[i//2+j+1] * b[i//2-j]
now -= a[i//2-j] * b[i//2-j] + a[i//2+j+1] * b[i//2+j+1]
mx = max(mx, now)
print(mx)
```
No
| 105,506 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
Submitted Solution:
```
import sys,os
from io import BytesIO,IOBase
mod = 10**9+7; Mod = 998244353; INF = float('inf')
# input = lambda: sys.stdin.readline().rstrip("\r\n")
# inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
#______________________________________________________________________________________________________
# 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)
# endregion'''
#______________________________________________________________________________________________________
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
# ______________________________________________________________________________________________________
# from math import *
# from bisect import *
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# from itertools import groupby
# sys.setrecursionlimit(5000) #this is must for dfs
# ______________________________________________________________________________________________________
# segment tree for range minimum query and update 0 indexing
# init = float('inf')
# st = [init for i in range(4*len(a))]
# def build(a,ind,start,end):
# if start == end:
# st[ind] = a[start]
# else:
# mid = (start+end)//2
# build(a,2*ind+1,start,mid)
# build(a,2*ind+2,mid+1,end)
# st[ind] = min(st[2*ind+1],st[2*ind+2])
# build(a,0,0,n-1)
# def query(ind,l,r,start,end):
# if start>r or end<l:
# return init
# if l<=start<=end<=r:
# return st[ind]
# mid = (start+end)//2
# return min(query(2*ind+1,l,r,start,mid),query(2*ind+2,l,r,mid+1,end))
# def update(ind,val,stind,start,end):
# if start<=ind<=end:
# if start==end:
# st[stind] = a[start] = val
# else:
# mid = (start+end)//2
# update(ind,val,2*stind+1,start,mid)
# update(ind,val,2*stind+2,mid+1,end)
# st[stind] = min(st[left],st[right])
# ______________________________________________________________________________________________________
# Checking prime in O(root(N))
# def isprime(n):
# if (n % 2 == 0 and n > 2) or n == 1: return 0
# else:
# s = int(n**(0.5)) + 1
# for i in range(3, s, 2):
# if n % i == 0:
# return 0
# return 1
# def lcm(a,b):
# return (a*b)//gcd(a,b)
# ______________________________________________________________________________________________________
# nCr under mod
# def C(n,r,mod = 10**9+7):
# if r>n: return 0
# if r>n-r: r = n-r
# num = den = 1
# for i in range(r):
# num = (num*(n-i))%mod
# den = (den*(i+1))%mod
# return (num*pow(den,mod-2,mod))%mod
# def C(n,r):
# if r>n:
# return 0
# if r>n-r:
# r = n-r
# ans = 1
# for i in range(r):
# ans = (ans*(n-i))//(i+1)
# return ans
# ______________________________________________________________________________________________________
# For smallest prime factor of a number
# M = 2*10**5+10
# spf = [i for i in range(M)]
# def spfs(M):
# for i in range(2,M):
# if spf[i]==i:
# for j in range(i*i,M,i):
# if spf[j]==j:
# spf[j] = i
# return
# spfs(M)
# ______________________________________________________________________________________________________
# def gtc(p):
# print('Case #'+str(p)+': ',end='')
# ______________________________________________________________________________________________________
tc = 1
# tc = int(input())
for test in range(1,tc+1):
n, = inp()
a = inp()
b = inp()
for i in range(n):
a[i] = a[i]+0.0
b[i] = b[i]+0.0
lt = [a[0]*b[0]]*(n+1)
for i in range(1,n):
lt[i] = lt[i-1]+(a[i]*b[i])
rt = [a[-1]*b[-1]]*(n+1)
for i in range(n-2,-1,-1):
rt[i] = rt[i+1]+(a[i]*b[i])
lt[-1] = rt[-1] = 0
ans = lt[n-1]
for i in range(n):
left,right = i,i
res = a[i]*b[i]
ans = max(ans,res+lt[i-1]+rt[i+1])
while(left>0 and right<n-1):
left-=1
right+=1
res+=a[left]*b[right]+a[right]*b[left]
ans = max(ans,res+lt[left-1]+rt[right+1])
if i<n-1:
left,right = i,i+1
res = a[i]*b[i+1]+a[i+1]*b[i]
ans = max(ans,res+lt[i-1]+rt[i+2])
while(left>0 and right<n-1):
left-=1
right+=1
res+=a[left]*b[right]+a[right]*b[left]
ans = max(ans,res+lt[left-1]+rt[right+1])
print(ans)
```
No
| 105,507 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
Submitted Solution:
```
import sys
input = sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[A[i]*B[i] for i in range(n)]
S=[0]
for i in range(n):
S.append(S[-1]+C[i])
ANS=S[-1]
for i in range(n):
XS=S[-1]
for j in range(i):
if i-j>=0 and i+j<n:
XS-=C[i-j]+C[i+j]
XS+=A[i-j]*B[i+j]+A[i+j]*B[i-j]
ANS=max(ANS,XS)
else:
break
for i in range(1,n):
XS=S[-1]-C[i-1]-C[i]+A[i-1]*B[i]+A[i]*B[i-1]
for j in range(1,i):
if i-1-j>=0 and i+j<n:
XS-=C[i-1-j]+C[i+j]
XS+=A[i-1-j]*B[i+j]+A[i+j]*B[i-1-j]
ANS=max(ANS,XS)
else:
break
print(ANS)
```
No
| 105,508 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two integer arrays a and b of length n.
You can reverse at most one subarray (continuous subsegment) of the array a.
Your task is to reverse such a subarray that the sum β_{i=1}^n a_i β
b_i is maximized.
Input
The first line contains one integer n (1 β€ n β€ 5000).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^7).
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^7).
Output
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of a.
Examples
Input
5
2 3 2 1 3
1 3 2 4 2
Output
29
Input
2
13 37
2 4
Output
174
Input
6
1 8 7 6 3 6
5 9 6 8 8 6
Output
235
Note
In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β
1 + 3 β
3 + 2 β
2 + 3 β
4 + 1 β
2 = 29.
In the second example, you don't need to use the reverse operation. 13 β
2 + 37 β
4 = 174.
In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β
5 + 8 β
9 + 3 β
6 + 6 β
8 + 7 β
8 + 6 β
6 = 235.
Submitted Solution:
```
import sys
input=sys.stdin.readline
def IN():
return int(input())
def INL():
return list(map(int,input().split()))
global n
global a
global b
def solve():
base=0
for i in range(n):
base+=a[i]*b[i]
res=base
for i in range(n):
temp=base
st = i
end= i+1
while st >=0 and end<n:
temp-= a[st]*b[st] + a[end]*b[end] # parejo
temp += a[st]*b[end] + a[end]*b[st] # cruzado
res= max(res,temp)
st-=1
end+=1
print(res)
n=IN()
a=INL()
b=INL()
solve()
```
No
| 105,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Tags: combinatorics, math
Correct Solution:
```
p = 998244353
def power(x, y, p):
b = bin(y)[2:]
start = x
answer = 1
for i in range(len(b)):
if b[len(b)-1-i]=='1':
answer = (answer*start) % p
start = (start*start) % p
return answer
def get_seq(S):
seq = []
n = len(S)
for i in range(n):
c = S[i]
if c=='0':
seq.append(i % 2)
return seq
def process1(seq, my_max):
n = len(seq)
if n==0:
return 1
maybe2 = 0
if seq[-1]==1 and my_max % 2==0:
my_count = my_max//2
else:
my_count = my_max//2+1
for i in range(1, n):
if seq[i-1]==0 and seq[i]==1:
maybe2+=1
# print(seq, my_max, maybe2, my_count)
num1 = my_count+maybe2
den1 = n
#print(num1, den1)
if num1 < den1:
return 0
num = 1
den = 1
for i in range(den1):
num = (num*num1) % p
den = (den*den1) % p
num1-=1
den1-=1
den2 = power(den, p-2, p)
return (num*den2) % p
def process(S):
n = len(S)
seq = get_seq(S)
return process1(seq, n-1)
t = int(input())
for i in range(t):
n = int(input())
S = input()
print(process(S))
```
| 105,510 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Tags: combinatorics, math
Correct Solution:
```
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
t = int(input())
for _ in range(t):
n = int(input())
str = input()
d = str.count("11")
x = str.count('0')
ans = ncr(d+x,d,998244353)
print(ans)
```
| 105,511 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Tags: combinatorics, math
Correct Solution:
```
#######puzzleVerma#######
import sys
import math
mod = 10**9+7
modd= 998244353
LI=lambda:[int(k) for k in input().split()]
input = lambda: sys.stdin.readline().rstrip()
IN=lambda:int(input())
S=lambda:input()
r=range
# def pow(x, y, p):
# res = 1
# x = x % p
# if (x == 0):
# return 0
# while (y > 0):
# if ((y & 1) == 1):
# res = (res * x) % p
# y = y >> 1
# x = (x * x) % p
# return res
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,p - 2, p)) % p
for t in r(IN()):
n=IN()
s=S()
once=False
zero=0
oneone=0
for ele in s:
if ele=="1":
if once:
oneone+=1
once=False
else:
once=True
else:
zero+=1
once=False
print(ncr(oneone+zero,oneone,modd))
```
| 105,512 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Tags: combinatorics, math
Correct Solution:
```
# _ _ _
# ___ ___ __| | ___ __| |_ __ ___ __ _ _ __ ___ ___ _ __ __| | __ _
# / __/ _ \ / _` |/ _ \/ _` | '__/ _ \/ _` | '_ ` _ \ / _ \ '__|/ _` |/ _` |
# | (_| (_) | (_| | __/ (_| | | | __/ (_| | | | | | | __/ | | (_| | (_| |
# \___\___/ \__,_|\___|\__,_|_| \___|\__,_|_| |_| |_|\___|_|___\__,_|\__, |
# |_____| |___/
from sys import *
'''sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w') '''
from collections import defaultdict as dd
from math import gcd
from bisect import *
#sys.setrecursionlimit(10 ** 8)
def sinp():
return stdin.readline()
def inp():
return int(stdin.readline())
def minp():
return map(int, stdin.readline().split())
def linp():
return list(map(int, stdin.readline().split()))
def strl():
return list(sinp())
def pr(x):
print(x)
mod = int(1e9+7)
mod = 998244353
def solve(n, r):
if n < r:
return 0
if not r:
return 1
return (pre_process[n] * pow(pre_process[r], mod - 2, mod) % mod * pow(pre_process[n - r], mod - 2,mod) % mod) % mod
pre_process = [1 for i in range(int(1e5+6) + 1)]
for i in range(1, int(1e5+7)):
pre_process[i] = (pre_process[i - 1] * i) % mod
for _ in range(inp()):
n = inp()
s = sinp()
cnt = 0
visited = [0 for i in range(n)]
for i in range(n):
if int(s[i]) == 0:
cnt += 1
cnt_val = 0
for i in range(1, n):
if int(s[i]) == int(s[i - 1]) == 1 and not visited[i - 1]:
cnt_val += 1
visited[i] = 1
pr(solve(cnt + cnt_val, cnt_val))
```
| 105,513 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Tags: combinatorics, math
Correct Solution:
```
import sys,os,io
input = sys.stdin.readline
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
for _ in range (int(input())):
n = int(input())
a = [int(i) for i in input().strip()]
o = [0]
z = [0]
for i in range (n):
if a[i]==1:
if z[-1]!=0:
z.append(0)
o[-1]+=1
else:
if o[-1]!=0:
o.append(0)
z[-1]+=1
for i in range (len(o)):
o[i] -= o[i]%2
ones = sum(o)//2
zeroes = sum(z)
mod = 998244353
ans = ncr(zeroes+ones, ones, mod)
print(ans)
```
| 105,514 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Tags: combinatorics, math
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
import io
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
from collections import Counter
import threading
#sys.setrecursionlimit(300000)
#threading.stack_size(10**8)
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")
#-------------------game starts now-----------------------------------------------------
#mod = 9223372036854775807
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: max(a,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
class SegmentTree1:
def __init__(self, data, default=0, func=lambda a, b: a+b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
MOD=10**9+7
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod=10**9+7
omod=998244353
#-------------------------------------------------------------------------
prime = [True for i in range(10001)]
prime[0]=prime[1]=False
#pp=[0]*10000
def SieveOfEratosthenes(n=10000):
p = 2
c=0
while (p <= n):
if (prime[p] == True):
c+=1
for i in range(p, n+1, p):
#pp[i]=1
prime[i] = False
p += 1
#-----------------------------------DSU--------------------------------------------------
class DSU:
def __init__(self, R, C):
#R * C is the source, and isn't a grid square
self.par = range(R*C + 1)
self.rnk = [0] * (R*C + 1)
self.sz = [1] * (R*C + 1)
def find(self, x):
if self.par[x] != x:
self.par[x] = self.find(self.par[x])
return self.par[x]
def union(self, x, y):
xr, yr = self.find(x), self.find(y)
if xr == yr: return
if self.rnk[xr] < self.rnk[yr]:
xr, yr = yr, xr
if self.rnk[xr] == self.rnk[yr]:
self.rnk[xr] += 1
self.par[yr] = xr
self.sz[xr] += self.sz[yr]
def size(self, x):
return self.sz[self.find(x)]
def top(self):
# Size of component at ephemeral "source" node at index R*C,
# minus 1 to not count the source itself in the size
return self.size(len(self.sz) - 1) - 1
#---------------------------------Lazy Segment Tree--------------------------------------
# https://github.com/atcoder/ac-library/blob/master/atcoder/lazysegtree.hpp
class LazySegTree:
def __init__(self, _op, _e, _mapping, _composition, _id, v):
def set(p, x):
assert 0 <= p < _n
p += _size
for i in range(_log, 0, -1):
_push(p >> i)
_d[p] = x
for i in range(1, _log + 1):
_update(p >> i)
def get(p):
assert 0 <= p < _n
p += _size
for i in range(_log, 0, -1):
_push(p >> i)
return _d[p]
def prod(l, r):
assert 0 <= l <= r <= _n
if l == r:
return _e
l += _size
r += _size
for i in range(_log, 0, -1):
if ((l >> i) << i) != l:
_push(l >> i)
if ((r >> i) << i) != r:
_push(r >> i)
sml = _e
smr = _e
while l < r:
if l & 1:
sml = _op(sml, _d[l])
l += 1
if r & 1:
r -= 1
smr = _op(_d[r], smr)
l >>= 1
r >>= 1
return _op(sml, smr)
def apply(l, r, f):
assert 0 <= l <= r <= _n
if l == r:
return
l += _size
r += _size
for i in range(_log, 0, -1):
if ((l >> i) << i) != l:
_push(l >> i)
if ((r >> i) << i) != r:
_push((r - 1) >> i)
l2 = l
r2 = r
while l < r:
if l & 1:
_all_apply(l, f)
l += 1
if r & 1:
r -= 1
_all_apply(r, f)
l >>= 1
r >>= 1
l = l2
r = r2
for i in range(1, _log + 1):
if ((l >> i) << i) != l:
_update(l >> i)
if ((r >> i) << i) != r:
_update((r - 1) >> i)
def _update(k):
_d[k] = _op(_d[2 * k], _d[2 * k + 1])
def _all_apply(k, f):
_d[k] = _mapping(f, _d[k])
if k < _size:
_lz[k] = _composition(f, _lz[k])
def _push(k):
_all_apply(2 * k, _lz[k])
_all_apply(2 * k + 1, _lz[k])
_lz[k] = _id
_n = len(v)
_log = _n.bit_length()
_size = 1 << _log
_d = [_e] * (2 * _size)
_lz = [_id] * _size
for i in range(_n):
_d[_size + i] = v[i]
for i in range(_size - 1, 0, -1):
_update(i)
self.set = set
self.get = get
self.prod = prod
self.apply = apply
MIL = 1 << 20
def makeNode(total, count):
# Pack a pair into a float
return (total * MIL) + count
def getTotal(node):
return math.floor(node / MIL)
def getCount(node):
return node - getTotal(node) * MIL
nodeIdentity = makeNode(0.0, 0.0)
def nodeOp(node1, node2):
return node1 + node2
# Equivalent to the following:
return makeNode(
getTotal(node1) + getTotal(node2), getCount(node1) + getCount(node2)
)
identityMapping = -1
def mapping(tag, node):
if tag == identityMapping:
return node
# If assigned, new total is the number assigned times count
count = getCount(node)
return makeNode(tag * count, count)
def composition(mapping1, mapping2):
# If assigned multiple times, take first non-identity assignment
return mapping1 if mapping1 != identityMapping else mapping2
#---------------------------------Pollard rho--------------------------------------------
def memodict(f):
"""memoization decorator for a function taking a single argument"""
class memodict(dict):
def __missing__(self, key):
ret = self[key] = f(key)
return ret
return memodict().__getitem__
def pollard_rho(n):
"""returns a random factor of n"""
if n & 1 == 0:
return 2
if n % 3 == 0:
return 3
s = ((n - 1) & (1 - n)).bit_length() - 1
d = n >> s
for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
p = pow(a, d, n)
if p == 1 or p == n - 1 or a % n == 0:
continue
for _ in range(s):
prev = p
p = (p * p) % n
if p == 1:
return math.gcd(prev - 1, n)
if p == n - 1:
break
else:
for i in range(2, n):
x, y = i, (i * i + 1) % n
f = math.gcd(abs(x - y), n)
while f == 1:
x, y = (x * x + 1) % n, (y * y + 1) % n
y = (y * y + 1) % n
f = math.gcd(abs(x - y), n)
if f != n:
return f
return n
@memodict
def prime_factors(n):
"""returns a Counter of the prime factorization of n"""
if n <= 1:
return Counter()
f = pollard_rho(n)
return Counter([n]) if f == n else prime_factors(f) + prime_factors(n // f)
def distinct_factors(n):
"""returns a list of all distinct factors of n"""
factors = [1]
for p, exp in prime_factors(n).items():
factors += [p**i * factor for factor in factors for i in range(1, exp + 1)]
return factors
def all_factors(n):
"""returns a sorted list of all distinct factors of n"""
small, large = [], []
for i in range(1, int(n**0.5) + 1, 2 if n & 1 else 1):
if not n % i:
small.append(i)
large.append(n // i)
if small[-1] == large[-1]:
large.pop()
large.reverse()
small.extend(large)
return small
#---------------------------------Binary Search------------------------------------------
def binarySearch(arr, n,i, key):
left = 0
right = n-1
mid = 0
res=n
while (left <= right):
mid = (right + left)//2
if (arr[mid][i] > key):
res=mid
right = mid-1
else:
left = mid + 1
return res
def binarySearch1(arr, n,i, key):
left = 0
right = n-1
mid = 0
res=-1
while (left <= right):
mid = (right + left)//2
if (arr[mid][i] > key):
right = mid-1
else:
res=mid
left = mid + 1
return res
#---------------------------------running code------------------------------------------
t=1
t=int(input())
for _ in range (t):
n=int(input())
#n,m=map(int,input().split())
#a=list(map(int,input().split()))
#tp=list(map(int,input().split()))
s=input()
groups=0
zeros=0
ones=0
for i in s:
if i=='0':
groups+=ones//2
ones=0
zeros+=1
else:
ones+=1
groups+=ones//2
S=Combination(omod)
print(S.ncr(groups+zeros, groups))
```
| 105,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Tags: combinatorics, math
Correct Solution:
```
import os, sys
from io import BytesIO, IOBase
from math import log2, ceil, sqrt, gcd
from _collections import deque
import heapq as hp
from bisect import bisect_left, bisect_right
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")
mod = 998244353
MX = 10**5+1
fact = [1]
for i in range(1, MX):
fact.append(fact[-1] * i % mod)
inv = [pow(fact[i], mod - 2, mod) for i in range(MX)]
for _ in range(int(input())):
n=int(input())
ar=input()
a=ar.count('0')
b=0
i=0
while i<n:
if ar[i]=='1':
if i+1<n and ar[i+1]=='1':
b+=1
i+=2
else:
i+=1
else:
i+=1
if b==0:
print(1)
continue
ans=fact[a+b]
ans*=inv[a]
ans%=mod
ans*=inv[b]
ans%=mod
print(ans)
```
| 105,516 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Tags: combinatorics, math
Correct Solution:
```
mod = 998244353
import sys
input = sys.stdin.readline
def ncr(n, r):
return ((fact[n]*pow(fact[r]*fact[n-r], mod-2, mod))%mod)
fact = [1, 1]
for i in range(2, 100010):
fact.append((fact[-1]*i)%mod)
for nt in range(int(input())):
n = int(input())
s = input()
i = 0
o, z = 0, 0
while i<n:
if s[i]=="0":
z += 1
i += 1
elif s[i]=="1" and s[i+1]=="1":
o += 1
i += 2
else:
i += 1
# print (z+o, o)
print (ncr(z+o, o))
```
| 105,517 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Submitted Solution:
```
'''input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
'''
from sys import stdin, stdout
from collections import defaultdict
def combination(a, b):
return (((factorial[a] * inverseFact[b]) % mod) * inverseFact[a - b]) % mod
mod = 998244353
def inverse(num, mod):
if num == 1:
return 1
return inverse(mod % num, mod) * (mod - mod//num) % mod
factorial = [1 for x in range(10** 5 + 1)]
inverseFact = [1 for x in range(10** 5 + 1)]
for i in range(2,len(factorial)):
factorial[i] = (i * factorial[i - 1]) % mod
inverseFact[i] = (inverse(i, mod) * inverseFact[i - 1]) % mod
# main starts
t = int(stdin.readline().strip())
for _ in range(t):
n = int(stdin.readline().strip())
string = stdin.readline().strip()
onePair = 0
zero = 0
i = 0
while i < n:
if string[i] == '1':
if i + 1 < n and string[i + 1] == '1':
onePair += 1
i += 2
else:
i += 1
else:
zero += 1
i += 1
print(combination(onePair + zero, onePair))
```
Yes
| 105,518 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Submitted Solution:
```
import sys
input=sys.stdin.readline
max_n=10**5
fact, inv_fact = [0] * (max_n+1), [0] * (max_n+1)
fact[0] = 1
mod=998244353
def make_nCr_mod():
global fact
global inv_fact
for i in range(max_n):
fact[i + 1] = fact[i] * (i + 1) % mod
inv_fact[-1] = pow(fact[-1], mod - 2, mod)
for i in reversed(range(max_n)):
inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod
make_nCr_mod()
def nCr_mod(n, r):
global fact
global inv_fact
res = 1
while n or r:
a, b = n % mod, r % mod
if(a < b):
return 0
res = res * fact[a] % mod * inv_fact[b] % mod * inv_fact[a - b] % mod
n //= mod
r //= mod
return res
t=int(input())
for _ in range(t):
length=int(input())
l=input().rstrip()
r=0
tmp=1
for i in range(1,length):
if(l[i]=='0'):
continue
if(l[i]==l[i-1]=='1'):
tmp+=1
else:
r+=(tmp//2)
tmp=1
r+=(tmp//2)
n=l.count('0')
print(nCr_mod(n+r,r))
```
Yes
| 105,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Submitted Solution:
```
N = 100001
factorialNumInverse = [None] * (N + 1)
naturalNumInverse = [None] * (N + 1)
fact = [None] * (N + 1)
def InverseofNumber(p):
naturalNumInverse[0] = naturalNumInverse[1] = 1
for i in range(2, N + 1, 1):
naturalNumInverse[i] = (naturalNumInverse[p % i] *
(p - int(p / i)) % p)
def InverseofFactorial(p):
factorialNumInverse[0] = factorialNumInverse[1] = 1
for i in range(2, N + 1, 1):
factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p
def factorial(p):
fact[0] = 1
for i in range(1, N + 1):
fact[i] = (fact[i - 1] * i) % p
def Binomial(N, R, p):
ans = ((fact[N] * factorialNumInverse[R])% p * factorialNumInverse[N - R])% p
return ans
p = 998244353
InverseofNumber(p)
InverseofFactorial(p)
factorial(p)
t=int(input())
while t:
t-=1
n=int(input())
s=input()
i=a=b=0
while i<n:
cnt1=0
while i<n and s[i]=='0':
cnt1+=1
i+=1
a+=cnt1
cnt2=0
while i<n and s[i]=='1':
cnt2+=1
i+=1
b+=(cnt2//2)
print(Binomial(a+b,b,p))
```
Yes
| 105,520 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Submitted Solution:
```
import sys,os,io
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
input = sys.stdin.readline
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
for _ in range (int(input())):
n = int(input())
s = list(input().strip())
z = s.count('0')
o = 0
co = 0
for i in range (n):
if s[i]=='1':
co+=1
else:
o+=co//2
co=0
o+=co//2
mod = 998244353
print(ncr(z+o, o, mod))
```
Yes
| 105,521 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Submitted Solution:
```
from sys import stdout
from math import comb as c
def solve():
n = int(input())
a = list(map(int, input()))
pair = 0
zeros = 0
cur = 0
for i in range(n):
if a[i] == 1:
cur += 1
else:
pair += cur//2
zeros += 1
cur = 0
pair += cur//2
print(c(pair+zeros, zeros))
stdout.flush()
def main():
for _ in range(int(input())):
solve()
if __name__ == '__main__':
main()
```
No
| 105,522 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Submitted Solution:
```
mod = 10**9+7
import sys
input = sys.stdin.readline
def ncr(n, r):
return ((fact[n]*pow(fact[r]*fact[n-r], mod-2, mod))%mod)
fact = [1, 1]
for i in range(2, 100010):
fact.append((fact[-1]*i)%mod)
for nt in range(int(input())):
n = int(input())
s = input()
i = 0
o, z = 0, 0
while i<n:
if s[i]=="0":
z += 1
i += 1
elif s[i]=="1" and s[i+1]=="1":
o += 1
i += 2
else:
i += 1
print (ncr(z+o, o))
```
No
| 105,523 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Submitted Solution:
```
import math
inf = 998244353
import sys
def mult(a,b):
stuff=1
for i in range(a,b+1):
stuff=(stuff*i)%inf
return stuff
input = sys.stdin.readline
for _ in range(int(input())):
le = int(input())
s = list(map(str,input().strip()))
twos = 0
zero = 0
temp = None
for i in s:
if i == '1' and temp == None:
temp=1
elif i == '1' and temp == 1:
temp=None
twos+=1
else:
temp = None
zero+=1
print((mult(max(zero,twos)+1,zero+twos)//mult(1,min(zero,twos)))%inf)
```
No
| 105,524 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Cirno gave AquaMoon a chessboard of size 1 Γ n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 β€ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 β₯ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 10 000) β the number of test cases.
The first line contains a single integer n (1 β€ n β€ 10^5) β the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Submitted Solution:
```
from math import factorial
def ncr(n, r):
cnt = 1
for i in range(r):
cnt = (cnt * (n-r+1+i)) % 998244353
# print(n, r, cnt, cnt/factorial(r))
return (cnt // factorial(r) % 998244353) % 998244353
for t in range(int(input())):
n = int(input())
a = input()
oc = 0
i = 0
while i < n-1:
if a[i] == '1' and a[i+1] == '1':
oc += 1
i += 1
i += 1
zc = a.count('0')
print(ncr(oc+zc, min(oc, zc)) % 998244353)
```
No
| 105,525 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Tags: implementation, math
Correct Solution:
```
n,k=input().split(" ")
n=int(n)
k=int(k)
if 3*n>k:
print((3*n)-k)
else:
print(0)
```
| 105,526 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Tags: implementation, math
Correct Solution:
```
z=input
mod = 10**9 + 7
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
def lcd(xnum1,xnum2):
return (xnum1*xnum2//gcd(xnum1,xnum2))
################################################################################
"""
n=int(z())
for _ in range(int(z())):
x=int(z())
l=list(map(int,z().split()))
n=int(z())
l=sorted(list(map(int,z().split())))[::-1]
a,b=map(int,z().split())
l=set(map(int,z().split()))
led=(6,2,5,5,4,5,6,3,7,6)
vowel={'a':0,'e':0,'i':0,'o':0,'u':0}
color-4=["G", "GB", "YGB", "YGBI", "OYGBI" ,"OYGBIV",'ROYGBIV' ]
"""
###########################---START-CODING---###############################################
for _ in range(1):
n,k=map(int,input().split())
if 3*n<=k:
print(0)
else:print(3*n-k)
```
| 105,527 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Tags: implementation, math
Correct Solution:
```
n,k=map(int,input().split())
k=k-2*n
print(max(n-k,0))
```
| 105,528 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Tags: implementation, math
Correct Solution:
```
n,k=[int(x) for x in input().split()]
if k//n>2:
print(0)
else:
print(n-(k%n))
```
| 105,529 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Tags: implementation, math
Correct Solution:
```
def readln(inp=None): return tuple(map(int, (inp or input()).split()))
n, k = readln()
ans = 100000
for c5 in range(n + 1):
for c4 in range(n - c5 + 1):
for c3 in range(n - c5 - c4 + 1):
c2 = n - c3 - c4 - c5
if c2 >= 0 and 2 * c2 + 3 * c3 + 4 * c4 + 5 * c5 == k and c2 < ans:
ans = c2
print(ans)
```
| 105,530 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Tags: implementation, math
Correct Solution:
```
n, k = list(map(int, input().split()))
a = k - 2*n
if a >= n:
print(0)
else:
print(n - a)
```
| 105,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Tags: implementation, math
Correct Solution:
```
n,k = map(int,input().split())
if k/n >= 3:
print(0)
else:
c = 0
while k%n != 0:
k -= 1
c += 1
print(n-c)
```
| 105,532 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Tags: implementation, math
Correct Solution:
```
n, k = map(int, input().split())
print(n*3-k if k <= n * 3 else 0)
```
| 105,533 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Submitted Solution:
```
nk = input().split(' ')
n = int(nk[0])
k = int(nk[1])
for a in range(0,n+1):
for b in range(n-a+1):
for c in range(n-b+1):
for d in range(n-c+1):
sum = ((a*2) + (b*3) + (c * 4) + (d * 5))
if sum == k:
# d = d / 5
if a + b + c + d == n:
print(a)
exit()
```
Yes
| 105,534 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Submitted Solution:
```
## necessary imports
import sys
input = sys.stdin.readline
# import random
from math import log2, log, ceil
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp
## gcd function
def gcd(a,b):
if a == 0:
return b
return gcd(b%a, a)
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b
# find function
def find(x, link):
while(x != link[x]):
x = link[x]
return x
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, size, link):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e6 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
# spf = [0 for i in range(MAXN)]
# spf_sieve()
def factoriazation(x):
ret = {};
while x != 1:
ret[spf[x]] = ret.get(spf[x], 0) + 1;
x = x//spf[x]
return ret
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
## taking integer array input
def int_array():
return list(map(int, input().strip().split()))
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################# ---------------- TEMPLATE ENDS HERE ---------------- #################
n, k = int_array();
if k//n > 2:
print(0);
else:
print(n - k%n);
```
Yes
| 105,535 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Submitted Solution:
```
n, k=map(int, input().split())
if k<(3*n):
print(3*n-k)
else:
print(0)
```
Yes
| 105,536 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Submitted Solution:
```
n, k = input().split()
n = int(n)
k = int(k)
l = n*2
m = k - l
if m == 0:
print(n)
elif n >= m > 0:
print(n - m)
elif m > n:
print(0)
```
Yes
| 105,537 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Submitted Solution:
```
n, k = map(int, input().split())
m = 2*n
r = k-m
while r!=0:
n = n-1
r = r-1
print(n)
```
No
| 105,538 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Submitted Solution:
```
nk=input().split()
n=int(nk[0])
k=int(nk[1])
if(k==2*n):
print(n)
elif(k<2*n):
print(k//2)
else:
if(k>=3*n):
print(0)
else:
m=k-2*n
print(m)
```
No
| 105,539 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Submitted Solution:
```
n,k=map(int,input().split())
if k//n>2:
print(0)
elif k/n==2:
print(k//n)
elif k//n<2:
print(n)
else:
i=1
r=0
while(True):
r+=2
if (k-r)%3==0 and (k-r)/3==n-i:
print(i)
break
if r>=k:
print(i)
break
i+=1
```
No
| 105,540 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day the Codeforces round author sat exams. He had n exams and he needed to get an integer from 2 to 5 for each exam. He will have to re-sit each failed exam, i.e. the exam that gets mark 2.
The author would need to spend too much time and effort to make the sum of his marks strictly more than k. That could have spoilt the Codeforces round. On the other hand, if the sum of his marks is strictly less than k, the author's mum won't be pleased at all.
The Codeforces authors are very smart and they always get the mark they choose themselves. Also, the Codeforces authors just hate re-sitting exams.
Help the author and find the minimum number of exams he will have to re-sit if he passes the exams in the way that makes the sum of marks for all n exams equal exactly k.
Input
The single input line contains space-separated integers n and k (1 β€ n β€ 50, 1 β€ k β€ 250) β the number of exams and the required sum of marks.
It is guaranteed that there exists a way to pass n exams in the way that makes the sum of marks equal exactly k.
Output
Print the single number β the minimum number of exams that the author will get a 2 for, considering that the sum of marks for all exams must equal k.
Examples
Input
4 8
Output
4
Input
4 10
Output
2
Input
1 3
Output
0
Note
In the first sample the author has to get a 2 for all his exams.
In the second sample he should get a 3 for two exams and a 2 for two more.
In the third sample he should get a 3 for one exam.
Submitted Solution:
```
n,k= map(int, input().split())
if n==1:
print(0)
exit()
if n*2==k:
print(n)
exit()
if 3*n>k:
print(3*n-k)
elif k>=3*n:
print(0)
```
No
| 105,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
if __name__ == '__main__':
n,k = map(int,input().split())
l = list(map(int,input().split()))
for i in range(1,len(l),2):
if l[i]-1 > l[i-1] and l[i]-1 > l[i+1]:
l[i] -= 1
k -= 1
if k == 0:
break
print(*l,sep=" ")
```
| 105,542 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
"""609C"""
# import math
def main():
n,k = map(int,input().split())
a = list( map(int,input().split()) )
for i in range(len(a)-2):
# print(i)
# print(a)
if a[i+1]-1>a[i] and a[i+1]-1>a[i+2]:
a[i+1]-=1
k-=1
if k==0:
# print("hello")
break
for x in a:
print(x,end=' ')
print()
main()
# t= int(input())
# while t:
# main()
# t-=1
```
| 105,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
a, b = map(int,input().split())
ans = 0
l = list(map(int,input().split()))
for i in range(b):
for i in range(1 ,len(l ) - 1):
if ans == b:
print(*l)
exit()
if l[i] > l[i + 1] and l[i] > l[i - 1] :
if l[i] - 1 <= l[i -1 ] or l[i] -1 <= l[i + 1]:
pass
else:
l[i] -= 1
ans += 1
print(*l)
```
| 105,544 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
n, k = map(int, input().split())
points = list(map(int, input().split()))
c = 0
i = 1
while c < k:
if points[i-1]<(points[i]-1) and (points[i]-1)>points[i+1]:
points[i]-=1
c+=1
#print(c)
i+=2
print(" ".join(list(map(str, points))))
```
| 105,545 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 24 12:40:45 2020
@author: lalo
"""
n,x=[int(x) for x in input().split()]
l=[int(x) for x in input().split()]
a=0
print(l[0],end=' ')
for i in range(1,len(l)-1):
k=l[i]-1
if (k>l[i-1] and k>l[i+1] and a<x):
print(k,end=' ')
a+=1
else:
print(l[i],end=' ')
print(l[len(l)-1],end=' ')
```
| 105,546 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
n, k = map(int, input().split())
l = list(map(int, input().split()))
j = 0
for i in range(1, 2 * n + 1, 2):
if l[i] > (l[i-1] + 1) and l[i] > (l[i+1] + 1):
l[i] -= 1
j += 1
if j == k:
break
print(" ".join(map(str, l)))
```
| 105,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
#!/usr/bin/env python3
n, k = map(int,input().split())
y_points = list(map(int,input().split()))
k_tot=0
last_odd = len(y_points)-2 if (len(y_points)-1)%2==0 else len(y_points)-1
for i in range(last_odd,0,-2):
if k_tot<k:
if (y_points[i]-1)>y_points[i+1] and (y_points[i]-1)>y_points[i-1]:
k_tot += 1
y_points[i]-=1
print(" ".join(map(str,y_points)))
```
| 105,548 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Tags: brute force, constructive algorithms, implementation
Correct Solution:
```
def arr_inp():
return [int(x) for x in input().split()]
def print_arr(arr):
print(*arr, sep=' ')
n, k = map(int, input().split())
y = arr_inp()
for i in range(1, n * 2, 2):
if (k == 0):
break
if (y[i] - y[i - 1] > 1 and y[i] - y[i + 1] > 1):
y[i] -= 1
k -= 1
print_arr(y)
```
| 105,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Submitted Solution:
```
n,k=input().split()
n=int(n)
k=int(k)
arr=list(map(int,input().split()))
count=0
for index in range(1,(2*n)+1,2):
if arr[index]-1>arr[index-1] and arr[index]-1>arr[index+1] and count<k:
arr[index]=arr[index]-1
count=count+1
print(*arr)
```
Yes
| 105,550 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Submitted Solution:
```
nk = input().strip().split(' ')
n = int(nk[0])
k = int(nk[1])
pks = input().strip().split(' ')
pks = [int(p) for p in pks]
for i in range(1, 2*n, 2):
if pks[i]-1>pks[i-1] and pks[i]-1>pks[i+1]:
pks[i]-= 1
k -= 1
if k==0:
break
for i in range(2*n+1):
print(pks[i], end=' ')
```
Yes
| 105,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Submitted Solution:
```
def main():
n, k = map(int, input().split())
r = list(map(int, input().split()))
for i in range(1, n * 2, 2):
if k and r[i - 1] < r[i] - 1 > r[i + 1]:
r[i] -= 1
k -= 1
print(*r)
if __name__ == '__main__':
main()
```
Yes
| 105,552 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Submitted Solution:
```
n, k = [int(i) for i in input().split()]
points = [int(i) for i in input().split()]
peaks = {}
for i in range(2 * n - 1):
if points[i] < points[i + 1] and points[i + 1] > points[i + 2]:
if points[i] < points[i + 1] - 1 and points[i + 1] - 1 > points[i + 2]:
peaks[i + 1] = points[i + 1] - 1
high = list(peaks.keys())
selected = high[:k]
for i in range(len(points)):
if i in selected:
print(peaks[i], end=" ")
else:
print(points[i], end=" ")
```
Yes
| 105,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Submitted Solution:
```
n , k = map(int , input().split())
l = list(map (int , input().split()))
i = 1
while k:
if l[i-1] < l[i]-1 and l[i+1] < l[i]-1 :
l[i]-=1
k-=1
i+=2
print(*l)
```
No
| 105,554 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Submitted Solution:
```
_, k = map(int, input().strip().split())
ys = [*map(int, input().strip().split())]
new_ys = [y - 1 if k and i >= 2 and not i % 2 and ys[i-1] < y - 1 and (k := k - 1) else y for i, y in enumerate(ys, 1) ]
print(' '.join(str(y) for y in new_ys))
```
No
| 105,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Submitted Solution:
```
n,k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(2,2*n):
if (i+1)%2!=0:
continue
if k==0:
break
if a[i]>a[i-1] and a[i]>a[i+1]:
a[i]-=1
k-=1
print(*a)
```
No
| 105,556 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 β€ i β€ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.
We shall call a vertex of a polyline with an even x coordinate a mountain peak.
<image> The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.
Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).
Given Bolek's final picture, restore the initial one.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 β€ ri β€ 100) β the y coordinates of the polyline vertices on Bolek's picture.
It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.
Output
Print 2n + 1 integers y1, y2, ..., y2n + 1 β the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.
Examples
Input
3 2
0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2
Input
1 1
0 2 0
Output
0 1 0
Submitted Solution:
```
def capitalization(inpu):
m=inpu[0].upper()
return m+inpu[1:len(inpu)]
u=input()
print(capitalization(u))
```
No
| 105,557 |
Provide tags and a correct Python 3 solution for this coding contest problem.
LiLand is a country, consisting of n cities. The cities are numbered from 1 to n. The country is well known because it has a very strange transportation system. There are many one-way flights that make it possible to travel between the cities, but the flights are arranged in a way that once you leave a city you will never be able to return to that city again.
Previously each flight took exactly one hour, but recently Lily has become the new manager of transportation system and she wants to change the duration of some flights. Specifically, she wants to change the duration of some flights to exactly 2 hours in such a way that all trips from city 1 to city n take the same time regardless of their path.
Your task is to help Lily to change the duration of flights.
Input
First line of the input contains two integer numbers n and m (2 β€ n β€ 1000; 1 β€ m β€ 5000) specifying the number of cities and the number of flights.
Each of the next m lines contains two integers ai and bi (1 β€ ai < bi β€ n) specifying a one-directional flight from city ai to city bi. It is guaranteed that there exists a way to travel from city number 1 to city number n using the given flights. It is guaranteed that there is no sequence of flights that forms a cyclical path and no two flights are between the same pair of cities.
Output
If it is impossible for Lily to do her task, print "No" (without quotes) on the only line of the output.
Otherwise print "Yes" (without quotes) on the first line of output, then print an integer ansi (1 β€ ansi β€ 2) to each of the next m lines being the duration of flights in new transportation system. You should print these numbers in the order that flights are given in the input.
If there are multiple solutions for the input, output any of them.
Examples
Input
3 3
1 2
2 3
1 3
Output
Yes
1
1
2
Input
4 4
1 2
2 3
3 4
1 4
Output
No
Input
5 6
1 2
2 3
3 5
1 4
4 5
1 3
Output
Yes
1
1
1
2
1
2
Tags: graphs, shortest paths
Correct Solution:
```
#Flights
from queue import Queue
def BFS(node ,mk,G):
q = Queue()
q.put(node)
mk[node] = True
while not q.empty():
top = q.get()
for ady in G[top]:
if not mk[ady]:
mk[ady] = True
q.put(ady)
def InitValue(n,m):
G1 = [[]for _ in range(n+1)]
G2 = [[]for _ in range(n+1)]
E = []
for _ in range(m):
u,v = map(int,input().split())
G1[u].append(v)
G2[v].append(u)
E.append((u,v))
return G1,G2,E
def Cleen_Graf(mk1,mk2,n,m,G1,G2):
E2 = []
for e in E:
if mk1[e[0]] and mk2[e[0]] and mk1[e[1]] and mk2[e[1]]:
E2.append((e[0],e[1],2))
E2.append((e[1],e[0],-1))
return E2
def Bellman_Ford(n,E2):
dist = [10e12 for _ in range(n+1)]
dist[1] = 0
is_posibol = True
for _ in range(n):
for e in E2:
dist[e[1]] = min(dist[e[1]],dist[e[0]]+e[2])
for e in E2:
if dist[e[0]]+e[2] < dist[e[1]]:
is_posibol = False
return dist,is_posibol
if __name__ == '__main__':
n,m = map(int,input().split())
G1,G2,E = InitValue(n,m)
mk1 = [False for _ in range(n+1)]
mk2 = [False for _ in range(n+1)]
BFS(1,mk1,G1)
BFS(n,mk2,G2)
E2 = Cleen_Graf(mk1,mk2,n,m,G1,G2)
dist,is_posibol = Bellman_Ford(n,E2)
if not is_posibol:
print("No")
else:
print("Yes")
for e in E:
if mk1[e[0]] and mk2[e[0]] and mk1[e[1]] and mk2[e[1]]:
print(dist[e[1]]- dist[e[0]])
else:
print(1)
```
| 105,558 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A motorcade of n trucks, driving from city Β«ZΒ» to city Β«ΠΒ», has approached a tunnel, known as Tunnel of Horror. Among truck drivers there were rumours about monster DravDe, who hunts for drivers in that tunnel. Some drivers fear to go first, others - to be the last, but let's consider the general case. Each truck is described with four numbers:
* v β value of the truck, of its passangers and cargo
* c β amount of passanger on the truck, the driver included
* l β total amount of people that should go into the tunnel before this truck, so that the driver can overcome his fear (Β«if the monster appears in front of the motorcade, he'll eat them firstΒ»)
* r β total amount of people that should follow this truck, so that the driver can overcome his fear (Β«if the monster appears behind the motorcade, he'll eat them firstΒ»).
Since the road is narrow, it's impossible to escape DravDe, if he appears from one side. Moreover, the motorcade can't be rearranged. The order of the trucks can't be changed, but it's possible to take any truck out of the motorcade, and leave it near the tunnel for an indefinite period. You, as the head of the motorcade, should remove some of the trucks so, that the rest of the motorcade can move into the tunnel and the total amount of the left trucks' values is maximal.
Input
The first input line contains integer number n (1 β€ n β€ 105) β amount of trucks in the motorcade. The following n lines contain four integers each. Numbers in the i-th line: vi, ci, li, ri (1 β€ vi β€ 104, 1 β€ ci β€ 105, 0 β€ li, ri β€ 105) β describe the i-th truck. The trucks are numbered from 1, counting from the front of the motorcade.
Output
In the first line output number k β amount of trucks that will drive into the tunnel. In the second line output k numbers β indexes of these trucks in ascending order. Don't forget please that you are not allowed to change the order of trucks. If the answer is not unique, output any.
Examples
Input
5
1 1 0 3
1 1 1 2
1 1 2 1
1 1 3 0
2 1 3 0
Output
4
1 2 3 5
Input
5
1 1 0 3
10 1 2 1
2 2 1 1
10 1 1 2
3 1 3 0
Output
3
1 3 5
Tags: binary search, data structures, dp, hashing
Correct Solution:
```
n = int(input())
v = n*[-1]
c = n*[-1]
L = n*[-1]
R = n*[-1]
for i in range(n):
v[i], c[i], L[i], R[i] = map(int, input().split())
index = [i for i in range(n)]
index.sort(key = lambda i: (c[i]+L[i]+R[i], i))
prev = n*[-1]
best_res = 0
best_last = -1
ii = 0
while ii < n:
i = index[ii]
jj = ii
d = {0: (0, -1)}
while jj < n:
j = index[jj]
if c[i]+L[i]+R[i] != c[j]+L[j]+R[j]: break
x = d.get(L[j])
if x is not None:
cur = v[j]+x[0]
prev[j] = x[1]
if R[j] == 0 and cur > best_res:
best_res = cur
best_last = j
y = d.get(L[j]+c[j])
if y is None or cur > y[0]:
d[L[j]+c[j]] = (cur, j)
jj += 1
ii = jj
ans = []
while best_last != -1:
ans.append(best_last)
best_last = prev[best_last]
ans.reverse()
for i in range(len(ans)):
ans[i] += 1
print (len(ans))
if len(ans):
print (" ".join(map(str, ans)))
```
| 105,559 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's introduce the designation <image>, where x is a string, n is a positive integer and operation " + " is the string concatenation operation. For example, [abc, 2] = abcabc.
We'll say that string s can be obtained from string t, if we can remove some characters from string t and obtain string s. For example, strings ab and aΡba can be obtained from string xacbac, and strings bx and aaa cannot be obtained from it.
Sereja has two strings, w = [a, b] and q = [c, d]. He wants to find such maximum integer p (p > 0), that [q, p] can be obtained from string w.
Input
The first line contains two integers b, d (1 β€ b, d β€ 107). The second line contains string a. The third line contains string c. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed 100.
Output
In a single line print an integer β the largest number p. If the required value of p doesn't exist, print 0.
Examples
Input
10 3
abab
bab
Output
3
Tags: binary search, dfs and similar, strings
Correct Solution:
```
b, p = map(int, input().split())
s1 = input()
s2 = input()
cnt = [0] * len(s2)
nxt = [0] * len(s2)
for i in range(len(s2)):
pos = i
for j in range(len(s1)):
if s1[j] == s2[pos]:
pos += 1
if pos == len(s2):
cnt[i] += 1
pos = 0
nxt[i] = pos
ans = 0
poss = 0
for i in range(b):
ans += cnt[poss]
poss = nxt[poss]
print(ans // p)
```
| 105,560 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's introduce the designation <image>, where x is a string, n is a positive integer and operation " + " is the string concatenation operation. For example, [abc, 2] = abcabc.
We'll say that string s can be obtained from string t, if we can remove some characters from string t and obtain string s. For example, strings ab and aΡba can be obtained from string xacbac, and strings bx and aaa cannot be obtained from it.
Sereja has two strings, w = [a, b] and q = [c, d]. He wants to find such maximum integer p (p > 0), that [q, p] can be obtained from string w.
Input
The first line contains two integers b, d (1 β€ b, d β€ 107). The second line contains string a. The third line contains string c. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed 100.
Output
In a single line print an integer β the largest number p. If the required value of p doesn't exist, print 0.
Examples
Input
10 3
abab
bab
Output
3
Submitted Solution:
```
b, p = map(int, input().split())
s1 = input()
s2 = input()
cnt = 0
pos = 0
flag = True
while flag:
for i in range(len(s1)):
if s1[i] == s2[pos]:
pos += 1
if i == len(s1) - 1:
cnt += 1
if pos == len(s2):
flag = False
break
ans = b // cnt // p
print(ans)
```
No
| 105,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's introduce the designation <image>, where x is a string, n is a positive integer and operation " + " is the string concatenation operation. For example, [abc, 2] = abcabc.
We'll say that string s can be obtained from string t, if we can remove some characters from string t and obtain string s. For example, strings ab and aΡba can be obtained from string xacbac, and strings bx and aaa cannot be obtained from it.
Sereja has two strings, w = [a, b] and q = [c, d]. He wants to find such maximum integer p (p > 0), that [q, p] can be obtained from string w.
Input
The first line contains two integers b, d (1 β€ b, d β€ 107). The second line contains string a. The third line contains string c. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed 100.
Output
In a single line print an integer β the largest number p. If the required value of p doesn't exist, print 0.
Examples
Input
10 3
abab
bab
Output
3
Submitted Solution:
```
import math
def a_can_from_b(a, b):
pt = 0
for chr in a:
try:
while b[pt] != chr:
pt += 1
except IndexError:
return False
return True
class CodeforcesTask314BSolution:
def __init__(self):
self.result = ''
self.b_d = []
self.a = ''
self.c = ''
def read_input(self):
self.b_d = [int(x) for x in input().split(" ")]
self.a = input()
self.c = input()
def process_task(self):
left = 1
right = math.ceil(self.b_d[0] / self.b_d[1])
while right - left > 1:
mid = left + (right - left) // 2
#print(left, right, mid)
if a_can_from_b(self.c * (self.b_d[1] * mid), self.a * self.b_d[0]):
left = mid
else:
right = mid
if not a_can_from_b(self.c * (self.b_d[1] * left), self.a * self.b_d[0]):
left -= 1
self.result = str(left)
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask314BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
```
No
| 105,562 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's introduce the designation <image>, where x is a string, n is a positive integer and operation " + " is the string concatenation operation. For example, [abc, 2] = abcabc.
We'll say that string s can be obtained from string t, if we can remove some characters from string t and obtain string s. For example, strings ab and aΡba can be obtained from string xacbac, and strings bx and aaa cannot be obtained from it.
Sereja has two strings, w = [a, b] and q = [c, d]. He wants to find such maximum integer p (p > 0), that [q, p] can be obtained from string w.
Input
The first line contains two integers b, d (1 β€ b, d β€ 107). The second line contains string a. The third line contains string c. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed 100.
Output
In a single line print an integer β the largest number p. If the required value of p doesn't exist, print 0.
Examples
Input
10 3
abab
bab
Output
3
Submitted Solution:
```
b, p = map(int, input().split())
s1 = input()
s2 = input()
cnt = 0
pos = 0
flag = True
while flag and cnt < b:
for i in range(len(s1)):
if i == 0:
cnt += 1
if s1[i] == s2[pos]:
pos += 1
if pos == len(s2):
flag = False
break
if flag == False:
ans = b // cnt // p
else:
ans = 0
print(ans)
```
No
| 105,563 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's introduce the designation <image>, where x is a string, n is a positive integer and operation " + " is the string concatenation operation. For example, [abc, 2] = abcabc.
We'll say that string s can be obtained from string t, if we can remove some characters from string t and obtain string s. For example, strings ab and aΡba can be obtained from string xacbac, and strings bx and aaa cannot be obtained from it.
Sereja has two strings, w = [a, b] and q = [c, d]. He wants to find such maximum integer p (p > 0), that [q, p] can be obtained from string w.
Input
The first line contains two integers b, d (1 β€ b, d β€ 107). The second line contains string a. The third line contains string c. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed 100.
Output
In a single line print an integer β the largest number p. If the required value of p doesn't exist, print 0.
Examples
Input
10 3
abab
bab
Output
3
Submitted Solution:
```
b, p = map(int, input().split())
s1 = input()
s2 = input()
cnt = 0
pos = 0
flag = True
while flag and cnt < b:
for i in range(len(s1)):
if i == 0:
cnt += 1
if s1[i] == s2[pos]:
pos += 1
if pos == len(s2):
flag = False
break
if flag:
ans = b // cnt // p
else:
ans = 0
print(ans)
```
No
| 105,564 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Tags: binary search, greedy, math, matrices, number theory
Correct Solution:
```
n, corecte, k = map(int, input().split())
incorecte = n - corecte
mod = 10**9 + 9
corecte_consecutive = max(0, n - incorecte * k)
dublari = corecte_consecutive // k
corecte_ramase = corecte - corecte_consecutive
def power(b, exp):
if exp == 0: return 1
half = power(b, exp//2)
if exp%2 == 0: return (half*half) % mod
return (half*half*b) % mod
score = (power(2, dublari+1) - 2) * k + corecte_ramase + corecte_consecutive % k
print(score % mod)
```
| 105,565 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Tags: binary search, greedy, math, matrices, number theory
Correct Solution:
```
n,m,k = map(int,input().split())
chunks = n//k
freespots = chunks*(k-1) + n%k
if m <= freespots:
print(m)
else:
doubles = m-freespots
dchunks = doubles
chunks -= dchunks
total = (pow(2,dchunks,1000000009)-1)*k*2
total += n%k + chunks*(k-1)
print(total%1000000009)
```
| 105,566 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Tags: binary search, greedy, math, matrices, number theory
Correct Solution:
```
n,m,k=map(int,input().split())
MOD=1000000009
x=m-(n//k*(k-1)+(n%k))
if (x<=0):exit(print(m%MOD))
print(((m-x)+((pow(2,x+1, MOD)+2*MOD)-2)*k-x*(k-1))%MOD)
```
| 105,567 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Tags: binary search, greedy, math, matrices, number theory
Correct Solution:
```
from sys import stdin
n, m, k = map(int, stdin.readline().split(' '))
doublesNeeded = int(m - (n - n / k))
score = 0
if doublesNeeded > 0:
score = 1 << (doublesNeeded +1)
score *= k
score -= 2 * k
if m > 0 and doublesNeeded >= 0:
score += m - doublesNeeded * k
elif doublesNeeded < 0:
score = m
score %= 1000000009
print(score)
```
| 105,568 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Tags: binary search, greedy, math, matrices, number theory
Correct Solution:
```
MOD = 1000000009
n, m, k = map(int, input().split())
x = m - (n // k * (k - 1) + n % k)
if x <= 0:
print(m)
else:
print(((m - x) + (pow(2, x + 1, MOD) - 2) * k - x * (k - 1)) % MOD)
```
| 105,569 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Tags: binary search, greedy, math, matrices, number theory
Correct Solution:
```
from math import *
from collections import *
import sys
sys.setrecursionlimit(10**9)
mod = 1000000009
def power(x, y) :
res = 1
x = x % mod
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % mod
y = y >> 1
x = (x * x) % mod
return res
n,m,k = map(int,input().split())
s = n//k
ct = (k-1)*s + n%k
if(ct >= m):
print(m)
else:
diff = m - ct
ans = power(2,diff+1)-2
ans *= k
ans %= mod
ans += ((k-1)*(s-diff))%mod
ans %= mod
ans += n%k
ans %= mod
print(ans)
```
| 105,570 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Tags: binary search, greedy, math, matrices, number theory
Correct Solution:
```
a,b,c=map(int,input().split())
ans=min(a-b,b//(c-1))*(c-1)
b-=ans
ans+=(((pow(2,b//c,1000000009)-1)*(2*c))+b%c)%1000000009
print(ans%1000000009)
```
| 105,571 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Tags: binary search, greedy, math, matrices, number theory
Correct Solution:
```
n,m,k = map(int,input().split())
p = k-1
MOD = 1000000009
tMatches = n//k * (k-1) + n%k
if (tMatches >= m):
print(m)
else:
leftMatches = m - tMatches
m -= (k*leftMatches)
cost = k*2*(pow(2,leftMatches,MOD) - 1)
cost += m
print(cost%MOD)
```
| 105,572 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Submitted Solution:
```
n, m, k = map(int, input().split())
x, c, ic, ans, mod = min(m//(k-1), n-m), m, n-m, 0, 10**9 + 9
c = c - (k-1)*x
p, r = c//k, c%k
ans = ((((pow(2, p+1, mod) - 2 + mod)%mod) * (k%mod))%mod + (k-1)*x + r)%mod
print(ans)
```
Yes
| 105,573 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Submitted Solution:
```
n,m,k=map(int,input().split())
MOD=1000000009
x=m-(n//k*(k-1)+(n%k))
if (x<=0):exit(print(m%MOD))
print(((m-x)+((pow(2,x+1, MOD)+2*MOD)-2)*k-x*(k-1))%MOD)
# Made By Mostafa_Khaled
```
Yes
| 105,574 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Submitted Solution:
```
d = 1000000009
n, m, k = map(int, input().split())
if (n - m) * k > n: print(m)
else:
t = n // k - n + m + 1
print(((pow(2, t, d) - 1 - t) * k + m) % d)
```
Yes
| 105,575 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Submitted Solution:
```
z = 10**9+9
n,m,k = map(int,input().split())
i = n-m
x = (n-k+1)//k
if k*i>=n-k+1:
print((n-i)%z)
else:
l = n-k+1
f = l-(i-1)*k-1
t = f//k
f = t*k
v = 2*(pow(2,t,z)-1)*k+(n-f-i)
print(v%z)
```
Yes
| 105,576 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Submitted Solution:
```
MOD = 10**9 + 9
n,m,k = map(int,input().split())
x = (n//k) + m -n
if (n - n//k) < m:
x = n//k +m -n
ans = pow(2,x+1,MOD)-2
ans = ans*k+m-x*k
print(ans)
else :
print(m)
```
No
| 105,577 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Submitted Solution:
```
from math import ceil
n,m ,p=map(int,input().split())
mod=10**9+9
if ((m//(p-1))+m-1 )<=n:
print(m%mod)
elif m==n:
an=0
an+=2*(m//p)*(1<<(m//p)-1)*p + m%p
print(an)
else:
an=0
z=n-m
m-=(n-m)*(p-1)
an+=(p-1)*z+(m%(p))
an+=2*(m//p)*(1<<(m//p)-1)*p
print(an%mod)
```
No
| 105,578 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Submitted Solution:
```
MOD = 1000000009
def solve( n, m, k ):
score = 0
rem = m
available = n
min_score = 0
first = True
prev_rem = m
prev_score = 0
prev_available = n
for z in range( 0, m+1 ):
if z > 0:
#per iteration
score = prev_score
rem = prev_rem
available = prev_available
if rem >= k:
score = (score+k)
score = ( score * 2 )
rem -= (k)
available -= k
prev_rem = rem
prev_score = score
prev_available = available
#end of per iteration
t = available // (k)
available -= k*t
k_t = (k-1)*t
score = ( score + k_t )
rem -= k_t
if available >= rem:
ans = ( score + rem )
if first:
first = False
min_score = ans
else:
min_score = min( min_score, ans )
print( min_score )
n, m, k = list( map( int, input().split() ) )
solve( n, m, k )
```
No
| 105,579 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).
Input
The single line contains three space-separated integers n, m and k (2 β€ k β€ n β€ 109; 0 β€ m β€ n).
Output
Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).
Examples
Input
5 3 2
Output
3
Input
5 4 2
Output
6
Note
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.
Submitted Solution:
```
from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
def main():
n, m, k = [ int(x) for x in input().split() ]
mod = 1000000009
positions = n / k
availablePositions = int(
int(positions)*(k - 1) + (positions - int(positions)) / (1/k)
)
if availablePositions >= m:
points = m
else:
positionsLeft = m - availablePositions
points = (
pow(2, positionsLeft, mod)*k + 2*k*(pow(2, positionsLeft - 1, mod) - 1)
+ (m - k * positionsLeft)
) % mod
print(points)
BUFFSIZE = 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, BUFFSIZE))
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, BUFFSIZE))
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")
input = lambda: sys.stdin.readline().rstrip("\r\n")
def print(*args, **kwargs):
sep = kwargs.pop("sep", " ")
file = kwargs.pop("file", sys.stdout)
atStart = True
for x in args:
if not atStart:
file.write(sep)
file.write(str(x))
atStart = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
main()
```
No
| 105,580 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Tags: implementation
Correct Solution:
```
import math
def coder(n):
cant = math.ceil(n ** 2 / 2)
m = list()
for i in range(n):
if n % 2 == 0 and i % 2 == 0:
m.append("C." * (n // 2))
elif n % 2 == 0 and i % 2 != 0:
m.append(".C" * (n // 2))
elif n % 2 != 0 and i % 2 == 0:
m.append("C." * (n // 2) + "C")
elif n % 2 != 0 and i % 2 != 0:
m.append(".C" * (n // 2) + ".")
return [cant, m]
n = int(input().strip())
r = coder(n)
print(r[0])
for i in range(n):
print(r[1][i])
```
| 105,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Tags: implementation
Correct Solution:
```
n = int(input())
k = n // 2
print(n * k + (n % 2) * (k + 1))
s = 'C.' * (k + 1)
for i in range(n):
q = i % 2
print(s[q : n + q])
```
| 105,582 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Tags: implementation
Correct Solution:
```
# import sys
# sys.stdin=open("input.in",'r')
# sys.stdout=open("outp.out",'w')
n=int(input())
c='C.'
if n%2==0:
print(int(n*n/2))
i=0
while i < int(n/2):
print(c*int(n/2))
print(c[::-1]*int(n/2))
i+=1
else:
print(int((n*n)/2)+1)
i=0
while i<int(n/2):
print(c*int(n/2)+c[0])
print(c[::-1]*int(n/2)+c[-1])
i+=1
print(c*int(n/2)+c[0])
# for i in range(n):
# for j in range(n):
# if c==True:
# print('C',end='')
# c=False
# else:
# print(".",end='')
# c=True
# print()
# if i %2==0:
# c=False
# else:
# c=True
```
| 105,583 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Tags: implementation
Correct Solution:
```
#==========3 Π·Π°Π΄Π°Π½ΠΈΠ΅===============
import math
n = int(input())
r1 = math.ceil(n/2)
r2 = math.floor(n/2)
r3 = r1*r1 + r2*r2
print(r3)
l = []
l1 = ["C." for i in range(n)]
l2 = [".C" for i in range(n)]
for i in range(n):
l += ["".join(l1)[:n]]
l += ["".join(l2)[:n]]
l = l[:n]
for i in l:
print(i)
```
| 105,584 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Tags: implementation
Correct Solution:
```
x = int(input())
temp = ""
for i in range(1,x+1):
if i % 2 != 0:
temp += ('C.' * (x//2) + 'C' * (x & 1))
else: temp += ('.C' * (x//2) + '.' * (x & 1))
temp += '\n'
print(temp.count('C'))
print(temp)
```
| 105,585 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Tags: implementation
Correct Solution:
```
def main():
n = int(input())
total = (n // 2) * n if n % 2 == 0 \
else (n // 2) * n + (n + 1) // 2
print(total)
for i in range(n):
s = ''
if n % 2 == 0:
s = 'C.' * (n // 2) if i % 2 == 0 \
else '.C' * (n // 2)
else:
s = 'C.' * (n // 2) + 'C' if i % 2 == 0 \
else '.C' * (n // 2) + '.'
print(s)
if __name__ == '__main__':
main()
```
| 105,586 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Tags: implementation
Correct Solution:
```
n = int(input())
m = ''
for i in range(n):
for j in range(n):
if i%2 == 0 and j%2 ==0 :
m +='C'
elif i%2 != 0 and j%2 !=0 :
m +='C'
else:
m +='.'
m +='\n'
print(int((n*n)/2+ (n*n)%2))
print(m)
```
| 105,587 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Tags: implementation
Correct Solution:
```
n=int(input())
p=0
q=0
a=[]
b=[]
for x in range(1,n+1):
a+=['C']
p+=1
if p==n:
break
a+=['.']
p+=1
if p==n:
break
#a=
for y in range(1,n+1):
b+=['.']
q+=1
if q==n:
break
b+=['C']
q+=1
if q==n:
break
#b=
if n%2==0:
print((n*n)//2)
p=0
for x in range(1,n+1):
print(''.join([str(x) for x in b]))
p+=1
#print('')
if p==n:
break
print(''.join([str(x) for x in a]))
p+=1
#print('')
if p==n:
break
else:
temp=(n*n)//2
print(temp+1)
p=0
for x in range(1,n+1):
print(''.join([str(x) for x in a]))
p+=1
#print('')
if p==n:
break
print(''.join([str(x) for x in b]))
p+=1
#print('')
if p==n:
break
```
| 105,588 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Submitted Solution:
```
n = int(input())
if n%2 == 0:
print(n* n//2)
for i in range(n//2):
print('C.'*(n//2))
print('.C'*(n//2))
else:
ans = (n//2)**2 + (n//2+1)**2
print(ans)
for i in range(n//2):
print('C.'*(n//2) + 'C')
print('.C'*(n//2) + '.')
print('C.'*(n//2) + 'C')
```
Yes
| 105,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Submitted Solution:
```
a = int(input())
if (a * a) % 2 == 0:
print(a * a // 2)
for i in range(a):
if i % 2 == 0:
for i in range(a // 2):
print("C.",end="")
else:
for i in range(a // 2):
print(".C",end="")
print()
else:
print(a * a // 2 + 1)
for i in range(a):
if i % 2 == 0:
for i in range(a // 2):
print("C.",end="")
print("C",end="")
else:
for i in range(a // 2):
print(".C",end="")
print(".",end="")
print()
```
Yes
| 105,590 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Submitted Solution:
```
from math import ceil, trunc
n = int(input())
k1 = 0
line1 = []
b = True
for i in range(n):
line1.append('C' if b else '.')
k1 += b
b = not b
line1 = ''.join(line1)
k2 = 0
line2 = []
b = False
for i in range(n):
line2.append('C' if b else '.')
k2 += b
b = not b
line2 = ''.join(line2)
print(ceil(n / 2) * k1 + trunc(n / 2) * k2)
b = True
for i in range(n):
print(line1 if b else line2)
b = not b
```
Yes
| 105,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Submitted Solution:
```
#!/usr/bin/python3
# -*- coding: <utf-8> -*-
import itertools as ittls
from collections import Counter
import string
def sqr(x):
return x*x
def inputarray(func = int):
return map(func, input().split())
# -------------------------------
# -------------------------------
N = int(input())
print( (sqr(N) + 1)//2 )
for i in range(N):
if i&1:
s = '.C'*(N//2)
if N&1:
s = s + '.'
else:
s = 'C.'*(N//2)
if N&1:
s = s + 'C'
print(s)
```
Yes
| 105,592 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Submitted Solution:
```
n=int(input())
a=[]
for i in range(n):
if(not i&1):
s=''
for j in range(n):
if(not j&1):
s+='C'
else:
s+='.'
a.append(s)
else:
s=''
for j in range(n):
if(not j&1):
s+='.'
else:
s+='C'
a.append(s)
#print(a)
for i in a:
print(i)
```
No
| 105,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Submitted Solution:
```
n = int(input())
m = n
for i in range(n):
if i % 2 == 0:
for j in range(n):
if j % 2 == 0:
print("C",end="")
elif j % 2 != 0:
print(".",end="")
elif i % 2 != 0:
for j in range(n):
if j % 2 == 0:
print(".",end="")
elif j % 2 != 0:
print("C",end="")
print()
```
No
| 105,594 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Submitted Solution:
```
n = int(input())
if n%2 == 0:
print((n*n)//2)
else:
t = n//2
print(t + n + t*t)
for j in range(n):
for i in range(n):
if i%2 == j%2:
print('C', end='')
else:
print('.', end='')
print()
```
No
| 105,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (xβ1, y), (x, y + 1) and (x, yβ1).
Iahub wants to know how many Coders can be placed on an n Γ n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 β€ n β€ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
Examples
Input
2
Output
2
C.
.C
Submitted Solution:
```
n = int(input())
print(((n**2)//2) if (n**2) % 2 == 0 else (((n//2)+1)**2 + (n//2)**2))
for i in range(n):
for j in range(n):
if (i+j) % 2 == 0:
print("c", end="")
else:
print(".", end="")
print()
```
No
| 105,596 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has a strip infinite in both directions and consisting of cells. The cells are numbered by integers. The cell number 0 has a robot.
The robot has instructions β the sequence of moves that he must perform. In one move, the robot moves one cell to the left or one cell to the right, according to instructions. Before the robot starts moving, Valera puts obstacles in some cells of the strip, excluding cell number 0. If the robot should go into the cell with an obstacle according the instructions, it will skip this move.
Also Valera indicates the finish cell in which the robot has to be after completing the entire instructions. The finishing cell should be different from the starting one. It is believed that the robot completed the instructions successfully, if during the process of moving he visited the finish cell exactly once β at its last move. Moreover, the latter move cannot be skipped.
Let's assume that k is the minimum number of obstacles that Valera must put to make the robot able to complete the entire sequence of instructions successfully and end up in some finishing cell. You need to calculate in how many ways Valera can choose k obstacles and the finishing cell so that the robot is able to complete the instructions successfully.
Input
The first line contains a sequence of characters without spaces s1s2... sn (1 β€ n β€ 106), consisting only of letters "L" and "R". If character si equals "L", then the robot on the i-th move must try to move one cell to the left. If the si-th character equals "R", then the robot on the i-th move must try to move one cell to the right.
Output
Print a single integer β the required number of ways. It's guaranteed that this number fits into 64-bit signed integer type.
Examples
Input
RR
Output
1
Input
RRL
Output
1
Note
In the first sample Valera mustn't add any obstacles and his finishing cell must be cell 2.
In the second sample, Valera must add an obstacle in cell number 1, and his finishing cell must be cell number - 1. In this case robot skips the first two moves and on the third move he goes straight from the starting cell to the finishing one. But if Valera doesn't add any obstacles, or adds an obstacle to another cell, then the robot visits the finishing cell more than once.
Submitted Solution:
```
def isBounded(bound):
global sequence
height = 0
minHeight = 0
for c in sequence:
height = min(bound, height + c)
minHeight = min(height, minHeight)
return height <= minHeight
sequence = input()
length = len(sequence) - 1
d = {"L": -1, "R": 1}
if sequence[-1] == "R":
d = {"R": -1, "L": 1}
sequence = list(map(lambda x: d[x], sequence[:-1]))
if isBounded(float("inf")): # I don't have to do nothing
print(1)
elif not isBounded(0): # I can't do anything
print(0)
else:
fr = 0
to = length
while fr + 1 < to:
print(fr, to)
mid = (fr + to) >> 1
if isBounded(mid):
fr = mid
else:
to = mid
print(fr + 1)
```
No
| 105,597 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has a strip infinite in both directions and consisting of cells. The cells are numbered by integers. The cell number 0 has a robot.
The robot has instructions β the sequence of moves that he must perform. In one move, the robot moves one cell to the left or one cell to the right, according to instructions. Before the robot starts moving, Valera puts obstacles in some cells of the strip, excluding cell number 0. If the robot should go into the cell with an obstacle according the instructions, it will skip this move.
Also Valera indicates the finish cell in which the robot has to be after completing the entire instructions. The finishing cell should be different from the starting one. It is believed that the robot completed the instructions successfully, if during the process of moving he visited the finish cell exactly once β at its last move. Moreover, the latter move cannot be skipped.
Let's assume that k is the minimum number of obstacles that Valera must put to make the robot able to complete the entire sequence of instructions successfully and end up in some finishing cell. You need to calculate in how many ways Valera can choose k obstacles and the finishing cell so that the robot is able to complete the instructions successfully.
Input
The first line contains a sequence of characters without spaces s1s2... sn (1 β€ n β€ 106), consisting only of letters "L" and "R". If character si equals "L", then the robot on the i-th move must try to move one cell to the left. If the si-th character equals "R", then the robot on the i-th move must try to move one cell to the right.
Output
Print a single integer β the required number of ways. It's guaranteed that this number fits into 64-bit signed integer type.
Examples
Input
RR
Output
1
Input
RRL
Output
1
Note
In the first sample Valera mustn't add any obstacles and his finishing cell must be cell 2.
In the second sample, Valera must add an obstacle in cell number 1, and his finishing cell must be cell number - 1. In this case robot skips the first two moves and on the third move he goes straight from the starting cell to the finishing one. But if Valera doesn't add any obstacles, or adds an obstacle to another cell, then the robot visits the finishing cell more than once.
Submitted Solution:
```
def isBounded(sequence, bound):
height = 0
minHeight = 0
for c in sequence:
height = min(bound, height + c)
minHeight = min(height, minHeight)
return height <= minHeight
s = input()
d = {"L": -1, "R": 1}
if s[-1] == "R":
d = {"R": -1, "L": 1}
s = list(map(lambda x: d[x], s[:-1]))
if isBounded(s, float("inf")): # I don't have to do nothing
print(1)
elif not isBounded(s, 0): # I can't do anything
print(0)
else:
fr = 0
to = len(s)
while fr + 1 < to:
mid = (fr + to) >> 1
if isBounded(s, mid):
fr = mid
else:
to = mid - 1
print(fr + 1)
```
No
| 105,598 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has a strip infinite in both directions and consisting of cells. The cells are numbered by integers. The cell number 0 has a robot.
The robot has instructions β the sequence of moves that he must perform. In one move, the robot moves one cell to the left or one cell to the right, according to instructions. Before the robot starts moving, Valera puts obstacles in some cells of the strip, excluding cell number 0. If the robot should go into the cell with an obstacle according the instructions, it will skip this move.
Also Valera indicates the finish cell in which the robot has to be after completing the entire instructions. The finishing cell should be different from the starting one. It is believed that the robot completed the instructions successfully, if during the process of moving he visited the finish cell exactly once β at its last move. Moreover, the latter move cannot be skipped.
Let's assume that k is the minimum number of obstacles that Valera must put to make the robot able to complete the entire sequence of instructions successfully and end up in some finishing cell. You need to calculate in how many ways Valera can choose k obstacles and the finishing cell so that the robot is able to complete the instructions successfully.
Input
The first line contains a sequence of characters without spaces s1s2... sn (1 β€ n β€ 106), consisting only of letters "L" and "R". If character si equals "L", then the robot on the i-th move must try to move one cell to the left. If the si-th character equals "R", then the robot on the i-th move must try to move one cell to the right.
Output
Print a single integer β the required number of ways. It's guaranteed that this number fits into 64-bit signed integer type.
Examples
Input
RR
Output
1
Input
RRL
Output
1
Note
In the first sample Valera mustn't add any obstacles and his finishing cell must be cell 2.
In the second sample, Valera must add an obstacle in cell number 1, and his finishing cell must be cell number - 1. In this case robot skips the first two moves and on the third move he goes straight from the starting cell to the finishing one. But if Valera doesn't add any obstacles, or adds an obstacle to another cell, then the robot visits the finishing cell more than once.
Submitted Solution:
```
def isBounded(bound):
global sequence
height = 0
minHeight = 0
for c in sequence:
height = min(bound, height + c)
minHeight = min(height, minHeight)
return height <= minHeight
sequence = input()
length = len(sequence) - 1
d = {"L": -1, "R": 1}
if sequence[-1] == "R":
d = {"R": -1, "L": 1}
sequence = map(lambda x: d[x], sequence[:-1])
if isBounded(float("inf")): # I don't have to do nothing
print(1)
elif not isBounded(0): # I can't do anything
print(0)
else:
fr = 0
to = length
while fr + 1 < to:
mid = (fr + to) >> 1
if isBounded(mid):
fr = mid
else:
to = mid
print(fr + 1)
```
No
| 105,599 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.