source
stringclasses 4
values | task_type
stringclasses 1
value | in_source_id
stringlengths 0
138
| problem
stringlengths 219
13.2k
| gold_standard_solution
stringlengths 0
413k
| problem_id
stringlengths 5
10
| metadata
dict | verification_info
dict |
---|---|---|---|---|---|---|---|
codeforces
|
verifiable_code
|
570
|
Solve the following coding problem using the programming language python:
The country of Byalechinsk is running elections involving *n* candidates. The country consists of *m* cities. We know how many people in each city voted for each candidate.
The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.
At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.
Determine who will win the elections.
The input will be provided via standard input and looks as follows:
The first line of the input contains two integers *n*, *m* (1<=≤<=*n*,<=*m*<=≤<=100) — the number of candidates and of cities, respectively.
Each of the next *m* lines contains *n* non-negative integers, the *j*-th number in the *i*-th line *a**ij* (1<=≤<=*j*<=≤<=*n*, 1<=≤<=*i*<=≤<=*m*, 0<=≤<=*a**ij*<=≤<=109) denotes the number of votes for candidate *j* in city *i*.
It is guaranteed that the total number of people in all the cities does not exceed 109.
You should write the solution as standard output as follows:
Print a single number — the index of the candidate who won the elections. The candidates are indexed starting from one.
Here are example inputs and outputs for the problem:
Example Input 1: 3 3
1 2 3
2 3 1
1 2 1
Example Output 1: 2
Example Input 2: 3 4
10 10 3
5 1 6
2 2 2
1 5 7
Example Output 2: 1
Now solve the problem by providing the code.
|
m, n = input().split()
cand = []
for i in range (int(m)):
cand.append(0)
for i in range (int(n)):
a = [int(x) for x in input().split()]
cand[a.index(max(a))] += 1
print(cand.index(max(cand)) + 1)
|
vfc_84225
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 3\n1 2 3\n2 3 1\n1 2 1",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 3\n5\n3\n2",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 1\n1 2 3",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 1\n100 100 100",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 2\n1 2\n2 1",
"output": "1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
54
|
Solve the following coding problem using the programming language python:
The Hedgehog likes to give presents to his friend, but no less he likes to receive them.
Having received another present today, the Hedgehog suddenly understood that he has no place to put it as there was no room left on the special shelf in the cupboard. He will have to choose another shelf, but which one should he choose, how large should it be?
In order to get to know this, the Hedgehog asks you to write him a program that will count the estimated number of presents that he will receive during the following *N* days. Besides, he is guided by the principle:
- on each holiday day the Hedgehog will necessarily receive a present, - he receives presents at least every *K* days (i.e., if he received a present on the *i*-th day, he will receive the next present no later than on the *i*<=+<=*K*-th day).
For the given *N* and *K*, as well as the list of holidays among the following *N* days count the minimal number of presents that could be given to the Hedgehog. The number of today's day is zero, and you should regard today's present as already given (i.e., you shouldn't count it in the answer).
The input will be provided via standard input and looks as follows:
The first line contains integers *N* and *K* (1<=≤<=*N*<=≤<=365, 1<=≤<=*K*<=≤<=*N*).
The second line contains a number *C* which represents the number of holidays (0<=≤<=*C*<=≤<=*N*). Then in the same line follow *C* numbers ranging from 1 to *N* which are the numbers of holiday days. The numbers are given in the increasing order, without repeating numbers among them.
You should write the solution as standard output as follows:
Print a single number — the minimal number of presents the Hedgehog will receive over the following *N* days.
Here are example inputs and outputs for the problem:
Example Input 1: 5 2
1 3
Example Output 1: 3
Example Input 2: 10 1
3 6 7 8
Example Output 2: 10
Now solve the problem by providing the code.
|
n, k = list(map(int,input().strip().split(' ')))
holidays = list(map(int,input().strip().split(' ')))[1:]
prev = 0
i = 0
inc = 0
pres = 0
while i < n:
i += 1
inc += 1
if inc == k or i in holidays:
prev = i
inc = 0
pres += 1
print(pres)
|
vfc_84229
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 2\n1 3",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 1\n3 6 7 8",
"output": "10",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 5\n1 3",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 3\n3 3 6 9",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 2\n0",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1\n0",
"output": "1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
426
|
Solve the following coding problem using the programming language python:
Sereja showed an interesting game to his friends. The game goes like that. Initially, there is a table with an empty cup and *n* water mugs on it. Then all players take turns to move. During a move, a player takes a non-empty mug of water and pours all water from it into the cup. If the cup overfills, then we assume that this player lost.
As soon as Sereja's friends heard of the game, they wanted to play it. Sereja, on the other hand, wanted to find out whether his friends can play the game in such a way that there are no losers. You are given the volumes of all mugs and the cup. Also, you know that Sereja has (*n*<=-<=1) friends. Determine if Sereja's friends can play the game so that nobody loses.
The input will be provided via standard input and looks as follows:
The first line contains integers *n* and *s* (2<=≤<=*n*<=≤<=100; 1<=≤<=*s*<=≤<=1000) — the number of mugs and the volume of the cup. The next line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=10). Number *a**i* means the volume of the *i*-th mug.
You should write the solution as standard output as follows:
In a single line, print "YES" (without the quotes) if his friends can play in the described manner, and "NO" (without the quotes) otherwise.
Here are example inputs and outputs for the problem:
Example Input 1: 3 4
1 1 1
Example Output 1: YES
Example Input 2: 3 4
3 1 3
Example Output 2: YES
Example Input 3: 3 4
4 4 4
Example Output 3: NO
Now solve the problem by providing the code.
|
k = int(input().split()[1])
if sum(sorted(list(map(int,input().split())))[:-1]) > k:
print('NO')
else:
print('YES')
|
vfc_84233
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 4\n1 1 1",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 4\n3 1 3",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 4\n4 4 4",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 1\n1 10",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 12\n5 6 6",
"output": "YES",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
550
|
Solve the following coding problem using the programming language python:
You are given string *s*. Your task is to determine if the given string *s* contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
The input will be provided via standard input and looks as follows:
The only line of input contains a string *s* of length between 1 and 105 consisting of uppercase Latin letters.
You should write the solution as standard output as follows:
Print "YES" (without the quotes), if string *s* contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
Here are example inputs and outputs for the problem:
Example Input 1: ABA
Example Output 1: NO
Example Input 2: BACFAB
Example Output 2: YES
Example Input 3: AXBYBXA
Example Output 3: NO
Now solve the problem by providing the code.
|
s = input()
if 'AB' in s and 'BA' in s:
# Find the first occurrence of "AB" and the first occurrence of "BA" after the "AB"
ab_index = s.find('AB')
ba_index = s.find('BA', ab_index + 2)
# Check if the substrings are non-overlapping
if ba_index != -1 and ba_index != ab_index + 1:
print('YES')
else:
# Find the first occurrence of "BA" and the first occurrence of "AB" after the "BA"
ba_index = s.find('BA')
ab_index = s.find('AB', ba_index + 2)
# Check if the substrings are non-overlapping
if ab_index != -1 and ab_index != ba_index + 1:
print('YES')
else:
print('NO')
else:
print('NO')
|
vfc_84237
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "ABA",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "BACFAB",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "AXBYBXA",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "ABABAB",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "BBBBBBBBBB",
"output": "NO",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
382
|
Solve the following coding problem using the programming language python:
Ksenia has ordinary pan scales and several weights of an equal mass. Ksenia has already put some weights on the scales, while other weights are untouched. Ksenia is now wondering whether it is possible to put all the remaining weights on the scales so that the scales were in equilibrium.
The scales is in equilibrium if the total sum of weights on the left pan is equal to the total sum of weights on the right pan.
The input will be provided via standard input and looks as follows:
The first line has a non-empty sequence of characters describing the scales. In this sequence, an uppercase English letter indicates a weight, and the symbol "|" indicates the delimiter (the character occurs in the sequence exactly once). All weights that are recorded in the sequence before the delimiter are initially on the left pan of the scale. All weights that are recorded in the sequence after the delimiter are initially on the right pan of the scale.
The second line contains a non-empty sequence containing uppercase English letters. Each letter indicates a weight which is not used yet.
It is guaranteed that all the English letters in the input data are different. It is guaranteed that the input does not contain any extra characters.
You should write the solution as standard output as follows:
If you cannot put all the weights on the scales so that the scales were in equilibrium, print string "Impossible". Otherwise, print the description of the resulting scales, copy the format of the input.
If there are multiple answers, print any of them.
Here are example inputs and outputs for the problem:
Example Input 1: AC|T
L
Example Output 1: AC|TL
Example Input 2: |ABC
XYZ
Example Output 2: XYZ|ABC
Example Input 3: W|T
F
Example Output 3: Impossible
Example Input 4: ABC|
D
Example Output 4: Impossible
Now solve the problem by providing the code.
|
def main():
mode="filee"
if mode=="file":f=open("test.txt","r")
#f.readline()
#input()
get = lambda :[str(x) for x in (f.readline()[:-1] if mode=="file" else input()).split("|")]
scale = get()
q = input()
if abs(len(scale[0]) - len(scale[1]))%2 != len(q)%2 or abs(len(scale[0]) - len(scale[1]))>len(q):
print("Impossible")
return
if len(scale[0])<len(scale[1]):
mid = len(scale[1])-len(scale[0])
scale[0]+=q[:mid]
else:
mid = len(scale[0])-len(scale[1])
scale[1]+=q[:mid]
for z in range(mid,len(q),2):
scale[0]+=q[z]
scale[1]+=q[z+1]
print("|".join(scale))
if mode=="file":f.close()
if __name__=="__main__":
main()
|
vfc_84241
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "AC|T\nL",
"output": "AC|TL",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "|ABC\nXYZ",
"output": "XYZ|ABC",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "W|T\nF",
"output": "Impossible",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "ABC|\nD",
"output": "Impossible",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "A|BC\nDEF",
"output": "ADF|BCE",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "|\nABC",
"output": "Impossible",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
843
|
Solve the following coding problem using the programming language python:
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to *x*.
More formally, there is a singly liked list built on an array of *n* elements. Element with index *i* contains two integers: *value**i* is the integer value in this element, and *next**i* that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if *next**i*<=≠<=<=-<=1, then *value**next**i*<=><=*value**i*.
You are given the number of elements in the list *n*, the index of the first element *start*, and the integer *x*.
You can make up to 2000 queries of the following two types:
- ? i (1<=≤<=*i*<=≤<=*n*) — ask the values *value**i* and *next**i*, - ! ans — give the answer for the problem: the minimum integer, greater than or equal to *x*, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
The input will be provided via standard input and looks as follows:
The first line contains three integers *n*, *start*, *x* (1<=≤<=*n*<=≤<=50000, 1<=≤<=*start*<=≤<=*n*, 0<=≤<=*x*<=≤<=109) — the number of elements in the list, the index of the first element and the integer *x*.
You should write the solution as standard output as follows:
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to *x*, or -1, if there is no such integer.
Here are example inputs and outputs for the problem:
Example Input 1: 5 3 80
97 -1
58 5
16 2
81 1
79 4
Example Output 1: ? 1
? 2
? 3
? 4
? 5
! 81
Now solve the problem by providing the code.
|
from random import sample
from sys import stdout
read=lambda :map(int,input().split())
def ask(x):
print('? {}'.format(x))
stdout.flush()
return tuple(read())
def answer(x):
print('! {}'.format(x))
stdout.flush()
exit(0)
n,st,x=read()
a=[ask(st)]
if a[0][0]>=x: answer(a[0][0])
for i in sample(range(1,n+1),min(n,999)):
b=ask(i)
if b[0]<x: a.append(b)
elif b[0]==x: answer(x)
b=max(a)
while True:
if b[0]>=x: answer(b[0])
if b[1]==-1: break
else: b=ask(b[1])
answer(-1)
|
vfc_84245
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 3 80\n97 -1\n58 5\n16 2\n81 1\n79 4",
"output": "81\n1003",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 1 6\n1 2\n2 3\n3 4\n4 5\n5 -1",
"output": "-1\n1002",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1 0\n0 -1",
"output": "0\n2",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
482
|
Solve the following coding problem using the programming language python:
Permutation *p* is an ordered set of integers *p*1,<=<=<=*p*2,<=<=<=...,<=<=<=*p**n*, consisting of *n* distinct positive integers not larger than *n*. We'll denote as *n* the length of permutation *p*1,<=<=<=*p*2,<=<=<=...,<=<=<=*p**n*.
Your task is to find such permutation *p* of length *n*, that the group of numbers |*p*1<=-<=*p*2|,<=|*p*2<=-<=*p*3|,<=...,<=|*p**n*<=-<=1<=-<=*p**n*| has exactly *k* distinct elements.
The input will be provided via standard input and looks as follows:
The single line of the input contains two space-separated positive integers *n*, *k* (1<=≤<=*k*<=<<=*n*<=≤<=105).
You should write the solution as standard output as follows:
Print *n* integers forming the permutation. If there are multiple answers, print any of them.
Here are example inputs and outputs for the problem:
Example Input 1: 3 2
Example Output 1: 1 3 2
Example Input 2: 3 1
Example Output 2: 1 2 3
Example Input 3: 5 2
Example Output 3: 1 3 2 4 5
Now solve the problem by providing the code.
|
n, k = map(int, input().split())
cur = 1
x = 0
a = [0] * (n + 1)
while k > 0:
a[cur] = 1
print(cur, end=(' '))
if x == 0:
cur = cur + k
x = 1
else:
cur = cur - k
x = 0
k = k - 1
for i in range(1, n + 1):
if a[i] == 0:
print(i, end=' ')
|
vfc_84253
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 2",
"output": "1 3 2",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
825
|
Solve the following coding problem using the programming language python:
Polycarp has just invented a new binary protocol for data transmission. He is encoding positive integer decimal number to binary string using following algorithm:
- Each digit is represented with number of '1' characters equal to the value of that digit (for 0 it is zero ones). - Digits are written one by one in order corresponding to number and separated by single '0' character.
Though Polycarp learnt how to encode the numbers, he has no idea how to decode them back. Help him calculate the decoded number.
The input will be provided via standard input and looks as follows:
The first line contains one integer number *n* (1<=≤<=*n*<=≤<=89) — length of the string *s*.
The second line contains string *s* — sequence of '0' and '1' characters, number in its encoded format. It is guaranteed that the number corresponding to the string is positive and doesn't exceed 109. The string always starts with '1'.
You should write the solution as standard output as follows:
Print the decoded number.
Here are example inputs and outputs for the problem:
Example Input 1: 3
111
Example Output 1: 3
Example Input 2: 9
110011101
Example Output 2: 2031
Now solve the problem by providing the code.
|
n = int(input())
# print(n)
s = input()
# print(s)
k = 0
ans = ''
for c in s:
# print(c)
if c == '0':
ans += str(k)
k = 0
else:
k += 1
print(ans + str(k))
|
vfc_84257
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n111",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "9\n110011101",
"output": "2031",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1",
"output": "1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
416
|
Solve the following coding problem using the programming language python:
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show.
The rules are simple. The host thinks of an integer *y* and the participants guess it by asking questions to the host. There are four types of acceptable questions:
- Is it true that *y* is strictly larger than number *x*? - Is it true that *y* is strictly smaller than number *x*? - Is it true that *y* is larger than or equal to number *x*? - Is it true that *y* is smaller than or equal to number *x*?
On each question the host answers truthfully, "yes" or "no".
Given the sequence of questions and answers, find any integer value of *y* that meets the criteria of all answers. If there isn't such value, print "Impossible".
The input will be provided via standard input and looks as follows:
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=10000) — the number of questions (and answers). Next *n* lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the sign is:
- ">" (for the first type queries), - "<" (for the second type queries), - ">=" (for the third type queries), - "<=" (for the fourth type queries).
All values of *x* are integer and meet the inequation <=-<=109<=≤<=*x*<=≤<=109. The answer is an English letter "Y" (for "yes") or "N" (for "no").
Consequtive elements in lines are separated by a single space.
You should write the solution as standard output as follows:
Print any of such integers *y*, that the answers to all the queries are correct. The printed number *y* must meet the inequation <=-<=2·109<=≤<=*y*<=≤<=2·109. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes).
Here are example inputs and outputs for the problem:
Example Input 1: 4
>= 1 Y
< 3 N
<= -3 N
> 55 N
Example Output 1: 17
Example Input 2: 2
> 100 Y
< -100 Y
Example Output 2: Impossible
Now solve the problem by providing the code.
|
"""
██╗ ██████╗ ██╗ ██████╗ ██████╗ ██╗ █████╗
██║██╔═══██╗██║ ╚════██╗██╔═████╗███║██╔══██╗
██║██║ ██║██║ █████╔╝██║██╔██║╚██║╚██████║
██║██║ ██║██║ ██╔═══╝ ████╔╝██║ ██║ ╚═══██║
██║╚██████╔╝██║ ███████╗╚██████╔╝ ██║ █████╔╝
╚═╝ ╚═════╝ ╚═╝ ╚══════╝ ╚═════╝ ╚═╝ ╚════╝
██████╗ ██╗██╗ ███████╗██╗ ██╗ ██████╗ ██████╗
██╔══██╗██║██║ ██╔════╝██║ ██║██╔═══██╗██╔══██╗
██║ ██║██║██║ ███████╗███████║██║ ██║██║ ██║
██║ ██║██║██║ ╚════██║██╔══██║██║ ██║██║ ██║
██████╔╝██║███████╗███████║██║ ██║╚██████╔╝██████╔╝
╚═════╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝
"""
m, M = -2000000000, 2000000000
n = int(input())
for i in range(n):
a, b, c = map(str, input().split())
b = int(b)
if c == "Y":
if a == ">":
m = max(b + 1, m)
elif a == ">=":
m = max(b, m)
elif a == "<":
M = min(b - 1, M)
elif a == "<=":
M = min(M, b)
else:
if a == ">":
M = min(b, M)
elif a == ">=":
M = min(b - 1, M)
elif a == "<":
m = max(m, b)
else:
m = max(b + 1, m)
if m > M:print("Impossible")
else:print(m)
|
vfc_84265
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n>= 1 Y\n< 3 N\n<= -3 N\n> 55 N",
"output": "17",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n> 100 Y\n< -100 Y",
"output": "Impossible",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n< 1 N\n> 1 N\n> 1 N\n> 1 N",
"output": "1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
108
|
Solve the following coding problem using the programming language python:
Tattah is asleep if and only if Tattah is attending a lecture. This is a well-known formula among Tattah's colleagues.
On a Wednesday afternoon, Tattah was attending Professor HH's lecture. At 12:21, right before falling asleep, he was staring at the digital watch around Saher's wrist. He noticed that the digits on the clock were the same when read from both directions i.e. a palindrome.
In his sleep, he started dreaming about such rare moments of the day when the time displayed on a digital clock is a palindrome. As soon as he woke up, he felt destined to write a program that finds the next such moment.
However, he still hasn't mastered the skill of programming while sleeping, so your task is to help him.
The input will be provided via standard input and looks as follows:
The first and only line of the input starts with a string with the format "HH:MM" where "HH" is from "00" to "23" and "MM" is from "00" to "59". Both "HH" and "MM" have exactly two digits.
You should write the solution as standard output as follows:
Print the palindromic time of day that comes soonest after the time given in the input. If the input time is palindromic, output the soonest palindromic time after the input time.
Here are example inputs and outputs for the problem:
Example Input 1: 12:21
Example Output 1: 13:31
Example Input 2: 23:59
Example Output 2: 00:00
Now solve the problem by providing the code.
|
s = input()
s1 = s
s1 = s1.split(":")
s1[0] = int(s1[0])
s1[1] = int(s1[1])
while(1):
s1[1] += 1
s1[1] = s1[1]%60
if s1[1] == 0:
s1[0] += 1
s1[0] = s1[0]%24
a = str(s1[0])
b = str(s1[1])
if len(a) == 1:
a = "0" + a
if len(b) == 1:
b = "0" + b
b1 = b[::-1]
if a == b1:
print(a+":"+b)
break
|
vfc_84269
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "12:21",
"output": "13:31",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "23:59",
"output": "00:00",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "15:51",
"output": "20:02",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10:44",
"output": "11:11",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
84
|
Solve the following coding problem using the programming language python:
The hero of our story, Valera, and his best friend Arcady are still in school, and therefore they spend all the free time playing turn-based strategy "GAGA: Go And Go Again". The gameplay is as follows.
There are two armies on the playing field each of which consists of *n* men (*n* is always even). The current player specifies for each of her soldiers an enemy's soldier he will shoot (a target) and then all the player's soldiers shot simultaneously. This is a game world, and so each soldier shoots perfectly, that is he absolutely always hits the specified target. If an enemy soldier is hit, he will surely die. It may happen that several soldiers had been indicated the same target. Killed soldiers do not participate in the game anymore.
The game "GAGA" consists of three steps: first Valera makes a move, then Arcady, then Valera again and the game ends.
You are asked to calculate the maximum total number of soldiers that may be killed during the game.
The input will be provided via standard input and looks as follows:
The input data consist of a single integer *n* (2<=≤<=*n*<=≤<=108, *n* is even). Please note that before the game starts there are 2*n* soldiers on the fields.
You should write the solution as standard output as follows:
Print a single number — a maximum total number of soldiers that could be killed in the course of the game in three turns.
Here are example inputs and outputs for the problem:
Example Input 1: 2
Example Output 1: 3
Example Input 2: 4
Example Output 2: 6
Now solve the problem by providing the code.
|
print((3 * 2 * int(input())) // 4)
|
vfc_84277
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4",
"output": "6",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
154
|
Solve the following coding problem using the programming language python:
Sergey attends lessons of the *N*-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the *N*-ish language. Sentences of the *N*-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks.
Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of *N*-ish. The spelling rules state that *N*-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair.
Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".
The input will be provided via standard input and looks as follows:
The first line contains a non-empty string *s*, consisting of lowercase Latin letters — that's the initial sentence in *N*-ish, written by Sergey. The length of string *s* doesn't exceed 105.
The next line contains integer *k* (0<=≤<=*k*<=≤<=13) — the number of forbidden pairs of letters.
Next *k* lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.
You should write the solution as standard output as follows:
Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.
Here are example inputs and outputs for the problem:
Example Input 1: ababa
1
ab
Example Output 1: 2
Example Input 2: codeforces
2
do
cs
Example Output 2: 1
Now solve the problem by providing the code.
|
import sys;sc = sys.stdin.readline;out=sys.stdout.write
s=str(sc());n=int(sc());ans=0
for e in range(n):
ss=str(sc());a,b=0,0
for e in s:
if e==ss[0]:a+=1
elif e==ss[1]:b+=1
else :ans+=min(a,b);a=0;b=0
ans+=min(a,b)
out(str(ans))
|
vfc_84281
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "ababa\n1\nab",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "codeforces\n2\ndo\ncs",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "nllnrlrnll\n1\nrl",
"output": "1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
555
|
Solve the following coding problem using the programming language python:
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of *n* matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to *n*. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1<=→<=2<=→<=4<=→<=5.
In one second, you can perform one of the two following operations:
- Having a matryoshka *a* that isn't nested in any other matryoshka and a matryoshka *b*, such that *b* doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put *a* in *b*; - Having a matryoshka *a* directly contained in matryoshka *b*, such that *b* is not nested in any other matryoshka, you may get *a* out of *b*.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1<=→<=2<=→<=...<=→<=*n*). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
The input will be provided via standard input and looks as follows:
The first line contains integers *n* (1<=≤<=*n*<=≤<=105) and *k* (1<=≤<=*k*<=≤<=105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next *k* lines contain the descriptions of the chains: the *i*-th line first contains number *m**i* (1<=≤<=*m**i*<=≤<=*n*), and then *m**i* numbers *a**i*1,<=*a**i*2,<=...,<=*a**im**i* — the numbers of matryoshkas in the chain (matryoshka *a**i*1 is nested into matryoshka *a**i*2, that is nested into matryoshka *a**i*3, and so on till the matryoshka *a**im**i* that isn't nested into any other matryoshka).
It is guaranteed that *m*1<=+<=*m*2<=+<=...<=+<=*m**k*<==<=*n*, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
You should write the solution as standard output as follows:
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
Here are example inputs and outputs for the problem:
Example Input 1: 3 2
2 1 2
1 3
Example Output 1: 1
Example Input 2: 7 3
3 1 3 7
2 2 5
2 4 6
Example Output 2: 10
Now solve the problem by providing the code.
|
n, k = map(int, input().split())
p = [0 for i in range(n+1)]
d = p.copy()
for i in range(k):
a = list(map(int, input().split()))
l = len(a)
for j in range(1, l - 1):
p[a[j]] = a[j+1]
d[a[j+1]] = 1
c = 1
while p[c] == c + 1:
c = p[c]
ans = 0
for i in range(c+1,n+1):
ans += 1 + d[i]
print(ans)
|
vfc_84285
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "3000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 2\n2 1 2\n1 3",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7 3\n3 1 3 7\n2 2 5\n2 4 6",
"output": "10",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1\n1 1",
"output": "0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
365
|
Solve the following coding problem using the programming language python:
Let's call a number *k*-good if it contains all digits not exceeding *k* (0,<=...,<=*k*). You've got a number *k* and an array *a* containing *n* numbers. Find out how many *k*-good numbers are in *a* (count each number every time it occurs in array *a*).
The input will be provided via standard input and looks as follows:
The first line contains integers *n* and *k* (1<=≤<=*n*<=≤<=100, 0<=≤<=*k*<=≤<=9). The *i*-th of the following *n* lines contains integer *a**i* without leading zeroes (1<=≤<=*a**i*<=≤<=109).
You should write the solution as standard output as follows:
Print a single integer — the number of *k*-good numbers in *a*.
Here are example inputs and outputs for the problem:
Example Input 1: 10 6
1234560
1234560
1234560
1234560
1234560
1234560
1234560
1234560
1234560
1234560
Example Output 1: 10
Example Input 2: 2 1
1
10
Example Output 2: 1
Now solve the problem by providing the code.
|
n,m=map(int,input().split())
c=0
for _ in range(n):
a= input()
if all(list(map(lambda x: str(x) in a , range(m+1)))):
c+=1
else:
continue
print(c)
|
vfc_84293
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "10 6\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560",
"output": "10",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 1\n1\n10",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 0\n1000000000",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1\n1000000000",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 0\n10\n102\n120\n1032\n1212103\n1999999",
"output": "5",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 3\n1000000000",
"output": "0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
376
|
Solve the following coding problem using the programming language python:
Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has *n* people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
The input will be provided via standard input and looks as follows:
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=100; 0<=≤<=*m*<=≤<=104). The next *m* lines contain the debts. The *i*-th line contains three integers *a**i*,<=*b**i*,<=*c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*; 1<=≤<=*c**i*<=≤<=100), which mean that person *a**i* owes person *b**i* *c**i* rubles.
Assume that the people are numbered by integers from 1 to *n*.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (*x*,<=*y*) and pair of people (*y*,<=*x*).
You should write the solution as standard output as follows:
Print a single integer — the minimum sum of debts in the optimal rearrangement.
Here are example inputs and outputs for the problem:
Example Input 1: 5 3
1 2 10
2 3 1
2 4 1
Example Output 1: 10
Example Input 2: 3 0
Example Output 2: 0
Example Input 3: 4 3
1 2 1
2 3 1
3 1 1
Example Output 3: 0
Now solve the problem by providing the code.
|
n, m = map(int, input().split())
d = [0 for i in range(n)]
for i in range(m):
a, b, c = map(int, input().split())
d[a-1] -= c
d[b-1] += c
print(sum(i for i in d if i > 0))
|
vfc_84297
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 3\n1 2 10\n2 3 1\n2 4 1",
"output": "10",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
237
|
Solve the following coding problem using the programming language python:
Valera runs a 24/7 fast food cafe. He magically learned that next day *n* people will visit his cafe. For each person we know the arrival time: the *i*-th person comes exactly at *h**i* hours *m**i* minutes. The cafe spends less than a minute to serve each client, but if a client comes in and sees that there is no free cash, than he doesn't want to wait and leaves the cafe immediately.
Valera is very greedy, so he wants to serve all *n* customers next day (and get more profit). However, for that he needs to ensure that at each moment of time the number of working cashes is no less than the number of clients in the cafe.
Help Valera count the minimum number of cashes to work at his cafe next day, so that they can serve all visitors.
The input will be provided via standard input and looks as follows:
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105), that is the number of cafe visitors.
Each of the following *n* lines has two space-separated integers *h**i* and *m**i* (0<=≤<=*h**i*<=≤<=23; 0<=≤<=*m**i*<=≤<=59), representing the time when the *i*-th person comes into the cafe.
Note that the time is given in the chronological order. All time is given within one 24-hour period.
You should write the solution as standard output as follows:
Print a single integer — the minimum number of cashes, needed to serve all clients next day.
Here are example inputs and outputs for the problem:
Example Input 1: 4
8 0
8 10
8 10
8 45
Example Output 1: 2
Example Input 2: 3
0 12
10 11
22 22
Example Output 2: 1
Now solve the problem by providing the code.
|
n = int(input())
d = {}
c = 1
for i in range(n):
m,h = list(map(str, input().split()))
if m+":"+h in d.keys():
d[m+":"+h] += 1
c = max(c, d[m+":"+h])
else:
d[m+":"+h] = 1
print(c)
|
vfc_84301
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n8 0\n8 10\n8 10\n8 45",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n0 12\n10 11\n22 22",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n12 8\n15 27\n15 27\n16 2\n19 52",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n5 6\n7 34\n7 34\n7 34\n12 29\n15 19\n20 23",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8\n0 36\n4 7\n4 7\n4 7\n11 46\n12 4\n15 39\n18 6",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "20\n4 12\n4 21\n4 27\n4 56\n5 55\n7 56\n11 28\n11 36\n14 58\n15 59\n16 8\n17 12\n17 23\n17 23\n17 23\n17 23\n17 23\n17 23\n20 50\n22 32",
"output": "6",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
397
|
Solve the following coding problem using the programming language python:
Our old friend Alexey has finally entered the University of City N — the Berland capital. Alexey expected his father to get him a place to live in but his father said it was high time for Alexey to practice some financial independence. So, Alexey is living in a dorm.
The dorm has exactly one straight dryer — a 100 centimeter long rope to hang clothes on. The dryer has got a coordinate system installed: the leftmost end of the dryer has coordinate 0, and the opposite end has coordinate 100. Overall, the university has *n* students. Dean's office allows *i*-th student to use the segment (*l**i*,<=*r**i*) of the dryer. However, the dean's office actions are contradictory and now one part of the dryer can belong to multiple students!
Alexey don't like when someone touch his clothes. That's why he want make it impossible to someone clothes touch his ones. So Alexey wonders: what is the total length of the parts of the dryer that he may use in a such way that clothes of the others (*n*<=-<=1) students aren't drying there. Help him! Note that Alexey, as the most respected student, has number 1.
The input will be provided via standard input and looks as follows:
The first line contains a positive integer *n* (1<=≤<=*n*<=≤<=100). The (*i*<=+<=1)-th line contains integers *l**i* and *r**i* (0<=≤<=*l**i*<=<<=*r**i*<=≤<=100) — the endpoints of the corresponding segment for the *i*-th student.
You should write the solution as standard output as follows:
On a single line print a single number *k*, equal to the sum of lengths of the parts of the dryer which are inside Alexey's segment and are outside all other segments.
Here are example inputs and outputs for the problem:
Example Input 1: 3
0 5
2 8
1 6
Example Output 1: 1
Example Input 2: 3
0 10
1 5
7 15
Example Output 2: 3
Now solve the problem by providing the code.
|
n = int(input())
seta = set()
ax,ay = map(int, input().split(" "))
for i in range(ax,ay):
seta.add(i+0.5)
for i in range(0,n-1):
nx,ny = map(int, input().split(" "))
for j in range(nx,ny):
seta.discard(j+0.5)
print(len(seta))
|
vfc_84305
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n0 5\n2 8\n1 6",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n0 10\n1 5\n7 15",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n0 100",
"output": "100",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1 9\n1 9",
"output": "0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
685
|
Solve the following coding problem using the programming language python:
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.
First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in *n* hours, and each hour in *m* minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to *n*<=-<=1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to *m*<=-<=1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation.
Note that to display number 0 section of the watches is required to have at least one place.
Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number.
The input will be provided via standard input and looks as follows:
The first line of the input contains two integers, given in the decimal notation, *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=109) — the number of hours in one day and the number of minutes in one hour, respectively.
You should write the solution as standard output as follows:
Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct.
Here are example inputs and outputs for the problem:
Example Input 1: 2 3
Example Output 1: 4
Example Input 2: 8 2
Example Output 2: 5
Now solve the problem by providing the code.
|
def D(n):
x,r=n-1,1
while x>=7:
x//=7
r+=1
return r
def H(n,l):
x,r=n,""
if x==0:r+='0'
while x:
r+=chr(ord('0')+x%7)
x//=7
r+='0'*(l-len(r))
return r
a,b=map(int,input().split())
la=D(a)
lb=D(b)
V=[0]*99
r=0
def F(deep,wa,wb):
global r
if wa>=a or wb>=b:return
if deep==la+lb:
r+=1
return
i=-1
while i<6:
i+=1
if V[i]:continue
V[i]=1
if deep>=la:
F(deep+1,wa,wb+i*(7**(lb-1-(deep-la))))
else:
F(deep+1,wa+i*(7**(la-1-deep)),wb)
V[i]=0
if la+lb<8:F(0,0,0)
print(r)
|
vfc_84309
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "3000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 3",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8 2",
"output": "5",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1",
"output": "0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 2",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8 8",
"output": "0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "50 50",
"output": "0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
576
|
Solve the following coding problem using the programming language python:
Vasya and Petya are playing a simple game. Vasya thought of number *x* between 1 and *n*, and Petya tries to guess the number.
Petya can ask questions like: "Is the unknown number divisible by number *y*?".
The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of.
Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers *y**i*, he should ask the questions about.
The input will be provided via standard input and looks as follows:
A single line contains number *n* (1<=≤<=*n*<=≤<=103).
You should write the solution as standard output as follows:
Print the length of the sequence of questions *k* (0<=≤<=*k*<=≤<=*n*), followed by *k* numbers — the questions *y**i* (1<=≤<=*y**i*<=≤<=*n*).
If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.
Here are example inputs and outputs for the problem:
Example Input 1: 4
Example Output 1: 3
2 4 3
Example Input 2: 6
Example Output 2: 4
2 4 3 5
Now solve the problem by providing the code.
|
def input_ints():
return list(map(int, input().split()))
def main():
n = int(input())
ans = []
for x in range(2, n + 1):
s = set()
xx = x
for y in range(2, n + 1):
while xx % y == 0:
xx /= y
s.add(y)
if len(s) == 1:
ans.append(x)
print(len(ans))
print(' '.join(str(x) for x in ans))
if __name__ == '__main__':
main()
|
vfc_84313
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4",
"output": "3\n2 4 3 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6",
"output": "4\n2 4 3 5 ",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
21
|
Solve the following coding problem using the programming language python:
Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In each square he wrote an integer number, possibly negative. He became interested in how many ways exist to cut this stripe into three pieces so that the sum of numbers from each piece is equal to the sum of numbers from any other piece, and each piece contains positive integer amount of squares. Would you help Bob solve this problem?
The input will be provided via standard input and looks as follows:
The first input line contains integer *n* (1<=≤<=*n*<=≤<=105) — amount of squares in the stripe. The second line contains n space-separated numbers — they are the numbers written in the squares of the stripe. These numbers are integer and do not exceed 10000 in absolute value.
You should write the solution as standard output as follows:
Output the amount of ways to cut the stripe into three non-empty pieces so that the sum of numbers from each piece is equal to the sum of numbers from any other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only.
Here are example inputs and outputs for the problem:
Example Input 1: 4
1 2 3 3
Example Output 1: 1
Example Input 2: 5
1 2 3 4 5
Example Output 2: 0
Now solve the problem by providing the code.
|
n,num,ways,q = int(input()),0,0,0
s = list(map(int,input().split()))
sums = sum(s)
for i in range(n):
num += s[i]
if num * 3 == sums * 2 and 0 < i < n - 1:
ways += q
if num * 3 == sums:
q += 1
print(ways)
|
vfc_84317
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1\n-3",
"output": "0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n0 0",
"output": "0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n0 0 0",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n-2 3 3 2",
"output": "0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
17
|
Solve the following coding problem using the programming language python:
Nick is interested in prime numbers. Once he read about Goldbach problem. It states that every even integer greater than 2 can be expressed as the sum of two primes. That got Nick's attention and he decided to invent a problem of his own and call it Noldbach problem. Since Nick is interested only in prime numbers, Noldbach problem states that at least *k* prime numbers from 2 to *n* inclusively can be expressed as the sum of three integer numbers: two neighboring prime numbers and 1. For example, 19 = 7 + 11 + 1, or 13 = 5 + 7 + 1.
Two prime numbers are called neighboring if there are no other prime numbers between them.
You are to help Nick, and find out if he is right or wrong.
The input will be provided via standard input and looks as follows:
The first line of the input contains two integers *n* (2<=≤<=*n*<=≤<=1000) and *k* (0<=≤<=*k*<=≤<=1000).
You should write the solution as standard output as follows:
Output YES if at least *k* prime numbers from 2 to *n* inclusively can be expressed as it was described above. Otherwise output NO.
Here are example inputs and outputs for the problem:
Example Input 1: 27 2
Example Output 1: YES
Example Input 2: 45 7
Example Output 2: NO
Now solve the problem by providing the code.
|
import math
def isprime(k):
for i in range (2, int(math.sqrt(k)+1)):
if(k%i==0):
return 0
return 1
n,k = map(int, input().split())
count = 0
lis = []
for i in range(2, n+1):
if(isprime(i)):
lis.append(i)
for i in range (0, len(lis)-1):
if((lis[i]+lis[i+1]+1) in lis):
count += 1
if(count >=k):
print("YES")
else:
print("NO")
|
vfc_84321
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "27 2",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "45 7",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 0",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "15 1",
"output": "YES",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
446
|
Solve the following coding problem using the programming language python:
DZY has a sequence *a*, consisting of *n* integers.
We'll call a sequence *a**i*,<=*a**i*<=+<=1,<=...,<=*a**j* (1<=≤<=*i*<=≤<=*j*<=≤<=*n*) a subsegment of the sequence *a*. The value (*j*<=-<=*i*<=+<=1) denotes the length of the subsegment.
Your task is to find the longest subsegment of *a*, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
The input will be provided via standard input and looks as follows:
The first line contains integer *n* (1<=≤<=*n*<=≤<=105). The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
You should write the solution as standard output as follows:
In a single line print the answer to the problem — the maximum length of the required subsegment.
Here are example inputs and outputs for the problem:
Example Input 1: 6
7 2 3 1 5 6
Example Output 1: 5
Now solve the problem by providing the code.
|
# Love u Atreyee
n = int(input())
a = list(map(int, input().split()))
left = [1] * n
for i in range(1, n):
if a[i] > a[i - 1]:
left[i] = left[i - 1] + 1
right = [1] * n
for i in range(n-2,-1,-1):
if a[i] < a[i + 1]:
right[i] = right[i + 1] + 1
ans = max(left)
if ans < n:
ans += 1
for i in range(1, n - 1):
if a[i - 1] + 1 < a[i + 1]:
ans = max(ans, left[i - 1] + right[i + 1] + 1)
print(ans)
|
vfc_84325
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "4000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6\n7 2 3 1 5 6",
"output": "5",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n424238336 649760493 681692778 714636916 719885387 804289384 846930887 957747794 596516650 189641422",
"output": "9",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
329
|
Solve the following coding problem using the programming language python:
You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming an *n*<=×<=*n* grid. The rows are numbered 1 through *n* from top to bottom, and the columns are numbered 1 through *n* from left to right. At the far side of the room lies a door locked with evil magical forces. The following inscriptions are written on the door:
Being a very senior adventurer, you immediately realize what this means. You notice that every single cell in the grid are initially evil. You should purify all of these cells.
The only method of tile purification known to you is by casting the "Purification" spell. You cast this spell on a single tile — then, all cells that are located in the same row and all cells that are located in the same column as the selected tile become purified (including the selected tile)! It is allowed to purify a cell more than once.
You would like to purify all *n*<=×<=*n* cells while minimizing the number of times you cast the "Purification" spell. This sounds very easy, but you just noticed that some tiles are particularly more evil than the other tiles. You cannot cast the "Purification" spell on those particularly more evil tiles, not even after they have been purified. They can still be purified if a cell sharing the same row or the same column gets selected by the "Purification" spell.
Please find some way to purify all the cells with the minimum number of spells cast. Print -1 if there is no such way.
The input will be provided via standard input and looks as follows:
The first line will contain a single integer *n* (1<=≤<=*n*<=≤<=100). Then, *n* lines follows, each contains *n* characters. The *j*-th character in the *i*-th row represents the cell located at row *i* and column *j*. It will be the character 'E' if it is a particularly more evil cell, and '.' otherwise.
You should write the solution as standard output as follows:
If there exists no way to purify all the cells, output -1. Otherwise, if your solution casts *x* "Purification" spells (where *x* is the minimum possible number of spells), output *x* lines. Each line should consist of two integers denoting the row and column numbers of the cell on which you should cast the "Purification" spell.
Here are example inputs and outputs for the problem:
Example Input 1: 3
.E.
E.E
.E.
Example Output 1: 1 1
2 2
3 3
Example Input 2: 3
EEE
E..
E.E
Example Output 2: -1
Example Input 3: 5
EE.EE
E.EE.
E...E
.EE.E
EE.EE
Example Output 3: 3 3
1 3
2 2
4 4
5 3
Now solve the problem by providing the code.
|
import traceback
import math
from collections import defaultdict
from functools import lru_cache
def main():
N = int(input())
grid = []
for _ in range(N):
s = input()
grid.append(s)
if all('.' in [grid[i][j] for j in range(N)] for i in range(N)):
for i in range(N):
for j in range(N):
if grid[i][j] == '.':
print(i+1, j+1)
break
elif all('.' in [grid[i][j] for i in range(N)] for j in range(N)):
for j in range(N):
for i in range(N):
if grid[i][j] == '.':
print(i+1, j+1)
break
else:
print(-1)
try:
ans = main()
# print(ans)
except Exception as e:
traceback.print_exc()
|
vfc_84329
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n.E.\nE.E\n.E.",
"output": "1 1\n2 2\n3 1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\nEEE\nE..\nE.E",
"output": "-1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\nEE.EE\nE.EE.\nE...E\n.EE.E\nEE.EE",
"output": "1 3\n2 2\n3 2\n4 1\n5 3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n.EE\n.EE\n.EE",
"output": "1 1\n2 1\n3 1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\nEE.EE\nEE..E\nEEE..\nEE..E\nEE.EE",
"output": "1 3\n2 3\n3 4\n4 3\n5 3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\nE",
"output": "-1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
407
|
Solve the following coding problem using the programming language python:
One day, little Vasya found himself in a maze consisting of (*n*<=+<=1) rooms, numbered from 1 to (*n*<=+<=1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (*n*<=+<=1)-th one.
The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number *i* (1<=≤<=*i*<=≤<=*n*), someone can use the first portal to move from it to room number (*i*<=+<=1), also someone can use the second portal to move from it to room number *p**i*, where 1<=≤<=*p**i*<=≤<=*i*.
In order not to get lost, Vasya decided to act as follows.
- Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1. - Let's assume that Vasya is in room *i* and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room *p**i*), otherwise Vasya uses the first portal.
Help Vasya determine the number of times he needs to use portals to get to room (*n*<=+<=1) in the end.
The input will be provided via standard input and looks as follows:
The first line contains integer *n* (1<=≤<=*n*<=≤<=103) — the number of rooms. The second line contains *n* integers *p**i* (1<=≤<=*p**i*<=≤<=*i*). Each *p**i* denotes the number of the room, that someone can reach, if he will use the second portal in the *i*-th room.
You should write the solution as standard output as follows:
Print a single number — the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109<=+<=7).
Here are example inputs and outputs for the problem:
Example Input 1: 2
1 2
Example Output 1: 4
Example Input 2: 4
1 1 2 3
Example Output 2: 20
Example Input 3: 5
1 1 1 1 1
Example Output 3: 62
Now solve the problem by providing the code.
|
def f(s):
return int(s)-1
n = int(input())
mod = 10**9+7
arr = list(map(f,input().split()))
dp=[0]*(n+1)
for i in range(n):
dp[i+1] = dp[i]+2+dp[i]-dp[arr[i]]
dp[i+1]%=mod
print(dp[-1])
|
vfc_84333
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1 2",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n1 1 2 3",
"output": "20",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n1 1 1 1 1",
"output": "62",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n1 2 1 3 1 2 1",
"output": "154",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1",
"output": "2",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
696
|
Solve the following coding problem using the programming language python:
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections *i* and 2*i* and another road between *i* and 2*i*<=+<=1 for every positive integer *i*. You can clearly see that there exists a unique shortest path between any two intersections.
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will *q* consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers *v*, *u* and *w*. As the result of this action, the passing fee of all roads on the shortest path from *u* to *v* increases by *w* dollars.
2. Barney starts moving from some intersection *v* and goes to intersection *u* where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
The input will be provided via standard input and looks as follows:
The first line of input contains a single integer *q* (1<=≤<=*q*<=≤<=1<=000).
The next *q* lines contain the information about the events in chronological order. Each event is described in form 1 *v* *u* *w* if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from *u* to *v* by *w* dollars, or in form 2 *v* *u* if it's an event when Barnie goes to cuddle from the intersection *v* to the intersection *u*.
1<=≤<=*v*,<=*u*<=≤<=1018,<=*v*<=≠<=*u*,<=1<=≤<=*w*<=≤<=109 states for every description line.
You should write the solution as standard output as follows:
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
Here are example inputs and outputs for the problem:
Example Input 1: 7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Example Output 1: 94
0
32
Now solve the problem by providing the code.
|
n=int(input())
d={}
def lca(u,v,w) :
res=0
while u!=v :
if u<v :
v,u=u,v
d[u]=d.get(u,0)+w
res+=d[u]
u=u//2
return res
for i in range(n) :
l=list(map(int,input().split()))
if l[0]==1 :
lca(l[1],l[2],l[3])
else :
print(lca(l[1],l[2],0))
|
vfc_84337
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7\n1 3 4 30\n1 4 1 2\n1 3 6 8\n2 4 3\n1 6 1 40\n2 3 7\n2 2 4",
"output": "94\n0\n32",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
442
|
Solve the following coding problem using the programming language python:
Andrey needs one more problem to conduct a programming contest. He has *n* friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends — the probability that this friend will come up with a problem if Andrey asks him.
Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset.
The input will be provided via standard input and looks as follows:
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=100) — the number of Andrey's friends. The second line contains *n* real numbers *p**i* (0.0<=≤<=*p**i*<=≤<=1.0) — the probability that the *i*-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point.
You should write the solution as standard output as follows:
Print a single real number — the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10<=-<=9.
Here are example inputs and outputs for the problem:
Example Input 1: 4
0.1 0.2 0.3 0.8
Example Output 1: 0.800000000000
Example Input 2: 2
0.1 0.2
Example Output 2: 0.260000000000
Now solve the problem by providing the code.
|
# Contest: 20 - 2100 <= Codeforces Rating <= 2199 (https://a2oj.com/ladder?ID=20)
# Problem: (16) Andrey and Problem (Difficulty: 4) (http://codeforces.com/problemset/problem/442/B)
def rint():
return int(input())
def rints():
return list(map(int, input().split()))
n = rint()
p = sorted((float(s) for s in input().split()), reverse=True)
pr = 0
inv = 1
for pi in p:
npr = pr * (1 - pi) + inv * pi
if npr < pr:
break
pr = npr
inv *= (1 - pi)
print(pr)
|
vfc_84341
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n0.1 0.2 0.3 0.8",
"output": "0.800000000000",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n0.1 0.2",
"output": "0.260000000000",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n0.217266",
"output": "0.217266000000",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n0.608183 0.375030",
"output": "0.608183000000",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n0.388818 0.399762 0.393874",
"output": "0.478724284024",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n0.801024 0.610878 0.808545 0.732504",
"output": "0.808545000000",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
45
|
Solve the following coding problem using the programming language python:
Today Vasya visited a widely known site and learned that the continuation of his favourite game Codecraft II will appear after exactly *k* months. He looked at the calendar and learned that at the moment is the month number *s*. Vasya immediately got interested in what month Codecraft III will appear. Help him understand that.
All the twelve months in Vasya's calendar are named using their usual English names: January, February, March, April, May, June, July, August, September, October, November, December.
The input will be provided via standard input and looks as follows:
The first input line contains the name of the current month. It is guaranteed that it is a proper English name of one of twelve months. The first letter is uppercase, the rest are lowercase. The second line contains integer *k* (0<=≤<=*k*<=≤<=100) — the number of months left till the appearance of Codecraft III.
You should write the solution as standard output as follows:
Print starting from an uppercase letter the name of the month in which the continuation of Codeforces II will appear. The printed name must be contained in the list January, February, March, April, May, June, July, August, September, October, November, December.
Here are example inputs and outputs for the problem:
Example Input 1: November
3
Example Output 1: February
Example Input 2: May
24
Example Output 2: May
Now solve the problem by providing the code.
|
l=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
n=input()
p=int(input())
pp=l.index(n)
print(l[(pp+p)%12])
|
vfc_84345
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "November\n3",
"output": "February",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "May\n24",
"output": "May",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "April\n0",
"output": "April",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "September\n0",
"output": "September",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "August\n0",
"output": "August",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
113
|
Solve the following coding problem using the programming language python:
Petya got interested in grammar on his third year in school. He invented his own language called Petya's. Petya wanted to create a maximally simple language that would be enough to chat with friends, that's why all the language's grammar can be described with the following set of rules:
- There are three parts of speech: the adjective, the noun, the verb. Each word in his language is an adjective, noun or verb. - There are two genders: masculine and feminine. Each word in his language has gender either masculine or feminine. - Masculine adjectives end with -lios, and feminine adjectives end with -liala. - Masculine nouns end with -etr, and feminime nouns end with -etra. - Masculine verbs end with -initis, and feminime verbs end with -inites. - Thus, each word in the Petya's language has one of the six endings, given above. There are no other endings in Petya's language. - It is accepted that the whole word consists of an ending. That is, words "lios", "liala", "etr" and so on belong to the Petya's language. - There aren't any punctuation marks, grammatical tenses, singular/plural forms or other language complications. - A sentence is either exactly one valid language word or exactly one statement.
Statement is any sequence of the Petya's language, that satisfy both conditions:
- Words in statement follow in the following order (from the left to the right): zero or more adjectives followed by exactly one noun followed by zero or more verbs. - All words in the statement should have the same gender.
After Petya's friend Vasya wrote instant messenger (an instant messaging program) that supported the Petya's language, Petya wanted to add spelling and grammar checking to the program. As Vasya was in the country and Petya didn't feel like waiting, he asked you to help him with this problem. Your task is to define by a given sequence of words, whether it is true that the given text represents exactly one sentence in Petya's language.
The input will be provided via standard input and looks as follows:
The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105.
It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible that given words do not belong to the Petya's language.
You should write the solution as standard output as follows:
If some word of the given text does not belong to the Petya's language or if the text contains more that one sentence, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes).
Here are example inputs and outputs for the problem:
Example Input 1: petr
Example Output 1: YES
Example Input 2: etis atis animatis etis atis amatis
Example Output 2: NO
Example Input 3: nataliala kataliala vetra feinites
Example Output 3: YES
Now solve the problem by providing the code.
|
import sys,os,math,cmath,timeit,functools,operator,bisect
from sys import stdin,stdout,setrecursionlimit
from io import BytesIO, IOBase
from collections import defaultdict as dd , Counter
from math import factorial , gcd
from queue import PriorityQueue
from heapq import merge, heapify, heappop, heappush
try :
from functools import cache , lru_cache
except :
pass
ONLINE_JUDGE = __debug__
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
self.BUFSIZE = 8192
def read(self):
while True:
a = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
if not a:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(a), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
a = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
self.newlines = a.count(b"\n") + (not a)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(a), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
stdin, stdout = IOWrapper(stdin), IOWrapper(stdout)
input = lambda: stdin.readline().rstrip("\r\n")
start = timeit.default_timer()
primes = []
mod=10**9+7
MOD=998244353
########################################################
class DSU:
def __init__(self,n):
self.par = [i for i in range(n+1)]
self.rank = [1 for i in range(n+1)]
self.sz = [1 for i in range(n+1)]
def find(self,x):
if self.par[x]!=x :
self.par[x] = self.find(self.par[x])
return self.par[x]
def union(self,x,y):
rx,ry = self.find(x),self.find(y)
if rx==ry :
return False
if self.rank[rx] > self.rank[ry] :
self.sz[rx]+=self.sz[ry]
self.par[ry]=rx
elif self.rank[ry] > self.rank[rx] :
self.sz[ry]+=self.sz[rx]
self.par[rx]=ry
else :
self.rank[rx]+=1
self.par[ry]=rx
self.sz[rx]+=self.sz[ry]
return True
def connected(self,x,y):
return self.find(x)==self.find(y)
def countinversions(a,ans):
if(len(a)==1) : return a
mid = len(a)//2
x = a[:mid]
y = a[mid:]
left = countinversions(x,ans)
right = countinversions(y,ans)
temp = []
i,j=0,0
while i<len(left) and j<len(right):
if left[i]<=right[j] :
temp.append(left[i])
i+=1
else :
temp.append(right[j])
j+=1
ans+=len(left)-i
while i<len(left) : temp.append(right[i]) ; i+=1
while j<len(right) : temp.append(left[j]) ; j+=1
return temp
########################################################
def seive(n):
a = []
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p ** 2,n + 1, p):
prime[i] = False
p = p + 1
for p in range(2,n + 1):
if prime[p]:
primes.append(p)
def lower_bound(arr, n, k):
start = 0
end = n-1
while start <= end:
mid = (start+end)//2
if arr[mid] == k:
return mid
elif arr[mid] > k:
if mid > 0:
if arr[mid-1] < k:
return (mid-1)
else:
return -1
else:
if mid < (n-1):
if arr[mid+1] > k:
return mid
else:
return mid
def upper_bound(arr, n, k):
start = 0
end = n-1
while start <= end:
mid = (start+end)//2
if arr[mid] == k:
return mid
elif arr[mid] > k:
if mid > 0:
if arr[mid-1] < k:
return (mid)
else:
return mid
else:
if mid < (n-1):
if arr[mid+1] > k:
return (mid+1)
else:
return -1
def is_sorted(arr):
return all(arr[i]<=arr[i+1] for i in range(len(arr)-1))
def invmod(n):
return pow(n,mod-2,mod)
def binary_search(a,x,lo=0,hi=None):
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
midval = a[mid]
if midval < x:
lo = mid+1
elif midval > x:
hi = mid
else:
return mid
return -1
def ceil(a:int,b:int)->int :
return (a+b-1)//b
def yn(x):
if x :
print("YES")
else :
print("NO")
def nCr(n,r):
return factorial(n)//(factorial(r)*factorial(n-r))
def countinversions(arr):
if len(arr)==1 :
return arr,0
left = arr[:len(arr)//2]
right = arr[len(arr)//2:]
left,lc = countinversions(left)
right,rc = countinversions(right)
c = []
i,j,ans = 0,0,lc+rc
while i<len(left) and j<len(right):
if left[i] <= right[j] :
c.append(left[i])
i+=1
else :
c.append(right[j])
ans+=len(left)-i
j+=1
c+=left[i:]
c+=right[j:]
return c,ans
########################################################
def solve():
s=input().split()
a=''
tt=[]
for x in s :
if x[-4:]=="lios" :
tt+=['m']
a+='a'
elif x[-5:]=="liala" :
tt+=['f']
a+='a'
elif x[-3:]=="etr" :
tt+=['m']
a+='n'
elif x[-4:]=="etra" :
tt+=['f']
a+='n'
elif x[-6:]=="initis" :
tt+=['m']
a+='v'
elif x[-6:]=="inites" :
tt+=['f']
a+='v'
else :
print("NO")
return
if len(tt)==1 :
print("YES")
return
if len(set(tt))==2 or a.count('n')!=1 :
print("NO")
return
noun = a.index('n')
temp = a.split('n')
adj=-1
fl=0
verb=len(a)+1
for i in range(len(a)):
if a[i]=='a' :
adj=i
if a[i]=='v' :
if fl==0 :
fl=1
verb=i
if noun > adj and noun < verb :
print("YES")
return
print("NO")
def main():
tc=1
#tc=int(input())
for _ in range(tc):
solve()
stop = timeit.default_timer()
if not ONLINE_JUDGE :
print("Time Elapsed :" , str(stop-start) , "seconds")
########################################################
def lst()->list :
return list(map(int,input().split()))
def rowinp() :
return map(int,input().split())
def strin()->str :
return input()
def chlst()->list :
return [x for x in input()]
def sint()->int :
return int(input())
########################################################
main()
########################################################
|
vfc_84349
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "petr",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "etis atis animatis etis atis amatis",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "nataliala kataliala vetra feinites",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "qweasbvflios",
"output": "YES",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
244
|
Solve the following coding problem using the programming language python:
One day Ms Swan bought an orange in a shop. The orange consisted of *n*·*k* segments, numbered with integers from 1 to *n*·*k*.
There were *k* children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the *i*-th (1<=≤<=*i*<=≤<=*k*) child wrote the number *a**i* (1<=≤<=*a**i*<=≤<=*n*·*k*). All numbers *a**i* accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
- each child gets exactly *n* orange segments; - the *i*-th child gets the segment with number *a**i* for sure; - no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.
The input will be provided via standard input and looks as follows:
The first line contains two integers *n*, *k* (1<=≤<=*n*,<=*k*<=≤<=30). The second line contains *k* space-separated integers *a*1,<=*a*2,<=...,<=*a**k* (1<=≤<=*a**i*<=≤<=*n*·*k*), where *a**i* is the number of the orange segment that the *i*-th child would like to get.
It is guaranteed that all numbers *a**i* are distinct.
You should write the solution as standard output as follows:
Print exactly *n*·*k* distinct integers. The first *n* integers represent the indexes of the segments the first child will get, the second *n* integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.
Here are example inputs and outputs for the problem:
Example Input 1: 2 2
4 1
Example Output 1: 2 4
1 3
Example Input 2: 3 1
2
Example Output 2: 3 2 1
Now solve the problem by providing the code.
|
l=[int(l) for l in input().split()]
p=[int(n) for n in input().split()]
num=0
if (l[0]==1):
for index in p:
print(index)
else:
for index in range(l[1]):
word=str(p[index])
for x in range(1,l[0]):
num+=1
if (num not in p):
word=word+" "+str(num)
else:
while (num in p):
num+=1
word=word+" "+str(num)
print(word)
|
vfc_84353
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 2\n4 1",
"output": "2 4 \n1 3 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 1\n2",
"output": "3 2 1 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 5\n25 24 23 22 21",
"output": "2 3 1 25 4 \n7 6 8 5 24 \n10 12 9 23 11 \n13 15 14 16 22 \n19 21 20 17 18 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 30\n8 22 13 25 10 30 12 27 6 4 7 2 20 16 26 14 15 17 23 3 24 9 5 11 29 1 19 28 21 18",
"output": "8 \n22 \n13 \n25 \n10 \n30 \n12 \n27 \n6 \n4 \n7 \n2 \n20 \n16 \n26 \n14 \n15 \n17 \n23 \n3 \n24 \n9 \n5 \n11 \n29 \n1 \n19 \n28 \n21 \n18 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "30 1\n29",
"output": "8 20 17 12 5 26 13 2 19 22 28 16 10 4 6 11 3 25 1 27 15 9 30 24 21 18 14 23 29 7 ",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
145
|
Solve the following coding problem using the programming language python:
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings *a* and *b* of the same length *n*. The strings consist only of lucky digits. Petya can perform operations of two types:
- replace any one digit from string *a* by its opposite (i.e., replace 4 by 7 and 7 by 4); - swap any pair of digits in string *a*.
Petya is interested in the minimum number of operations that are needed to make string *a* equal to string *b*. Help him with the task.
The input will be provided via standard input and looks as follows:
The first and the second line contains strings *a* and *b*, correspondingly. Strings *a* and *b* have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
You should write the solution as standard output as follows:
Print on the single line the single number — the minimum number of operations needed to convert string *a* into string *b*.
Here are example inputs and outputs for the problem:
Example Input 1: 47
74
Example Output 1: 1
Example Input 2: 774
744
Example Output 2: 1
Example Input 3: 777
444
Example Output 3: 3
Now solve the problem by providing the code.
|
a = input()
b = input()
a7,a4 = a.count('7'),a.count('4')
b7,b4 = b.count('7'),b.count('4')
c = 0
for i in range(len(a)):
if a[i]!=b[i]:
c += 1
if a7==b7 and a4==b4:
if c%2:
print(c//2+1)
else:
print(c//2)
else:
if b7>a7:
d = b7-a7
else:
d = b4-a4
tot = d
c -= d
if c%2:
print(tot+c//2+1)
else:
print(tot+c//2)
|
vfc_84357
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "47\n74",
"output": "1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
185
|
Solve the following coding problem using the programming language python:
Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
Help the dwarfs find out how many triangle plants that point "upwards" will be in *n* years.
The input will be provided via standard input and looks as follows:
The first line contains a single integer *n* (0<=≤<=*n*<=≤<=1018) — the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
You should write the solution as standard output as follows:
Print a single integer — the remainder of dividing the number of plants that will point "upwards" in *n* years by 1000000007 (109<=+<=7).
Here are example inputs and outputs for the problem:
Example Input 1: 1
Example Output 1: 3
Example Input 2: 2
Example Output 2: 10
Now solve the problem by providing the code.
|
def mult(a,b,m):
n1,m1=len(a),len(a[0])
n2,m2=len(b),len(b[0])
if m1!=n2: raise Exception('Dimension error')
c=[[0]*m2 for i in range(n1)]
for i in range(m2):
for j in range(n1):
for k in range(n2):
c[j][i]=(c[j][i]+b[k][i]*a[j][k])%m
return c
def matExp(a,b,m):
res=[[int(i==j) for j in range(len(a))] for i in range(len(a))]
while b:
if b&1: res=mult(res,a,m)
a=mult(a,a,m)
b>>=1
return res
n=int(input())
m=int(1e9)+7
a=[[1],
[0]]
z=[[3,1],
[1,3]]
z=matExp(z,n,m)
print(mult(z,a,m)[0][0])
|
vfc_84361
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2",
"output": "10",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "385599124",
"output": "493875375",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
934
|
Solve the following coding problem using the programming language python:
Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.
Little Tommy has *n* lanterns and Big Banban has *m* lanterns. Tommy's lanterns have brightness *a*1,<=*a*2,<=...,<=*a**n*, and Banban's have brightness *b*1,<=*b*2,<=...,<=*b**m* respectively.
Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns.
Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible.
You are asked to find the brightness of the chosen pair if both of them choose optimally.
The input will be provided via standard input and looks as follows:
The first line contains two space-separated integers *n* and *m* (2<=≤<=*n*,<=*m*<=≤<=50).
The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n*.
The third line contains *m* space-separated integers *b*1,<=*b*2,<=...,<=*b**m*.
All the integers range from <=-<=109 to 109.
You should write the solution as standard output as follows:
Print a single integer — the brightness of the chosen pair.
Here are example inputs and outputs for the problem:
Example Input 1: 2 2
20 18
2 14
Example Output 1: 252
Example Input 2: 5 3
-1 0 1 2 3
-1 0 1
Example Output 2: 2
Now solve the problem by providing the code.
|
n,m = input().split()
n = int(n)
m = int(m)
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort()
ans = 10000000000000000000
for i in range(n):
maxi = -10000000000000000000
for j in range(n):
if j != i:
for k in range(m):
pr = a[j] * b[k]
maxi = max(maxi,pr)
ans = min(ans,maxi)
print(ans)
|
vfc_84365
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 2\n20 18\n2 14",
"output": "252",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 3\n-1 0 1 2 3\n-1 0 1",
"output": "2",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
763
|
Solve the following coding problem using the programming language python:
Each New Year Timofey and his friends cut down a tree of *n* vertices and bring it home. After that they paint all the *n* its vertices, so that the *i*-th vertex gets color *c**i*.
Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.
Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.
A subtree of some vertex is a subgraph containing that vertex and all its descendants.
Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed.
The input will be provided via standard input and looks as follows:
The first line contains single integer *n* (2<=≤<=*n*<=≤<=105) — the number of vertices in the tree.
Each of the next *n*<=-<=1 lines contains two integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*n*, *u*<=≠<=*v*), denoting there is an edge between vertices *u* and *v*. It is guaranteed that the given graph is a tree.
The next line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=105), denoting the colors of the vertices.
You should write the solution as standard output as follows:
Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him.
Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.
Here are example inputs and outputs for the problem:
Example Input 1: 4
1 2
2 3
3 4
1 2 1 1
Example Output 1: YES
2
Example Input 2: 3
1 2
2 3
1 2 3
Example Output 2: YES
2
Example Input 3: 4
1 2
2 3
3 4
1 2 1 2
Example Output 3: NO
Now solve the problem by providing the code.
|
n = int(input())
u = []
v = []
for i in range(n-1):
a, b = map(int, input().split())
u.append(a)
v.append(b)
c = [0] + [int(x) for x in input().split()]
e = 0
dic = {}
for i in range(1, n+1):
dic[i] = 0
def plus(dic, n):
if n in dic:
dic[n] += 1
else:
dic[n] = 1
for i in range(n-1):
if c[u[i]] != c[v[i]]:
e += 1
dic[u[i]] += 1
dic[v[i]] += 1
for i in range(1, n+1):
if dic[i] == e:
print ('YES', i,sep='\n')
exit(0)
print ("NO")
|
vfc_84369
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 2\n2 3\n3 4\n1 2 1 1",
"output": "YES\n2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1 2\n2 3\n1 2 3",
"output": "YES\n2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n1 2\n2 3\n3 4\n1 2 1 2",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n2 1\n2 3\n1 2 3",
"output": "YES\n2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n1 2\n2 4\n4 3\n1 1 3 2",
"output": "YES\n4",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
106
|
Solve the following coding problem using the programming language python:
Vasya is choosing a laptop. The shop has *n* laptops to all tastes.
Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties.
If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one.
There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him.
The input will be provided via standard input and looks as follows:
The first line contains number *n* (1<=≤<=*n*<=≤<=100).
Then follow *n* lines. Each describes a laptop as *speed* *ram* *hdd* *cost*. Besides,
- *speed*, *ram*, *hdd* and *cost* are integers - 1000<=≤<=*speed*<=≤<=4200 is the processor's speed in megahertz - 256<=≤<=*ram*<=≤<=4096 the RAM volume in megabytes - 1<=≤<=*hdd*<=≤<=500 is the HDD in gigabytes - 100<=≤<=*cost*<=≤<=1000 is price in tugriks
All laptops have different prices.
You should write the solution as standard output as follows:
Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to *n* in the order in which they are given in the input data.
Here are example inputs and outputs for the problem:
Example Input 1: 5
2100 512 150 200
2000 2048 240 350
2300 1024 200 320
2500 2048 80 300
2000 512 180 150
Example Output 1: 4
Now solve the problem by providing the code.
|
l=[];c=[]
n=int(input())
for _ in range(n):
t=list(map(int,input().split()))
l.append(t)
c.append(t[3])
s = []
for i in range(n):
f=0
for j in range(n):
if i==j:
continue
if l[i][0]<l[j][0] and l[i][1]<l[j][1] and l[i][2]<l[j][2]:
f=1
break
s.append(f)
m = float('inf')
for i in range(len(s)):
if s[i]==0 and c[i]<m:
m=c[i]
k=c.index(m)
print(k+1)
|
vfc_84373
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n2100 512 150 200\n2000 2048 240 350\n2300 1024 200 320\n2500 2048 80 300\n2000 512 180 150",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1500 500 50 755\n1600 600 80 700",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1500 512 50 567\n1600 400 70 789",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n1000 300 5 700\n1100 400 10 600\n1200 500 15 500\n1300 600 20 400",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n2123 389 397 747\n2705 3497 413 241\n3640 984 470 250\n3013 2004 276 905\n3658 3213 353 602\n1428 626 188 523\n2435 1140 459 824\n2927 2586 237 860\n2361 4004 386 719\n2863 2429 476 310",
"output": "2",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
252
|
Solve the following coding problem using the programming language python:
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of *n* elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array *a* (the array elements are indexed from 1) consisting of *n* elements is called sorted if it meets at least one of the following two conditions:
1. *a*1<=≤<=*a*2<=≤<=...<=≤<=*a**n*; 1. *a*1<=≥<=*a*2<=≥<=...<=≥<=*a**n*.
Help Petya find the two required positions to swap or else say that they do not exist.
The input will be provided via standard input and looks as follows:
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105). The second line contains *n* non-negative space-separated integers *a*1,<=*a*2,<=...,<=*a**n* — the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
You should write the solution as standard output as follows:
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to *n*.
Here are example inputs and outputs for the problem:
Example Input 1: 1
1
Example Output 1: -1
Example Input 2: 2
1 2
Example Output 2: -1
Example Input 3: 4
1 2 3 4
Example Output 3: 1 2
Example Input 4: 3
1 1 1
Example Output 4: -1
Now solve the problem by providing the code.
|
n=int(input())
a=list(map(int,input().split()))
b,c=sorted(a),sorted(a,reverse=True)
for i in range(n-1):
if a[i]!=a[i+1]:
a[i],a[i+1]=a[i+1],a[i]
if a==b or a==c:
a[i],a[i+1]=a[i+1],a[i]
else:
print(i+1,i+2)
exit()
print(-1)
|
vfc_84377
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1\n1",
"output": "-1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1 2",
"output": "-1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
924
|
Solve the following coding problem using the programming language python:
An atom of element X can exist in *n* distinct states with energies *E*1<=<<=*E*2<=<<=...<=<<=*E**n*. Arkady wants to build a laser on this element, using a three-level scheme. Here is a simplified description of the scheme.
Three distinct states *i*, *j* and *k* are selected, where *i*<=<<=*j*<=<<=*k*. After that the following process happens:
1. initially the atom is in the state *i*,1. we spend *E**k*<=-<=*E**i* energy to put the atom in the state *k*,1. the atom emits a photon with useful energy *E**k*<=-<=*E**j* and changes its state to the state *j*,1. the atom spontaneously changes its state to the state *i*, losing energy *E**j*<=-<=*E**i*,1. the process repeats from step 1.
Let's define the energy conversion efficiency as , i. e. the ration between the useful energy of the photon and spent energy.
Due to some limitations, Arkady can only choose such three states that *E**k*<=-<=*E**i*<=≤<=*U*.
Help Arkady to find such the maximum possible energy conversion efficiency within the above constraints.
The input will be provided via standard input and looks as follows:
The first line contains two integers *n* and *U* (3<=≤<=*n*<=≤<=105, 1<=≤<=*U*<=≤<=109) — the number of states and the maximum possible difference between *E**k* and *E**i*.
The second line contains a sequence of integers *E*1,<=*E*2,<=...,<=*E**n* (1<=≤<=*E*1<=<<=*E*2...<=<<=*E**n*<=≤<=109). It is guaranteed that all *E**i* are given in increasing order.
You should write the solution as standard output as follows:
If it is not possible to choose three states that satisfy all constraints, print -1.
Otherwise, print one real number η — the maximum possible energy conversion efficiency. Your answer is considered correct its absolute or relative error does not exceed 10<=-<=9.
Formally, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct if .
Here are example inputs and outputs for the problem:
Example Input 1: 4 4
1 3 5 7
Example Output 1: 0.5
Example Input 2: 10 8
10 13 15 16 17 19 20 22 24 25
Example Output 2: 0.875
Example Input 3: 3 1
2 5 10
Example Output 3: -1
Now solve the problem by providing the code.
|
def read_ints():
return map(int, input().split())
def read_int():
return int(input())
n, max_delta = read_ints()
a = list(read_ints())
ans = -1
right = 0
for left in range(n):
while right + 1 < n and a[right + 1] - a[left] <= max_delta:
right += 1
if right > left + 1:
res = (a[right] - a[left + 1]) / (a[right] - a[left])
ans = max(ans, res)
print(ans)
|
vfc_84389
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4 4\n1 3 5 7",
"output": "0.5",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 8\n10 13 15 16 17 19 20 22 24 25",
"output": "0.875",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 1\n2 5 10",
"output": "-1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
773
|
Solve the following coding problem using the programming language python:
It can be shown that any positive integer *x* can be uniquely represented as *x*<==<=1<=+<=2<=+<=4<=+<=...<=+<=2*k*<=-<=1<=+<=*r*, where *k* and *r* are integers, *k*<=≥<=0, 0<=<<=*r*<=≤<=2*k*. Let's call that representation prairie partition of *x*.
For example, the prairie partitions of 12, 17, 7 and 1 are:
17<==<=1<=+<=2<=+<=4<=+<=8<=+<=2,
7<==<=1<=+<=2<=+<=4,
1<==<=1.
Alice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice's original sequence could contain. Find all possible options!
The input will be provided via standard input and looks as follows:
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the number of numbers given from Alice to Borys.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=1012; *a*1<=≤<=*a*2<=≤<=...<=≤<=*a**n*) — the numbers given from Alice to Borys.
You should write the solution as standard output as follows:
Output, in increasing order, all possible values of *m* such that there exists a sequence of positive integers of length *m* such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input.
If there are no such values of *m*, output a single integer -1.
Here are example inputs and outputs for the problem:
Example Input 1: 8
1 1 2 2 3 4 5 8
Example Output 1: 2
Example Input 2: 6
1 1 1 2 2 2
Example Output 2: 2 3
Example Input 3: 5
1 2 4 4 4
Example Output 3: -1
Now solve the problem by providing the code.
|
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
def get(cnt):
c = Counter(a)
last = []
while c[1] and (cnt is None or len(last) < cnt):
x = 1
while c[x]:
c[x] -= 1
x *= 2
last.append(x >> 1)
rem = sorted(c.elements())
i = 0
for x in last[::-1]:
if i < len(rem) and rem[i] < 2 * x:
i += 1
return len(last) if i == len(rem) else 0
mx = get(None)
lo, hi = 0, mx
while lo < hi:
mid = (lo + hi) >> 1
if get(mid):
hi = mid
else:
lo = mid + 1
if mx:
print(*range(lo, mx + 1))
else:
print(-1)
|
vfc_84397
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "8\n1 1 2 2 3 4 5 8",
"output": "2 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n1 1 1 2 2 2",
"output": "2 3 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n1 2 4 4 4",
"output": "-1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "20\n1 1 1 1 2 2 2 2 4 4 4 4 8 8 8 8 8 10 10 11",
"output": "4 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "20\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2",
"output": "9 10 11 12 13 14 15 16 17 ",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
816
|
Solve the following coding problem using the programming language python:
To stay woke and attentive during classes, Karen needs some coffee!
Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe".
She knows *n* coffee recipes. The *i*-th recipe suggests that coffee should be brewed between *l**i* and *r**i* degrees, inclusive, to achieve the optimal taste.
Karen thinks that a temperature is admissible if at least *k* recipes recommend it.
Karen has a rather fickle mind, and so she asks *q* questions. In each question, given that she only wants to prepare coffee with a temperature between *a* and *b*, inclusive, can you tell her how many admissible integer temperatures fall within the range?
The input will be provided via standard input and looks as follows:
The first line of input contains three integers, *n*, *k* (1<=≤<=*k*<=≤<=*n*<=≤<=200000), and *q* (1<=≤<=*q*<=≤<=200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively.
The next *n* lines describe the recipes. Specifically, the *i*-th line among these contains two integers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=200000), describing that the *i*-th recipe suggests that the coffee be brewed between *l**i* and *r**i* degrees, inclusive.
The next *q* lines describe the questions. Each of these lines contains *a* and *b*, (1<=≤<=*a*<=≤<=*b*<=≤<=200000), describing that she wants to know the number of admissible integer temperatures between *a* and *b* degrees, inclusive.
You should write the solution as standard output as follows:
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between *a* and *b* degrees, inclusive.
Here are example inputs and outputs for the problem:
Example Input 1: 3 2 4
91 94
92 97
97 99
92 94
93 97
95 96
90 100
Example Output 1: 3
3
0
4
Example Input 2: 2 1 1
1 1
200000 200000
90 100
Example Output 2: 0
Now solve the problem by providing the code.
|
n, k, q = map(int, input().split())
llist = [0] * 200001
for i in range(n):
a, b = map(int, input().split())
llist[b] += 1
llist[a - 1] -= 1
for i in range(len(llist) - 2, -1, -1):
llist[i] += llist[i + 1]
if llist[0] >= k:
llist[0] = 1
else:
llist[0] = 0
for i in range(1, len(llist)):
if llist[i] >= k:
llist[i] = llist[i - 1] + 1
else:
llist[i] = llist[i - 1]
for i in range(q):
a, b = map(int, input().split())
print(llist[b] - llist[a - 1])
|
vfc_84401
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2500"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 2 4\n91 94\n92 97\n97 99\n92 94\n93 97\n95 96\n90 100",
"output": "3\n3\n0\n4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 1 1\n1 1\n200000 200000\n90 100",
"output": "0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
901
|
Solve the following coding problem using the programming language python:
Suppose you have two polynomials and . Then polynomial can be uniquely represented in the following way:
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, denotes the degree of polynomial *P*(*x*). is called the remainder of division of polynomial by polynomial , it is also denoted as .
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials . If the polynomial is zero, the result is , otherwise the result is the value the algorithm returns for pair . On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer *n*. You have to build two polynomials with degrees not greater than *n*, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of *x*) are equal to one, and the described Euclid's algorithm performs exactly *n* steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair to pair .
The input will be provided via standard input and looks as follows:
You are given a single integer *n* (1<=≤<=*n*<=≤<=150) — the number of steps of the algorithm you need to reach.
You should write the solution as standard output as follows:
Print two polynomials in the following format.
In the first line print a single integer *m* (0<=≤<=*m*<=≤<=*n*) — the degree of the polynomial.
In the second line print *m*<=+<=1 integers between <=-<=1 and 1 — the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly *n* steps when called using these polynomials.
If there is no answer for the given *n*, print -1.
If there are multiple answer, print any of them.
Here are example inputs and outputs for the problem:
Example Input 1: 1
Example Output 1: 1
0 1
0
1
Example Input 2: 2
Example Output 2: 2
-1 0 1
1
0 1
Now solve the problem by providing the code.
|
f = [[1], [0, 1]]
n = int(input())
for i in range(2, n + 1):
l = [0] + f[i - 1]
for j in range(len(f[i - 2])):
l[j] = (l[j] + f[i - 2][j]) & 1
f.append(l)
print(n)
print(*f[n])
print(n - 1)
print(*f[n - 1])
# Made By Mostafa_Khaled
|
vfc_84405
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1",
"output": "1\n0 1\n0\n1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2",
"output": "2\n-1 0 1\n1\n0 1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3",
"output": "3\n0 0 0 1\n2\n-1 0 1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
679
|
Solve the following coding problem using the programming language python:
This is an interactive problem. In the output section below you will see the information about flushing the output.
Bear Limak thinks of some hidden number — an integer from interval [2,<=100]. Your task is to say if the hidden number is prime or composite.
Integer *x*<=><=1 is called prime if it has exactly two distinct divisors, 1 and *x*. If integer *x*<=><=1 is not prime, it's called composite.
You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2,<=100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".
For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.
When you are done asking queries, print "prime" or "composite" and terminate your program.
You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2,<=100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.
You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below).
The input will be provided via standard input and looks as follows:
After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise.
You should write the solution as standard output as follows:
Up to 20 times you can ask a query — print an integer from interval [2,<=100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input.
In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program.
To flush you can use (just after printing an integer and end-of-line):
- fflush(stdout) in C++; - System.out.flush() in Java; - stdout.flush() in Python; - flush(output) in Pascal; - See the documentation for other languages.
Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2,<=100]. Of course, his/her solution won't be able to read the hidden number from the input.
Here are example inputs and outputs for the problem:
Example Input 1: yes
no
yes
Example Output 1: 2
80
5
composite
Example Input 2: no
yes
no
no
no
Example Output 2: 58
59
78
78
2
prime
Now solve the problem by providing the code.
|
print("composite" if sum(input()=='yes' for i in [2,3,4,5,7,9,11,13,17,19,23,25,29,31,37,41,43,47,49] if not print(i))>1 else "prime")
# Made By Mostafa_Khaled
|
vfc_84409
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "30",
"output": "composite 4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "59",
"output": "prime 15",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2",
"output": "prime 16",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
842
|
Solve the following coding problem using the programming language python:
Kirill plays a new computer game. He came to the potion store where he can buy any potion. Each potion is characterized by two integers — amount of experience and cost. The efficiency of a potion is the ratio of the amount of experience to the cost. Efficiency may be a non-integer number.
For each two integer numbers *a* and *b* such that *l*<=≤<=*a*<=≤<=*r* and *x*<=≤<=*b*<=≤<=*y* there is a potion with experience *a* and cost *b* in the store (that is, there are (*r*<=-<=*l*<=+<=1)·(*y*<=-<=*x*<=+<=1) potions).
Kirill wants to buy a potion which has efficiency *k*. Will he be able to do this?
The input will be provided via standard input and looks as follows:
First string contains five integer numbers *l*, *r*, *x*, *y*, *k* (1<=≤<=*l*<=≤<=*r*<=≤<=107, 1<=≤<=*x*<=≤<=*y*<=≤<=107, 1<=≤<=*k*<=≤<=107).
You should write the solution as standard output as follows:
Print "YES" without quotes if a potion with efficiency exactly *k* can be bought in the store and "NO" without quotes otherwise.
You can output each of the letters in any register.
Here are example inputs and outputs for the problem:
Example Input 1: 1 10 1 10 1
Example Output 1: YES
Example Input 2: 1 5 6 10 1
Example Output 2: NO
Now solve the problem by providing the code.
|
def play(l,r,x,y,k):
for i in range(x,y+1):
if l<=i*k<=r:
return "YES"
return "NO"
l,r,x,y,k=map(int,input().split())
print(play(l,r,x,y,k))
|
vfc_84413
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1 10 1 10 1",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 5 6 10 1",
"output": "NO",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
117
|
Solve the following coding problem using the programming language python:
And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All *n* participants who have made it to the finals found themselves in a huge *m*-floored 108-star hotel. Of course the first thought to come in a place like this is "How about checking out the elevator?".
The hotel's elevator moves between floors according to one never changing scheme. Initially (at the moment of time 0) the elevator is located on the 1-st floor, then it moves to the 2-nd floor, then — to the 3-rd floor and so on until it reaches the *m*-th floor. After that the elevator moves to floor *m*<=-<=1, then to floor *m*<=-<=2, and so on until it reaches the first floor. This process is repeated infinitely. We know that the elevator has infinite capacity; we also know that on every floor people get on the elevator immediately. Moving between the floors takes a unit of time.
For each of the *n* participant you are given *s**i*, which represents the floor where the *i*-th participant starts, *f**i*, which represents the floor the *i*-th participant wants to reach, and *t**i*, which represents the time when the *i*-th participant starts on the floor *s**i*.
For each participant print the minimum time of his/her arrival to the floor *f**i*.
If the elevator stops on the floor *s**i* at the time *t**i*, then the *i*-th participant can enter the elevator immediately. If the participant starts on the floor *s**i* and that's the floor he wanted to reach initially (*s**i*<==<=*f**i*), then the time of arrival to the floor *f**i* for this participant is considered equal to *t**i*.
The input will be provided via standard input and looks as follows:
The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*<=≤<=105,<=2<=≤<=*m*<=≤<=108).
Next *n* lines contain information about the participants in the form of three space-separated integers *s**i* *f**i* *t**i* (1<=≤<=*s**i*,<=*f**i*<=≤<=*m*,<=0<=≤<=*t**i*<=≤<=108), described in the problem statement.
You should write the solution as standard output as follows:
Print *n* lines each containing one integer — the time of the arrival for each participant to the required floor.
Here are example inputs and outputs for the problem:
Example Input 1: 7 4
2 4 3
1 2 0
2 2 0
1 2 1
4 3 5
1 2 2
4 2 0
Example Output 1: 9
1
0
7
10
7
5
Example Input 2: 5 5
1 5 4
1 3 1
1 3 4
3 1 5
4 2 5
Example Output 2: 12
10
10
8
7
Now solve the problem by providing the code.
|
# n, m = tuple([int(x) for x in input().split()])
# cycle_length = (m - 1) * 2
# for _ in range(n):
# a, b, c = tuple([int(x) for x in input().split()])
# a -= 1
# b -= 1
# if(a > b):
# a = cycle_length - a
# b = cycle_length - b
# elif(a == b):
# print(c)
# continue
# if(a >= c % cycle_length):
# print((c // cycle_length) * cycle_length + b)
# else:
# print((c // cycle_length) * cycle_length + b + cycle_length)
# codeforces should increase the run time allotted...
# The solution runs in O(n) time but times out on input
# Below, replaced input statements with input statements
# copied from 205669844
from sys import stdin
n, m = map(int, stdin.readline().split())
cycle_length = (m - 1) * 2
for _ in range(n):
a, b, c = map(int, stdin.readline().split())
a -= 1
b -= 1
if(a > b):
a = cycle_length - a
b = cycle_length - b
elif(a == b):
print(c)
continue
if(a >= c % cycle_length):
print((c // cycle_length) * cycle_length + b)
else:
print((c // cycle_length) * cycle_length + b + cycle_length)
|
vfc_84417
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2500"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7 4\n2 4 3\n1 2 0\n2 2 0\n1 2 1\n4 3 5\n1 2 2\n4 2 0",
"output": "9\n1\n0\n7\n10\n7\n5",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
697
|
Solve the following coding problem using the programming language python:
Ted has a pineapple. This pineapple is able to bark like a bulldog! At time *t* (in seconds) it barks for the first time. Then every *s* seconds after it, it barks twice with 1 second interval. Thus it barks at times *t*, *t*<=+<=*s*, *t*<=+<=*s*<=+<=1, *t*<=+<=2*s*, *t*<=+<=2*s*<=+<=1, etc.
Barney woke up in the morning and wants to eat the pineapple, but he can't eat it when it's barking. Barney plans to eat it at time *x* (in seconds), so he asked you to tell him if it's gonna bark at that time.
The input will be provided via standard input and looks as follows:
The first and only line of input contains three integers *t*, *s* and *x* (0<=≤<=*t*,<=*x*<=≤<=109, 2<=≤<=*s*<=≤<=109) — the time the pineapple barks for the first time, the pineapple barking interval, and the time Barney wants to eat the pineapple respectively.
You should write the solution as standard output as follows:
Print a single "YES" (without quotes) if the pineapple will bark at time *x* or a single "NO" (without quotes) otherwise in the only line of output.
Here are example inputs and outputs for the problem:
Example Input 1: 3 10 4
Example Output 1: NO
Example Input 2: 3 10 3
Example Output 2: YES
Example Input 3: 3 8 51
Example Output 3: YES
Example Input 4: 3 8 52
Example Output 4: YES
Now solve the problem by providing the code.
|
t,s,x=map(int,input().split())
a=x-t
b=x-t-1
if(t==x):
print("YES")
else:
if(a>=s)or(b>=s):
if(a>=s)and(a%s==0):
print("YES")
elif((b%s==0)and(b>=s)):
print("YES")
else:
print("NO")
else:
print("NO")
|
vfc_84421
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 10 4",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 10 3",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 8 51",
"output": "YES",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
586
|
Solve the following coding problem using the programming language python:
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, *n* houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the *j*-th house of the *i*-th row to the (*j*<=+<=1)-th house of the same row has waiting time equal to *a**ij* (1<=≤<=*i*<=≤<=2,<=1<=≤<=*j*<=≤<=*n*<=-<=1). For the traffic light on the crossing from the *j*-th house of one row to the *j*-th house of another row the waiting time equals *b**j* (1<=≤<=*j*<=≤<=*n*). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
The input will be provided via standard input and looks as follows:
The first line of the input contains integer *n* (2<=≤<=*n*<=≤<=50) — the number of houses in each row.
Each of the next two lines contains *n*<=-<=1 space-separated integer — values *a**ij* (1<=≤<=*a**ij*<=≤<=100).
The last line contains *n* space-separated integers *b**j* (1<=≤<=*b**j*<=≤<=100).
You should write the solution as standard output as follows:
Print a single integer — the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Here are example inputs and outputs for the problem:
Example Input 1: 4
1 2 3
3 2 1
3 2 2 3
Example Output 1: 12
Example Input 2: 3
1 2
3 3
2 1 3
Example Output 2: 11
Example Input 3: 2
1
1
1 1
Example Output 3: 4
Now solve the problem by providing the code.
|
N = int(input())
fr, sr = [ int(i) for i in input().split() ], [ int(i) for i in input().split() ]
cross = [ int(i) for i in input().split() ]
spend_time = [sum(sr) + cross[0]]
for i in range(1, N):
summ = spend_time[-1] - sr[i - 1] - cross[i - 1]
summ = summ + fr[i - 1] + cross[i]
spend_time.append(summ)
spend_time.sort()
print(spend_time[0] + spend_time[1])
|
vfc_84425
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 2 3\n3 2 1\n3 2 2 3",
"output": "12",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1 2\n3 3\n2 1 3",
"output": "11",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1\n1\n1 1",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1\n1\n2 1",
"output": "5",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1 100\n1 1\n100 100 100",
"output": "204",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
377
|
Solve the following coding problem using the programming language python:
Pavel loves grid mazes. A grid maze is an *n*<=×<=*m* rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.
Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly *k* empty cells into walls so that all the remaining cells still formed a connected area. Help him.
The input will be provided via standard input and looks as follows:
The first line contains three integers *n*, *m*, *k* (1<=≤<=*n*,<=*m*<=≤<=500, 0<=≤<=*k*<=<<=*s*), where *n* and *m* are the maze's height and width, correspondingly, *k* is the number of walls Pavel wants to add and letter *s* represents the number of empty cells in the original maze.
Each of the next *n* lines contains *m* characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall.
You should write the solution as standard output as follows:
Print *n* lines containing *m* characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#").
It is guaranteed that a solution exists. If there are multiple solutions you can output any of them.
Here are example inputs and outputs for the problem:
Example Input 1: 3 4 2
#..#
..#.
#...
Example Output 1: #.X#
X.#.
#...
Example Input 2: 5 4 5
#...
#.#.
.#..
...#
.#.#
Example Output 2: #XXX
#X#.
X#..
...#
.#.#
Now solve the problem by providing the code.
|
directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]
def main():
height, width, walls_to_add = map(int, input().split())
total_open_squares = height * width
maze = []
for _ in range(height):
maze.append(list(input().strip()))
total_open_squares -= sum(row.count('#') for row in maze)
required_open_squares = total_open_squares - walls_to_add
def dfs(square):
stack = [square]
visited = set()
visited.add(square)
count = 1
while stack and count < required_open_squares:
x, y = stack.pop()
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0 <= nx < height and 0 <= ny < width and maze[nx][ny] == '.' and (nx, ny) not in visited:
visited.add((nx, ny))
stack.append((nx, ny))
count += 1
if count == required_open_squares:
return visited
return visited
visited = set()
for i in range(height):
for j in range(width):
if maze[i][j] == '.':
visited = dfs((i, j))
break
if visited:
break
for i in range(height):
for j in range(width):
if maze[i][j] == '.' and (i, j) not in visited:
maze[i][j] = 'X'
for row in maze:
print(''.join(row))
if __name__ == '__main__':
main()
|
vfc_84429
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 4 5\n#...\n#.#.\n.#..\n...#\n.#.#",
"output": "#XXX\n#X#.\nX#..\n...#\n.#.#",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
301
|
Solve the following coding problem using the programming language python:
Yaroslav has an array, consisting of (2·*n*<=-<=1) integers. In a single operation Yaroslav can change the sign of exactly *n* elements in the array. In other words, in one operation Yaroslav can select exactly *n* array elements, and multiply each of them by -1.
Yaroslav is now wondering: what maximum sum of array elements can be obtained if it is allowed to perform any number of described operations?
Help Yaroslav.
The input will be provided via standard input and looks as follows:
The first line contains an integer *n* (2<=≤<=*n*<=≤<=100). The second line contains (2·*n*<=-<=1) integers — the array elements. The array elements do not exceed 1000 in their absolute value.
You should write the solution as standard output as follows:
In a single line print the answer to the problem — the maximum sum that Yaroslav can get.
Here are example inputs and outputs for the problem:
Example Input 1: 2
50 50 50
Example Output 1: 150
Example Input 2: 2
-1 -100 -1
Example Output 2: 100
Now solve the problem by providing the code.
|
n, t = int(input()), list(map(int, input().split()))
p = list(map(abs, t))
s = sum(p)
if n & 1 == 0 and len([0 for i in t if i < 0]) & 1: s -= 2 * min(p)
print(s)
|
vfc_84433
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n50 50 50",
"output": "150",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n-1 -100 -1",
"output": "100",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
717
|
Solve the following coding problem using the programming language python:
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class.
Each of the *n* students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence *a*, where *a**i* represents both the laziness level of the *i*-th student and the difficulty of his task.
The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10<=007.
The input will be provided via standard input and looks as follows:
The first line of input contains integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of tasks. The next *n* lines contain exactly one integer number *a**i* (1<=≤<=*a**i*<=≤<=100<=000) — both the difficulty of the initial task and the laziness of the *i*-th students.
You should write the solution as standard output as follows:
Print the minimum total time to finish all tasks modulo 10<=007.
Here are example inputs and outputs for the problem:
Example Input 1: 2
1
3
Example Output 1: 6
Now solve the problem by providing the code.
|
n = int(input())
a = [int(input()) for i in range(n)]
mod = 10007
a.sort()
ans = 0
for i in range(n):
ans += a[i] * a[n - 1 - i] % mod
if ans >= mod:
ans -= mod
print(ans)
|
vfc_84437
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1\n3",
"output": "6",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
547
|
Solve the following coding problem using the programming language python:
Mike is the president of country What-The-Fatherland. There are *n* bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to *n* from left to right. *i*-th bear is exactly *a**i* feet high.
A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group.
Mike is a curious to know for each *x* such that 1<=≤<=*x*<=≤<=*n* the maximum strength among all groups of size *x*.
The input will be provided via standard input and looks as follows:
The first line of input contains integer *n* (1<=≤<=*n*<=≤<=2<=×<=105), the number of bears.
The second line contains *n* integers separated by space, *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109), heights of bears.
You should write the solution as standard output as follows:
Print *n* integers in one line. For each *x* from 1 to *n*, print the maximum strength among all groups of size *x*.
Here are example inputs and outputs for the problem:
Example Input 1: 10
1 2 3 4 5 4 3 2 1 6
Example Output 1: 6 4 4 3 3 2 2 1 1 1
Now solve the problem by providing the code.
|
n = int(input())
a = [int(i) for i in input().split()]
l = [-1 for _ in range(n)]
r = [n for _ in range(n)]
answer = [0 for _ in range(n+1)]
s = []
for i in range(n):
while s and a[s[-1]] >= a[i]:
s.pop()
if s:
l[i] = s[-1]
s.append(i)
while s:
s.pop()
for i in range(n-1, -1, -1):
while s and a[s[-1]] >= a[i]:
s.pop()
if s:
r[i] = s[-1]
s.append(i)
for i in range(n):
length = r[i] - l[i] - 1
answer[length] = max(answer[length], a[i])
for i in range(n-1, -1, -1):
answer[i] = max(answer[i], answer[i+1])
answer = answer[1:]
print(*answer)
|
vfc_84441
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "10\n1 2 3 4 5 4 3 2 1 6",
"output": "6 4 4 3 3 2 2 1 1 1 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n524125987 923264237 374288891",
"output": "923264237 524125987 374288891 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n585325539 365329221 412106895 291882089 564718673",
"output": "585325539 365329221 365329221 291882089 291882089 ",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
879
|
Solve the following coding problem using the programming language python:
*n* people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins *k* games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.
The input will be provided via standard input and looks as follows:
The first line contains two integers: *n* and *k* (2<=≤<=*n*<=≤<=500, 2<=≤<=*k*<=≤<=1012) — the number of people and the number of wins.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*) — powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all *a**i* are distinct.
You should write the solution as standard output as follows:
Output a single integer — power of the winner.
Here are example inputs and outputs for the problem:
Example Input 1: 2 2
1 2
Example Output 1: 2
Example Input 2: 4 2
3 1 2 4
Example Output 2: 3
Example Input 3: 6 2
6 5 3 1 2 4
Example Output 3: 6
Example Input 4: 2 10000000000
2 1
Example Output 4: 2
Now solve the problem by providing the code.
|
n, k = map(int, input().split())
v = list(map(int, input().split()))
count = 0
previous = v[0]
for i in range(1, n):
if previous>v[i]:
count+=1
else:
previous = v[i]
count = 1
if count==k:
break
print(previous)
|
vfc_84445
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 2\n1 2",
"output": "2 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 2\n3 1 2 4",
"output": "3 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 2\n6 5 3 1 2 4",
"output": "6 ",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
274
|
Solve the following coding problem using the programming language python:
A *k*-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by *k*. That is, there are no two integers *x* and *y* (*x*<=<<=*y*) from the set, such that *y*<==<=*x*·*k*.
You're given a set of *n* distinct positive integers. Your task is to find the size of it's largest *k*-multiple free subset.
The input will be provided via standard input and looks as follows:
The first line of the input contains two integers *n* and *k* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*k*<=≤<=109). The next line contains a list of *n* distinct positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
All the numbers in the lines are separated by single spaces.
You should write the solution as standard output as follows:
On the only line of the output print the size of the largest *k*-multiple free subset of {*a*1,<=*a*2,<=...,<=*a**n*}.
Here are example inputs and outputs for the problem:
Example Input 1: 6 2
2 3 6 5 4 10
Example Output 1: 3
Now solve the problem by providing the code.
|
n, k = [int(e) for e in input().split()]
a = sorted([int(e) for e in input().split()])
s = set()
for i in range(n):
if a[i] % k != 0:
s.add(a[i])
elif a[i] / k not in s:
s.add(a[i])
print(len(s))
|
vfc_84449
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6 2\n2 3 6 5 4 10",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 2\n1 2 3 4 5 6 7 8 9 10",
"output": "6",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
257
|
Solve the following coding problem using the programming language python:
Petya and Vasya decided to play a little. They found *n* red cubes and *m* blue cubes. The game goes like that: the players take turns to choose a cube of some color (red or blue) and put it in a line from left to right (overall the line will have *n*<=+<=*m* cubes). Petya moves first. Petya's task is to get as many pairs of neighbouring cubes of the same color as possible. Vasya's task is to get as many pairs of neighbouring cubes of different colors as possible.
The number of Petya's points in the game is the number of pairs of neighboring cubes of the same color in the line, the number of Vasya's points in the game is the number of neighbouring cubes of the different color in the line. Your task is to calculate the score at the end of the game (Petya's and Vasya's points, correspondingly), if both boys are playing optimally well. To "play optimally well" first of all means to maximize the number of one's points, and second — to minimize the number of the opponent's points.
The input will be provided via standard input and looks as follows:
The only line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105) — the number of red and blue cubes, correspondingly.
You should write the solution as standard output as follows:
On a single line print two space-separated integers — the number of Petya's and Vasya's points correspondingly provided that both players play optimally well.
Here are example inputs and outputs for the problem:
Example Input 1: 3 1
Example Output 1: 2 1
Example Input 2: 2 4
Example Output 2: 3 2
Now solve the problem by providing the code.
|
n,m = map(int,input().split())
vasya = min(n,m)
petya = n+m-1-vasya
print('{} {}'.format(petya,vasya))
|
vfc_84453
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 1",
"output": "2 1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 4",
"output": "3 2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1",
"output": "0 1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 1",
"output": "1 1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
362
|
Solve the following coding problem using the programming language python:
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.
Now Petya is on the first stair of the staircase, consisting of *n* stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number *n* without touching a dirty stair once.
One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
The input will be provided via standard input and looks as follows:
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=109, 0<=≤<=*m*<=≤<=3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains *m* different space-separated integers *d*1,<=*d*2,<=...,<=*d**m* (1<=≤<=*d**i*<=≤<=*n*) — the numbers of the dirty stairs (in an arbitrary order).
You should write the solution as standard output as follows:
Print "YES" if Petya can reach stair number *n*, stepping only on the clean stairs. Otherwise print "NO".
Here are example inputs and outputs for the problem:
Example Input 1: 10 5
2 4 8 3 6
Example Output 1: NO
Example Input 2: 10 5
2 4 5 7 9
Example Output 2: YES
Now solve the problem by providing the code.
|
n,k=map(int,input().split())
if(k>0):
l=list(map(int,input().split()))
l.sort()
c=0
if(k==0):
print("YES")
elif(l[0]==1 or l[-1]==n):
print("NO")
else:
for i in range(2,k):
if (l[i]-l[i-1]==1 and l[i-1]-l[i-2]==1):
c=1
break
if(c==0):
print("YES")
else:
print('NO')
|
vfc_84457
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "10 5\n2 4 8 3 6",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 5\n2 4 5 7 9",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 9\n2 3 4 5 6 7 8 9 10",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 2\n4 5",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "123 13\n36 73 111 2 92 5 47 55 48 113 7 78 37",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 10\n7 6 4 2 5 10 8 3 9 1",
"output": "NO",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
568
|
Solve the following coding problem using the programming language python:
Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable properties. Help Rikhail to convince the scientific community in this!
Let us remind you that a number is called prime if it is integer larger than one, and is not divisible by any positive integer other than itself and one.
Rikhail calls a number a palindromic if it is integer, positive, and its decimal representation without leading zeros is a palindrome, i.e. reads the same from left to right and right to left.
One problem with prime numbers is that there are too many of them. Let's introduce the following notation: π(*n*) — the number of primes no larger than *n*, *rub*(*n*) — the number of palindromic numbers no larger than *n*. Rikhail wants to prove that there are a lot more primes than palindromic ones.
He asked you to solve the following problem: for a given value of the coefficient *A* find the maximum *n*, such that π(*n*)<=≤<=*A*·*rub*(*n*).
The input will be provided via standard input and looks as follows:
The input consists of two positive integers *p*, *q*, the numerator and denominator of the fraction that is the value of *A* (, ).
You should write the solution as standard output as follows:
If such maximum number exists, then print it. Otherwise, print "Palindromic tree is better than splay tree" (without the quotes).
Here are example inputs and outputs for the problem:
Example Input 1: 1 1
Example Output 1: 40
Example Input 2: 1 42
Example Output 2: 1
Example Input 3: 6 4
Example Output 3: 172
Now solve the problem by providing the code.
|
n = 2 * 10 ** 6 + 10 ** 5
p, q = map(int, input().split(' '))
pi = 0
si = 0
pr = [0] * n
pr[1] = 1
for i in range(2, n):
if pr[i] == 0:
for j in range(i*i, n, i):
pr[j] = 1
res = 'Palindromic tree is better than splay tree'
for i in range(1, n):
if pr[i] == 0:
pi += 1
j = str(i)
if j == j[::-1]:
si += 1
if q*pi <= si*p:
res = i
print(res)
|
vfc_84461
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "3000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1 1",
"output": "40",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 42",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 4",
"output": "172",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
742
|
Solve the following coding problem using the programming language python:
There exists an island called Arpa’s land, some beautiful girls live there, as ugly ones do.
Mehrdad wants to become minister of Arpa’s land. Arpa has prepared an exam. Exam has only one question, given *n*, print the last digit of 1378*n*.
Mehrdad has become quite confused and wants you to help him. Please help, although it's a naive cheat.
The input will be provided via standard input and looks as follows:
The single line of input contains one integer *n* (0<=<=≤<=<=*n*<=<=≤<=<=109).
You should write the solution as standard output as follows:
Print single integer — the last digit of 1378*n*.
Here are example inputs and outputs for the problem:
Example Input 1: 1
Example Output 1: 8
Example Input 2: 2
Example Output 2: 4
Now solve the problem by providing the code.
|
x = int(input())
if not x:
print(1)
exit()
print('8426'[(x-1) % 4])
|
vfc_84465
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1",
"output": "8",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1000",
"output": "6",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3",
"output": "2",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
203
|
Solve the following coding problem using the programming language python:
A boy Valera registered on site Codeforces as Valera, and wrote his first Codeforces Round #300. He boasted to a friend Arkady about winning as much as *x* points for his first contest. But Arkady did not believe his friend's words and decided to check whether Valera could have shown such a result.
He knows that the contest number 300 was unusual because there were only two problems. The contest lasted for *t* minutes, the minutes are numbered starting from zero. The first problem had the initial cost of *a* points, and every minute its cost reduced by *d**a* points. The second problem had the initial cost of *b* points, and every minute this cost reduced by *d**b* points. Thus, as soon as the zero minute of the contest is over, the first problem will cost *a*<=-<=*d**a* points, and the second problem will cost *b*<=-<=*d**b* points. It is guaranteed that at any moment of the contest each problem has a non-negative cost.
Arkady asks you to find out whether Valera could have got exactly *x* points for this contest. You should assume that Valera could have solved any number of the offered problems. You should also assume that for each problem Valera made no more than one attempt, besides, he could have submitted both problems at the same minute of the contest, starting with minute 0 and ending with minute number *t*<=-<=1. Please note that Valera can't submit a solution exactly *t* minutes after the start of the contest or later.
The input will be provided via standard input and looks as follows:
The single line of the input contains six integers *x*,<=*t*,<=*a*,<=*b*,<=*d**a*,<=*d**b* (0<=≤<=*x*<=≤<=600; 1<=≤<=*t*,<=*a*,<=*b*,<=*d**a*,<=*d**b*<=≤<=300) — Valera's result, the contest's duration, the initial cost of the first problem, the initial cost of the second problem, the number of points that the first and the second problem lose per minute, correspondingly.
It is guaranteed that at each minute of the contest each problem has a non-negative cost, that is, *a*<=-<=*i*·*d**a*<=≥<=0 and *b*<=-<=*i*·*d**b*<=≥<=0 for all 0<=≤<=*i*<=≤<=*t*<=-<=1.
You should write the solution as standard output as follows:
If Valera could have earned exactly *x* points at a contest, print "YES", otherwise print "NO" (without the quotes).
Here are example inputs and outputs for the problem:
Example Input 1: 30 5 20 20 3 5
Example Output 1: YES
Example Input 2: 10 4 100 5 5 1
Example Output 2: NO
Now solve the problem by providing the code.
|
x, t, a, b, da, db = map(int, input().split())
A = [0] + [a-i*da for i in range(t)]
B = {0} | {b-i*db for i in range(t)}
print('YES' if any(x-a in B for a in A) else 'NO')
|
vfc_84469
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "30 5 20 20 3 5",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 4 100 5 5 1",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "0 7 30 50 3 4",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "50 10 30 20 1 2",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "40 1 40 5 11 2",
"output": "YES",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
65
|
Solve the following coding problem using the programming language python:
A long time ago (probably even in the first book), Nicholas Flamel, a great alchemist and the creator of the Philosopher's Stone, taught Harry Potter three useful spells. The first one allows you to convert *a* grams of sand into *b* grams of lead, the second one allows you to convert *c* grams of lead into *d* grams of gold and third one allows you to convert *e* grams of gold into *f* grams of sand. When Harry told his friends about these spells, Ron Weasley was amazed. After all, if they succeed in turning sand into lead, lead into gold, and then turning part of the gold into sand again and so on, then it will be possible to start with a small amount of sand and get huge amounts of gold! Even an infinite amount of gold! Hermione Granger, by contrast, was skeptical about that idea. She argues that according to the law of conservation of matter getting an infinite amount of matter, even using magic, is impossible. On the contrary, the amount of matter may even decrease during transformation, being converted to magical energy. Though Hermione's theory seems convincing, Ron won't believe her. As far as Ron is concerned, Hermione made up her law of conservation of matter to stop Harry and Ron wasting their time with this nonsense, and to make them go and do homework instead. That's why Ron has already collected a certain amount of sand for the experiments. A quarrel between the friends seems unavoidable...
Help Harry to determine which one of his friends is right, and avoid the quarrel after all. To do this you have to figure out whether it is possible to get the amount of gold greater than any preassigned number from some finite amount of sand.
The input will be provided via standard input and looks as follows:
The first line contains 6 integers *a*, *b*, *c*, *d*, *e*, *f* (0<=≤<=*a*,<=*b*,<=*c*,<=*d*,<=*e*,<=*f*<=≤<=1000).
You should write the solution as standard output as follows:
Print "Ron", if it is possible to get an infinitely large amount of gold having a certain finite amount of sand (and not having any gold and lead at all), i.e., Ron is right. Otherwise, print "Hermione".
Here are example inputs and outputs for the problem:
Example Input 1: 100 200 250 150 200 250
Example Output 1: Ron
Example Input 2: 100 50 50 200 200 100
Example Output 2: Hermione
Example Input 3: 100 10 200 20 300 30
Example Output 3: Hermione
Example Input 4: 0 0 0 0 0 0
Example Output 4: Hermione
Example Input 5: 1 1 0 1 1 1
Example Output 5: Ron
Example Input 6: 1 0 1 2 1 2
Example Output 6: Hermione
Example Input 7: 100 1 100 1 0 1
Example Output 7: Ron
Now solve the problem by providing the code.
|
# LUOGU_RID: 112089745
a, b, c, d, e, f = map(int, input().split());
if a * c * e < b * d * f or a == 0 and b * d > 0 or c == 0 and d > 0:
print("Ron");
else:
print("Hermione");
|
vfc_84473
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "100 200 250 150 200 250",
"output": "Ron",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "100 50 50 200 200 100",
"output": "Hermione",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
417
|
Solve the following coding problem using the programming language python:
While resting on the ship after the "Russian Code Cup" a boy named Misha invented an interesting game. He promised to give his quadrocopter to whoever will be the first one to make a rectangular table of size *n*<=×<=*m*, consisting of positive integers such that the sum of the squares of numbers for each row and each column was also a square.
Since checking the correctness of the table manually is difficult, Misha asks you to make each number in the table to not exceed 108.
The input will be provided via standard input and looks as follows:
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100) — the size of the table.
You should write the solution as standard output as follows:
Print the table that meets the condition: *n* lines containing *m* integers, separated by spaces. If there are multiple possible answers, you are allowed to print anyone. It is guaranteed that there exists at least one correct answer.
Here are example inputs and outputs for the problem:
Example Input 1: 1 1
Example Output 1: 1
Example Input 2: 1 2
Example Output 2: 3 4
Now solve the problem by providing the code.
|
import math
n,m = input().split(' ')
n,m = int(n),int(m)
fix = [[1],[3,4],[1,2,2],[1,1,3,5],[1,1,1,2,3]]
a = n
parts = list()
b = a
while b > 0:
parts.append(math.floor(b**0.5))
b-=math.floor(b**0.5)**2
lcm = math.lcm(*parts)
a_out = list()
for i in range(len(parts)):
part = parts[i]
for j in range(part**2):
a_out.append(int(lcm*fix[len(parts)-1][i]/part))
a = m
parts = list()
b = a
while b > 0:
parts.append(math.floor(b**0.5))
b-=math.floor(b**0.5)**2
lcm = math.lcm(*parts)
b_out = list()
for i in range(len(parts)):
part = parts[i]
for j in range(part**2):
b_out.append(int(lcm*fix[len(parts)-1][i]/part))
for i in range(n):
tp = ''
for j in range(m):
tp += str(a_out[i]*b_out[j]) +' '
print(tp.strip())
|
vfc_84477
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1 1",
"output": "1 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 2",
"output": "3 4 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 1",
"output": "1 \n1 \n1 \n1 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 4",
"output": "1 1 1 1 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 1",
"output": "3 \n4 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 4",
"output": "3 3 3 3 \n4 4 4 4 ",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
98
|
Solve the following coding problem using the programming language python:
This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive.
Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, but not happily ever after: one day a vicious dragon attacked the kingdom and stole Victoria. The King was full of grief, yet he gathered his noble knights and promised half of his kingdom and Victoria's hand in marriage to the one who will save the girl from the infernal beast.
Having travelled for some time, the knights found the dragon's lair and all of them rushed there to save Victoria. Each knight spat on the dragon once and, as the dragon had quite a fragile and frail heart, his heart broke and poor beast died. As for the noble knights, they got Victoria right to the King and started brawling as each one wanted the girl's hand in marriage.
The problem was that all the noble knights were equally noble and equally handsome, and Victoria didn't want to marry any of them anyway. Then the King (and he was a very wise man and didn't want to hurt anybody's feelings) decided to find out who will get his daughter randomly, i.e. tossing a coin. However, there turned out to be *n* noble knights and the coin only has two sides. The good thing is that when a coin is tossed, the coin falls on each side with equal probability. The King got interested how to pick one noble knight using this coin so that all knights had equal probability of being chosen (the probability in that case should always be equal to 1<=/<=*n*). First the King wants to know the expected number of times he will need to toss a coin to determine the winner. Besides, while tossing the coin, the King should follow the optimal tossing strategy (i.e. the strategy that minimizes the expected number of tosses). Help the King in this challenging task.
The input will be provided via standard input and looks as follows:
The first line contains a single integer *n* from the problem's statement (1<=≤<=*n*<=≤<=10000).
You should write the solution as standard output as follows:
Print the sought expected number of tosses as an irreducible fraction in the following form: "*a*/*b*" (without the quotes) without leading zeroes.
Here are example inputs and outputs for the problem:
Example Input 1: 2
Example Output 1: 1/1
Example Input 2: 3
Example Output 2: 8/3
Example Input 3: 4
Example Output 3: 2/1
Now solve the problem by providing the code.
|
import math
from fractions import Fraction
import time
MAXN = 10000
def solve(free_nodes, n):
seq = []
used = {}
used[0] = 0
pos = 0
while used.get(free_nodes) == None:
used[free_nodes] = pos
toss = 0
while free_nodes < n:
toss += 1
free_nodes *= 2
seq.append((toss, Fraction(free_nodes - n, free_nodes)))
free_nodes -= n
pos += 1
first_loop_idx = used.get(free_nodes)
ans = Fraction(0, 1)
prod_prob = Fraction(1, 1)
pos = len(seq) - 1
while pos >= 0:
tosses = seq[pos][0]
prob = seq[pos][1]
ans = ans * prob + Fraction(tosses, 1)
prod_prob *= prob
if pos == first_loop_idx:
break
pos -= 1
expected = ans / (Fraction(1, 1) - prod_prob)
seq[first_loop_idx] = (expected, 0)
pos = first_loop_idx
ans = Fraction(0, 1)
while pos >= 0:
tosses = seq[pos][0]
prob = seq[pos][1]
ans = ans * prob + Fraction(tosses, 1)
pos -= 1
return ans
def main():
answers = {}
answers[7901]="154524871223548423123495185797654506574461761702003065846910905746685998647514825069390588667489327248905358174327876552502957660057253253483877516101528529986709707135176302376822177221161432613294608372321628357823583864101273914414227874808941949788705240048101067284633674852304568823278847645592602559676080522967116394164201940415879553271492035834028327398486009600093157465503523211488955021953127268647916111031703682619831400080346816600935122503273822918215392148364075637874362562003886513246910604701515426048343641513851237428592540111811008678402471757951449650412535345381923708040483001118261857832491735489869517576740499550045310104023129873482509874323682634142866901829084234882445385200295319524648351689094720128553785639719221969264129589951800322116024545902369342877667788536967565125990256796799578322951449803408736681264649241378320858137626918605730290281622575456429027323727694782739314124549860068706433370934863613425473482843884549752732489665702585505388955369522536754702394875839852529518821561333302341509362321195291824828007269021295402144559299990594887703714374425464288060958835387801765108977136997785318571620158461213961462833468536927703562738/11708004285457312755126666863217644760055861406773594249600054075715122508974581228961888366743801655492643437810966154300770740887430126947893305147118944410195839406784735953274330671167286093169745122932079008472101285489067230450306319942053264911861843858846013256127918927680942561098504170833048864333768657182434590844327804335990621076825744270870324389495315888792715548357390675237849554405351811596599797655317280518276360549141571034933129894218745679847870164916740677879764869800871601845140782167652942474700432778572851450966443631222817719445949290846552185879120812492319719862315771781394854730275436023186795781303781840036204405022306942239374739830768947586427274411392455925485082460607204057421762514961554176729112109598477706758573572158753606767581854626464460620360300538015606769824377407345840454826888698131026097851727716692518589246731498897676124408346168821318136621626754567759664177038839955381358225129907645563115937666757425864999645653438548784189677256397395962427979423494151205540932406527491201269878128444061666062993821362430091038452053553637944869816002598967413541372965358839069476857294840674149717585402596923888822980913065149411098625"
answers[9677]="355015421701713202212473860076525882009902216644337983789712546902855564673657169603761249467720400461970489489113546637794041573523526730170748241877537168935035448171103590132975908684211409578927894713007115704550974932706751558843727054948620629495926397359768216611724650526720850411510463876908039969375451684402837509632771646645177985818783287784638536610973174086480221990592966939073544073129241828876855517834517927054871606428776208634273614802546913903241862411771943212010860464482597415685421153568152651624488000580022986646520292360832102090929980870528643545255623373496573726523959723895369828813695219952013125760986556499948313326699376723278370296387425751974594381046366289927557778079267582992094864234023846489168838099075131570386619007407914793038746270712835369525282370958100431637609331765278757991507826864319380122050098235701341246980552596050268124188631452719152745485969575451660773928902074857476078067406169526046841544496200004071166470664478121574731743106703176076616567026951873962889620766354000786634778217195180169674610973441887042266158981969468691882394838540158036461706007682008948872776640135753623571550802521470741987543328917863072840442897261808591862855854318665037353750640231558222883736432456737760890388302545705798404006886873175954856778651125124414825317569798557735990408364421387615142166977843160724913799826226147631507851204157493078199200729703642516155768326800341864259284428724430981938/24161229041093868938167096036170656581148522733477688547261282469337063357483394938167166903472548746587404081486671795113165954899343611098115607541397404292873362806248728502588800585512998777600033616792172309254883839206637069189555255442891066800434624158543262785265015150098229042435359388539219921639220218269440815388818036980365533565004620040497306764512807813441994883170239109645345986288639407216784179724443178968696693613260562923444367236930624063728947412648829923452314809429787376836652426691660599576798962545915528618911236512479993206515809978978648695129649871910364398615156344266149547982458433036808163793383537632482513089121772014669802480412996400494134868452868234286759470106774637235981426506704638332034883255172423927559642487526813597003924444637677544919662326969340692257915396913234997704693823575079725300545776339756575064121649630028865772015503098586755819568277907891249679180033268400909838077906851564035966964679394900898147925032824546395476864580855337672397183685376407486551748336626559011399276003179795542628358706535984818677212988570736994567317130991320183536176021625899392029640170609055751805596338312028330140696084842046763810806230674825998696642581652399790312867450216407727422365888228987418320643334241435540378709106406300100247499616647356316514697146655316118779950550134717711148638498868581991387487601985619566383393531523585077391874513674532507245281651626638820005842165499579858945"
answers[9733]="95484332877590589554469754771446691003599663592905494408439345619582478641998729504995236393657485029791665442799860526876865749777294323119083076321567009541636671190882179584977739221093063736924544373790908085293665964560075463518378551209058371534457516011892072208377822471345716624828410934324137904318677235430150163578608344109579889521882494969598385295234012317570397732508935799426108056469502224626374842457401918802043515225722679525347961446924556134853285440364336064603065829017067847225685993600461140265253652742105117690561160743911477362779575542189272136064250743190600552890952283014623955797131750064699495418563936864723524709763859874646053576454946899546950910302422000561333155108232250954810214949863400691198989713949529202484236526054871760424497926157074028106143333163969074021979292056212512327926099849931861845561313953950570409114884706721641699210621190980872119389261240676519621068057243738027574759222417016494977306477751003082809341460666604302257397590847967794595829725469987512493547459114166418595444720830389398509346323286112779037870871618987335442875512148343912833469647271576937635892165979513514129039128435307048334834528232324783005922951454548165693723699348030381290479329276604925890470367714103163692490772390053007326510368853943687690444780249282100467642857923059896714215481944561286077482708178927733059970371869942524490998102645615476873969100436610441403602888640392767805625730515962311606242963562/6485730535166475447221120228665262693180004703687449791010059910801300620066946132654995251961761606272418258470135901243541274707080736345809323851092051098573818695148817085012621865665248820792507609538936031065407763604464299894401815432008945474901129334187177041490444422663526557798579047986406673549508346775576876143909187057849285049205320822677633008103880183241664826013469861026682448107160666895807152137916859092551706675767886855171481809876932110911653059114408828368367220124838074484190631672482484444631436531339755862298487996751416667267973622916483976740932542486600258468425261784376839276865109473281064232399599707268603813104371510285927118259593757093001718738645698978681113120430418435675253831857746647974863294508973975619783598336234520138748692085861726551470040106036066745609189179825229044018439871760076297406742520636567156335724257909029876673773613338548753986464403339513606040545936498368623199329089225116307992564649264026669147811640072079662988481212151438123916615699776475294332432339591869935873851983425866472210907920557265210694985396108573392747296754266765509537102861414164709599224713359678465358076426719595086236297691989511016878068258838104680788657074879171206938956666398706992546491805593270055164647640460163376165191669527688682783272454558284018103059264318657168846189582863810223461059222943290931488307133356293744648513461983935004483125773001369169210484002829933395570164359907187244048318465"
answers[9749]="24420738504895369936029275850163071991656213953040750251776948750246796077962845691317677269631705913994021086756383098235089231208690232714718182051661959094144212987702253303432217073773124260159318146666200117041612592906844115211509040675212920139551853176678612535045654292046757526749492513241228125837513438956933418658716846512730365072509590131877465449441381623988912185373433489319776194980015068338689021210658939139364253499030464561574097560424964521204779116215457677726768619050046856738764484240389262833903373023132839740002736774915426122802238815407817050693856360952407545226333981896371433729874311657730340419746680805378905702168666514377525913884873553751714155904205031709262115464030534807143888700039849995711800671406127912163382933986015685240389569396807457398304726818373517814077265886528890585763062131364555734521607919713137187807658107106103376981378323172002338180348049030319707116151597120087047456178402809544871620530165936798803297978162622508652805080201485681178041361165961347313090217348403318922506112704312304156650558494700459948184483142107408891022462564899591639479697445627266468118776891821012071431633275985890307805299829762601232691986762912806985999108672619322038677748498306049452995705026758802905382276104042099895012248388280300961696806504069643072689548038490993315819434426936765296065726033493251456080278483753254193225043853870782435870985742176970373530465074443098411876172230726422871158542366506/1660347017002617714488606778538307249454081204143987146498575337165132958737138209959678784502210971205739074168354790718346566325012668504527186905879565081234897585958097173763231197610303698122881948041967623952744387482742860772966864750594290041574689109551917322621553772201862798796436236284520108428674136774547680292840751886809416972596562130605474050074593326909866195459448284422830706715433130725326630947306715927693236908996579034923899343328494620393383183133288660062302008351958547067952801708155516017825647752022977500748412927168362666820601247466619898045678730876569666167916867016800470854877468025159952443494297525060762576154719106633197342274456001815808439997093298938542364958830187119532864980955583141881565003394297337758664601174076037155519665173980601997176330267145233086875952430035258635268720607170579532136126085282961192021945410024711648428486045014668481020534887254915483146379759743582367539028246841629774846096550211590827301839779858452393725051190310768159722653619142777675349102678935518703583706107757021816885992427662659893937916261403794788543307969092291970441498332522026165657401526620077687131667565240216342076492209149314820320785474262554798281896211169067828976372906598068990091901902231877134122149795957801824298289067399088302792517748366920708634383171665576235224624533213135417206031161073482478461006626139211198630019446267887361147680197888350507317883904724462949265962076136239934476369526785"
answers[9803]="3273210180788326072665779956874407303695209430301336271835983946088930098968971592446649088493870209185601637297946849124915345946101994703881396454817629317067995483358046696632270413949397783371061147815617923539634120806955265009854911188530306501324761896490486400736980978270851173985761450205468514371802366150508965142349402634775190541932571215772934087049601511151015580218962449832712507548850463067009640345032653747650848021793733486635063672276271184797891474270402353352104959589784879937226619631450622372801836784254780047580520552480710915034334830077235403144885444713231997122651029623193719081606830187339528932202520708400927484122175520586418859966084212848690640894333604345214778602427783597772052518000554782737270290980515925878265370322995156392497748140171133095749065170222804510706059714322514254283376693941404435829879255457277967393238087816555358380272466214436430777426033426837629266695879765238233176316258666048369521314622222658677664616511902093975562338612751867496209652181946196463606422353383104107812596334613198740452548306659208445516691530536050604634513689227620071954907294939937244234855896302003859771540054305586466389588586625478638436931865675356838997676239216955771564982882752970004654747040198185582364456872545570483489206688716013710536947301312846273423736064935003964044795795191383397598165999437078614987833270147703891978941201213909953516817760145834793630851002075671081977655885477265842182227630711346029216/222848004313668719691213483700810759987656019547710139664241936991138106539616439762775058065688367531907719095700069508131964048744509937894796740738505066831483388299980505865722101281973991752051079878406943136495731052685385981167936406108052259153180314590401441086107951134763582222178806130979450523610292690241037876575460384059273115066548829780705991363970146861803501538571064868730128783979777341880332463641995130956398538891263597839918422762242505609422357073557925121726514010836900537641686656503152430580165941404831443945570282690367110620682083028935478555282639476176629026776268383873097027471871476729615653553852994769678614918913374970497476655715836999681683299634189187555967856521001252998565559074621638218445299839894876830416575083610618206257432118972401116133269463833866230950916952995411373918148349361215693246613829888213288305357239073545221307206167441574530195787313912090912979929382777969487951949322618906724196994628638037640106093305472621641881937827447206916332715678892320927194478168405438778896510491602870644308869938666086561401068093659871427096523225215861650585301063106894861911088059286678346350307257457833611661977586521721847940184057530922580500894412994920507883101337204349029037389584516440678117026220089119744731571552913544501272187787585483807869056891982387618102642674500927575453725610336484859306845245953350338792277922673893920973157108631164802759853751459885883070656697593169142468287187573383626753"
answers[9851]="18265268265520540785908331599388596086601686893936229266975936149663252475233854868431481692229058024923431589387659887867902680080183462504251855907294546963177054676412752415280943300166703724954104193781416518797928286826415325332840928794180900409037356267851073782046598387404127155718808844538373667244444328956542214789965312060454053447621394245918870851186507344747417605653775585109692134356662244321759849102328545344784420668995358085194630127711431602472710880078819974685888683913959613604493112607285755338616455009301468715782989705834386644957400388390566758392150728632659510383880551633428476788516339901525016398990001474110188269351024831528207885540825997319998166104138747638272941441134463442026326873236217051663959231958241680370115809166365792021138069848917961267525948375559165503280962387996982888846030231981715588925232378210229430804794913608024007017149934655717953874398908493758773482266278494809575155035699163146831855305715514980662636600137664244032908275561765291731574659214609335181436215261784472370334656269736145983983299651958393592650363097522621151977579876823874400736328643285043574846932479789412227618717036882664289326702680070901993665489716988043011337934536983844751946964735793973495903486597880356428661975609996251202975190898308197830114324065800497484920958433532502516025186842292166331207944400540709960586195814487225935384488510616614943463793147585780366578287019794729442704404133845784855565244538577819709563958432/1246256367846450620900980639386993831812354124550718439512384817719571366415385855683688636193531976923400898445294912450981239116673724014069196731821966341108744135306881914232828896393851522002126470051095673633568787649603366883142133786512837363700294408277038834604302565168457909291751273693855510998640993429131661506495946387601793951488114697259379016535820590484066498289646362217565672105548688032210727964777966327667928289687709964632481600353819720330163506617403066093010672828945673696843569213490397669589483104930733526222241625292450044726359124768794924595114927847314053111365079450069955139617840563110578472218053108499256124358477392389676597769300743988150510653851837661496994939303282185942527358251895780888237326532893921959098083385984467179971163421779223854669648860314987290589806372821954543027180392425525902729393830873936730656417452366836021212933409233154424397748685187651419567159639870891380259750468877027931901801811833381101396730100689385124042639143217506344002632937121036312287344746207473989440999387962515673209693892302562038440994012679964488208874332821052512662040786924668729190832388557802846413972174912561798304372318744538711603874337650904937446964586669662754528164628055399933183519045194193602651940724032900402408812654228655141151923101627926769707222397692395088211182106973251377914484189782346388440184323310828185863741946910405291084529004480172075833154172217606931875716892455759609241742431983724318960560811"
answers[9859]="97375564875859552710857257263475485245872015150154159409742126964779182762643082446089319960510145599477278715485258439846460300625068332427651118034416501442686377626793456360113581501157759087688960055802143216287038684355484042396762264359960403926704844149909425763766341457216806367693536067206432651075198691127588916420476804076478746658658023803466680068762064908145889385697518113114359392346598640883967611367897988991624393548051091664507952791730942125971565547106392956691209669780229555235202438855255794019000921171549420192999015238112509496532316750570319815809655206552721206293067341273639421021965567073260870827807392294831930629646379401978966733087112069358716912846766412303196634451076031066079956837629059177170048030972622525029555337583764651631049334455610692138052593093947511364692321205405385002454933574475419016360524655415844182022437368453118181570693029677857477506665909191395707456794665378068881663661605928129286724609088224972033263553402215822565698200814687889578567152857719719092867521377534941657609474679845672885532933976895375591415049759550178150185246432147540404349497656619717599874838652250947951177310462729418401362305425416426586758305028116511635937540760865154551702859543508608294300521944993342492698120674763653834888118184019182478585368460360033050972822901924636673968468405685748843065150057388284549646737772327988070397810180542766311272621109742146466204069612665219252089658629990789090591243706759175949093962008/6646700628514403311471896743397300436332555330937165010732719027837713954215391230313006059698837210258138125041572866405233275288926528075035715903050487152579968721636703542575087447433874784011341173605843592712366867464551290043424713528068465939734903510810873784556280347565108849556006793033896058659418631622035528034645047400542901074603278385383354754857709815915021324211447265160350251229593002838457215812149153747562284211667786478039901868553705175094205368626149685829390255087710259716499035805282120904477243226297245473185288668226400238540581998766906264507279615185674949927280423733706427411295149669923085185162949911996032663245212759411608521436270634603469390153876467527983973009617504991693479244010110831403932408175434250448523111391917158293179538249489193891571460588346598883145633988383757562811628759602804814556767097994329230167559745956458779802311515910156930121326321000807571024851412644754028052002500677482303476276329778032540782560537010053994894075430493367168014042331312193665532505313106527943685330069133416923785034092280330871685301400959810603780663108378946734197550863598233222351106072308281847541184932866996257623319033304206461887329800804826333050477795571534690816878016295466310312101574369032547477017194842135479513667489219494086143589875348942771771852787692773803792971237190674015543915678839180738347649724324416991273290383522161552450821357227584404443488918493903636670490093097384582622626303913196367789657657"
answers[9883]="3585169013008320587733453934540655406968508071250655174069676351966247095891666666457951370861261022535907218197677774600785190516321049571099175528120462561383326438548822604717683501452327840790897918994708328294710567693091965543331903184071652577937298158191755916197924842106107578819028940776121678861464188820255086932973183740793372165945865009415954786595728774195980645745127346052687115724325004061229398519190748075580806663121266449693574278118081790297078495193488775980028423355059820522243653725874632878510045835553519741832072550463732084440328479802757883822909816376632890811035439142421989449863044208586517399880008863407492496206323270830482631638372716663110223514171732041108713906301160805034170356396842166510003100992178793709386042537964760180749873610550892321524236421203672563269319950307992448377781123935998679097946148059114351779548141469866569341951745396332639499438188190815082774623731116948453999214458330207016238401484362043219831905888672028360776820475855330015482747522043053354249914178134040244185110051911485974319449824722583973072546384468573330564589187898015237282681548921919611841954714952547395797273043248344370035395990109256951956434133363924387603461250518602022484830305070939839063616330207734382452009330287037414957658873609136317223268183549799388228885998953454699718350330403675879708887230727150794790578035321318346228048669908308488864491704843219546622006983096396305733734212669415968084578686794694798437666747022528/245023971969554963674100001548598083284963319719667650955650954242209487208196182314258655384737934918956003841532542147162519460250987530958116631050053158392707966954415439393488023662202360037794081023805818201748692202213218756160808639498715928402387483022532051193882718732640172630032634418401544306420808436114717705469155027373613505214175254398771989683074614653891346095730791982871151661327716456636886803699066403750136045178921280726462942482363787574672786709034382018414642363553351014189020455925920105022649094294221657123502481465498018393560014802543232534796355734204721354119265540519353740089984397432044612265846985555821748097871523162949536534226680674022295598632502098951601181026539704013788418851188725688874564294979208208534355978351634123319770498029169643618890323128809021228280651347778838795487882593997796683820662300462952740896922474938896458632411722512025071992573497373770298260122475736212490109020184974707635349450620937391583408311636338630467775196669707487281669656501492707286190275862359046116016007668534281478411496777822117253806950844982458097770364827281492409458515035685269508751174249572502027758241365208950041026032843726267011014525776869117941572813455949054842273391192716070063345312437540015830192761870660482316791838322587429991597297164863426338597581165506413503024091687797006909010907584727558738447759437495307966298576698160963469547078512837671485404775491359263662220946791941985253800496067456070902197939863553"
answers[9901]="1838099364825091485842998238489502638352163208605775246129613825541675258139803932850287467779059152113767067936585018625986257156889365449198856216156597215096982506298229063072584364817360767528951048690402306736891920567120457283597208730483707878597646111737127186752299112571526574444743928839110241560725998077538870670908652964981237795923920133705668072188172055098674658571105524472734987571439013958589827622877679807533101349696441175687163721291688130495577638100992957486490904022264860238167307326966426591671355541318530110693823370267769955298274501764387363143753324201392980117580626370923156755357235271953717705218606174361705745607690592162188659919159503909577911890370970307505310006952184882295896878809489448279733854445719768942424699644398057419073652907805636830184662379668640311223728733045708550684860813272116912712928434533540137586553447726767616634480071951755086848411168207402152500267945846598199304627785126081886349021085239907165847936787381451395789868857912620418181602257637943517461795756567745027242135635746499764849207120672778956961370729031070654229684060835173403463861644712283677533769437289456513866506359310854677128562818680682992941321465381508384264781767572418820540784203355739555956391413113575042931607212249685559517635004403000728597654807705351363988397543735139434999563378705775136326919194852966154650224921931475263079667563204688934744417553326412692788253071675108998704506853416086820413944387432670284519676275301802962/125452273648412141401139200792882218641901219696469837289293288572011257450596445344900431556985822678505473966864661579347209963648505615850555715097627217097066479080660704969465868115047608339350569484188578919295330407533168003154334023423342555342022391307536410211267951991111768386576708822221590684887453919290735465200207374015290114669657730252171258717734202702792369201014165495230029650599790825798086043493921998720069655131607695731949026550970259238232466795025603593428296890139315719264778473434071093771596336278641488447233270510334985417502727578902135057815734135912817333309063956745909114926072011485206841480113656604580735026110219859430162705524060505099415346499841074663219804685588328455059670451808627552703776919029354602769590260916036671139722494990934857532871845441950218868879693490062765463289795888126871902116179097837031803339224307168714986819794801926156836860197630655370392709182707576940794935818334707050309298918717919944490705055557805378799500900694890233488214864128764266130529421241527831611400195926289552116946686350244924033949158832631018546058426791568124113642759698270857988480601215781121038212219578986982421005328815987848709639437197756988386085280489445916079243976290670627872432799968020488105058694077778166946197421221164764155697816148410074285361961556739283713548334944152067537413584683380510074085252831997597678744871269458413296408104198572887800527245051575942995057124757474296449945853986537508301925345210138625"
answers[9907]="1633354865670482634892575473274177878269848372373759525765811325846788929611278968402771127221384695483915945198456285004476851582174958025985911899376499975337986661537173411180428490907671107896805361798951388912266345066652077866312114944286489782939084013056891167373799137114155515056010000276816186615290579398553645497061457349859874907706345855178899820840538136570385474191197090563464515778875629438803382571649568677198891486414476226932831825123576296441036124053704853315256369077062694497961110434982641810098930629547385147383059244374581651992456044925971934850442830194217280514603049112460209344747724704460094333483276967669942685299188841394315505390707812866736298697875298127848875145262191230878125445765654264972808396487147578694015479599857513297812474321479310509988898419240869755115441035610394724742733426143862263998840699035405386591348806972955989366740531496447026566482443584635769122576528379572484919769116532440328453434380368399747202088827429149682924434791294983263532249848151359239961565497074342143353501637548919300241662090353407294661111131471268545214977964856225076533564370498248492613685805161596682009974473114515513380958196492686912557866989055667956732635644620975053458902179010611928423215547799006570194462929051080094813251146339256886128285111370771379853589184069970476491616306084104329480579259858092153081246668787325700941970854871522339560167691629337094064099025875633061751502297990098463516213737680898668940099498771690328/111513132131921903467679289593673083237245528619084299812705145397343339956085729195467050272876286825338199081657476959419742189909782769644938413420113081864059092516142848861747438324486762968311617319278736817151404806696149336137185798598526715859575458940032364632238179547654905232512630064196969497677736817147320413511295443569146768595251315779707785526874846846926550400901480440204470800533147400709409816439041776640061915672540173983954690267529119322873303817800536527491819457901613972679803087496952083352530076692125767508651796009186653704446868959024120051391763676366948740719167961551919213267619565764628303537878805870738431134320195430604589071576942671199480307999858733033973159720522958626719707068274335602403357261359426313572969120814254818790864439991942095584774973726177972327893060880055791522924263011668330579658825864744028269634866050816635543839817601712139410542397893915884793519273517846169595498505186406266941599038860373283991737827162493670044000800617680207545079879225568236560470596659135850321244618601146268548397054533551043585732585629005349818718601592504999212126897509574095989760534414027663145077528514655095485338070058655865519679499731339545232075804879507480959327978925040558108829155527129322760052172513580592841064374418813123693953614354142288253655076939323807745376297728135171144367630829671564510298002517331197936662107795074145152362759287620344711579773379178615995606333117754930177729647988033340712822529075678777"
answers[9923]="3760299283807515305392881669921685380087483626665387583310740045779392618490518061864438198412014466565364228291295311284131760183238506480296641521584717001189575119537493891940454933801650905249443352246953782854872613017048635048443614120554701976326536039594670095564945312513189153593550500517921950184962024588992584781239533465993501341159180970745350678104403343694528834994019085606787711931776684194936418570665879951352474415566347678136222813280481902061127946763865268688300550988394467968839427831476642475507856733491281467764508214106535203243052393575405292358868470813735971960765501058279775977978030573791129899519277415389902700239026919247980979527451730380980472670105835211342752731828345683483462323646594371504049372940439266303990974893632430579149551268895678583153720791224950534759778851291627442428157985110658561730352163307118794880980048721449385869746576091026550888327297357898605889141961077282796364157526745937107341872728740687038596285146407533730421930092930276945716734155916067113852174300467379937502904191927521252135950255956504149093128341783431227487283983351865444285852258683713459524558123548207017084006902917163157763229391288430112144112020068120688896821326535586200626213610044286687320856131797617153232959645033928534332319957752653966344480388578387275931286170730869888406739091108118872791757012867192042173164675250862437159941800851341264571676718462429556497603409387737027536322645374934694161916847152916606849505635131538412760/256926256431948065589533083223822783778613697938370226768472654995479055258821520066356083828706964845579210684138826914503086005552139501261938104519940540614792149157193123777466097899617501878989966303618209626716836674627928070460076079971005553340461857397834568112676765677796901655709099667909817722649505626707426232730024701983314154843459031556446737853919647135318772123677010934231100724428371611234480217075552253378702653709532560859031606376387090919900091996212436159341152031005318593054266313592977600044229296698657768339933738005166050135045586081591572598406623510349449898616962983415621867368595479521703611351272768726181345333473730272112973220913275914443602629631674520910274159996084896675962205085304069227937335130172118226472120854356043102494151669741434588227321539465114048243465612267648543668817501978883833655533934792370241133238731381081528293006939754344769201889684747582198564268406185117574748028555949480039033444185534300046316963953782385415781377844623135198183864041735709217035324254702648999140147601257041002735506813645301604421527877289228325982327658069131518184740371862058717160408271289919735886258625697765339998218913415143114157341567381006312214702654442385236130291663443293445882742374334505959639160205471289685905812318660945436990869127471943832136421297268202053045346989965623434316623021431563284631726597799931080046069496359850830431043797398677274215479797865627531253876991503307359129489108964428817002343106990363901953"
answers[9941]="384853449965821966704972732966307410533754716673001872990644754365090705756559909905259906468556983837473030874407257670396254711587695297525843978796573345385105966534793732922560851527360127165003492854386049121963401202049881466162284067795049692605928261294087682986229601310196379407661027753366447296128334837195369728352306910364221724819790584234590499773230968446949449654139435810921574651200199324826389837566367344938559238192678820640156721227690796296835372410818703029961426437218612250923546166131087887183128506236914642265107372940583059256790238734873109550064606852454241895688532803264957771260552966093023026993324786723535480684212796655647178515320455962010444291723038825881485337386595953872058254168410359964399040839348392327614519090725780698083807461735099275217105332435035753958774263103832408352846440135520908788781982960980537030064122924914534253061986335785798932585118262145429733521285255543276010510520192751011713072317121752117777795703412147228931454823584324564303527450001365318783625485155921101661017155244139248803962776096262658198375191699627453070323264267286645773802280627801063123133551237542788441825414046345338526837487440831294887788650949450267282162592637307013507372887692529215654653541232042283216484427420262459679041403322783917834492903078518685769388196722883649292951071615901964473014159404289255892968209177423128684793429579384323572081016759500108948811565542981757113040677115518347788350471128322691656105081491299216747682/26309248658631481916368187722119453058930042668889111221091599871537055258503323654794862984059593200187311174055815876045116006968539084929222461902841911358954716073696575874812528424920832192408572549490504665775804075481899834415111790589030968662063294197538259774738100805406402729544611805993965334799309376174840446231554529483091369455970204831380145956241371866656642265464525919665264714181465252990410774228536550745979151739856134231964836492942038110197769420412153462716533967974944623928756870511920906244529079981942555478009214771729003533828668014754977034076838247459783669618377009501759679218544177103022449802370331517560969762147709979864368457821519453639024909274283470941212073983599093419618529800735136688940783117329624906390745175486058813695401130981522901834477725641227678540130878696207210871686912202637704566326674922738712692043646093422748497203910630844904366273503718152417132981084793356039654198124129226755997024684598712324742857108867316266576013091289409044294027677873736623824417203681551257511951114368720998680115897717278884292764454634416980580590352186279067462117414078674812637225806980087780954752883271451170815817616733710654889711776499815046370785551814900248179741866336593248858392819131853410267050005040260063836755181430880812747864998653127048410769540840263890231843531772479839674022197394592080346288803614712942596717516427248725036138884853624552879665131301440259200397003929938673574859684757957510861039934155813263559885"
answers[9949]="30771945768974312829849286149718032439798677467423493967010334358283994984326026515504661301394540080141512302827338824723593201789481739830982595229625700583149586238955431037653778739309457671890129351276923685142834902559798917246250215421521527437619966091451717584873598334082323796428001565577186322128798336073715122147162994673808366455578086459869026609967714613554824824759058832899783907543792360923791441961728662642435663487942507846671256543700983615951249949208724967800869867388054589031851260302269048408876735464545077279886559546809841455560742265338090287549868226487740814828047510635070623293003545071913364258117360949495583083158833097103857044434279928869747427297854639426953768566959891591203122862851297692620246536344125431027520526966343015915582695313902053937737605214941486508347934274945698807835217128834978971566963674180680330348426256461353890985386815655822602029905923905821026891198095526938372914444181903627938094196468774691975665647890550812376683751593905633065125611092957601442907415902644417519682901059153440286626841355350656757754077474910571424577932014202554351172899008539276212598195566148386803283414780184354222934329064815395828988176783012436153939221036332468551883344836802385258800011254159939415282139047054155201180580357201111671543663885937215403704436525087738509669353351862725364108467051762959998292239822805587007389198288780422715556343928293849868727893528481926649471509098886000214133618311655761794432794819144278520611314/2104739892690518553309455017769556244714403413511128897687327989722964420680265892383589038724767456014984893924465270083609280557483126794337796952227352908716377285895726069985002273993666575392685803959240373262064326038551986753208943247122477492965063535803060781979048064432512218363568944479517226783944750093987235698524362358647309556477616386510411676499309749332531381237162073573221177134517220239232861938282924059678332139188490738557186919435363048815821553632972277017322717437995569914300549640953672499562326398555404438240737181738320282706293441180398162726147059796782693569470160760140774337483534168241795984189626521404877580971816798389149476625721556291121992741942677675296965918687927473569482384058810935115262649386369992511259614038884705095632090478521832146758218051298214283210470295696576869734952976211016365306133993819097015363491687473819879776312850467592349301880297452193370638486783468483172335849930338140479761974767896985979428568709385301326081047303152723543522214229898929905953376294524100600956089149497679894409271817382310743421156370753358446447228174902325396969393126293985010978064558407022476380230661716093665265409338696852391176942119985203709662844145192019854379349306927459908671425530548272821364000403220805106940414514470465019829199892250163872861563267221111218547482541798387173921775791567366427703104289177035407737401314179898002891110788289964230373210504115220736031760314395093885988774780636600868883194732465061084790785"
"""for i in range(1, MAXN + 1):
start_time = time.time()
ans = solve(1, i)
elapsed_time = time.time() - start_time
if elapsed_time > 0.8:
print("answers[{}]={}/{}".format(i, ans.numerator, ans.denominator))
"""
n = int(input())
if answers.get(n) != None:
print(answers[n])
else:
ans = solve(1, n)
print("{}/{}".format(ans.numerator, ans.denominator))
if __name__ == '__main__':
main();
|
vfc_84481
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2",
"output": "1/1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3",
"output": "8/3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4",
"output": "2/1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8",
"output": "3/1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7",
"output": "24/7",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6",
"output": "11/3",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
846
|
Solve the following coding problem using the programming language python:
Hideo Kojima has just quit his job at Konami. Now he is going to find a new place to work. Despite being such a well-known person, he still needs a CV to apply for a job.
During all his career Hideo has produced *n* games. Some of them were successful, some were not. Hideo wants to remove several of them (possibly zero) from his CV to make a better impression on employers. As a result there should be no unsuccessful game which comes right after successful one in his CV.
More formally, you are given an array *s*1,<=*s*2,<=...,<=*s**n* of zeros and ones. Zero corresponds to an unsuccessful game, one — to a successful one. Games are given in order they were produced, and Hideo can't swap these values. He should remove some elements from this array in such a way that no zero comes right after one.
Besides that, Hideo still wants to mention as much games in his CV as possible. Help this genius of a man determine the maximum number of games he can leave in his CV.
The input will be provided via standard input and looks as follows:
The first line contains one integer number *n* (1<=≤<=*n*<=≤<=100).
The second line contains *n* space-separated integer numbers *s*1,<=*s*2,<=...,<=*s**n* (0<=≤<=*s**i*<=≤<=1). 0 corresponds to an unsuccessful game, 1 — to a successful one.
You should write the solution as standard output as follows:
Print one integer — the maximum number of games Hideo can leave in his CV so that no unsuccessful game comes after a successful one.
Here are example inputs and outputs for the problem:
Example Input 1: 4
1 1 0 1
Example Output 1: 3
Example Input 2: 6
0 1 0 0 1 0
Example Output 2: 4
Example Input 3: 1
0
Example Output 3: 1
Now solve the problem by providing the code.
|
n, a = int(input()), [int(x) for x in input().split()]
print(n - min([a[:i].count(1) + a[i + 1:].count(0) for i in range(n)]))
|
vfc_84485
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 1 0 1",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n0 1 0 0 1 0",
"output": "4",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
319
|
Solve the following coding problem using the programming language python:
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money.
The manager of logging factory wants them to go to the jungle and cut *n* trees with heights *a*1,<=*a*2,<=...,<=*a**n*. They bought a chain saw from a shop. Each time they use the chain saw on the tree number *i*, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is *i* (the tree that have height *a**i* in the beginning), then the cost of charging the chain saw would be *b**i*. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each *i* < *j*, *a**i*<=<<=*a**j* and *b**i*<=><=*b**j* and also *b**n*<==<=0 and *a*1<==<=1. Kalila and Dimna want to cut all the trees completely, with minimum cost.
They want you to help them! Will you?
The input will be provided via standard input and looks as follows:
The first line of input contains an integer *n* (1<=≤<=*n*<=≤<=105). The second line of input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109). The third line of input contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (0<=≤<=*b**i*<=≤<=109).
It's guaranteed that *a*1<==<=1, *b**n*<==<=0, *a*1<=<<=*a*2<=<<=...<=<<=*a**n* and *b*1<=><=*b*2<=><=...<=><=*b**n*.
You should write the solution as standard output as follows:
The only line of output must contain the minimum cost of cutting all the trees completely.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Here are example inputs and outputs for the problem:
Example Input 1: 5
1 2 3 4 5
5 4 3 2 0
Example Output 1: 25
Example Input 2: 6
1 2 3 10 20 30
6 5 4 3 2 0
Example Output 2: 138
Now solve the problem by providing the code.
|
f = lambda: list(map(int, input().split()))
g = lambda j: a[i] * b[j] + t[j]
h = lambda j, k: (t[i] - t[j]) * (b[j] - b[k]) < (t[j] - t[k]) * (b[i] - b[j])
n = int(input())
a, b = f(), f()
t = [0] * n
p = [0]
for i in range(1, n):
while len(p) > 1 and g(p[1]) < g(p[0]): p.pop(0)
t[i] = g(p[0])
while len(p) > 1 and h(p[-2], p[-1]): p.pop()
p.append(i)
print(t[-1])
|
vfc_84489
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n1 2 3 4 5\n5 4 3 2 0",
"output": "25",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n1 2 3 10 20 30\n6 5 4 3 2 0",
"output": "138",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
484
|
Solve the following coding problem using the programming language python:
Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negative integer *x*.
You are given multiple queries consisting of pairs of integers *l* and *r*. For each query, find the *x*, such that *l*<=≤<=*x*<=≤<=*r*, and is maximum possible. If there are multiple such numbers find the smallest of them.
The input will be provided via standard input and looks as follows:
The first line contains integer *n* — the number of queries (1<=≤<=*n*<=≤<=10000).
Each of the following *n* lines contain two integers *l**i*,<=*r**i* — the arguments for the corresponding query (0<=≤<=*l**i*<=≤<=*r**i*<=≤<=1018).
You should write the solution as standard output as follows:
For each query print the answer in a separate line.
Here are example inputs and outputs for the problem:
Example Input 1: 3
1 2
2 4
1 10
Example Output 1: 1
3
7
Now solve the problem by providing the code.
|
def main():
cases = int(input())
for i in range(cases):
left, right = list(map(int, str(input()).split(" ")))
res = left
for j in range(65):
if ((1 << j) & left) == 0:
if (res | (1 << j)) > right:
break
res |= (1 << j)
print(res)
if __name__ == "__main__":
main()
|
vfc_84493
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n1 2\n2 4\n1 10",
"output": "1\n3\n7",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "55\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n2 2\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n3 3\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n4 4\n4 5\n4 6\n4 7\n4 8\n4 9\n4 10\n5 5\n5 6\n5 7\n5 8\n5 9\n5 10\n6 6\n6 7\n6 8\n6 9\n6 10\n7 7\n7 8\n7 9\n7 10\n8 8\n8 9\n8 10\n9 9\n9 10\n10 10",
"output": "1\n1\n3\n3\n3\n3\n7\n7\n7\n7\n2\n3\n3\n3\n3\n7\n7\n7\n7\n3\n3\n3\n3\n7\n7\n7\n7\n4\n5\n5\n7\n7\n7\n7\n5\n5\n7\n7\n7\n7\n6\n7\n7\n7\n7\n7\n7\n7\n7\n8\n9\n9\n9\n9\n10",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "18\n1 10\n1 100\n1 1000\n1 10000\n1 100000\n1 1000000\n1 10000000\n1 100000000\n1 1000000000\n1 10000000000\n1 100000000000\n1 1000000000000\n1 10000000000000\n1 100000000000000\n1 1000000000000000\n1 10000000000000000\n1 100000000000000000\n1 1000000000000000000",
"output": "7\n63\n511\n8191\n65535\n524287\n8388607\n67108863\n536870911\n8589934591\n68719476735\n549755813887\n8796093022207\n70368744177663\n562949953421311\n9007199254740991\n72057594037927935\n576460752303423487",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
430
|
Solve the following coding problem using the programming language python:
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.
Iahub wants to draw *n* distinct points and *m* segments on the *OX* axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [*l**i*,<=*r**i*] consider all the red points belong to it (*r**i* points), and all the blue points belong to it (*b**i* points); each segment *i* should satisfy the inequality |*r**i*<=-<=*b**i*|<=≤<=1.
Iahub thinks that point *x* belongs to segment [*l*,<=*r*], if inequality *l*<=≤<=*x*<=≤<=*r* holds.
Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
The input will be provided via standard input and looks as follows:
The first line of input contains two integers: *n* (1<=≤<=*n*<=≤<=100) and *m* (1<=≤<=*m*<=≤<=100). The next line contains *n* space-separated integers *x*1,<=*x*2,<=...,<=*x**n* (0<=≤<=*x**i*<=≤<=100) — the coordinates of the points. The following *m* lines contain the descriptions of the *m* segments. Each line contains two integers *l**i* and *r**i* (0<=≤<=*l**i*<=≤<=*r**i*<=≤<=100) — the borders of the *i*-th segment.
It's guaranteed that all the points are distinct.
You should write the solution as standard output as follows:
If there is no good drawing for a given test, output a single integer -1. Otherwise output *n* integers, each integer must be 0 or 1. The *i*-th number denotes the color of the *i*-th point (0 is red, and 1 is blue).
If there are multiple good drawings you can output any of them.
Here are example inputs and outputs for the problem:
Example Input 1: 3 3
3 7 14
1 5
6 10
11 15
Example Output 1: 0 0 0
Example Input 2: 3 4
1 2 3
1 2
2 3
5 6
2 2
Example Output 2: 1 0 1
Now solve the problem by providing the code.
|
n, m = map(int, input().split())
t = list(enumerate(list(map(int, input().split()))))
t.sort(key=lambda x: x[1])
p = ['0' for i in range(n)]
for i, x in t[::2]:
p[i] = '1'
print(' '.join(p))
|
vfc_84497
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 3\n3 7 14\n1 5\n6 10\n11 15",
"output": "0 0 0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 4\n1 2 3\n1 2\n2 3\n5 6\n2 2",
"output": "1 0 1 ",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
83
|
Solve the following coding problem using the programming language python:
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
The input will be provided via standard input and looks as follows:
The first line of the input data contains an integer *n* (1<=≤<=*n*<=≤<=105). The second line contains an array of original integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≤<=*a**i*<=≤<=109).
You should write the solution as standard output as follows:
Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Here are example inputs and outputs for the problem:
Example Input 1: 4
2 1 1 4
Example Output 1: 5
Example Input 2: 5
-2 -2 -2 0 1
Example Output 2: 8
Now solve the problem by providing the code.
|
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
c, x = 0, a[0]
ans = 0
for i in a:
if not i ^ x:
c += 1
else:
ans += c * (c + 1) // 2
c, x = 1, i
ans += c * (c + 1) // 2
print(ans)
|
vfc_84501
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "3000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n2 1 1 4",
"output": "5",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n-2 -2 -2 0 1",
"output": "8",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n10",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n5 6",
"output": "2",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
156
|
Solve the following coding problem using the programming language python:
As Sherlock Holmes was investigating a crime, he identified *n* suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to *n*. After that, he asked each one: "Which one committed the crime?". Suspect number *i* answered either "The crime was committed by suspect number *a**i*", or "Suspect number *a**i* didn't commit the crime". Also, the suspect could say so about himself (*a**i*<==<=*i*).
Sherlock Holmes understood for sure that exactly *m* answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth?
The input will be provided via standard input and looks as follows:
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=105,<=0<=≤<=*m*<=≤<=*n*) — the total number of suspects and the number of suspects who told the truth. Next *n* lines contain the suspects' answers. The *i*-th line contains either "+*a**i*" (without the quotes), if the suspect number *i* says that the crime was committed by suspect number *a**i*, or "-*a**i*" (without the quotes), if the suspect number *i* says that the suspect number *a**i* didn't commit the crime (*a**i* is an integer, 1<=≤<=*a**i*<=≤<=*n*).
It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly *m* people told the truth.
You should write the solution as standard output as follows:
Print *n* lines. Line number *i* should contain "Truth" if suspect number *i* has told the truth for sure. Print "Lie" if the suspect number *i* lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime.
Here are example inputs and outputs for the problem:
Example Input 1: 1 1
+1
Example Output 1: Truth
Example Input 2: 3 2
-1
-2
-3
Example Output 2: Not defined
Not defined
Not defined
Example Input 3: 4 1
+2
-3
+4
-1
Example Output 3: Lie
Not defined
Lie
Not defined
Now solve the problem by providing the code.
|
[n, m] = input().split()
n = int(n)
m = int(m)
arr = []
ps = []
ns = []
suspects = []
for i in range(0, n):
a = input()
a = int(a)
arr.append(a)
ps.append(0)
ns.append(0)
suspects.append(0)
sum_ps = 0
sum_ns = 0
for i in arr:
if i > 0:
ps[i - 1] += 1
sum_ps += 1
else:
ns[-i - 1] += 1
sum_ns += 1
num_suspects = 0
for i in range(0, n):
if ps[i] + sum_ns - ns[i] == m:
suspects[i] = 1
num_suspects += 1
for i in arr:
if i > 0:
if suspects[i - 1]:
if num_suspects == 1:
print("Truth")
else:
print("Not defined")
else:
print("Lie")
else:
if suspects[-i - 1]:
if num_suspects == 1:
print("Lie")
else:
print("Not defined")
else:
print("Truth")
|
vfc_84505
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1 1\n+1",
"output": "Truth",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 2\n-1\n-2\n-3",
"output": "Not defined\nNot defined\nNot defined",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 1\n+2\n-3\n+4\n-1",
"output": "Lie\nNot defined\nLie\nNot defined",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 0\n-1",
"output": "Lie",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
383
|
Solve the following coding problem using the programming language python:
Iahub helps his grandfather at the farm. Today he must milk the cows. There are *n* cows sitting in a row, numbered from 1 to *n* from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk).
Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost.
The input will be provided via standard input and looks as follows:
The first line contains an integer *n* (1<=≤<=*n*<=≤<=200000). The second line contains *n* integers *a*1, *a*2, ..., *a**n*, where *a**i* is 0 if the cow number *i* is facing left, and 1 if it is facing right.
You should write the solution as standard output as follows:
Print a single integer, the minimum amount of lost milk.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Here are example inputs and outputs for the problem:
Example Input 1: 4
0 0 1 0
Example Output 1: 1
Example Input 2: 5
1 0 1 0 1
Example Output 2: 3
Now solve the problem by providing the code.
|
from math import inf
from sys import stdin
input = stdin.readline
n = int(input())
a = [int(x) for x in input().split()]
sm = [0]
for x in a: sm.append(sm[-1]+x)
ans1 = ans2 = 0
for i in range(1, n+1):
if a[i-1] == 0:
ans1 += sm[i-1]
for i in range(1, n+1):
if a[i-1] == 1:
ans2 += n-i-(sm[n]-sm[i])
print(min(ans1, ans2))
|
vfc_84513
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n0 0 1 0",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n1 0 1 0 1",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "50\n1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 0 0 1 0 1 1 0 1 1 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0",
"output": "416",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "100\n1 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1 0 0 1 0 0 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 0 1 1 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 0 0 1 0 0",
"output": "1446",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
605
|
Solve the following coding problem using the programming language python:
An infinitely long railway has a train consisting of *n* cars, numbered from 1 to *n* (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
The input will be provided via standard input and looks as follows:
The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of cars in the train.
The second line contains *n* integers *p**i* (1<=≤<=*p**i*<=≤<=*n*, *p**i*<=≠<=*p**j* if *i*<=≠<=*j*) — the sequence of the numbers of the cars in the train.
You should write the solution as standard output as follows:
Print a single integer — the minimum number of actions needed to sort the railway cars.
Here are example inputs and outputs for the problem:
Example Input 1: 5
4 1 2 5 3
Example Output 1: 2
Example Input 2: 4
4 1 3 2
Example Output 2: 2
Now solve the problem by providing the code.
|
n = int(input())
nums = list(map(int, input().split()))
note_nums = sorted([(val, idx) for idx, val in enumerate(nums)])
tmp = [0] * n
for i in range(n):
tmp[note_nums[i][1]] = i
ans = [0] * (n + 1)
for val in tmp:
ans[val + 1] = ans[val] + 1
print(n - max(ans))
|
vfc_84517
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n4 1 2 5 3",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n4 1 3 2",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1",
"output": "0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1 2",
"output": "0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
830
|
Solve the following coding problem using the programming language python:
There are *n* people and *k* keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all *n* people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
The input will be provided via standard input and looks as follows:
The first line contains three integers *n*, *k* and *p* (1<=≤<=*n*<=≤<=1<=000, *n*<=≤<=*k*<=≤<=2<=000, 1<=≤<=*p*<=≤<=109) — the number of people, the number of keys and the office location.
The second line contains *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains *k* distinct integers *b*1,<=*b*2,<=...,<=*b**k* (1<=≤<=*b**j*<=≤<=109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
You should write the solution as standard output as follows:
Print the minimum time (in seconds) needed for all *n* to reach the office with keys.
Here are example inputs and outputs for the problem:
Example Input 1: 2 4 50
20 100
60 10 40 80
Example Output 1: 50
Example Input 2: 1 2 10
11
15 7
Example Output 2: 7
Now solve the problem by providing the code.
|
import copy
import random
import heapq
import math
import sys
import bisect
import datetime
from functools import lru_cache
from collections import deque
from collections import Counter
from collections import defaultdict
from itertools import combinations
from itertools import permutations
from types import GeneratorType
from functools import cmp_to_key
from decimal import Decimal
from heapq import nlargest
inf = float("inf")
sys.setrecursionlimit(10000000)
class FastIO:
def __init__(self):
return
@staticmethod
def _read():
return sys.stdin.readline().strip()
def read_int(self):
return int(self._read())
def read_float(self):
return float(self._read())
def read_ints(self):
return map(int, self._read().split())
def read_floats(self):
return map(float, self._read().split())
def read_ints_minus_one(self):
return map(lambda x: int(x) - 1, self._read().split())
def read_list_ints(self):
return list(map(int, self._read().split()))
def read_list_floats(self):
return list(map(float, self._read().split()))
def read_list_ints_minus_one(self):
return list(map(lambda x: int(x) - 1, self._read().split()))
def read_str(self):
return self._read()
def read_list_strs(self):
return self._read().split()
def read_list_str(self):
return list(self._read())
@staticmethod
def st(x):
return sys.stdout.write(str(x) + '\n')
@staticmethod
def lst(x):
return sys.stdout.write(" ".join(str(w) for w in x) + '\n')
@staticmethod
def round_5(f):
res = int(f)
if f - res >= 0.5:
res += 1
return res
@staticmethod
def max(a, b):
return a if a > b else b
@staticmethod
def min(a, b):
return a if a < b else b
@staticmethod
def bootstrap(f, queue=[]):
def wrappedfunc(*args, **kwargs):
if queue:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if isinstance(to, GeneratorType):
queue.append(to)
to = next(to)
else:
queue.pop()
if not queue:
break
to = queue[-1].send(to)
return to
return wrappedfunc
def main(ac=FastIO()):
n, k, p = ac.read_ints()
person = sorted(ac.read_list_ints())
keys = sorted(ac.read_list_ints())
def check(x):
lst = []
for y in keys:
if abs(y - p) <= x:
z = x - abs(y - p)
lst.append([y - z, y + z])
lst.sort(key=lambda it: [it[1], it[0]])
m = len(lst)
i = 0
for j in range(n):
y = person[j]
while i < m and not (lst[i][0] <= y <= lst[i][1]):
i += 1
if i == m:
return False
i += 1
return True
low = 0
high = 10**15
while low < high - 1:
mid = low + (high - low) // 2
if check(mid):
high = mid
else:
low = mid
ans = low if check(low) else high
ac.st(ans)
return
main()
|
vfc_84521
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 4 50\n20 100\n60 10 40 80",
"output": "50",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 2 10\n11\n15 7",
"output": "7",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
57
|
Solve the following coding problem using the programming language python:
Meg the Rabbit decided to do something nice, specifically — to determine the shortest distance between two points on the surface of our planet. But Meg... what can you say, she wants everything simple. So, she already regards our planet as a two-dimensional circle. No, wait, it's even worse — as a square of side *n*. Thus, the task has been reduced to finding the shortest path between two dots on a square (the path should go through the square sides). To simplify the task let us consider the vertices of the square to lie at points whose coordinates are: (0,<=0), (*n*,<=0), (0,<=*n*) and (*n*,<=*n*).
The input will be provided via standard input and looks as follows:
The single line contains 5 space-separated integers: *n*,<=*x*1,<=*y*1,<=*x*2,<=*y*2 (1<=≤<=*n*<=≤<=1000,<=0<=≤<=*x*1,<=*y*1,<=*x*2,<=*y*2<=≤<=*n*) which correspondingly represent a side of the square, the coordinates of the first point and the coordinates of the second point. It is guaranteed that the points lie on the sides of the square.
You should write the solution as standard output as follows:
You must print on a single line the shortest distance between the points.
Here are example inputs and outputs for the problem:
Example Input 1: 2 0 0 1 0
Example Output 1: 1
Example Input 2: 2 0 1 2 1
Example Output 2: 4
Example Input 3: 100 0 0 100 100
Example Output 3: 200
Now solve the problem by providing the code.
|
n,x1,y1,x2,y2 =map(int, input().split())
def f(x,y,n):
if y==0 or x==n:
return x+y
else:
return 4*n - x - y
a = abs(f(x1,y1,n) - f(x2,y2,n))
print(min(a, 4*n-a))
|
vfc_84525
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 0 0 1 0",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 0 1 2 1",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "100 0 0 100 100",
"output": "200",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 0 3 1 4",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 8 10 10 0",
"output": "12",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
356
|
Solve the following coding problem using the programming language python:
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event.
As for you, you're just a simple peasant. There's no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows:
- There are *n* knights participating in the tournament. Each knight was assigned his unique number — an integer from 1 to *n*. - The tournament consisted of *m* fights, in the *i*-th fight the knights that were still in the game with numbers at least *l**i* and at most *r**i* have fought for the right to continue taking part in the tournament. - After the *i*-th fight among all participants of the fight only one knight won — the knight number *x**i*, he continued participating in the tournament. Other knights left the tournament. - The winner of the last (the *m*-th) fight (the knight number *x**m*) became the winner of the tournament.
You fished out all the information about the fights from your friends. Now for each knight you want to know the name of the knight he was conquered by. We think that the knight number *b* was conquered by the knight number *a*, if there was a fight with both of these knights present and the winner was the knight number *a*.
Write the code that calculates for each knight, the name of the knight that beat him.
The input will be provided via standard input and looks as follows:
The first line contains two integers *n*, *m* (2<=≤<=*n*<=≤<=3·105; 1<=≤<=*m*<=≤<=3·105) — the number of knights and the number of fights. Each of the following *m* lines contains three integers *l**i*,<=*r**i*,<=*x**i* (1<=≤<=*l**i*<=<<=*r**i*<=≤<=*n*; *l**i*<=≤<=*x**i*<=≤<=*r**i*) — the description of the *i*-th fight.
It is guaranteed that the input is correct and matches the problem statement. It is guaranteed that at least two knights took part in each battle.
You should write the solution as standard output as follows:
Print *n* integers. If the *i*-th knight lost, then the *i*-th number should equal the number of the knight that beat the knight number *i*. If the *i*-th knight is the winner, then the *i*-th number must equal 0.
Here are example inputs and outputs for the problem:
Example Input 1: 4 3
1 2 1
1 3 3
1 4 4
Example Output 1: 3 1 4 0
Example Input 2: 8 4
3 5 4
3 7 6
2 8 8
1 8 1
Example Output 2: 0 8 4 6 4 8 6 1
Now solve the problem by providing the code.
|
import sys
input = sys.stdin.readline
def inint():
return(int(input()))
def inlst():
return(list(map(int,input().split())))
# returns a List of Characters, which is easier to use in Python as Strings are Immutable
def instr():
s = input()
return(list(s[:len(s) - 1]))
def invar():
return(map(int,input().split()))
############ ---- Input function template ---- ############
def isOdd(num):
return (num & 1) == 1
n, m = invar()
# build function
tree = [{'round_num':m, 'winner':-1} for _ in range(2*n+1, 0, -1)]
def modify(left, right, pair):
left += n
right += n
while left <= right:
if isOdd(left):
tree[left] = pair
if not isOdd(right):
tree[right] = pair
left = int((left+1)/2)
right = int((right-1)/2)
def query(pos):
pos += n
min = 1000000
winner = -1
while pos > 0:
if tree[pos]['round_num'] < min:
winner = tree[pos]['winner']
min = tree[pos]['round_num']
pos >>= 1
return winner + 1 if winner > -1 else 0
rounds = []
for _ in range(m):
li, ri, xi = invar()
rounds.append({'left':li-1, 'right':ri-1, 'winner':xi-1})
for idx in range(m-1, -1, -1):
round = rounds[idx]
if round['winner'] > 0: modify(round['left'], round['winner']-1, {'round_num': idx, 'winner':round['winner']})
if round['winner'] < n: modify(round['winner']+1, round['right'], {'round_num': idx, 'winner':round['winner']})
print(*[query(i) for i in range(n)])
|
vfc_84529
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "3000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4 3\n1 2 1\n1 3 3\n1 4 4",
"output": "3 1 4 0 ",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
86
|
Solve the following coding problem using the programming language python:
For each positive integer *n* consider the integer ψ(*n*) which is obtained from *n* by replacing every digit *a* in the decimal notation of *n* with the digit (9<=<=-<=<=*a*). We say that ψ(*n*) is the reflection of *n*. For example, reflection of 192 equals 807. Note that leading zeros (if any) should be omitted. So reflection of 9 equals 0, reflection of 91 equals 8.
Let us call the weight of the number the product of the number and its reflection. Thus, the weight of the number 10 is equal to 10·89<==<=890.
Your task is to find the maximum weight of the numbers in the given range [*l*,<=*r*] (boundaries are included).
The input will be provided via standard input and looks as follows:
Input contains two space-separated integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=109) — bounds of the range.
You should write the solution as standard output as follows:
Output should contain single integer number: maximum value of the product *n*·ψ(*n*), where *l*<=≤<=*n*<=≤<=*r*.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).
Here are example inputs and outputs for the problem:
Example Input 1: 3 7
Example Output 1: 20
Example Input 2: 1 1
Example Output 2: 8
Example Input 3: 8 10
Example Output 3: 890
Now solve the problem by providing the code.
|
from sys import stdin
inp = stdin.readline
a, b = inp().split()
mid = "5" + "".join(["0" for _ in range(len(b)-1)])
if int(a) > int(mid):
ans = a
elif int(b) < int(mid):
ans = b
else:
ans = mid
ref = ""
for c in ans:
ref += str(9 - int(c))
print(int(ref)*int(ans))
|
vfc_84533
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 7",
"output": "20",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1",
"output": "8",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8 10",
"output": "890",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 6",
"output": "20",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
142
|
Solve the following coding problem using the programming language python:
Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.
He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space):
Simon faced a quite natural challenge: placing in the given *n*<=×<=*m* cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.
Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him?
The input will be provided via standard input and looks as follows:
The only line contains two space-separated integers *n* and *m* — the sizes of the warehouse (1<=≤<=*n*,<=*m*<=≤<=9).
You should write the solution as standard output as follows:
In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next *n* lines print *m* characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them.
Here are example inputs and outputs for the problem:
Example Input 1: 3 3
Example Output 1: 1
AAA
.A.
.A.
Example Input 2: 5 6
Example Output 2: 4
A..C..
AAAC..
ABCCCD
.B.DDD
BBB..D
Example Input 3: 2 2
Example Output 3: 0
..
..
Now solve the problem by providing the code.
|
n, m = map(int, input().split())
swapped = False
if n < m:
n, m = m, n
swapped = True
ans = ''
if n == 1 and m == 1:
ans = '''0
.'''
if n == 2 and m == 1:
ans = '''0
.
.'''
if n == 2 and m == 2:
ans = '''0
..
..'''
if n == 3 and m == 1:
ans = '''0
.
.
.'''
if n == 3 and m == 2:
ans = '''0
..
..
..'''
if n == 3 and m == 3:
ans = '''1
.A.
.A.
AAA'''
if n == 4 and m == 1:
ans = '''0
.
.
.
.'''
if n == 4 and m == 2:
ans = '''0
..
..
..
..'''
if n == 4 and m == 3:
ans = '''1
.A.
.A.
AAA
...'''
if n == 4 and m == 4:
ans = '''2
...A
BAAA
BBBA
B...'''
if n == 5 and m == 1:
ans = '''0
.
.
.
.
.'''
if n == 5 and m == 2:
ans = '''0
..
..
..
..
..'''
if n == 5 and m == 3:
ans = '''2
..A
AAA
.BA
.B.
BBB'''
if n == 5 and m == 4:
ans = '''2
.A..
.A..
AAAB
.BBB
...B'''
if n == 5 and m == 5:
ans = '''4
BBB.A
.BAAA
DB.CA
DDDC.
D.CCC'''
if n == 6 and m == 1:
ans = '''0
.
.
.
.
.
.'''
if n == 6 and m == 2:
ans = '''0
..
..
..
..
..
..'''
if n == 6 and m == 3:
ans = '''2
.A.
.A.
AAA
.B.
.B.
BBB'''
if n == 6 and m == 4:
ans = '''3
.A..
.A..
AAAB
CBBB
CCCB
C...'''
if n == 6 and m == 5:
ans = '''4
.A..B
.ABBB
AAACB
.D.C.
.DCCC
DDD..'''
if n == 6 and m == 6:
ans = '''5
.A..B.
.ABBB.
AAACB.
ECCCD.
EEECD.
E..DDD'''
if n == 7 and m == 1:
ans = '''0
.
.
.
.
.
.
.'''
if n == 7 and m == 2:
ans = '''0
..
..
..
..
..
..
..'''
if n == 7 and m == 3:
ans = '''3
..A
AAA
B.A
BBB
BC.
.C.
CCC'''
if n == 7 and m == 4:
ans = '''4
...A
BAAA
BBBA
B..C
DCCC
DDDC
D...'''
if n == 7 and m == 5:
ans = '''5
.A..B
.ABBB
AAACB
.CCC.
.D.CE
.DEEE
DDD.E'''
if n == 7 and m == 6:
ans = '''6
.A..B.
.ABBB.
AAACB.
EEECCC
.EDC.F
.EDFFF
.DDD.F'''
if n == 7 and m == 7:
ans = '''8
B..ACCC
BBBA.C.
BDAAACE
.DDDEEE
GDHHHFE
GGGH.F.
G..HFFF'''
if n == 8 and m == 1:
ans = '''0
.
.
.
.
.
.
.
.'''
if n == 8 and m == 2:
ans = '''0
..
..
..
..
..
..
..
..'''
if n == 8 and m == 3:
ans = '''3
.A.
.A.
AAA
..B
BBB
.CB
.C.
CCC'''
if n == 8 and m == 4:
ans = '''4
.A..
.A..
AAAB
CBBB
CCCB
CD..
.D..
DDD.'''
if n == 8 and m == 5:
ans = '''6
.A..B
.ABBB
AAACB
DDDC.
.DCCC
FD.E.
FFFE.
F.EEE'''
if n == 8 and m == 6:
ans = '''7
.A..B.
.ABBB.
AAA.BC
DDDCCC
.DGGGC
.DFGE.
FFFGE.
..FEEE'''
if n == 8 and m == 7:
ans = '''9
B..ACCC
BBBA.C.
BDAAACE
.DDDEEE
FD.IIIE
FFFHIG.
FHHHIG.
...HGGG'''
if n == 9 and m == 8:
ans = '''12
.A..BDDD
.ABBBCD.
AAAEBCD.
FEEECCCG
FFFEHGGG
FKKKHHHG
.IKLH.J.
.IKLLLJ.
IIIL.JJJ'''
if n == 8 and m == 8:
ans = '''10
.A..BCCC
.A..B.C.
AAABBBCD
E.GGGDDD
EEEGJJJD
EF.GIJH.
.FIIIJH.
FFF.IHHH'''
if n == 9 and m == 9:
ans = '''13
AAA.BCCC.
.ABBB.CD.
.AE.BFCD.
EEEFFFDDD
G.E.HFIII
GGGJHHHI.
GK.JHL.IM
.KJJJLMMM
KKK.LLL.M'''
if n == 9 and m == 1:
ans = '''0
.
.
.
.
.
.
.
.
.'''
if n == 9 and m == 2:
ans = '''0
..
..
..
..
..
..
..
..
..'''
if n == 9 and m == 3:
ans = '''4
..A
AAA
B.A
BBB
B.C
CCC
.DC
.D.
DDD'''
if n == 9 and m == 4:
ans = '''5
.A..
.A..
AAAB
CBBB
CCCB
C..D
EDDD
EEED
E...'''
if n == 9 and m == 5:
ans = '''7
.A..B
.ABBB
AAACB
DCCC.
DDDCF
DEFFF
.E.GF
EEEG.
..GGG'''
if n == 9 and m == 6:
ans = '''8
.A..B.
.ABBB.
AAACB.
ECCCD.
EEECD.
E.GDDD
.FGGGH
.FGHHH
FFF..H'''
if n == 9 and m == 7:
ans = '''10
.ACCC.B
.A.CBBB
AAACD.B
...EDDD
FEEED.G
FFFEGGG
FHJJJIG
.H.J.I.
HHHJIII'''
if swapped:
cnt = ans.split('\n')[0]
lst = ans.split('\n')[1:]
ans = ''
for j in range(m):
for i in range(n):
ans = ans + lst[i][j]
ans += '\n'
ans = cnt + '\n' + ans
print(ans)
# Made By Mostafa_Khaled
|
vfc_84537
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 3",
"output": "1\nAAA\n.A.\n.A.",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 6",
"output": "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 2",
"output": "0\n..\n..",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
28
|
Solve the following coding problem using the programming language python:
There are *n* students living in the campus. Every morning all students wake up at the same time and go to wash. There are *m* rooms with wash basins. The *i*-th of these rooms contains *a**i* wash basins. Every student independently select one the rooms with equal probability and goes to it. After all students selected their rooms, students in each room divide into queues by the number of wash basins so that the size of the largest queue is the least possible. Calculate the expected value of the size of the largest queue among all rooms.
The input will be provided via standard input and looks as follows:
The first line contains two positive integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=50) — the amount of students and the amount of rooms. The second line contains *m* integers *a*1,<=*a*2,<=... ,<=*a**m* (1<=≤<=*a**i*<=≤<=50). *a**i* means the amount of wash basins in the *i*-th room.
You should write the solution as standard output as follows:
Output single number: the expected value of the size of the largest queue. Your answer must have an absolute or relative error less than 10<=-<=9.
Here are example inputs and outputs for the problem:
Example Input 1: 1 1
2
Example Output 1: 1.00000000000000000000
Example Input 2: 2 2
1 1
Example Output 2: 1.50000000000000000000
Example Input 3: 2 3
1 1 1
Example Output 3: 1.33333333333333350000
Example Input 4: 7 5
1 1 2 3 1
Example Output 4: 2.50216960000000070000
Now solve the problem by providing the code.
|
import functools,math,itertools,time
u=functools.lru_cache(maxsize=None)
n,m=map(int,input().split())
s=[*map(int,input().split())]
t=u(lambda x:1 if x<2 else x*t(x-1))
c=u(lambda r,n:t(n)/t(r)/t(n-r))
p=u(lambda n,k:n**k)
w=u(lambda n,k:math.ceil(n/k))
r=u(lambda k,n:max(k,n))
h=u(lambda i,j,l:c(l,i)*p(j-1,i-l)/p(j,i))
for i,j in itertools.product(range(1,n+1),range(1,m+1)):
for l in range(i+1):
h(i,j,l)
@u
def d(i,j,k):
if j:
q=0
for l in range(i + 1):
q+=h(i,j,l)*d(i-l,j-1,r(k,w(l,s[j-1])))
return q
return k
print(d(n,m,0))
|
vfc_84541
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1 1\n2",
"output": "1.00000000000000000000",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 2\n1 1",
"output": "1.50000000000000000000",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 3\n1 1 1",
"output": "1.33333333333333350000",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
980
|
Solve the following coding problem using the programming language python:
A necklace can be described as a string of links ('-') and pearls ('o'), with the last link or pearl connected to the first one.
You can remove a link or a pearl and insert it between two other existing links or pearls (or between a link and a pearl) on the necklace. This process can be repeated as many times as you like, but you can't throw away any parts.
Can you make the number of links between every two adjacent pearls equal? Two pearls are considered to be adjacent if there is no other pearl between them.
Note that the final necklace should remain as one circular part of the same length as the initial necklace.
The input will be provided via standard input and looks as follows:
The only line of input contains a string $s$ ($3 \leq |s| \leq 100$), representing the necklace, where a dash '-' represents a link and the lowercase English letter 'o' represents a pearl.
You should write the solution as standard output as follows:
Print "YES" if the links and pearls can be rejoined such that the number of links between adjacent pearls is equal. Otherwise print "NO".
You can print each letter in any case (upper or lower).
Here are example inputs and outputs for the problem:
Example Input 1: -o-o--
Example Output 1: YES
Example Input 2: -o---
Example Output 2: YES
Example Input 3: -o---o-
Example Output 3: NO
Example Input 4: ooo
Example Output 4: YES
Now solve the problem by providing the code.
|
s = [x for x in input().lower()]
b = s.count('o')
v = s.count('-')
if b == 0 or v % b == 0:
print("YES")
else:
print("NO")
|
vfc_84545
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "3000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "-o-o--",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "-o---",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "-o---o-",
"output": "NO",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "ooo",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "---",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "--o-o-----o----o--oo-o-----ooo-oo---o--",
"output": "YES",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
704
|
Solve the following coding problem using the programming language python:
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are *n* applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
*q* events are about to happen (in chronological order). They are of three types:
1. Application *x* generates a notification (this new notification is unread). 1. Thor reads all notifications generated so far by application *x* (he may re-read some notifications). 1. Thor reads the first *t* notifications generated by phone applications (notifications generated in first *t* events of the first type). It's guaranteed that there were at least *t* events of the first type before this event. Please note that he doesn't read first *t* unread notifications, he just reads the very first *t* notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
The input will be provided via standard input and looks as follows:
The first line of input contains two integers *n* and *q* (1<=≤<=*n*,<=*q*<=≤<=300<=000) — the number of applications and the number of events to happen.
The next *q* lines contain the events. The *i*-th of these lines starts with an integer *type**i* — type of the *i*-th event. If *type**i*<==<=1 or *type**i*<==<=2 then it is followed by an integer *x**i*. Otherwise it is followed by an integer *t**i* (1<=≤<=*type**i*<=≤<=3,<=1<=≤<=*x**i*<=≤<=*n*,<=1<=≤<=*t**i*<=≤<=*q*).
You should write the solution as standard output as follows:
Print the number of unread notifications after each event.
Here are example inputs and outputs for the problem:
Example Input 1: 3 4
1 3
1 1
1 2
2 3
Example Output 1: 1
2
3
2
Example Input 2: 4 6
1 2
1 4
1 2
3 3
1 3
1 3
Example Output 2: 1
2
3
0
1
2
Now solve the problem by providing the code.
|
import sys
n,m=map(int,input().split())
k=0
pos=0
L=[]
L1=[]
d={i:[0,-1] for i in range(1,n+1)}
for i in range(m) :
a,b=map(int,input().split())
if a==1 :
d[b][0]+=1
k+=1
L.append(b)
elif a==2 :
k-=d[b][0]
d[b][0]=0
d[b][1]=len(L)
else :
for j in range(pos,b) :
if d[L[j]][0]>0 and d[L[j]][1]<j+1 :
k-=1
d[L[j]][0]-=1
pos=max(pos,b)
L1.append(k)
sys.stdout.write('\n'.join(map(str,L1)))
|
vfc_84549
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 4\n1 3\n1 1\n1 2\n2 3",
"output": "1\n2\n3\n2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 6\n1 2\n1 4\n1 2\n3 3\n1 3\n1 3",
"output": "1\n2\n3\n0\n1\n2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10 85\n2 2\n1 10\n1 1\n2 6\n1 2\n1 4\n1 7\n2 1\n1 1\n3 3\n1 9\n1 6\n1 8\n1 10\n3 8\n2 8\n1 6\n1 3\n1 9\n1 6\n1 3\n1 8\n1 1\n1 6\n1 10\n2 1\n2 10\n1 10\n1 1\n1 10\n1 6\n1 2\n1 8\n1 3\n1 4\n1 9\n1 5\n1 5\n2 2\n2 4\n1 7\n1 1\n2 4\n1 9\n1 1\n1 7\n1 8\n3 33\n1 10\n2 2\n1 3\n1 10\n1 6\n3 32\n2 3\n1 5\n2 10\n2 2\n2 4\n2 3\n3 16\n1 3\n2 2\n1 1\n3 18\n2 2\n2 5\n1 5\n1 9\n2 4\n1 3\n1 4\n1 3\n1 6\n1 10\n2 2\n1 7\n1 7\n2 8\n1 1\n3 1\n1 8\n1 10\n1 7\n1 8",
"output": "0\n1\n2\n2\n3\n4\n5\n4\n5\n3\n4\n5\n6\n7\n2\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n9\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n17\n16\n17\n18\n18\n19\n20\n21\n22\n3\n4\n4\n5\n6\n7\n7\n6\n7\n5\n5\n5\n5\n5\n6\n6\n7\n7\n7\n6\n7\n8\n8\n9\n10\n11\n12\n13\n13\n14\n15\n14\n15\n15\n16\n17\n18\n19",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "300000 1\n1 300000",
"output": "1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
173
|
Solve the following coding problem using the programming language python:
Nikephoros and Polycarpus play rock-paper-scissors. The loser gets pinched (not too severely!).
Let us remind you the rules of this game. Rock-paper-scissors is played by two players. In each round the players choose one of three items independently from each other. They show the items with their hands: a rock, scissors or paper. The winner is determined by the following rules: the rock beats the scissors, the scissors beat the paper and the paper beats the rock. If the players choose the same item, the round finishes with a draw.
Nikephoros and Polycarpus have played *n* rounds. In each round the winner gave the loser a friendly pinch and the loser ended up with a fresh and new red spot on his body. If the round finished in a draw, the players did nothing and just played on.
Nikephoros turned out to have worked out the following strategy: before the game began, he chose some sequence of items *A*<==<=(*a*1,<=*a*2,<=...,<=*a**m*), and then he cyclically showed the items from this sequence, starting from the first one. Cyclically means that Nikephoros shows signs in the following order: *a*1, *a*2, ..., *a**m*, *a*1, *a*2, ..., *a**m*, *a*1, ... and so on. Polycarpus had a similar strategy, only he had his own sequence of items *B*<==<=(*b*1,<=*b*2,<=...,<=*b**k*).
Determine the number of red spots on both players after they've played *n* rounds of the game. You can consider that when the game began, the boys had no red spots on them.
The input will be provided via standard input and looks as follows:
The first line contains integer *n* (1<=≤<=*n*<=≤<=2·109) — the number of the game's rounds.
The second line contains sequence *A* as a string of *m* characters and the third line contains sequence *B* as a string of *k* characters (1<=≤<=*m*,<=*k*<=≤<=1000). The given lines only contain characters "R", "S" and "P". Character "R" stands for the rock, character "S" represents the scissors and "P" represents the paper.
You should write the solution as standard output as follows:
Print two space-separated integers: the numbers of red spots Nikephoros and Polycarpus have.
Here are example inputs and outputs for the problem:
Example Input 1: 7
RPS
RSPP
Example Output 1: 3 2
Example Input 2: 5
RRRRRRRR
R
Example Output 2: 0 0
Now solve the problem by providing the code.
|
n = int(input())
a = input()
b = input()
ai = 0
alen = len(a)
bi = 0
blen = len(b)
nik = 0
pol = 0
if alen == blen: rnd = alen
else: rnd = alen*blen
numofrounds = 0
for i in range(n):
#print(i,rnd)
if i == rnd:
numofrounds = n//rnd
# print(numofrounds)
nik *= numofrounds
pol *= numofrounds
break
#print(a[ai%alen], b[bi%blen])
if a[ai] == b[bi]: pass
elif (a[ai] == 'R' and b[bi] == 'S') or (a[ai] == 'S'
and b[bi] == 'P') or (a[ai] == 'P' and b[bi] == 'R'):
pol += 1
else: nik += 1
ai = (ai+1)%alen
bi = (bi+1)%blen
if n%rnd != 0 and numofrounds != 0:
n -= rnd*numofrounds
ai = 0
bi = 0
for i in range(n):
if a[ai] == b[bi]: pass
elif (a[ai] == 'R' and b[bi] == 'S') or (a[ai] == 'S'
and b[bi] == 'P') or (a[ai] == 'P' and b[bi] == 'R'):
pol += 1
else: nik += 1
ai = (ai+1)%alen
bi = (bi+1)%blen
print(nik, pol)
|
vfc_84553
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "3000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7\nRPS\nRSPP",
"output": "3 2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\nRRRRRRRR\nR",
"output": "0 0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
613
|
Solve the following coding problem using the programming language python:
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly *n* skills. Each skill is represented by a non-negative integer *a**i* — the current skill level. All skills have the same maximum level *A*.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
- The number of skills that a character has perfected (i.e., such that *a**i*<==<=*A*), multiplied by coefficient *c**f*.- The minimum skill level among all skills (*min* *a**i*), multiplied by coefficient *c**m*.
Now Lesha has *m* hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to *A* yet). Help him spend his money in order to achieve the maximum possible value of the Force.
The input will be provided via standard input and looks as follows:
The first line of the input contains five space-separated integers *n*, *A*, *c**f*, *c**m* and *m* (1<=≤<=*n*<=≤<=100<=000, 1<=≤<=*A*<=≤<=109, 0<=≤<=*c**f*,<=*c**m*<=≤<=1000, 0<=≤<=*m*<=≤<=1015).
The second line contains exactly *n* integers *a**i* (0<=≤<=*a**i*<=≤<=*A*), separated by spaces, — the current levels of skills.
You should write the solution as standard output as follows:
On the first line print the maximum value of the Force that the character can achieve using no more than *m* currency units.
On the second line print *n* integers *a*'*i* (*a**i*<=≤<=*a*'*i*<=≤<=*A*), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than *m* currency units. Numbers should be separated by spaces.
Here are example inputs and outputs for the problem:
Example Input 1: 3 5 10 1 5
1 3 1
Example Output 1: 12
2 5 2
Example Input 2: 3 5 10 1 339
1 3 1
Example Output 2: 35
5 5 5
Now solve the problem by providing the code.
|
import itertools
import bisect
n, A, cf, cm, m = [int(x) for x in input().split()]
skills = [int(x) for x in input().split()]
sorted_skills = list(sorted((k, i) for i, k in enumerate(skills)))
bottom_lift = [0 for i in range(n)]
for i in range(1, n):
bottom_lift[i] = bottom_lift[i-1] + i * (sorted_skills[i][0] - sorted_skills[i-1][0])
root_lift = [0 for i in range(n+1)]
for i in range(1, n+1):
root_lift[i] = root_lift[i-1] + A - sorted_skills[n-i][0]
max_level = -1
for i in range(n+1):
money_left = m - root_lift[i]
if money_left < 0: break
k = min(bisect.bisect(bottom_lift, money_left), n-i)
money_left -= bottom_lift[k-1]
min_level = min(A, sorted_skills[k-1][0] + money_left//k) if k > 0 else A
level = cf*i + cm*min_level
if max_level < level:
max_level = level
argmax = i
argmax_min_level = min_level
argmax_k = k
ans = [0 for i in range(n)]
for i, skill in enumerate(sorted_skills):
if i < argmax_k:
ans[skill[1]] = argmax_min_level
elif i >= n - argmax:
ans[skill[1]] = A
else:
ans[skill[1]] = skill[0]
print(max_level)
for a in ans:
print(a, end = ' ')
|
vfc_84557
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 5 10 1 5\n1 3 1",
"output": "12\n2 5 2 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 5 10 1 339\n1 3 1",
"output": "35\n5 5 5 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 6 0 1 4\n5 1",
"output": "5\n5 5 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1000000000 1000 1000 1000000000000000\n0",
"output": "1000000001000\n1000000000 ",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
277
|
Solve the following coding problem using the programming language python:
The "BerCorp" company has got *n* employees. These employees can use *m* approved official languages for the formal correspondence. The languages are numbered with integers from 1 to *m*. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.
Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
The input will be provided via standard input and looks as follows:
The first line contains two integers *n* and *m* (2<=≤<=*n*,<=*m*<=≤<=100) — the number of employees and the number of languages.
Then *n* lines follow — each employee's language list. At the beginning of the *i*-th line is integer *k**i* (0<=≤<=*k**i*<=≤<=*m*) — the number of languages the *i*-th employee knows. Next, the *i*-th line contains *k**i* integers — *a**ij* (1<=≤<=*a**ij*<=≤<=*m*) — the identifiers of languages the *i*-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages.
The numbers in the lines are separated by single spaces.
You should write the solution as standard output as follows:
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
Here are example inputs and outputs for the problem:
Example Input 1: 5 5
1 2
2 2 3
2 3 4
2 4 5
1 5
Example Output 1: 0
Example Input 2: 8 7
0
3 1 2 3
1 1
2 5 4
2 6 7
1 3
2 7 4
1 1
Example Output 2: 2
Example Input 3: 2 2
1 2
0
Example Output 3: 1
Now solve the problem by providing the code.
|
n,m = map(int,input().split())
arr = []
ki = False
for i in range(n):
a = list(map(int,input().split()))
set_a = set(());
for j in range (a[0]):
ki=True
set_a.add(a[j+1])
arr.append(set_a)
change = 0
if ki:
while(change!=-1):
changed = -1
change = -1
for i in range(len(arr)):
for j in range(i+1,len(arr)):
for x in arr[i]:
if x in arr[j]:
arr[i] = arr[i].union(arr[j])
changed =j
change = 1
break;
if (change==1):
break;
if (change==1):
break;
if (change==1):
arr.pop(changed)
print(len(arr)-1)
else:
print(n)
|
vfc_84561
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "3000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5",
"output": "0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 2\n1 2\n0",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 2\n0\n0",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 5\n1 3\n0\n0\n2 4 1\n0",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 2\n0\n0\n2 1 2\n1 1\n1 1\n0",
"output": "3",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
708
|
Solve the following coding problem using the programming language python:
You are given a non-empty string *s* consisting of lowercase English letters. You have to pick exactly one non-empty substring of *s* and shift all its letters 'z' 'y' 'x' 'b' 'a' 'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' is replaced with 'z'.
What is the lexicographically minimum string that can be obtained from *s* by performing this shift exactly once?
The input will be provided via standard input and looks as follows:
The only line of the input contains the string *s* (1<=≤<=|*s*|<=≤<=100<=000) consisting of lowercase English letters.
You should write the solution as standard output as follows:
Print the lexicographically minimum string that can be obtained from *s* by shifting letters of exactly one non-empty substring.
Here are example inputs and outputs for the problem:
Example Input 1: codeforces
Example Output 1: bncdenqbdr
Example Input 2: abacaba
Example Output 2: aaacaba
Now solve the problem by providing the code.
|
s = input()
i = 0
if set(s)=={'a'}:
print('a'*(len(s)-1)+'z')
exit()
while i<len(s) and s[i]=='a':
print('a',end='')
i+=1
while i<len(s) and s[i]!='a':
print(chr(ord(s[i])-1),end="")
i+=1
while i<len(s):
print(s[i],end="")
i+=1
print()
|
vfc_84565
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "codeforces",
"output": "bncdenqbdr",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
993
|
Solve the following coding problem using the programming language python:
You are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coordinate axes. Find whether the two squares intersect.
The interior of the square is considered to be part of the square, i.e. if one square is completely inside another, they intersect. If the two squares only share one common point, they are also considered to intersect.
The input will be provided via standard input and looks as follows:
The input data consists of two lines, one for each square, both containing 4 pairs of integers. Each pair represents coordinates of one vertex of the square. Coordinates within each line are either in clockwise or counterclockwise order.
The first line contains the coordinates of the square with sides parallel to the coordinate axes, the second line contains the coordinates of the square at 45 degrees.
All the values are integer and between $-100$ and $100$.
You should write the solution as standard output as follows:
Print "Yes" if squares intersect, otherwise print "No".
You can print each letter in any case (upper or lower).
Here are example inputs and outputs for the problem:
Example Input 1: 0 0 6 0 6 6 0 6
1 3 3 5 5 3 3 1
Example Output 1: YES
Example Input 2: 0 0 6 0 6 6 0 6
7 3 9 5 11 3 9 1
Example Output 2: NO
Example Input 3: 6 0 6 6 0 6 0 0
7 4 4 7 7 10 10 7
Example Output 3: YES
Now solve the problem by providing the code.
|
def outp(v1,v2):
a1,a2=v1
b1,b2=v2
det = a1*b2-a2*b1
return 'M' if det==0 else ('L' if det>0 else 'R')
def check(p,PS):
sides = {'M':0,'L':0,'R':0}
for i in range(4):
v1 = [PS[i+1][0]-PS[i][0],PS[i+1][1]-PS[i][1]]
v2 = [p[0]-PS[i][0],p[1]-PS[i][1]]
sides[outp(v1,v2)] += 1
return not (sides['L'] and sides['R'])
def solv(S1,S2):
PS1 = [S1[i:i+2] for i in range(0,8,2)]
PS1.append(PS1[0])
PS2 = [S2[i:i+2] for i in range(0,8,2)]
PS2.append(PS2[0])
for i in range(-100,101):
for j in range(-100,101):
p = [i,j]
if check(p,PS1) and check(p,PS2):
return 'YES'
return 'NO'
S1 = list(map(int,input().split()))
S2 = list(map(int,input().split()))
print(solv(S1,S2))
|
vfc_84569
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "0 0 6 0 6 6 0 6\n1 3 3 5 5 3 3 1",
"output": "YES",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "0 0 6 0 6 6 0 6\n7 3 9 5 11 3 9 1",
"output": "NO",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
571
|
Solve the following coding problem using the programming language python:
You've got array *A*, consisting of *n* integers and a positive integer *k*. Array *A* is indexed by integers from 1 to *n*.
You need to permute the array elements so that value
The input will be provided via standard input and looks as follows:
The first line contains two integers *n*,<=*k* (2<=≤<=*n*<=≤<=3·105, 1<=≤<=*k*<=≤<=*min*(5000,<=*n*<=-<=1)).
The second line contains *n* integers *A*[1],<=*A*[2],<=...,<=*A*[*n*] (<=-<=109<=≤<=*A*[*i*]<=≤<=109), separate by spaces — elements of the array *A*.
You should write the solution as standard output as follows:
Print the minimum possible value of the sum described in the statement.
Here are example inputs and outputs for the problem:
Example Input 1: 3 2
1 2 4
Example Output 1: 1
Example Input 2: 5 2
3 -5 3 -5 3
Example Output 2: 0
Example Input 3: 6 3
4 3 4 3 2 5
Example Output 3: 3
Now solve the problem by providing the code.
|
def solve(n, k, As):
As.sort()
m, r = divmod(n, k)
dp = [0] * (r + 1)
for i in range(1, k):
im = i * m
new_dp = [0] * (r + 1)
new_dp[0] = dp[0] + As[im] - As[im - 1]
for h in range(1, min(i, r) + 1):
new_dp[h] = max(dp[h], dp[h - 1]) + As[im + h] - As[im + h - 1]
dp = new_dp
return As[-1] - As[0] - max(dp[r], dp[r-1])
n, k = map(int, input().split())
As = list(map(int, input().split()))
print(solve(n, k, As))
|
vfc_84573
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 2\n1 2 4",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 2\n3 -5 3 -5 3",
"output": "0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 3\n4 3 4 3 2 5",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 1\n1 100",
"output": "99",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
587
|
Solve the following coding problem using the programming language python:
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of *i*-th of them is 2*w**i* pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2*a*1,<=...,<=2*a**k* if and only if there exists a non-negative integer *x* such that 2*a*1<=+<=2*a*2<=+<=...<=+<=2*a**k*<==<=2*x*, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
The input will be provided via standard input and looks as follows:
The first line of input contains integer *n* (1<=≤<=*n*<=≤<=106), the number of weights.
The second line contains *n* integers *w*1,<=...,<=*w**n* separated by spaces (0<=≤<=*w**i*<=≤<=106 for each 1<=≤<=*i*<=≤<=*n*), the powers of two forming the weights values.
You should write the solution as standard output as follows:
Print the minimum number of steps in a single line.
Here are example inputs and outputs for the problem:
Example Input 1: 5
1 1 2 3 3
Example Output 1: 2
Example Input 2: 4
0 1 2 3
Example Output 2: 4
Now solve the problem by providing the code.
|
n = int(input())
a = [int(x) for x in input().split()]
l = [0] * (10**6 + 100)
for x in a:
l[x] += 1
cur = 0
ans = 0
for x in l:
cur += x
if cur % 2:
ans += 1
cur //= 2
print(ans)
|
vfc_84577
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n1 1 2 3 3",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n0 1 2 3",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n120287",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n28288 0",
"output": "2",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
316
|
Solve the following coding problem using the programming language python:
Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there.
The content special agent has got an important task: to get the latest research by British scientists on the English Language. These developments are encoded and stored in a large safe. The Beaver's teeth are strong enough, so the authorities assured that upon arriving at the place the beaver won't have any problems with opening the safe.
And he finishes his aspen sprig and leaves for this important task. Of course, the Beaver arrived at the location without any problems, but alas. He can't open the safe with his strong and big teeth. At this point, the Smart Beaver get a call from the headquarters and learns that opening the safe with the teeth is not necessary, as a reliable source has sent the following information: the safe code consists of digits and has no leading zeroes. There also is a special hint, which can be used to open the safe. The hint is string *s* with the following structure:
- if *s**i* = "?", then the digit that goes *i*-th in the safe code can be anything (between 0 to 9, inclusively); - if *s**i* is a digit (between 0 to 9, inclusively), then it means that there is digit *s**i* on position *i* in code; - if the string contains letters from "A" to "J", then all positions with the same letters must contain the same digits and the positions with distinct letters must contain distinct digits. - The length of the safe code coincides with the length of the hint.
For example, hint "?JGJ9" has such matching safe code variants: "51919", "55959", "12329", "93539" and so on, and has wrong variants such as: "56669", "00111", "03539" and "13666".
After receiving such information, the authorities change the plan and ask the special agents to work quietly and gently and not to try to open the safe by mechanical means, and try to find the password using the given hint.
At a special agent school the Smart Beaver was the fastest in his platoon finding codes for such safes, but now he is not in that shape: the years take their toll ... Help him to determine the number of possible variants of the code to the safe, matching the given hint. After receiving this information, and knowing his own speed of entering codes, the Smart Beaver will be able to determine whether he will have time for tonight's show "Beavers are on the trail" on his favorite TV channel, or he should work for a sleepless night...
The input will be provided via standard input and looks as follows:
The first line contains string *s* — the hint to the safe code. String *s* consists of the following characters: ?, 0-9, A-J. It is guaranteed that the first character of string *s* doesn't equal to character 0.
The input limits for scoring 30 points are (subproblem A1):
- 1<=≤<=|*s*|<=≤<=5.
The input limits for scoring 100 points are (subproblems A1+A2):
- 1<=≤<=|*s*|<=≤<=105.
Here |*s*| means the length of string *s*.
You should write the solution as standard output as follows:
Print the number of codes that match the given hint.
Here are example inputs and outputs for the problem:
Example Input 1: AJ
Example Output 1: 81
Example Input 2: 1?AA
Example Output 2: 100
Now solve the problem by providing the code.
|
def main():
hint = input()
hint_nodigits = [h for h in hint if not h.isdigit()]
letters = [h for h in hint_nodigits if h != '?']
combs_letters = 1
for i in range(10, 10 - len(set(letters)), -1):
combs_letters *= i
combs_jolly = 10 ** (len(hint_nodigits) - len(letters))
if hint[0] == '?':
combs_jolly = (combs_jolly // 10) * 9
elif hint[0].isalpha():
combs_letters = (combs_letters // 10) * 9
print(combs_letters * combs_jolly)
if __name__ == '__main__':
main()
|
vfc_84581
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "AJ",
"output": "81",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1?AA",
"output": "100",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "?",
"output": "9",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7",
"output": "1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
788
|
Solve the following coding problem using the programming language python:
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function *f*, which is defined as follows:
In the above formula, 1<=≤<=*l*<=<<=*r*<=≤<=*n* must hold, where *n* is the size of the Main Uzhlyandian Array *a*, and |*x*| means absolute value of *x*. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of *f* among all possible values of *l* and *r* for the given array *a*.
The input will be provided via standard input and looks as follows:
The first line contains single integer *n* (2<=≤<=*n*<=≤<=105) — the size of the array *a*.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (-109<=≤<=*a**i*<=≤<=109) — the array elements.
You should write the solution as standard output as follows:
Print the only integer — the maximum value of *f*.
Here are example inputs and outputs for the problem:
Example Input 1: 5
1 4 2 3 1
Example Output 1: 3
Example Input 2: 4
1 5 4 7
Example Output 2: 6
Now solve the problem by providing the code.
|
n = int(input())
arr = list(map(int, input().split()))
diff = [abs(arr[i] - arr[i-1]) for i in range(1, n)]
res0, res1, res, curr_sum = -2e9, 0, -2e9, 0
for i in range(n-1):
curr_sum += diff[i] * (1 if i % 2 == 0 else -1)
res = max(res, curr_sum - res1, res0 - curr_sum)
if i % 2 == 0:
res0 = max(res0, curr_sum)
else:
res1 = min(res1, curr_sum)
print(res)
|
vfc_84585
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n1 4 2 3 1",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n1 5 4 7",
"output": "6",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
627
|
Solve the following coding problem using the programming language python:
Two positive integers *a* and *b* have a sum of *s* and a bitwise XOR of *x*. How many possible values are there for the ordered pair (*a*,<=*b*)?
The input will be provided via standard input and looks as follows:
The first line of the input contains two integers *s* and *x* (2<=≤<=*s*<=≤<=1012, 0<=≤<=*x*<=≤<=1012), the sum and bitwise xor of the pair of positive integers, respectively.
You should write the solution as standard output as follows:
Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.
Here are example inputs and outputs for the problem:
Example Input 1: 9 5
Example Output 1: 4
Example Input 2: 3 3
Example Output 2: 2
Example Input 3: 5 2
Example Output 3: 0
Now solve the problem by providing the code.
|
def func(S,X):
if S - X < 0 :
return 0
elif (S - X) % 2:
return 0
nd = (S - X) // 2
c = 0
while X:
if X & 1:
if nd & 1:
return 0
c += 1
X >>= 1
nd >>= 1
return 2 ** c
S,X = map(int,input().split())
print(func(S,X) - 2*(S == X))
|
vfc_84589
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "9 5",
"output": "4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 3",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 2",
"output": "0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
216
|
Solve the following coding problem using the programming language python:
A new Berland businessman Vitaly is going to open a household appliances' store. All he's got to do now is to hire the staff.
The store will work seven days a week, but not around the clock. Every day at least *k* people must work in the store.
Berland has a law that determines the order of working days and non-working days. Namely, each employee must work for exactly *n* consecutive days, then rest for exactly *m* days, then work for *n* more days and rest for *m* more, and so on. Vitaly doesn't want to break the law. Fortunately, there is a loophole: the law comes into force on the day when the employee is hired. For example, if an employee is hired on day *x*, then he should work on days [*x*,<=*x*<=+<=1,<=...,<=*x*<=+<=*n*<=-<=1], [*x*<=+<=*m*<=+<=*n*,<=*x*<=+<=*m*<=+<=*n*<=+<=1,<=...,<=*x*<=+<=*m*<=+<=2*n*<=-<=1], and so on. Day *x* can be chosen arbitrarily by Vitaly.
There is one more thing: the key to the store. Berland law prohibits making copies of keys, so there is only one key. Vitaly is planning to entrust the key to the store employees. At the same time on each day the key must be with an employee who works that day — otherwise on this day no one can get inside the store. During the day the key holder can give the key to another employee, if he also works that day. The key will handed to the first hired employee at his first working day.
Each employee has to be paid salary. Therefore, Vitaly wants to hire as few employees as possible provided that the store can operate normally on each day from 1 to infinity. In other words, on each day with index from 1 to infinity, the store must have at least *k* working employees, and one of the working employees should have the key to the store.
Help Vitaly and determine the minimum required number of employees, as well as days on which they should be hired.
The input will be provided via standard input and looks as follows:
The first line contains three integers *n*, *m* and *k* (1<=≤<=*m*<=≤<=*n*<=≤<=1000, *n*<=≠<=1, 1<=≤<=*k*<=≤<=1000).
You should write the solution as standard output as follows:
In the first line print a single integer *z* — the minimum required number of employees.
In the second line print *z* positive integers, separated by spaces: the *i*-th integer *a**i* (1<=≤<=*a**i*<=≤<=104) should represent the number of the day, on which Vitaly should hire the *i*-th employee.
If there are multiple answers, print any of them.
Here are example inputs and outputs for the problem:
Example Input 1: 4 3 2
Example Output 1: 4
1 1 4 5
Example Input 2: 3 3 1
Example Output 2: 3
1 3 5
Now solve the problem by providing the code.
|
n, m, k = map(int, input().split())
span = n+m+1
count = (span+1)*[0]
count[span] = k
key = (span+1)*[False]
key[1] = True
hire = []
day = 1
while True:
while day <= span and count[day] >= k:
day += 1
if day > span:
if key[span]:
break
day -= 1
while not key[day]:
day -= 1
hire.append(day)
last = min(span, day+n-1)
for i in range(day, last+1):
count[i] += 1
key[i] = True
print(len(hire))
print(' '.join([str(x) for x in hire]))
|
vfc_84593
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4 3 2",
"output": "4\n1 1 4 5",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
321
|
Solve the following coding problem using the programming language python:
Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string *s*. Each character of *s* is one move operation. There are four move operations at all:
- 'U': go up, (x, y) <=→<= (x, y+1); - 'D': go down, (x, y) <=→<= (x, y-1); - 'L': go left, (x, y) <=→<= (x-1, y); - 'R': go right, (x, y) <=→<= (x+1, y).
The robot will do the operations in *s* from left to right, and repeat it infinite times. Help Fox Ciel to determine if after some steps the robot will located in (*a*,<=*b*).
The input will be provided via standard input and looks as follows:
The first line contains two integers *a* and *b*, (<=-<=109<=≤<=*a*,<=*b*<=≤<=109). The second line contains a string *s* (1<=≤<=|*s*|<=≤<=100, *s* only contains characters 'U', 'D', 'L', 'R') — the command.
You should write the solution as standard output as follows:
Print "Yes" if the robot will be located at (*a*,<=*b*), and "No" otherwise.
Here are example inputs and outputs for the problem:
Example Input 1: 2 2
RU
Example Output 1: Yes
Example Input 2: 1 2
RU
Example Output 2: No
Example Input 3: -1 1000000000
LRRLU
Example Output 3: Yes
Example Input 4: 0 0
D
Example Output 4: Yes
Now solve the problem by providing the code.
|
#!/usr/bin/python3
import sys
MAPPING = {
'U': (0, 1),
'D': (0, -1),
'L': (-1, 0),
'R': (1, 0),
}
def read_ints():
return [int(x) for x in input().split()]
def get_vectors(moves):
vector = (0, 0)
yield vector
for move in moves:
delta = MAPPING[move]
vector = (vector[0] + delta[0], vector[1] + delta[1])
yield vector
def mul(pair, a):
return pair[0] * a, pair[1] * a
def is_achievable(diff, period):
if diff == (0, 0):
return True
if period == (0, 0):
return False
if period[0] != 0:
if diff[0] % period[0] == 0:
times = diff[0] // period[0]
if times > 0 and mul(period, times) == diff:
return True
if period[1] != 0:
if diff[1] % period[1] == 0:
times = diff[1] // period[1]
if times > 0 and mul(period, times) == diff:
return True
return False
def main():
a, b = read_ints()
moves = input().strip()
vectors = list(get_vectors(moves))
period = vectors[-1]
for vector in vectors:
diff = (a - vector[0], b - vector[1])
if is_achievable(diff, period):
print('Yes')
sys.exit(0)
print('No')
if __name__ == '__main__':
main()
|
vfc_84597
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "4000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 2\nRU",
"output": "Yes",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 2\nRU",
"output": "No",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "-1 1000000000\nLRRLU",
"output": "Yes",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
744
|
Solve the following coding problem using the programming language python:
Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries.
The world can be modeled as an undirected graph with *n* nodes and *m* edges. *k* of the nodes are home to the governments of the *k* countries that make up the world.
There is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions is stable.
Hongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add.
The input will be provided via standard input and looks as follows:
The first line of input will contain three integers *n*, *m* and *k* (1<=≤<=*n*<=≤<=1<=000, 0<=≤<=*m*<=≤<=100<=000, 1<=≤<=*k*<=≤<=*n*) — the number of vertices and edges in the graph, and the number of vertices that are homes of the government.
The next line of input will contain *k* integers *c*1,<=*c*2,<=...,<=*c**k* (1<=≤<=*c**i*<=≤<=*n*). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world.
The following *m* lines of input will contain two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*). This denotes an undirected edge between nodes *u**i* and *v**i*.
It is guaranteed that the graph described by the input is stable.
You should write the solution as standard output as follows:
Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.
Here are example inputs and outputs for the problem:
Example Input 1: 4 1 2
1 3
1 2
Example Output 1: 2
Example Input 2: 3 3 1
2
1 2
1 3
2 3
Example Output 2: 0
Now solve the problem by providing the code.
|
def dfs(vertice, soma, temCapital,aux, used, capitais, am):
soma += len(aux[vertice])
am += 1
if vertice in capitais:
temCapital = True
used[vertice] = True
for i in aux[vertice]:
if not used[i]:
tmpC, am, soma = dfs(i, soma, temCapital, aux, used, capitais, am)
temCapital |= tmpC
return [temCapital, am, soma]
def hongcowBuildsANation(n,m,k):
aux = []
for i in range(n):
aux.append([])
capitais = [int(i) - 1 for i in input().split()]
for i in range(m):
u, v = [int(i) - 1 for i in input().split()]
aux[u].append(v)
aux[v].append(u)
resultado = 0
used = [False] * n
comp = []
while False in used:
comp.append(dfs(used.index(False), 0, False,aux, used,capitais, 0))
comp[len(comp) - 1][2] /= 2
for i in range(len(comp)):
resultado -= comp[i][2]
comp.sort(reverse=True)
totalNs = comp[0][1]
for i in range(1, len(comp)):
if not comp[i][0]:
totalNs += comp[i][1]
else:
resultado += comp[i][1] * (comp[i][1] - 1) / 2
resultado += (totalNs) * (totalNs - 1) / 2
print(int(resultado))
n, m, k = [int(i) for i in input().split()]
hongcowBuildsANation(n,m,k)
|
vfc_84601
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4 1 2\n1 3\n1 2",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 3 1\n2\n1 2\n1 3\n2 3",
"output": "0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
176
|
Solve the following coding problem using the programming language python:
Let's consider one interesting word game. In this game you should transform one word into another through special operations.
Let's say we have word *w*, let's split this word into two non-empty parts *x* and *y* so, that *w*<==<=*xy*. A split operation is transforming word *w*<==<=*xy* into word *u*<==<=*yx*. For example, a split operation can transform word "wordcut" into word "cutword".
You are given two words *start* and *end*. Count in how many ways we can transform word *start* into word *end*, if we apply exactly *k* split operations consecutively to word *start*.
Two ways are considered different if the sequences of applied operations differ. Two operation sequences are different if exists such number *i* (1<=≤<=*i*<=≤<=*k*), that in the *i*-th operation of the first sequence the word splits into parts *x* and *y*, in the *i*-th operation of the second sequence the word splits into parts *a* and *b*, and additionally *x*<=≠<=*a* holds.
The input will be provided via standard input and looks as follows:
The first line contains a non-empty word *start*, the second line contains a non-empty word *end*. The words consist of lowercase Latin letters. The number of letters in word *start* equals the number of letters in word *end* and is at least 2 and doesn't exceed 1000 letters.
The third line contains integer *k* (0<=≤<=*k*<=≤<=105) — the required number of operations.
You should write the solution as standard output as follows:
Print a single number — the answer to the problem. As this number can be rather large, print it modulo 1000000007 (109<=+<=7).
Here are example inputs and outputs for the problem:
Example Input 1: ab
ab
2
Example Output 1: 1
Example Input 2: ababab
ababab
1
Example Output 2: 2
Example Input 3: ab
ba
2
Example Output 3: 0
Now solve the problem by providing the code.
|
# Problem: B. Word Cut
# Contest: Codeforces - Croc Champ 2012 - Round 2
# URL: https://codeforces.com/problemset/problem/176/B
# Memory Limit: 256 MB
# Time Limit: 2000 ms
import sys
import random
from types import GeneratorType
import bisect
import io, os
from bisect import *
from collections import *
from contextlib import redirect_stdout
from itertools import *
from array import *
from functools import lru_cache, reduce
from heapq import *
from math import sqrt, gcd, inf
if sys.version >= '3.8': # ACW没有comb
from math import comb
RI = lambda: map(int, sys.stdin.buffer.readline().split())
RS = lambda: map(bytes.decode, sys.stdin.buffer.readline().strip().split())
RILST = lambda: list(RI())
DEBUG = lambda *x: sys.stderr.write(f'{str(x)}\n')
# print = lambda d: sys.stdout.write(str(d) + "\n") # 打开可以快写,但是无法使用print(*ans,sep=' ')这种语法,需要print(' '.join(map(str, p))),确实会快。
DIRS = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 右下左上
DIRS8 = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0),
(-1, 1)] # →↘↓↙←↖↑↗
RANDOM = random.randrange(2 ** 62)
MOD = 10 ** 9 + 7
PROBLEM = """https://codeforces.com/problemset/problem/176/B
输入两个长度相等的字符串 s 和 t,长度在 [2,1000] 内,只包含小写字母。
然后输入 k(0≤k≤1e5) 表示操作次数。
你需要恰好执行 k 次操作。
每次操作你可以把 s 分割成两个非空字符串 s1 和 s2,然后替换 s = s2 + s1。
把 s 变成 t 有多少种方案?模 1e9+7。
输入
ab
ab
2
输出 1
输入
ababab
ababab
1
输出 2
输入
ab
ba
2
输出 0
"""
"""https://codeforces.com/contest/176/submission/210387377
手玩一下发现操作与「把 s 循环右移(左移)」是一样的。
假设有 c 种不同的循环右移可以让 s=t。那么有 n-c 种不同的循环右移让 s≠t。
定义 f[i] 表示操作 i 次后 s=t,g[i] 表示表示操作 i 次后 s≠t。
那么
f[i] = f[i-1] * (c-1) + g[i-1] * c
g[i] = f[i-1] * (n-c) + g[i-1] * (n-c-1)
初始值 f[0]=(s==t), g[0]=f[0]^1"""
# ms
def solve():
s, = RS()
t, = RS()
k, = RI()
n = len(s)
f = int(s == t)
g = f ^ 1
s += s
c = 0
for i in range(n):
if t == s[i:i + n]:
c += 1
if not c:
return print(0)
# print(c)
for _ in range(k):
f, g = (f * (c - 1) + g * c) % MOD, (f * (n - c) + g * (n - c - 1)) % MOD
print(f)
if __name__ == '__main__':
t = 0
if t:
t, = RI()
for _ in range(t):
solve()
else:
solve()
|
vfc_84605
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "ab\nab\n2",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "ababab\nababab\n1",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "ab\nba\n2",
"output": "0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "aaa\naaa\n0",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "hi\nhi\n1",
"output": "0",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
925
|
Solve the following coding problem using the programming language python:
One department of some software company has $n$ servers of different specifications. Servers are indexed with consecutive integers from $1$ to $n$. Suppose that the specifications of the $j$-th server may be expressed with a single integer number $c_j$ of artificial resource units.
In order for production to work, it is needed to deploy two services $S_1$ and $S_2$ to process incoming requests using the servers of the department. Processing of incoming requests of service $S_i$ takes $x_i$ resource units.
The described situation happens in an advanced company, that is why each service may be deployed using not only one server, but several servers simultaneously. If service $S_i$ is deployed using $k_i$ servers, then the load is divided equally between these servers and each server requires only $x_i / k_i$ (that may be a fractional number) resource units.
Each server may be left unused at all, or be used for deploying exactly one of the services (but not for two of them simultaneously). The service should not use more resources than the server provides.
Determine if it is possible to deploy both services using the given servers, and if yes, determine which servers should be used for deploying each of the services.
The input will be provided via standard input and looks as follows:
The first line contains three integers $n$, $x_1$, $x_2$ ($2 \leq n \leq 300\,000$, $1 \leq x_1, x_2 \leq 10^9$) — the number of servers that the department may use, and resource units requirements for each of the services.
The second line contains $n$ space-separated integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 10^9$) — the number of resource units provided by each of the servers.
You should write the solution as standard output as follows:
If it is impossible to deploy both services using the given servers, print the only word "No" (without the quotes).
Otherwise print the word "Yes" (without the quotes).
In the second line print two integers $k_1$ and $k_2$ ($1 \leq k_1, k_2 \leq n$) — the number of servers used for each of the services.
In the third line print $k_1$ integers, the indices of the servers that will be used for the first service.
In the fourth line print $k_2$ integers, the indices of the servers that will be used for the second service.
No index may appear twice among the indices you print in the last two lines. If there are several possible answers, it is allowed to print any of them.
Here are example inputs and outputs for the problem:
Example Input 1: 6 8 16
3 5 2 9 8 7
Example Output 1: Yes
3 2
1 2 6
5 4
Example Input 2: 4 20 32
21 11 11 12
Example Output 2: Yes
1 3
1
2 3 4
Example Input 3: 4 11 32
5 5 16 16
Example Output 3: No
Example Input 4: 5 12 20
7 8 4 11 9
Example Output 4: No
Now solve the problem by providing the code.
|
I = lambda:list(map(int, input().split()))
n,x1,x2 = I()
servers = I()
idx = list(range(n))
idx.sort(key = lambda i : servers[i])
def solve(a, b, rev):
j = len(idx)-1
while j >= 0 and (n-j)*servers[idx[j]] < b:
j -= 1
if j < 0:
return
i = j-1
while i >= 0 and (j-i)*servers[idx[i]] < a:
i -= 1
if i < 0:
return
print("Yes")
if not rev:
print(j-i, n-j)
print(" ".join(map(lambda x:str(x+1),idx[i:j])))
print(" ".join(map(lambda x:str(x+1), idx[j:])))
else:
print(n-j, j-i)
print(" ".join(map(lambda x:str(x+1), idx[j:])))
print(" ".join(map(lambda x:str(x+1),idx[i:j])))
exit()
solve(x1,x2,False)
solve(x2,x1,True)
print("No")
|
vfc_84609
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6 8 16\n3 5 2 9 8 7",
"output": "Yes\n4 2\n3 1 2 6\n5 4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 20 32\n21 11 11 12",
"output": "Yes\n1 3\n1\n2 3 4",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 11 32\n5 5 16 16",
"output": "No",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 12 20\n7 8 4 11 9",
"output": "No",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 1 1\n1 1",
"output": "Yes\n1 1\n1\n2",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
730
|
Solve the following coding problem using the programming language python:
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time.
Polycarp is good at marketing, so he has already collected *n* requests from clients. The requests are numbered from 1 to *n* in order they came.
The *i*-th request is characterized by two values: *s**i* — the day when a client wants to start the repair of his car, *d**i* — duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on.
Polycarp is making schedule by processing requests in the order from the first to the *n*-th request. He schedules the *i*-th request as follows:
- If the car repair shop is idle for *d**i* days starting from *s**i* (*s**i*,<=*s**i*<=+<=1,<=...,<=*s**i*<=+<=*d**i*<=-<=1), then these days are used to repair a car of the *i*-th client. - Otherwise, Polycarp finds the first day *x* (from 1 and further) that there are *d**i* subsequent days when no repair is scheduled starting from *x*. In other words he chooses the smallest positive *x* that all days *x*,<=*x*<=+<=1,<=...,<=*x*<=+<=*d**i*<=-<=1 are not scheduled for repair of any car. So, the car of the *i*-th client will be repaired in the range [*x*,<=*x*<=+<=*d**i*<=-<=1]. It is possible that the day *x* when repair is scheduled to start will be less than *s**i*.
Given *n* requests, you are asked to help Polycarp schedule all of them according to the rules above.
The input will be provided via standard input and looks as follows:
The first line contains integer *n* (1<=≤<=*n*<=≤<=200) — the number of requests from clients.
The following *n* lines contain requests, one request per line. The *i*-th request is given as the pair of integers *s**i*,<=*d**i* (1<=≤<=*s**i*<=≤<=109, 1<=≤<=*d**i*<=≤<=5·106), where *s**i* is the preferred time to start repairing the *i*-th car, *d**i* is the number of days to repair the *i*-th car.
The requests should be processed in the order they are given in the input.
You should write the solution as standard output as follows:
Print *n* lines. The *i*-th line should contain two integers — the start day to repair the *i*-th car and the finish day to repair the *i*-th car.
Here are example inputs and outputs for the problem:
Example Input 1: 3
9 2
7 3
2 4
Example Output 1: 9 10
1 3
4 7
Example Input 2: 4
1000000000 1000000
1000000000 1000000
100000000 1000000
1000000000 1000000
Example Output 2: 1000000000 1000999999
1 1000000
100000000 100999999
1000001 2000000
Now solve the problem by providing the code.
|
n = int(input())
l, r = [0] * n, [0] * n
f = lambda x, y: all(x > r[j] or y < l[j] for j in range(i))
for i in range(n):
x, d = map(int, input().split())
y = x + d - 1
if not f(x, y):
k = min(r[j] for j in range(i + 1) if f(r[j] + 1, r[j] + d))
x, y = k + 1, k + d
l[i], r[i] = x, y
print(x, y)
|
vfc_84617
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n9 2\n7 3\n2 4",
"output": "9 10\n1 3\n4 7",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000",
"output": "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1 1",
"output": "1 1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
936
|
Solve the following coding problem using the programming language python:
Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after *k* minutes after turning on.
During cooking, Julia goes to the kitchen every *d* minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.
It is known that the chicken needs *t* minutes to be cooked on the stove, if it is turned on, and 2*t* minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off.
The input will be provided via standard input and looks as follows:
The single line contains three integers *k*, *d* and *t* (1<=≤<=*k*,<=*d*,<=*t*<=≤<=1018).
You should write the solution as standard output as follows:
Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10<=-<=9.
Namely, let's assume that your answer is *x* and the answer of the jury is *y*. The checker program will consider your answer correct if .
Here are example inputs and outputs for the problem:
Example Input 1: 3 2 6
Example Output 1: 6.5
Example Input 2: 4 2 20
Example Output 2: 20.0
Now solve the problem by providing the code.
|
def give(n,d):
if n%d:
return n//d+1
else:
return n//d
k,d,t=list(map(int,input().split()))
p=give(k,d)*d
if d>=k: a=k
else: a=p-p%k
q=(p-a)/2
ans=t//(a+q)*p
r=t-t//(a+q)*(a+q)
if r<=a:
ans+=r
else:
ans+=a
r-=a
ans+=r*2
print(ans)
|
vfc_84621
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 2 6",
"output": "6.5",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 2 20",
"output": "20.0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8 10 9",
"output": "10.0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "43 50 140",
"output": "150.5",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
101
|
Solve the following coding problem using the programming language python:
Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of *n* small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than *k* characters, it will be very suspicious.
Find the least number of distinct characters that can remain in the string after no more than *k* characters are deleted. You also have to find any possible way to delete the characters.
The input will be provided via standard input and looks as follows:
The first input data line contains a string whose length is equal to *n* (1<=≤<=*n*<=≤<=105). The string consists of lowercase Latin letters. The second line contains the number *k* (0<=≤<=*k*<=≤<=105).
You should write the solution as standard output as follows:
Print on the first line the only number *m* — the least possible number of different characters that could remain in the given string after it loses no more than *k* characters.
Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly *m* distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly *m* distinct characters, print any of them.
Here are example inputs and outputs for the problem:
Example Input 1: aaaaa
4
Example Output 1: 1
aaaaa
Example Input 2: abacaba
4
Example Output 2: 1
aaaa
Example Input 3: abcdefgh
10
Example Output 3: 0
Now solve the problem by providing the code.
|
from collections import Counter
s = input()
k = int(input())
C = sorted(Counter(s).items(), key=lambda x: -x[1])
while k and C:
ch, n = C.pop()
x = min(k,n)
s = s.replace(ch, '', x)
k -= x
print(len(set(s)))
print(s)
|
vfc_84625
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "aaaaa\n4",
"output": "1\naaaaa",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "abacaba\n4",
"output": "1\naaaa",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "abcdefgh\n10",
"output": "0",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "aaaaaaaaaaaaaaaaaaaa\n19",
"output": "1\naaaaaaaaaaaaaaaaaaaa",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "abcdefghijjihgedcba\n0",
"output": "10\nabcdefghijjihgedcba",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "aababcabcdabcde\n9",
"output": "2\naabababab",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
51
|
Solve the following coding problem using the programming language python:
Cheaterius is a famous in all the Berland astrologist, magician and wizard, and he also is a liar and a cheater. One of his latest inventions is Cheaterius' amulets! They bring luck and wealth, but are rather expensive. Cheaterius makes them himself. The technology of their making is kept secret. But we know that throughout long nights Cheaterius glues together domino pairs with super glue to get squares 2<=×<=2 which are the Cheaterius' magic amulets!
After a hard night Cheaterius made *n* amulets. Everyone of them represents a square 2<=×<=2, every quarter contains 1 to 6 dots. Now he wants sort them into piles, every pile must contain similar amulets. Two amulets are called similar if they can be rotated by 90, 180 or 270 degrees so that the following condition is met: the numbers of dots in the corresponding quarters should be the same. It is forbidden to turn over the amulets.
Write a program that by the given amulets will find the number of piles on Cheaterius' desk.
The input will be provided via standard input and looks as follows:
The first line contains an integer *n* (1<=≤<=*n*<=≤<=1000), where *n* is the number of amulets. Then the amulet's descriptions are contained. Every description occupies two lines and contains two numbers (from 1 to 6) in each line. Between every pair of amulets the line "**" is located.
You should write the solution as standard output as follows:
Print the required number of piles.
Here are example inputs and outputs for the problem:
Example Input 1: 4
31
23
**
31
23
**
13
32
**
32
13
Example Output 1: 1
Example Input 2: 4
51
26
**
54
35
**
25
61
**
45
53
Example Output 2: 2
Now solve the problem by providing the code.
|
def contains(cont: list, temp: str) -> bool:
for i in range(4):
if temp in cont:
return True
temp = f'{temp[2]}{temp[0]}{temp[3]}{temp[1]}'
return False
n = int(input())
cont = []
for i in range(n):
temp = input() + input()
if not contains(cont, temp):
cont.append(temp)
if i < n - 1:
input()
print(len(cont))
|
vfc_84629
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "1000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n31\n23\n**\n31\n23\n**\n13\n32\n**\n32\n13",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n51\n26\n**\n54\n35\n**\n25\n61\n**\n45\n53",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n56\n61\n**\n31\n31\n**\n33\n11\n**\n11\n33",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n36\n44\n**\n32\n46\n**\n66\n41\n**\n64\n34",
"output": "3",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n63\n63\n**\n66\n33\n**\n36\n36",
"output": "1",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
641
|
Solve the following coding problem using the programming language python:
Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him.
The area looks like a strip of cells 1<=×<=*n*. Each cell contains the direction for the next jump and the length of that jump. Grasshopper starts in the first cell and follows the instructions written on the cells. Grasshopper stops immediately if it jumps out of the strip. Now Artem wants to find out if this will ever happen.
The input will be provided via standard input and looks as follows:
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — length of the strip.
Next line contains a string of length *n* which consists of characters "<" and ">" only, that provide the direction of the jump from the corresponding cell. Next line contains *n* integers *d**i* (1<=≤<=*d**i*<=≤<=109) — the length of the jump from the *i*-th cell.
You should write the solution as standard output as follows:
Print "INFINITE" (without quotes) if grasshopper will continue his jumps forever. Otherwise print "FINITE" (without quotes).
Here are example inputs and outputs for the problem:
Example Input 1: 2
><
1 2
Example Output 1: FINITE
Example Input 2: 3
>><
2 1 1
Example Output 2: INFINITE
Now solve the problem by providing the code.
|
n = int(input())
str = input()
inp = list(map(int, input().split()))
for i in range(len(str)):
if str[i] == '<':
inp[i] *= -1
visited = [0 for i in range(n)]
cur = 0
while cur >= 0 and cur < n and visited[cur] != 1:
visited[cur] = 1
cur += inp[cur]
if cur >= 0 and cur < n:
print("INFINITE")
else:
print("FINITE")
|
vfc_84637
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n><\n1 2",
"output": "FINITE",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n>><\n2 1 1",
"output": "INFINITE",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n>\n1000000000",
"output": "FINITE",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n<\n1000000000",
"output": "FINITE",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n>>\n1 1",
"output": "FINITE",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
906
|
Solve the following coding problem using the programming language python:
Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.
Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.
The input will be provided via standard input and looks as follows:
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the number of actions Valentin did.
The next *n* lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types:
1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and *w* is the word that Valentin said. 1. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and *w* is the word that Valentin said. 1. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and *s* is the guess — a lowercase English letter.
All words consist only of lowercase English letters. The total length of all words does not exceed 105.
It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.
You should write the solution as standard output as follows:
Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.
Here are example inputs and outputs for the problem:
Example Input 1: 5
! abc
. ad
. b
! cd
? c
Example Output 1: 1
Example Input 2: 8
! hello
! codeforces
? c
. o
? d
? h
. l
? e
Example Output 2: 2
Example Input 3: 7
! ababahalamaha
? a
? b
? a
? b
? a
? h
Example Output 3: 0
Now solve the problem by providing the code.
|
import math,sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda:int(input())
li = lambda:list(map(int,input().split()))
def solve():
n=I()
vis=[1]*26
c=0
flag=0
ans=0
for i in range(n-1):
a,b=input().split(" ")
b=b.strip()
if(flag and a!='.'):
ans+=1
continue
if(a!='!'):
for i in range(len(b)):
vis[ord(b[i])-97]=0
elif(a=='!'):
c=Counter(b)
for i in range(26):
if(chr(97+i) not in c and vis[i]==1):
vis[i]=0
c=0
#print(*vis)
for i in range(26):
if(vis[i]==1):
c+=1
if(c==1):
flag=1
a,b=input().split(" ")
print(ans)
for _ in range(1):
solve()
|
vfc_84641
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "4500"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n! abc\n. ad\n. b\n! cd\n? c",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e",
"output": "2",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
838
|
Solve the following coding problem using the programming language python:
There is an airplane which has *n* rows from front to back. There will be *m* people boarding this airplane.
This airplane has an entrance at the very front and very back of the plane.
Each person has some assigned seat. It is possible for multiple people to have the same assigned seat. The people will then board the plane one by one starting with person 1. Each person can independently choose either the front entrance or back entrance to enter the plane.
When a person walks into the plane, they walk directly to their assigned seat and will try to sit in it. If it is occupied, they will continue walking in the direction they walked in until they are at empty seat - they will take the earliest empty seat that they can find. If they get to the end of the row without finding a seat, they will be angry.
Find the number of ways to assign tickets to the passengers and board the plane without anyone getting angry. Two ways are different if there exists a passenger who chose a different entrance in both ways, or the assigned seat is different. Print this count modulo 109<=+<=7.
The input will be provided via standard input and looks as follows:
The first line of input will contain two integers *n*,<=*m* (1<=≤<=*m*<=≤<=*n*<=≤<=1<=000<=000), the number of seats, and the number of passengers, respectively.
You should write the solution as standard output as follows:
Print a single number, the number of ways, modulo 109<=+<=7.
Here are example inputs and outputs for the problem:
Example Input 1: 3 3
Example Output 1: 128
Now solve the problem by providing the code.
|
MOD = 10 ** 9 + 7
n, m = input().split(' ')
n = int(n)
m = int(m)
ans = pow(2 * (n + 1), m, MOD)
ans = (ans * (n + 1 - m)) % MOD
ans = (ans * pow(n + 1, MOD - 2, MOD)) % MOD
print(ans)
|
vfc_84645
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 3",
"output": "128",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1000000 1000000",
"output": "233176135",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1000000 500000",
"output": "211837745",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
19
|
Solve the following coding problem using the programming language python:
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations:
- the final tournament features *n* teams (*n* is always even) - the first *n*<=/<=2 teams (according to the standings) come through to the knockout stage - the standings are made on the following principle: for a victory a team gets 3 points, for a draw — 1 point, for a defeat — 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place — in decreasing order of the difference between scored and missed goals; in the third place — in the decreasing order of scored goals - it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity.
You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage.
The input will be provided via standard input and looks as follows:
The first input line contains the only integer *n* (1<=≤<=*n*<=≤<=50) — amount of the teams, taking part in the final tournament of World Cup. The following *n* lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following *n*·(*n*<=-<=1)<=/<=2 lines describe the held matches in the format name1-name2 num1:num2, where *name*1, *name*2 — names of the teams; *num*1, *num*2 (0<=≤<=*num*1,<=*num*2<=≤<=100) — amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions only once.
You should write the solution as standard output as follows:
Output *n*<=/<=2 lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.
Here are example inputs and outputs for the problem:
Example Input 1: 4
A
B
C
D
A-B 1:1
A-C 2:2
A-D 1:0
B-C 1:0
B-D 0:3
C-D 0:3
Example Output 1: A
D
Example Input 2: 2
a
A
a-A 2:1
Example Output 2: a
Now solve the problem by providing the code.
|
class Info:
def __init__(self, newTeamName, newPoints, newGoalDiff, newScoredGoals):
self.teamName = newTeamName
self.points = newPoints
self.goalDiff = newGoalDiff
self.scoredGoals = newScoredGoals
def __str__(self):
return f'teamName: \'{self.teamName}\', points: {self.points}, goalDiff: {self.goalDiff}, scoredGoals: {self.scoredGoals}'
def out(cont):
for item in cont:
print(item)
def findIndexByName(cont, searchName):
for i in range(len(cont)):
if cont[i].teamName == searchName:
return i
return -1
n, cont = int(input()), []
for i in range(n):
cont.append(Info(input(), 0, 0, 0))
'''
01234567890123456789
line = 'Barcelona-Real 15:10'
'''
for i in range(n * (n - 1) // 2):
line = input()
dashInd = line.index('-')
spaceInd = line.index(' ')
colonInd = line.index(':')
team1Name = line[:dashInd]
team2Name = line[dashInd + 1:spaceInd]
goal1 = int(line[spaceInd + 1: colonInd])
goal2 = int(line[colonInd + 1:])
team1Index = findIndexByName(cont, team1Name)
team2Index = findIndexByName(cont, team2Name)
# update points
if goal1 > goal2:
cont[team1Index].points += 3
elif goal1 < goal2:
cont[team2Index].points += 3
else:
cont[team1Index].points += 1
cont[team2Index].points += 1
# update goal diff
cont[team1Index].goalDiff += goal1 - goal2
cont[team2Index].goalDiff += goal2 - goal1
# update scored goals
cont[team1Index].scoredGoals += goal1
cont[team2Index].scoredGoals += goal2
cont.sort(key=lambda obj: (obj.points, obj.goalDiff, obj.scoredGoals), reverse=True)
del cont[n // 2:]
cont.sort(key=lambda obj: obj.teamName)
# out(cont)
for obj in cont:
print(obj.teamName)
|
vfc_84649
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3",
"output": "A\nD",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
226
|
Solve the following coding problem using the programming language python:
There are *n* piles of stones of sizes *a*1,<=*a*2,<=...,<=*a**n* lying on the table in front of you.
During one move you can take one pile and add it to the other. As you add pile *i* to pile *j*, the size of pile *j* increases by the current size of pile *i*, and pile *i* stops existing. The cost of the adding operation equals the size of the added pile.
Your task is to determine the minimum cost at which you can gather all stones in one pile.
To add some challenge, the stone piles built up conspiracy and decided that each pile will let you add to it not more than *k* times (after that it can only be added to another pile).
Moreover, the piles decided to puzzle you completely and told you *q* variants (not necessarily distinct) of what *k* might equal.
Your task is to find the minimum cost for each of *q* variants.
The input will be provided via standard input and looks as follows:
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of stone piles. The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — the initial sizes of the stone piles.
The third line contains integer *q* (1<=≤<=*q*<=≤<=105) — the number of queries. The last line contains *q* space-separated integers *k*1,<=*k*2,<=...,<=*k**q* (1<=≤<=*k**i*<=≤<=105) — the values of number *k* for distinct queries. Note that numbers *k**i* can repeat.
You should write the solution as standard output as follows:
Print *q* whitespace-separated integers — the answers to the queries in the order, in which the queries are given in the input.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Here are example inputs and outputs for the problem:
Example Input 1: 5
2 3 4 1 1
2
2 3
Example Output 1: 9 8
Now solve the problem by providing the code.
|
n = int(input())
stones = list(map(lambda t : int(t), input().split()))
q = int(input())
queries = list(map(lambda t : int(t), input().split()))
stones.sort()
added_stones = []
added_stones.append(stones[0])
for i in range(1, n, 1):
added_stones.append(stones[i] + added_stones[i - 1])
computed_queries = {}
for qidx, qq in enumerate(queries):
if qq in computed_queries:
queries[qidx] = computed_queries[qq]
continue
i = n - 2
multiplier = 1
cost = 0
while i >= 0:
pp = pow(qq, multiplier)
nexti = i - pp
if nexti < 0:
cost += added_stones[i] * multiplier
break
cost += (added_stones[i] - added_stones[nexti]) * multiplier
multiplier += 1
i = nexti
queries[qidx] = cost
computed_queries[qq] = cost
print(*queries, sep=' ')
|
vfc_84653
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n2 3 4 1 1\n2\n2 3",
"output": "9 8 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n2 9\n5\n4 10 7 3 4",
"output": "2 2 2 2 2 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n7\n4\n6 2 3 3",
"output": "0 0 0 0 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n7 10\n2\n2 4",
"output": "7 7 ",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
126
|
Solve the following coding problem using the programming language python:
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them.
A little later they found a string *s*, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring *t* of the string *s*.
Prefix supposed that the substring *t* is the beginning of the string *s*; Suffix supposed that the substring *t* should be the end of the string *s*; and Obelix supposed that *t* should be located somewhere inside the string *s*, that is, *t* is neither its beginning, nor its end.
Asterix chose the substring *t* so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring *t* aloud, the temple doors opened.
You know the string *s*. Find the substring *t* or determine that such substring does not exist and all that's been written above is just a nice legend.
The input will be provided via standard input and looks as follows:
You are given the string *s* whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters.
You should write the solution as standard output as follows:
Print the string *t*. If a suitable *t* string does not exist, then print "Just a legend" without the quotes.
Here are example inputs and outputs for the problem:
Example Input 1: fixprefixsuffix
Example Output 1: fix
Example Input 2: abcdabc
Example Output 2: Just a legend
Now solve the problem by providing the code.
|
def kmp():
now = -1
sub[0] = now
for i in range(1, len(p)):
while now != -1 and p[i] != p[now + 1]:
now = sub[now]
if p[i] == p[now + 1]:
now += 1
sub[i] = now
else:
sub[i] = -1
p = input()
sub = [-1] * 1000001
kmp()
maxi = sub[len(p) - 1]
found = sub[maxi] if maxi != -1 else -1
for i in range(1, len(p) - 1):
if sub[i] == maxi:
found = maxi
if found == -1:
print("Just a legend")
else:
print(p[:found + 1])
|
vfc_84657
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "2000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "fixprefixsuffix",
"output": "fix",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "abcdabc",
"output": "Just a legend",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "qwertyqwertyqwerty",
"output": "qwerty",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "papapapap",
"output": "papap",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "aaaaaaaaaa",
"output": "aaaaaaaa",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "ghbdtn",
"output": "Just a legend",
"type": "stdin_stdout"
}
]
}
|
codeforces
|
verifiable_code
|
193
|
Solve the following coding problem using the programming language python:
You've gotten an *n*<=×<=*m* sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as *A*. Set *A* is connected. Your task is to find the minimum number of squares that we can delete from set *A* to make it not connected.
A set of painted squares is called connected, if for every two squares *a* and *b* from this set there is a sequence of squares from the set, beginning in *a* and ending in *b*, such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition.
The input will be provided via standard input and looks as follows:
The first input line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=50) — the sizes of the sheet of paper.
Each of the next *n* lines contains *m* characters — the description of the sheet of paper: the *j*-th character of the *i*-th line equals either "#", if the corresponding square is painted (belongs to set *A*), or equals "." if the corresponding square is not painted (does not belong to set *A*). It is guaranteed that the set of all painted squares *A* is connected and isn't empty.
You should write the solution as standard output as follows:
On the first line print the minimum number of squares that need to be deleted to make set *A* not connected. If it is impossible, print -1.
Here are example inputs and outputs for the problem:
Example Input 1: 5 4
####
#..#
#..#
#..#
####
Example Output 1: 2
Example Input 2: 5 5
#####
#...#
#####
#...#
#####
Example Output 2: 2
Now solve the problem by providing the code.
|
m, n = list(map(int, input().split()))
grid = [list(input()) for _ in range(m)]
count = sum(i.count('#') for i in grid)
if count <= 2:
print(-1)
exit()
table = {}
node = 0
graph = [[] for _ in range(count)]
for i in range(m):
for j in range(n):
if grid[i][j] == "#":
if (i, j) not in table:
table[(i, j)] = node
node += 1
for dx, dy in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
if 0 <= dx < m and 0 <= dy < n and grid[dx][dy] == "#":
if (dx, dy) not in table:
table[(dx, dy)] = node
node += 1
graph[table[(i, j)]].append(table[(dx, dy)])
cnt = [-1] * count
low = [-1] * count
t = 0
res = []
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
@bootstrap
def dfs(fa, son):
global t
if cnt[son] == -1:
c = 0
cnt[son] = t
low[son] = t
t += 1
for i in graph[son]:
if i != fa:
if cnt[i] == -1:
c += 1
yield dfs(son, i)
low[son] = min(low[son], low[i])
if fa == -1:
if c >= 2:
res.append(0)
else:
if (fa != 0 and low[son] >= cnt[fa]) or (fa == 0 and low[son] > 0):
res.append(son)
yield
dfs(-1, 0)
print(2 if not res else 1)
|
vfc_84661
|
{
"difficulty": "N/A",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": null,
"time_limit": "3000"
}
|
{
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 4\n####\n#..#\n#..#\n#..#\n####",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 5\n#####\n#...#\n#####\n#...#\n#####",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 10\n.########.",
"output": "1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1\n#",
"output": "-1",
"type": "stdin_stdout"
}
]
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.