message stringlengths 2 48.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 318 108k | cluster float64 8 8 | __index_level_0__ int64 636 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor. | instruction | 0 | 90,333 | 8 | 180,666 |
Tags: implementation, math
Correct Solution:
```
import math
n1 = int(input())
for _ in range(n1):
n2 = input()
l = [int(item) for item in n2.split()]
n,x = l[0],l[1]
if n<=2 : print(1)
else : print(1+math.ceil((n-2)/x))
``` | output | 1 | 90,333 | 8 | 180,667 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor. | instruction | 0 | 90,334 | 8 | 180,668 |
Tags: implementation, math
Correct Solution:
```
t=int(input())
for i in range(t):
n,x=map(int,input().split())
print(max(0,(n-3)//x+1)+1)
``` | output | 1 | 90,334 | 8 | 180,669 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor. | instruction | 0 | 90,335 | 8 | 180,670 |
Tags: implementation, math
Correct Solution:
```
#codeforces div-2 round 668
'''for i in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
print(*arr[::-1])'''
'''for i in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
run=0
for i in range(n-1,-1,-1):
if arr[i]<0:
run+=arr[i]
else:
if run<0:
temp=arr[i]
arr[i]=max(0,arr[i]+run)
run=min(0,temp+run)
res=0
for i in range(n):
res+=arr[i] if arr[i]>0 else 0
print(res)'''
"""for i in range(int(input())):
n,k=[int(i) for i in input().split()]
s=intput()"""
#Codechef long challenge
'''from collections import defaultdict
for i in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
cnt=defaultdict(int)
for i in range(n):
cnt[arr[i]]+=1
print(len(cnt))'''
#Codeforces round 669 div-02
'''for i in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
ones=0
for i in range(n):
if arr[i]==1:
ones+=1
if ones<=n//2:
print(n-ones)
print(*[0 for j in range(n-ones)])
elif ones%2:
print(ones-1)
print(*[1 for j in range(ones-1)])
else:
print(ones)
print(*[1 for j in range(ones)])'''
'''def gcd(a,b):
if b==0:
return a
return gcd(b,a%b)
for i in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
k=max(arr)
imp=[]
run=k
arr0=arr[:]
for j in range(n):
for i in range(len(arr0)):
for i in range(n):
imp.append((gcd(k,arr[i]),arr[i]))
print(imp)
imp.sort(key=lambda x:x[0],reverse=True)
res=[]
for i in imp:
res.append(i[1])
print(*res)'''
#Codeforces round 670 DIV-02
'''from collections import defaultdict
for i in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
cnt=defaultdict(int)
for i in arr:
cnt[i]+=1
first=0
second=0
tot=0
for j in range(0,max(arr)+2):
if cnt[j]==0:
first=j
break
res=0
for t in range(0,max(arr)+2):
if cnt[t]==1 or cnt[t]==0:
second=t
break
print(first+second)'''
'''for i in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
arr.sort()
pre=[arr[0] for i in range(len(arr))]
suff=[arr[-1] for i in range(len(arr))]
for i in range(1,n):
pre[i]=pre[i-1]*arr[i]
for j in range(n-2,-1,-1):
suff[j]=suff[j+1]*arr[j]
res=float("-inf")
if n>=5:
res=max(res,pre[1]*suff[-3],pre[3]*suff[-1],suff[-5])
print(res)
pos=[]
zero=0
neg=[]
res=0
for i in range(n):
if arr[i]>0:
pos.append(arr[i])
elif arr[i]==0:
zero+=1
else:
neg.append(arr[i])
pos.sort(reverse=True)
neg.sort()
pos_pro=[pos[0] for i in range(len(pos))]
neg_pro=[neg[0] for i in range(len(neg))]
if len(pos)>1:
for k in range(1,len(pos)):
pos_pro[k]=pos_pro[k-1]*pos[k]
if len(neg)>1:
for k in range(1,len(neg)):
neg_pro[k]=neg_pro[k-1]*neg[k]
if len(pos)>=5:
if len(neg)>=4:
res=max(res,pos_pro[4],pos_pro[2]*neg_pro[1],pos_pro[0]*neg_pro[3])
elif len(neg)>=2 and len(neg)<4:
res=max(res,pos_pro[4],pos_pro[2]*neg_pro[1])
else:
res=pos_pro[4]
print(res)
else:
if len(neg)>=2 and len(neg)<4:
if len(pos)<3:
res=0
else:
res=max(res,neg_pro[1]*pos_pro[2])
elif len(neg)>=4:
if len(pos)>=1:
res=max(res,neg_pro[3]*pos_pro[0])
else:
res=0 if zero else neg_pro[-1]
print(res)'''
#Codeforces educational round
'''for i in range(int(input())):
x,y,k=[int(i) for i in input().split()]
sticks=k*(y+1)-1
if sticks%(x-1):
temp=sticks//(x-1)+1
else:
temp=sticks//(x-1)
print(temp +k )'''
'''for i in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
lock=[int(i) for i in input().split()]
neg=[]
for i in range(n):
if lock[i]==0:
neg.append(arr[i])
neg.sort(reverse=True)
j=0
for i in range(len(arr)):
if lock[i]==0:
arr[i]=neg[j]
j+=1
print(*arr)'''
'''for _ in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
if n==1:
print(arr[0])
else:
dp=[[0,0] for i in range(n)]
dp[-1]=[arr[-1],0]
dp[-2]=[arr[-2],0]
for k in range(n-3,-1,-1):
dp[k][0]=arr[k]+dp[k+1][1]
if arr[k]+arr[k+1]+dp[k+2][1]<dp[k][0]:
dp[k][0]=arr[k]+arr[k+1]+dp[k+2][1]
dp[k][1]=min(dp[k+1][0],dp[k+2][0])
print(dp[0][0])
'''
#Codeforces round 673
'''for i in range(int(input())):
n,k=[int(i) for i in input().split()]
arr=[int(i) for i in input().split()]
res=0
flag=0
temp=min(arr)
app=0
for i in range(n):
if arr[i]==temp and not app:
app=1
continue
elif arr[i]==temp and app:
res+=(k-arr[i])//temp
else:
res+=(k-arr[i])//temp
print(res)'''
'''from collections import defaultdict
for i in range(int(input())):
n,t=[int(i) for i in input().split()]
app=defaultdict(lambda:-1)
arr=[int(i) for i in input().split()]
ans=[0 for i in range(n)]
for i in range(n):
if arr[i]>t:
continue
elif app[t-arr[i]]!=-1:
ans[i]=ans[app[t-arr[i]]]^1
app[arr[i]]=i
else:
app[arr[i]]=i
print(*ans)'''
'''for i in range(int(input())):
n=int(input())
arr=[int(i) for i in input().split()]
where={}
for i in range(n):
if arr[i] in where:
where[arr[i]].append(i)
else:
where[arr[i]]=[i]
ans=[-1 for i in range(n)]
last=0
for k in range(1,n+1):
if k not in where:
continue
else:
res=0
for t in range(1,len(where[k])):
res=max(res,where[k][t]-where[k][t-1])
res=max(res,where[k][0]+1,n-where[k][-1])
if res>0 and ans[res-1]==-1:
ans[res-1]=k
mn=-1
for i in range(n):
if ans[i]!=-1 and mn<ans[i]:
ans[i]=mn
elif ans[i]==-1:
ans[i]=mn
else:
mn=min(mn if mn!=-1 else float("inf"),ans[i])
print(ans[i],end=" ")'''
#Codeforces round 674 DIV-3
for i in range(int(input())):
n,x=[int(i) for i in input().split()]
if n<3:
print(1)
else:
temp=(n-2)//x
temp+=1 if (n-2)%x!=0 else 0
print(1+temp)
``` | output | 1 | 90,335 | 8 | 180,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor. | instruction | 0 | 90,336 | 8 | 180,672 |
Tags: implementation, math
Correct Solution:
```
for _ in range(int(input())):
sim=2
n,x=map(int, input().split())
if n<=sim:
print(1)
else:
while n>sim:
sim+=x
print(int((sim-2)/x+1))
``` | output | 1 | 90,336 | 8 | 180,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor. | instruction | 0 | 90,337 | 8 | 180,674 |
Tags: implementation, math
Correct Solution:
```
for _ in range(int(input())):
n,x=map(int,input().split())
if n<=2:
print(1)
else:
n-=2
s=n/x
if int(s)!=s:
s=int(s)+1
print(int(s)+1)
``` | output | 1 | 90,337 | 8 | 180,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor. | instruction | 0 | 90,338 | 8 | 180,676 |
Tags: implementation, math
Correct Solution:
```
import math
n1=int(input())
for x in range(n1):
n2=input().split(" ")
a=int(n2[0])
b=int(n2[1])
if(a<=2):
print(1)
else:
print(1+math.ceil((a-2)/b))
``` | output | 1 | 90,338 | 8 | 180,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor. | instruction | 0 | 90,339 | 8 | 180,678 |
Tags: implementation, math
Correct Solution:
```
def read_int():
return int(input())
def read_ints():
return map(int, input().split(' '))
t = read_int()
for case_num in range(t):
fn = 1
cnt = 2
n, x = read_ints()
if n <= 2:
print(1)
else:
while cnt < n:
cnt += x
fn += 1
print(fn)
fn = 1
``` | output | 1 | 90,339 | 8 | 180,679 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor.
Submitted Solution:
```
for _ in range(0, int(input())):
n, x = map(int, input().split())
if n <= 2:
print(1)
else:
print(((n-3)//x)+2)
``` | instruction | 0 | 90,340 | 8 | 180,680 |
Yes | output | 1 | 90,340 | 8 | 180,681 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor.
Submitted Solution:
```
# import itertools as it
# import functools as ft
import math
teststring = """4
7 3
1 5
22 5
987 13
"""
online = __file__ != "/home/jhli/py/674/Problem_A.py"
true, false = True, False
if True:
def spitout():
for c in teststring.splitlines():
yield c
_ito = spitout()
if not online:
def input():
return next(_ito)
def build_enum(*a):
built = dict()
for i, c in enumerate(a):
built[c] = i
return lambda x: built[x]
# T = 1
T = int(input())
##-----------------start coding-----------------
for ti in range(1, T + 1):
[n, x] = map(int, input().split(" "))
if n <= 2:
print(1)
else:
print(int(math.ceil((n-2)/x))+1)
# print('Case #{}: {}'.format(ti, '...'))
``` | instruction | 0 | 90,341 | 8 | 180,682 |
Yes | output | 1 | 90,341 | 8 | 180,683 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor.
Submitted Solution:
```
for __ in range(int(input())):
n, x = list(map(int, input().split()))
ans = 1
n -= 2
num = x
while n > 0:
if num == x:
ans += 1
num = 0
n -= 1
num += 1
print(ans)
``` | instruction | 0 | 90,342 | 8 | 180,684 |
Yes | output | 1 | 90,342 | 8 | 180,685 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor.
Submitted Solution:
```
t = int(input())
from math import ceil
for _ in range(t):
n,x = map(int, input().split(' '))
if n == 1 or n == 2:
print(1)
else:
print(ceil((n-2)/x)+1)
``` | instruction | 0 | 90,343 | 8 | 180,686 |
Yes | output | 1 | 90,343 | 8 | 180,687 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor.
Submitted Solution:
```
import math
t = int(input())
for i in range(t):
n,x = map(int,input().split())
if(x==1 or x==2):
print(1)
elif(n%x>2):
a = math.ceil(n/x) + 1
print(a)
else:
a = math.ceil((n/x))
print(a)
``` | instruction | 0 | 90,344 | 8 | 180,688 |
No | output | 1 | 90,344 | 8 | 180,689 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor.
Submitted Solution:
```
t = int(input())
while t:
a = input()
n,x=[int(x) for x in a.split()]
if n==1 or n==2:
print(1)
t-=1
continue
k = (int(n-2)/x)+2
t-=1
``` | instruction | 0 | 90,345 | 8 | 180,690 |
No | output | 1 | 90,345 | 8 | 180,691 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor.
Submitted Solution:
```
t=int(input())
for i in range(t):
n,x=map(int,input().split())
if n<=2:
print("1")
else:
n-=2
flor=round(n/x)
print(flor+1)
``` | instruction | 0 | 90,346 | 8 | 180,692 |
No | output | 1 | 90,346 | 8 | 180,693 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is n.
There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains 2 apartments, every other floor contains x apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers 1 and 2, apartments on the second floor have numbers from 3 to (x + 2), apartments on the third floor have numbers from (x + 3) to (2 β
x + 2), and so on.
Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least n apartments.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and x (1 β€ n, x β€ 1000) β the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).
Output
For each test case, print the answer: the number of floor on which Petya lives.
Example
Input
4
7 3
1 5
22 5
987 13
Output
3
1
5
77
Note
Consider the first test case of the example: the first floor contains apartments with numbers 1 and 2, the second one contains apartments with numbers 3, 4 and 5, the third one contains apartments with numbers 6, 7 and 8. Therefore, Petya lives on the third floor.
In the second test case of the example, Petya lives in the apartment 1 which is on the first floor.
Submitted Solution:
```
t = int(input())
while t > 0:
n, x = map(int, input().split())
print(int((n/x) + 0.5) + 1)
t -= 1
``` | instruction | 0 | 90,347 | 8 | 180,694 |
No | output | 1 | 90,347 | 8 | 180,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. | instruction | 0 | 90,451 | 8 | 180,902 |
Tags: brute force, dp
Correct Solution:
```
# cf 363 B 1200
n, k = map(int, input().split())
A = [*map(int, input().split())]
wsz = 0
wsum = 0
min_ = float("inf")
mini = 0
for i in range(len(A)):
wsum += A[i]
wsz += 1
if wsz > k:
wsum -= A[1 + i - wsz]
wsz -= 1
if wsum < min_ and wsz == k:
min_ = wsum
mini = 1 + i - wsz
print(1 + mini)
``` | output | 1 | 90,451 | 8 | 180,903 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. | instruction | 0 | 90,452 | 8 | 180,904 |
Tags: brute force, dp
Correct Solution:
```
a,b=map(int,input().split());c=list(map(int,input().split()));s=k=sum(c[:b]);j=0
for i in range(1,a-b+1):
n=s-c[i-1]+c[i+b-1]
if k>n:j=i;k=n
s=n
print(j+1)
``` | output | 1 | 90,452 | 8 | 180,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. | instruction | 0 | 90,453 | 8 | 180,906 |
Tags: brute force, dp
Correct Solution:
```
n, k = [int(i) for i in input().split()] #int(input())
arr = [int(i) for i in input().split()]
acc = 0
for i in range(n):
acc += arr[i]
arr[i] = acc
arr = [0] + arr
min_idx = 0
min_br = arr[-1] + 1
for i in range(n-k+1):
if arr[k+i] - arr[i] < min_br:
min_br = arr[k+i] - arr[i]
min_idx = i+1
print(min_idx)
``` | output | 1 | 90,453 | 8 | 180,907 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. | instruction | 0 | 90,454 | 8 | 180,908 |
Tags: brute force, dp
Correct Solution:
```
import os
import sys
from collections import Counter
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def main():
t=1
for _ in range(t):
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=sum(a[:k])
# print(s)
mins=s
idx=0
for i in range(1,n-k+1):
# print('i',i)
s=s-a[i-1]+a[i+k-1]
# print(s)
if(s<mins):
mins=s
idx=i
print(idx+1)
if __name__ == "__main__":
main()
``` | output | 1 | 90,454 | 8 | 180,909 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. | instruction | 0 | 90,455 | 8 | 180,910 |
Tags: brute force, dp
Correct Solution:
```
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[0]*(n+1)
for i in range(n):
dp[i+1]=dp[i]+l[i]
mn=999999999
ans=-1
for i in range(k,n+1):
if(dp[i]-dp[i-k]<mn):
mn=dp[i]-dp[i-k]
ans=i-k+1
print(ans)
``` | output | 1 | 90,455 | 8 | 180,911 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. | instruction | 0 | 90,456 | 8 | 180,912 |
Tags: brute force, dp
Correct Solution:
```
n,k = map(int,input().split())
lis = list(map(int,input().split()))
ans=[]
mi=sum(lis[:k])
su=mi
ind=0
for i in range(k,n):
su+=lis[i]
su-=lis[i-k]
if su<mi:
ind=i-k+1
mi=su
print(ind+1)
``` | output | 1 | 90,456 | 8 | 180,913 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. | instruction | 0 | 90,457 | 8 | 180,914 |
Tags: brute force, dp
Correct Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=[0]*len(a)
c[0]=a[0]
for i in range(1,n):
c[i]=c[i-1]+a[i]
mini=c[k-1]
h=1
#print(mini)
for i in range(k,n):
t=mini
#print('i',i)
mini=min(mini,c[i]-c[i-k])
#print(c[i]-c[i-k])
# print(mini)
if mini!=t:
h=i-k+2
print(h)
#print(i)
``` | output | 1 | 90,457 | 8 | 180,915 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. | instruction | 0 | 90,458 | 8 | 180,916 |
Tags: brute force, dp
Correct Solution:
```
n,k=map(int,input().split())
l=list(map(int,input().split()))
s=0
d=[0]
for i in l:
s+=i
d.append(s)
prev=float('inf')
idx=-1
for i in range(k-1,n):
s=d[i+1]-d[i-k+1]
#print(i,i-k+1,s,d[i+1],d[i-k+1])
if prev>s:
prev=s
idx=i+1-k+1
print(idx)
``` | output | 1 | 90,458 | 8 | 180,917 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.
Submitted Solution:
```
arr=[]
x=input()
xx=x.split(' ')
n=int(xx[0])
k=int(xx[1])
l = 0
t = 15000001
x=input()
xx=x.split(' ')
arr.append(0)
for i in xx:
arr.append(int(i))
for i in range(1,n+1):
arr[i] += arr[i - 1]
for i in range(k,n+1):
if t > arr[i] - arr[i - k]:
t = arr[i] - arr[i - k]
z = i
print(z - k + 1)
``` | instruction | 0 | 90,459 | 8 | 180,918 |
Yes | output | 1 | 90,459 | 8 | 180,919 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.
Submitted Solution:
```
#from functools import reduce
#mod=int(1e9+7)
#import resource
#resource.setrlimit(resource.RLIMIT_STACK, [0x100000000, resource.RLIM_INFINITY])
#import threading
#threading.stack_size(2**26)
"""fact=[1]
#for i in range(1,100001):
# fact.append((fact[-1]*i)%mod)
#ifact=[0]*100001
#ifact[100000]=pow(fact[100000],mod-2,mod)
#for i in range(100000,0,-1):
# ifact[i-1]=(i*ifact[i])%mod"""
#from collections import deque, defaultdict, Counter, OrderedDict
#from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
#from heapq import heappush, heappop, heapify, nlargest, nsmallest
# sys.setrecursionlimit(10**6)
from sys import stdin, stdout
import bisect #c++ upperbound
from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
from bisect import bisect_right as br #c++ upperbound
import itertools
from collections import Counter
from math import sqrt
import collections
import math
import heapq
import re
def modinv(n,p):
return pow(n,p-2,p)
def cin():
return map(int,sin().split())
def ain(): #takes array as input
return list(map(int,sin().split()))
def sin():
return input()
def inin():
return int(input())
def Divisors(n) :
l = []
for i in range(1, int(math.sqrt(n) + 1)) :
if (n % i == 0) :
if (n // i == i) :
l.append(i)
else :
l.append(i)
l.append(n//i)
return l
def most_frequent(list):
return max(set(list), key = list.count)
def GCD(x,y):
while(y):
x, y = y, x % y
return x
def ncr(n,r,p): #To use this, Uncomment 19-25
t=((fact[n])*((ifact[r]*ifact[n-r])%p))%p
return t
def Convert(string):
li = list(string.split(""))
return li
def SieveOfEratosthenes(n):
global prime
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
f=[]
for p in range(2, n):
if prime[p]:
f.append(p)
return f
prime=[]
q=[]
def dfs(n,d,v,c):
global q
v[n]=1
x=d[n]
q.append(n)
j=c
for i in x:
if i not in v:
f=dfs(i,d,v,c+1)
j=max(j,f)
# print(f)
return j
#Implement heapq
#grades = [110, 25, 38, 49, 20, 95, 33, 87, 80, 90]
#print(heapq.nlargest(3, grades)) #top 3 largest
#print(heapq.nsmallest(4, grades))
#Always make a variable of predefined function for ex- fn=len
#n,k=map(int,input().split())
"""*******************************************************"""
def main():
n,k = map(int,input().split())
f = list(map(int,input().split()))
s = sum(f[:k])
best =s
j = 1
for i in range(n-k):
s-=f[i]
s+=f[k+i]
if s<best:
best = s
j = i+2
print(j)
"""*******************************************************"""
######## Python 2 and 3 footer by Pajenegod and c1729
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s:self.buffer.write(s.encode('ascii'))
self.read = lambda:self.buffer.read().decode('ascii')
self.readline = lambda:self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
if __name__== "__main__":
main()
#threading.Thread(target=main).start()
``` | instruction | 0 | 90,460 | 8 | 180,920 |
Yes | output | 1 | 90,460 | 8 | 180,921 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.
Submitted Solution:
```
n,m=map(int,input().strip().split())
arr=list(map(int,input().strip().split()))
s=sum(arr[0:m])
minimum=s
index=0
for i in range(m, n):
s = s - arr[i - m] + arr[i]
if s<minimum:
minimum=s
index = i - m + 1
print(index + 1)
``` | instruction | 0 | 90,461 | 8 | 180,922 |
Yes | output | 1 | 90,461 | 8 | 180,923 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.
Submitted Solution:
```
def func():
n, k = map(int, input().split())
arr = list(map(int, input().split()))
min_possible = k
curr_sum = sum(arr[:k])
min_sum = float("inf")
index = None
for i in range(k, n):
if curr_sum == min_possible:
return i - k + 1
if curr_sum < min_sum:
index = i - k + 1
min_sum = curr_sum
curr_sum += arr[i] - arr[i-k]
if curr_sum < min_sum:
return n - k + 1
return index
print(func())
``` | instruction | 0 | 90,462 | 8 | 180,924 |
Yes | output | 1 | 90,462 | 8 | 180,925 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 12 18:07:43 2019
@author: avina
"""
n,l = map(int, input().strip().split())
L = list(map(int, input().strip().split()))
s = sum(L[:l])
m = s
k = l - 1
for i in range(l,n-l+1):
s-= L[i-l] - L[i]
if s<m:
k = i
m = s
print(k+2 - l)
``` | instruction | 0 | 90,463 | 8 | 180,926 |
No | output | 1 | 90,463 | 8 | 180,927 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.
Submitted Solution:
```
x,y=map(int,input().split())
a=list(map(int,input().split()))
for i in range(1,x):
a[i]+=a[i-1]
s=1000000
p=-1
for i in range(y+1,x):
if s>=a[i]-a[i-y-1]:
s=a[i]-a[i-y-1]
p=i-y
p=1 if p==-1 else p
print(p)
``` | instruction | 0 | 90,464 | 8 | 180,928 |
No | output | 1 | 90,464 | 8 | 180,929 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.
Submitted Solution:
```
n,k=map(int,input().split())
l=list(map(int,input().split()))
s=[11**11,sum(l[0:k])]
for i in range(1,n-k):
s+=[s[-1]-l[i-1]+l[i+k-1]]
print(s.index(min(s)))
``` | instruction | 0 | 90,465 | 8 | 180,930 |
No | output | 1 | 90,465 | 8 | 180,931 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.
<image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1]
Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.
Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).
Input
The first line of the input contains integers n and k (1 β€ n β€ 1.5Β·105, 1 β€ k β€ n) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 β€ hi β€ 100), where hi is the height of the i-th plank of the fence.
Output
Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.
Examples
Input
7 3
1 2 6 1 1 7 1
Output
3
Note
In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.
Submitted Solution:
```
n, k = [int(i) for i in input().split(" ")]
A = [int(i) for i in input().split(" ")]
cons = [A[0]]
for i in range(1, len(A)):
cons.append(cons[-1] + A[i])
a = []
Min = 999999
ans = 0
i = 0
j = k
while j < n:
a.append(cons[j] - cons[i])
if cons[j] - cons[i] < Min:
Min = cons[j] - cons[i]
ans = i
j += 1
i += 1
print(i + 2)
``` | instruction | 0 | 90,466 | 8 | 180,932 |
No | output | 1 | 90,466 | 8 | 180,933 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. | instruction | 0 | 90,490 | 8 | 180,980 |
Tags: combinatorics, dp
Correct Solution:
```
import sys
input=sys.stdin.readline
def solve(n,st,k):
mod=10**9+7
dp=[0]*(n+1)
pre=[0]*(n+1)
dp[st]=1
for t in range(k):
pre[0]=0
for i in range(1,n+1):
pre[i]=pre[i-1]+dp[i]
pre[i]%=mod
for i in range(1,n+1):
dp[i]=(pre[n]-pre[i]+pre[i-1]-pre[i>>1])%mod
return sum(dp)%mod
n,a,b,k=map(int,input().split())
mod=10**9+7
if a>b:
print(solve(n-b,a-b,k))
else:
print(solve(b-1,b-a,k))
``` | output | 1 | 90,490 | 8 | 180,981 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. | instruction | 0 | 90,491 | 8 | 180,982 |
Tags: combinatorics, dp
Correct Solution:
```
def solve(n, st, k):
MOD = int(1e9 + 7)
dp = [0] * (n + 1)
prefix_sum = [0] * (n + 1)
dp[st] = 1
for times in range(k):
prefix_sum[0] = 0
for i in range(1, n + 1):
prefix_sum[i] = prefix_sum[i - 1] + dp[i]
if prefix_sum[i] >= MOD: prefix_sum[i] -= MOD
for i in range(1, n + 1):
dp[i] = prefix_sum[n] - prefix_sum[i] + prefix_sum[i - 1] - prefix_sum[i >> 1]
while dp[i] < 0: dp[i] += MOD
while dp[i] >= MOD: dp[i] -= MOD
return sum(dp) % MOD
def main():
n, a, b, k = [int(i) for i in input().split()]
if a > b:
print(solve(n - b, a - b, k))
else:
print(solve(b - 1, b - a, k))
main()
``` | output | 1 | 90,491 | 8 | 180,983 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. | instruction | 0 | 90,492 | 8 | 180,984 |
Tags: combinatorics, dp
Correct Solution:
```
def solve(n, st, k):
MOD = int(1e9 + 7)
prev = [0] * (n + 1)
current = [0] * (n + 1)
prefix_sum = [0] * (n + 1)
prev[st] = 1
for times in range(k):
prefix_sum[0] = 0
for i in range(1, n + 1):
prefix_sum[i] = prefix_sum[i - 1] + prev[i]
if prefix_sum[i] >= MOD:
prefix_sum[i] -= MOD
for i in range(1, n + 1):
current[i] = prefix_sum[n] - prefix_sum[i >> 1] - prev[i]
while current[i] < 0: current[i] += MOD
while current[i] >= MOD: current[i] -= MOD
prev, current = current, prev
return sum(prev) % MOD
def main():
n, a, b, k = [int(i) for i in input().split()]
if a > b:
print(solve(n - b, a - b, k))
else:
print(solve(b - 1, b - a, k))
main()
``` | output | 1 | 90,492 | 8 | 180,985 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. | instruction | 0 | 90,493 | 8 | 180,986 |
Tags: combinatorics, dp
Correct Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
mod = 1000000007
n, a, b, k = rint()
if a > b:
a, b = n-a+1, n-b+1
a -= 1
b -= 1
printd(n, a, b, k)
d = [0]*n
d[a] = 1
ps = [0]*b
ps[0] = d[0]
for j in range(1, b):
ps[j] = ps[j-1]+d[j]
ps[j] %= mod
printd(n, a, b, k)
printd(d, ps)
for i in range(k):
for j in range(b):
#b-t > t-j
#2*t < b+j
#t < (b+j)/2
if (b+j)%2:
t = (b+j)//2
else:
t = (b+j)//2 - 1
if j == 0:
d[j] = ps[t] - ps[j]
else:
d[j] = ps[t] - ps[j] + ps[j-1]
d[j] %= mod
#d[j] %=mod
ps[0] = d[0]
for j in range(1, b):
ps[j] = (ps[j-1]+d[j])# %mod
ps[j] %= mod
printd(d,ps)
ans = ps[b-1]
print(ans%mod)
``` | output | 1 | 90,493 | 8 | 180,987 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. | instruction | 0 | 90,494 | 8 | 180,988 |
Tags: combinatorics, dp
Correct Solution:
```
from sys import stdin
#parser
def parser():
return map(int, stdin.readline().split())
#Guardando el valor de 10^9+7
div=pow(10,9)+7
#Recibiendo los valores de n,a,b,k de la entrada
n,a,b,k=parser()
#Actualizando los valores de a,b y n
if a>b:
a=n-a+1
n=n-b
b=n+1
else:
n=b-1
#Lista para ir guardando las sumas parciales
prefix_sum=[0 for x in range(n+1)]
#Lista para ir guandando los valores d(i,j)
secuences=[0 for x in range(n+1)]
secuences[a]=1
for i in range(k):
#0 piso ficticio que se usa como comodΓn
prefix_sum[0]=secuences[0]
#Calculando los valores de las sumas parciales
for j in range(1,n+1):
prefix_sum[j]=prefix_sum[j-1]+secuences[j]
prefix_sum[j]%=div
#Calculando los nuevos valores de ''secuences''
for j in range(1,n+1):
distance=b-j
mid_distance=0
#distancia necesaria entre el piso j y un piso de mayor numeracion que el j
if distance % 2 == 0:
mid_distance=distance//2-1
else:
mid_distance=distance//2
#prefix_sum[j-1] es la cantidad de formas de alcanzar el piso j por uno de menor numeracion
#prefix_sum[j+mid_distance]-prefix_sum[j] es la cantidad de formas de alcanzar el piso j por un piso de mayor numeracion
secuences[j]=prefix_sum[j-1]+prefix_sum[j+mid_distance]-prefix_sum[j]
secuences[j]%=div
#Imprimiendo el resultado
print(sum(secuences)%div)
``` | output | 1 | 90,494 | 8 | 180,989 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. | instruction | 0 | 90,495 | 8 | 180,990 |
Tags: combinatorics, dp
Correct Solution:
```
def solve(n, st, k):
MOD = int(1e9 + 7)
dp = [0] * (n + 1)
prefix_sum = [0] * (n + 1)
dp[st] = 1
for times in range(k):
prefix_sum[0] = 0
for i in range(1, n + 1):
prefix_sum[i] = prefix_sum[i - 1] + dp[i]
if prefix_sum[i] >= MOD: prefix_sum[i] -= MOD
for i in range(1, n + 1):
dp[i] = prefix_sum[n] - dp[i] - prefix_sum[i >> 1]
while dp[i] < 0: dp[i] += MOD
while dp[i] >= MOD: dp[i] -= MOD
return sum(dp) % MOD
def main():
n, a, b, k = [int(i) for i in input().split()]
if a > b:
print(solve(n - b, a - b, k))
else:
print(solve(b - 1, b - a, k))
main()
``` | output | 1 | 90,495 | 8 | 180,991 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. | instruction | 0 | 90,496 | 8 | 180,992 |
Tags: combinatorics, dp
Correct Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
mod = 1000000007
n, a, b, k = rint()
if a > b:
a, b = n-a+1, n-b+1
a -= 1
b -= 1
printd(n, a, b, k)
d = [0]*n
d[a] = 1
ps = [0]*b
ps[0] = d[0]
for j in range(1, b):
ps[j] = ps[j-1]+d[j]
while ps[j] > mod:
ps[j] -= mod
printd(n, a, b, k)
printd(d, ps)
for i in range(k):
for j in range(b):
#b-t > t-j
#2*t < b+j
#t < (b+j)/2
if (b+j)%2:
t = (b+j)//2
else:
t = (b+j)//2 - 1
if j == 0:
d[j] = ps[t] - ps[j]
else:
d[j] = ps[t] - ps[j] + ps[j-1]
while d[j] > mod:
d[j] -= mod
while d[j] <0:
d[j] += mod
#d[j] %=mod
ps[0] = d[0]
for j in range(1, b):
ps[j] = (ps[j-1]+d[j])# %mod
while ps[j] > mod:
ps[j] -= mod
while ps[j] < 0:
ps[j] += mod
printd(d,ps)
ans = ps[b-1]
print(ans%mod)
``` | output | 1 | 90,496 | 8 | 180,993 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. | instruction | 0 | 90,497 | 8 | 180,994 |
Tags: combinatorics, dp
Correct Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
mod = 1000000007
n, a, b, k = rint()
if a > b:
a, b = n-a+1, n-b+1
a -= 1
b -= 1
printd(n, a, b, k)
d = [0]*n
d[a] = 1
ps = [0]*b
ps[0] = d[0]
for j in range(1, b):
ps[j] = ps[j-1]+d[j]
while ps[j] > mod:
ps[j] -= mod
ps[j] %= mod
printd(n, a, b, k)
printd(d, ps)
for i in range(k):
for j in range(b):
#b-t > t-j
#2*t < b+j
#t < (b+j)/2
if (b+j)%2:
t = (b+j)//2
else:
t = (b+j)//2 - 1
if j == 0:
d[j] = ps[t] - ps[j]
else:
d[j] = ps[t] - ps[j] + ps[j-1]
while d[j] > mod:
d[j] -= mod
while d[j] <0:
d[j] += mod
d[j] %= mod
#d[j] %=mod
ps[0] = d[0]
for j in range(1, b):
ps[j] = (ps[j-1]+d[j])# %mod
while ps[j] > mod:
ps[j] -= mod
while ps[j] < 0:
ps[j] += mod
ps[j] %= mod
printd(d,ps)
ans = ps[b-1]
print(ans%mod)
``` | output | 1 | 90,497 | 8 | 180,995 |
Provide tags and a correct Python 2 solution for this coding contest problem.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. | instruction | 0 | 90,498 | 8 | 180,996 |
Tags: combinatorics, dp
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
mod=10**9+7
n,a,b,k=in_arr()
dp=[[0 for i in range(n+1)] for j in range(k+1)]
dp[0][a-1]=1
dp[0][a]=(-1)%mod
b-=1
for i in range(k):
temp=0
for j in range(n):
temp=(temp+dp[i][j])%mod
x=int(abs(b-j))
x-=1
if x<=0:
continue
dp[i+1][max(0,j-x)]=(dp[i+1][max(0,j-x)]+temp)%mod
dp[i+1][min(n,j+x+1)]=(dp[i+1][min(n,j+x+1)]-temp)%mod
dp[i+1][j]=(dp[i+1][j]-temp)%mod
dp[i+1][j+1]=(dp[i+1][j+1]+temp)%mod
ans=0
temp=0
for i in range(n):
temp+=dp[k][i]
temp%=mod
ans+=temp
ans%=mod
pr_num(ans)
``` | output | 1 | 90,498 | 8 | 180,997 |
Provide tags and a correct Python 2 solution for this coding contest problem.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. | instruction | 0 | 90,499 | 8 | 180,998 |
Tags: combinatorics, dp
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
mod=10**9+7
n,a,b,k=in_arr()
dp=[[0 for i in range(n+1)] for j in range(k+1)]
dp[0][a-1]=1
dp[0][a]=(-1)%mod
b-=1
for i in range(k):
temp=0
for j in range(n+1):
temp=(temp+dp[i][j])%mod
if i and int(abs(b-j))>1:
temp-=dp[i-1][j]
temp%=mod
dp[i][j]=temp
if j<n:
x=int(abs(b-j))
x-=1
if x<=0:
continue
dp[i+1][max(0,j-x)]=(dp[i+1][max(0,j-x)]+temp)%mod
dp[i+1][min(n,j+x+1)]=(dp[i+1][min(n,j+x+1)]-temp)%mod
#dp[i+1][j]=(dp[i+1][j]-1)%mod
#dp[i+1][j+1]=(dp[i+1][j+1]+1)%mod
if i and int(abs(b-j))>1:
temp+=dp[i-1][j]
temp%=mod
ans=0
temp=0
for i in range(n):
temp+=dp[k][i]
temp%=mod
if int(abs(i-b))>1:
temp-=dp[k-1][i]
temp%=mod
ans+=temp
ans%=mod
if int(abs(i-b))>1:
temp+=dp[k-1][i]
temp%=mod
pr_num(ans)
``` | output | 1 | 90,499 | 8 | 180,999 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
Submitted Solution:
```
from sys import stdin
#parser
def parser():
return map(int, stdin.readline().split())
mod=pow(10,9)+7
n,a,b,k=parser()
if a>b:
a=n-a+1
n=n-b
b=n+1
else:
n=b-1
prefix_sum=[0 for x in range(n+1)]
secuences=[0 for x in range(n+1)]
secuences[a]=1
for i in range(k):
prefix_sum[0]=secuences[0]
for j in range(1,n+1):
prefix_sum[j]=prefix_sum[j-1]+secuences[j]
prefix_sum[j]%=mod
for j in range(1,n+1):
distance=b-j
mid_distance=0
if distance % 2 == 0:
mid_distance=distance//2-1
else:
mid_distance=distance//2
secuences[j]=prefix_sum[j-1]+prefix_sum[j+mid_distance]-prefix_sum[j]
secuences[j]%=mod
print(sum(secuences)%mod)
``` | instruction | 0 | 90,500 | 8 | 181,000 |
Yes | output | 1 | 90,500 | 8 | 181,001 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
Submitted Solution:
```
def solve(n, st, k):
MOD = int(1e9 + 7)
dp = [0] * (n + 1)
prefix_sum = [0] * (n + 1)
dp[st] = 1
for times in range(k):
prefix_sum[0] = 0
for i in range(1, n + 1):
prefix_sum[i] = prefix_sum[i - 1] + dp[i]
if prefix_sum[i] >= MOD: prefix_sum[i] -= MOD
for i in range(1, n + 1):
t = prefix_sum[n] - prefix_sum[i] + prefix_sum[i - 1] - prefix_sum[i >> 1]
while t < 0: t += MOD
while t >= MOD: t -= MOD
dp[i] = t
return sum(dp) % MOD
def main():
n, a, b, k = [int(i) for i in input().split()]
if a > b:
print(solve(n - b, a - b, k))
else:
print(solve(b - 1, b - a, k))
main()
``` | instruction | 0 | 90,501 | 8 | 181,002 |
Yes | output | 1 | 90,501 | 8 | 181,003 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
Submitted Solution:
```
n,a,b,k = map(int,input().split())
mod = 1000000007
arr = [[0 for i in range(k+1)] for j in range(n+1)]
for i in range(1,n+1):
if i!=b and i!=a:
if abs(a-i) < abs(a-b): arr[i][1] = 1
#print(arr[2][1])
for i in range(2,k+1):
for j in range(1,n+1):
arr[j][i-1] = (arr[j][i-1]%mod + arr[j-1][i-1])%mod
for j in range(1,n+1):
if b>(j+1) and a<b:
r = j+(b-j-1)//2
arr[j][i] = ((arr[r][i-1]-arr[j][i-1]) + (arr[j-1][i-1]-arr[0][i-1]))%mod
elif b<(j-1) and a>b:
l = j-(j-b-1)//2
arr[j][i] = ((arr[j-1][i-1]-arr[l-1][i-1]) + (arr[n][i-1]-arr[j][i-1]))%mod
elif b==(j+1) and a<b:
arr[j][i] = (arr[j-1][i-1]-arr[0][i-1])
elif b==(j-1) and a>b:
arr[j][i] = (arr[n][i-1]-arr[j][i-1])
req = 0
for i in range(1,n+1):
req = (req+arr[i][k])%mod
print(req%mod)
``` | instruction | 0 | 90,502 | 8 | 181,004 |
Yes | output | 1 | 90,502 | 8 | 181,005 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
Submitted Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
mod = 1000000007
n, a, b, k = rint()
if a > b:
a, b = n-a+1, n-b+1
a -= 1
b -= 1
printd(n, a, b, k)
d = [0]*n
d[a] = 1
ps = [0]*b
ps[0] = d[0]
for j in range(1, b):
ps[j] = ps[j-1]+d[j]
ps[j] %= mod
while ps[j] > mod:
ps[j] -= mod
printd(n, a, b, k)
printd(d, ps)
for i in range(k):
for j in range(b):
#b-t > t-j
#2*t < b+j
#t < (b+j)/2
if (b+j)%2:
t = (b+j)//2
else:
t = (b+j)//2 - 1
if j == 0:
d[j] = ps[t] - ps[j]
else:
d[j] = ps[t] - ps[j] + ps[j-1]
d[j] %= mod
while d[j] > mod:
d[j] -= mod
while d[j] <0:
d[j] += mod
#d[j] %=mod
ps[0] = d[0]
for j in range(1, b):
ps[j] = (ps[j-1]+d[j])# %mod
ps[j] %= mod
while ps[j] > mod:
ps[j] -= mod
while ps[j] < 0:
ps[j] += mod
printd(d,ps)
ans = ps[b-1]
print(ans%mod)
``` | instruction | 0 | 90,503 | 8 | 181,006 |
Yes | output | 1 | 90,503 | 8 | 181,007 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
Submitted Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
mod=10**9+7
n,a,b,k=in_arr()
dp=[[0 for i in range(n+1)] for j in range(k+1)]
dp[0][a-1]=1
dp[0][a]=-1
b-=1
for i in range(k):
temp=0
for j in range(n+1):
temp=(temp+dp[i][j])%mod
if i and int(abs(b-j))>1:
temp-=dp[i-1][j]
temp%=mod
if j<n:
x=int(abs(b-j))
x-=1
if x<=0:
continue
dp[i+1][max(0,j-x)]=(dp[i+1][max(0,j-x)]+temp)%mod
dp[i+1][min(n,j+x+1)]=(dp[i+1][min(n,j+x+1)]-temp)%mod
#dp[i+1][j]=(dp[i+1][j]-1)%mod
#dp[i+1][j+1]=(dp[i+1][j+1]+1)%mod
if i and int(abs(b-j))>1:
temp+=dp[i-1][j]
temp%=mod
ans=0
temp=0
for i in range(n):
temp+=dp[k][i]
temp%=mod
if int(abs(i-b))>1:
temp-=dp[k-1][i]
temp%=mod
ans+=temp
ans%=mod
if int(abs(i-b))>1:
temp+=dp[k-1][i]
temp%=mod
pr_num(ans)
``` | instruction | 0 | 90,504 | 8 | 181,008 |
No | output | 1 | 90,504 | 8 | 181,009 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
Submitted Solution:
```
n,a,b,k=map(int,input().split())
dp=[[0 for i in range(n+1)] for j in range(2)]
dp[0][a]=1
now=1
last=0
mod=1000000007
for i in range(k):
for j in range(1,n+1):
d=max(abs(j-b)-1,0)
if j!=n:
dp[now][j+1]=(dp[last][j]+dp[now][j+1])%mod
dp[now][min(j+d+1,n)]=(dp[now][min(j+d+1,n)]-dp[last][j])%mod
if j!=1:
dp[now][j]=(dp[now][j]-dp[last][j])%mod
dp[now][max(j-d,1)]=(dp[now][max(j-d,1)]+dp[last][j])%mod
for i1 in range(1,n+1):
dp[now][i1]=(dp[now][i1]+dp[now][i1-1])%mod
dp[last][i1]=0
aux=now
now=last
last=aux
print(sum(dp[last])%mod)
``` | instruction | 0 | 90,505 | 8 | 181,010 |
No | output | 1 | 90,505 | 8 | 181,011 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
Submitted Solution:
```
n,a,b,k=map(int,input().split())
dp=[[0 for i in range(n+1)] for j in range(a)]
dp[0][a]=1
now=1
last=0
mod=1000000007
for i in range(k):
for j in range(1,n+1):
d=abs(j-b)-1
if j!=n:
dp[now][j+1]=(dp[last][j]+dp[now][j+1])%mod
dp[now][min(j+d+1,n)]=(dp[now][min(j+d+1,n)]+dp[last][j])%mod
if j!=1:
dp[now][j]=(dp[now][j]-dp[last][j])%mod
dp[now][max(j-d,1)]=(dp[now][max(j-d,1)]+dp[last][j])%mod
for i in range(1,n+1):
dp[now][i]=(dp[now][i]+dp[now][i-1])%mod
dp[last][i]=0
aux=now
now=last
last=aux
print(sum(dp[last])%mod)
``` | instruction | 0 | 90,506 | 8 | 181,012 |
No | output | 1 | 90,506 | 8 | 181,013 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
Input
The first line of the input contains four space-separated integers n, a, b, k (2 β€ n β€ 5000, 1 β€ k β€ 5000, 1 β€ a, b β€ n, a β b).
Output
Print a single integer β the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
Examples
Input
5 2 4 1
Output
2
Input
5 2 4 2
Output
2
Input
5 3 4 1
Output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 β€ j β€ k), that pj β qj.
Notes to the samples:
1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
Submitted Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
mod = 1000000007
n, a, b, k = rint()
a -= 1
b -= 1
d = [0]*n
d[a] = 1
ps = [0]*n
ps[0] = d[0]
for j in range(1, n):
ps[j] = ps[j-1]+d[j]
printd(n, a, b, k)
printd(d, ps)
for i in range(k):
d = [0]*n
for j in range(1, b):
d[j] = ps[j-1]
for j in range(b):
#b-t > t-j
#2*t < b+j
#t < (b+j)/2
if (b+j)%2:
t = (b+j)//2
else:
t = (b+j)//2 - 1
d[j] += ps[t] - ps[j]
ps = [0]*n
ps[0] = d[0]
for j in range(1, n):
ps[j] = (ps[j-1]+d[j])%mod
printd(d,ps)
print(ps[n-1]%mod)
``` | instruction | 0 | 90,507 | 8 | 181,014 |
No | output | 1 | 90,507 | 8 | 181,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you'd like to buy several of them. The store has n different matryoshkas. Any matryoshka is a figure of volume out_i with an empty space inside of volume in_i (of course, out_i > in_i).
You don't have much free space inside your bag, but, fortunately, you know that matryoshkas can be nested one inside another. Formally, let's call a set of matryoshkas nested if we can rearrange dolls in such a way, that the first doll can be nested inside the second one, the second doll β inside the third one and so on. Matryoshka i can be nested inside matryoshka j if out_i β€ in_j. So only the last doll will take space inside your bag.
Let's call extra space of a nested set of dolls as a total volume of empty space inside this structure. Obviously, it's equal to in_{i_1} + (in_{i_2} - out_{i_1}) + (in_{i_3} - out_{i_2}) + ... + (in_{i_k} - out_{i_{k-1}}), where i_1, i_2, ..., i_k are the indices of the chosen dolls in the order they are nested in each other.
Finally, let's call a nested subset of the given sequence as big enough if there isn't any doll from the sequence that can be added to the nested subset without breaking its nested property.
You want to buy many matryoshkas, so you should choose a big enough nested subset to buy it. But you will be disappointed if too much space in your bag will be wasted, so you want to choose a big enough subset so that its extra space is minimum possible among all big enough subsets. Now you wonder, how many different nested subsets meet these conditions (they are big enough, and there is no big enough subset such that its extra space is less than the extra space of the chosen subset). Two subsets are considered different if there exists at least one index i such that one of the subsets contains the i-th doll, and another subset doesn't.
Since the answer can be large, print it modulo 10^9 + 7.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of matryoshkas.
The next n lines contain a description of each doll: two integers out_i and in_i (1 β€ in_i < out_i β€ 10^9) β the outer and inners volumes of the i-th matryoshka.
Output
Print one integer β the number of big enough nested subsets such that extra space of each of these subsets is minimum possible. Since the answer can be large, print it modulo 10^9 + 7.
Example
Input
7
4 1
4 2
4 2
2 1
5 4
6 4
3 2
Output
6
Note
There are 6 big enough nested subsets with minimum possible extra space in the example:
* \{1, 5\}: we can't add any other matryoshka and keep it nested; it's extra space is 1;
* \{1, 6\};
* \{2, 4, 5\};
* \{2, 4, 6\};
* \{3, 4, 5\};
* \{3, 4, 6\}.
There are no more "good" subsets because, for example, subset \{6, 7\} is not big enough (we can add the 4-th matryoshka to it) or subset \{4, 6, 7\} has extra space equal to 2. | instruction | 0 | 91,004 | 8 | 182,008 |
Tags: binary search, combinatorics, data structures, dp, shortest paths, sortings
Correct Solution:
```
from sys import stdin, stdout
mod = 10**9+7
n = int(input())
dolls = []
for i in range(n):
o, i = map(int, stdin.readline().split())
dolls.append((o, i))
dolls.sort()
dolls = [(i, o) for (o, i) in dolls]
#print(dolls)
def bin_search(i):
lo = -1
hi = n-1
while lo+1 < hi:
mid = (lo+hi-1)//2 + 1
top = dolls[mid][1]
if top <= i:
lo = mid
else:
hi = mid
return hi
counts = [1]*(n+1)
dp = [0]*(n+1)
for k in range(n):
i, o = dolls[k]
m_prev = dp[k]+o-(dolls[k-1][1] if k > 0 else 0)
kk = bin_search(i)
m_with = dp[kk] + i-(dolls[kk-1][1] if kk > 0 else 0)
if m_prev < m_with:
dp[k+1] = m_prev
counts[k+1] = counts[k]
elif m_prev > m_with:
dp[k+1] = m_with
counts[k+1] = counts[kk]
else:
dp[k+1] = m_prev
counts[k+1] = (counts[k]+counts[kk]) % mod
#print(dp)
#print(counts)
best = 10**9+10
best_count = 0
maximal = max([i for i, o in dolls])
for i in range(bin_search(maximal), n):
ii = bin_search(dolls[i][0])
cur = dp[ii] + dolls[i][0] - (dolls[ii-1][1] if ii > 0 else 0)
#print(cur, "via", ii)
if cur < best:
best = cur
best_count = counts[ii]
elif cur == best:
best_count += counts[ii]
best_count %= mod
print(best_count)
``` | output | 1 | 91,004 | 8 | 182,009 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.