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 |
---|---|---|---|---|---|---|---|
apps | verifiable_code | 2393 | Solve the following coding problem using the programming language python:
You are given a non-empty string $s=s_1s_2\dots s_n$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string $s$ if there is an integer $j$ ($1 \le j \le n-2$), that $s_{j}s_{j+1}s_{j+2}=$"one" or $s_{j}s_{j+1}s_{j+2}=$"two".
For example:
Polycarp does not like strings "oneee", "ontwow", "twone" and "oneonetwo" (they all have at least one substring "one" or "two"), Polycarp likes strings "oonnee", "twwwo" and "twnoe" (they have no substrings "one" and "two").
Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time.
For example, if the string looks like $s=$"onetwone", then if Polycarp selects two indices $3$ and $6$, then "onetwone" will be selected and the result is "ontwne".
What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be?
-----Input-----
The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Next, the test cases are given.
Each test case consists of one non-empty string $s$. Its length does not exceed $1.5\cdot10^5$. The string $s$ consists only of lowercase Latin letters.
It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $1.5\cdot10^6$.
-----Output-----
Print an answer for each test case in the input in order of their appearance.
The first line of each answer should contain $r$ ($0 \le r \le |s|$) — the required minimum number of positions to be removed, where $|s|$ is the length of the given line. The second line of each answer should contain $r$ different integers — the indices themselves for removal in any order. Indices are numbered from left to right from $1$ to the length of the string. If $r=0$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them.
-----Examples-----
Input
4
onetwone
testme
oneoneone
twotwo
Output
2
6 3
0
3
4 1 7
2
1 4
Input
10
onetwonetwooneooonetwooo
two
one
twooooo
ttttwo
ttwwoo
ooone
onnne
oneeeee
oneeeeeeetwooooo
Output
6
18 11 12 1 6 21
1
1
1
3
1
2
1
6
0
1
4
0
1
1
2
1 11
-----Note-----
In the first example, answers are:
"onetwone", "testme" — Polycarp likes it, there is nothing to remove, "oneoneone", "twotwo".
In the second example, answers are: "onetwonetwooneooonetwooo", "two", "one", "twooooo", "ttttwo", "ttwwoo" — Polycarp likes it, there is nothing to remove, "ooone", "onnne" — Polycarp likes it, there is nothing to remove, "oneeeee", "oneeeeeeetwooooo".
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def main():
t = int(input())
s1 = "one"
s2 = "two"
for i in range(t):
s = input()
n = len(s)
count = 0
i = 2
ans = []
while i < n:
s3 = s[i - 2:i + 1]
if s3 == s1 or s3 == s2:
count += 1
if i + 2 < n and s[i:i + 3] == s1:
ans.append(i + 1)
i += 5
else:
ans.append(i)
i += 2
else:
i += 1
print(count)
for i in range(len(ans)):
print(ans[i], end=" ")
print()
main()
``` | vfc_19320 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1259/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\nonetwone\ntestme\noneoneone\ntwotwo\n",
"output": "2\n6 2 \n0\n\n3\n2 5 8 \n2\n2 5 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\nonetwonetwooneooonetwooo\ntwo\none\ntwooooo\nttttwo\nttwwoo\nooone\nonnne\noneeeee\noneeeeeeetwooooo\n",
"output": "6\n6 2 13 18 10 21 \n1\n2 \n1\n2 \n1\n2 \n1\n5 \n0\n\n1\n4 \n0\n\n1\n2 \n2\n2 11 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\nzzzone\n",
"output": "1\n5 \n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2396 | Solve the following coding problem using the programming language python:
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form $\frac{a + b}{c}$.
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!
-----Input-----
The first line of the input contains a single integer m (1 ≤ m ≤ 200 000) – the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.
-----Output-----
Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).
-----Example-----
Input
4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7
Output
1 2 2 1
-----Note-----
In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions – easy and hard.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n=int(input())
dict1={}
dict2={}
for i in range(n):
s=input()
s=s.split('/')
c=int(s[1])
s=s[0].strip('(').strip(')').split('+')
a=int(s[0])
b=int(s[1])
ans=(a+b)/c
try:
dict2[ans] += 1
except:
dict2[ans] = 1
dict1[i] = ans
for i in range(n):
print(dict2[dict1[i]],end=' ')
``` | vfc_19332 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/958/D1",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n(99+98)/97\n(26+4)/10\n(12+33)/15\n(5+1)/7\n",
"output": "1 2 2 1 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n(44+98)/19\n(36+58)/47\n(62+74)/68\n(69+95)/82\n(26+32)/29\n(32+46)/39\n(32+24)/28\n(47+61)/54\n(39+13)/26\n(98+98)/98\n",
"output": "1 9 9 9 9 9 9 9 9 9 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "30\n(89+76)/87\n(81+78)/18\n(60+97)/32\n(41+14)/48\n(55+65)/27\n(29+15)/95\n(64+13)/96\n(78+30)/75\n(43+6)/60\n(69+34)/48\n(62+2)/97\n(85+42)/3\n(4+97)/42\n(1+18)/39\n(46+55)/76\n(22+59)/24\n(62+81)/98\n(64+8)/51\n(9+59)/48\n(47+2)/80\n(33+74)/76\n(61+83)/44\n(86+4)/51\n(65+41)/49\n(53+36)/45\n(6+19)/15\n(51+21)/68\n(98+36)/86\n(92+65)/86\n(27+58)/78\n",
"output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2397 | Solve the following coding problem using the programming language python:
You are given an array $a_1, a_2, \dots, a_n$ and an integer $k$.
You are asked to divide this array into $k$ non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let $f(i)$ be the index of subarray the $i$-th element belongs to. Subarrays are numbered from left to right and from $1$ to $k$.
Let the cost of division be equal to $\sum\limits_{i=1}^{n} (a_i \cdot f(i))$. For example, if $a = [1, -2, -3, 4, -5, 6, -7]$ and we divide it into $3$ subbarays in the following way: $[1, -2, -3], [4, -5], [6, -7]$, then the cost of division is equal to $1 \cdot 1 - 2 \cdot 1 - 3 \cdot 1 + 4 \cdot 2 - 5 \cdot 2 + 6 \cdot 3 - 7 \cdot 3 = -9$.
Calculate the maximum cost you can obtain by dividing the array $a$ into $k$ non-empty consecutive subarrays.
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 3 \cdot 10^5$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($ |a_i| \le 10^6$).
-----Output-----
Print the maximum cost you can obtain by dividing the array $a$ into $k$ nonempty consecutive subarrays.
-----Examples-----
Input
5 2
-1 -2 5 -4 8
Output
15
Input
7 6
-3 0 -1 -2 -2 -4 -1
Output
-45
Input
4 1
3 -1 6 0
Output
8
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = a[::-1]
s = [a[0]]
S = 0
for x in a[1:]:
s.append(s[-1]+x)
S += s[-1]
s.pop()
k -= 1
s = sorted(s)
i=0
while i<=k-1:
S += s.pop()
i += 1
print(S)
``` | vfc_19336 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1175/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 2\n-1 -2 5 -4 8\n",
"output": "15\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7 6\n-3 0 -1 -2 -2 -4 -1\n",
"output": "-45\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 1\n3 -1 6 0\n",
"output": "8\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2399 | Solve the following coding problem using the programming language python:
Alice and Bob play a game. Initially they have a string $s_1, s_2, \dots, s_n$, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length $a$, and Bob must select a substring of length $b$. It is guaranteed that $a > b$.
For example, if $s =$ ...X.. and $a = 3$, $b = 2$, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string $s =$ ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries.
The first line of each query contains two integers $a$ and $b$ ($1 \le b < a \le 3 \cdot 10^5$).
The second line of each query contains the string $s$ ($1 \le |s| \le 3 \cdot 10^5$), consisting of only characters . and X.
It is guaranteed that sum of all $|s|$ over all queries not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print YES if Alice can win and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
-----Example-----
Input
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
-----Note-----
In the first query Alice can select substring $s_3 \dots s_5$. After that $s$ turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring $s_2 \dots s_6$. After that $s$ turns into .XXXXX.X..X, and Bob can't make a move after that.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import os, sys
# zeilen = [l.strip() for l in sys.stdin.readlines()]
q = int(sys.stdin.readline().strip())
for _ in range(q):
a, b = list(map(int, sys.stdin.readline().strip().split()))
word = sys.stdin.readline().strip()
gaps = sorted([len(gap) for gap in word.split('X') if len(gap)>= b])
if len(gaps) == 0:
print('NO')
elif gaps[0] < a:
print('NO')
elif len(gaps) > 1 and gaps[-2] >= 2*b:
print('NO')
elif gaps[-1] < 2*b: # no problematic, need only count
print('YES' if (len(gaps) % 2) else 'NO')
else: # exactly one problematic gap
p = gaps[-1]
if (len(gaps) % 2): # A tries to make this gap into zero or two
if p <= (a + 2*b - 2): # short enough for 0
print('YES')
elif p < 3*a: # we have to try two
print('NO') # not long enough
elif p > (a + 4*b - 2): # too long
print('NO')
else:
print('YES')
else: # A tries to make this gap into one
if p < 2*a: # too short
print('NO')
elif p > (a + 3*b - 2):# too long
print('NO')
else:
print('YES')
``` | vfc_19344 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1221/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n3 2\nXX......XX...X\n4 2\nX...X.X..X\n5 3\n.......X..X\n",
"output": "YES\nNO\nYES\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2400 | Solve the following coding problem using the programming language python:
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew $n$ distinct lines, given by equations $y = x + p_i$ for some distinct $p_1, p_2, \ldots, p_n$.
Then JLS drew on the same paper sheet $m$ distinct lines given by equations $y = -x + q_i$ for some distinct $q_1, q_2, \ldots, q_m$.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer $n$ ($1 \le n \le 10^5$), the number of lines drawn by DLS.
The second line of a test case contains $n$ distinct integers $p_i$ ($0 \le p_i \le 10^9$) describing the lines drawn by DLS. The integer $p_i$ describes a line given by the equation $y = x + p_i$.
The third line of a test case contains an integer $m$ ($1 \le m \le 10^5$), the number of lines drawn by JLS.
The fourth line of a test case contains $m$ distinct integers $q_i$ ($0 \le q_i \le 10^9$) describing the lines drawn by JLS. The integer $q_i$ describes a line given by the equation $y = -x + q_i$.
The sum of the values of $n$ over all test cases in the input does not exceed $10^5$. Similarly, the sum of the values of $m$ over all test cases in the input does not exceed $10^5$.
In hacks it is allowed to use only one test case in the input, so $t=1$ should be satisfied.
-----Output-----
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
-----Example-----
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
-----Note-----
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates. [Image]
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
n = int(input())
r1 = list(map(int, input().split()))
m = int(input())
r2 = list(map(int, input().split()))
o11 = sum(t % 2 for t in r1) # Counts odd number
o12 = n - o11
o21 = sum(t % 2 for t in r2) # Counts odd number
o22 = m - o21
print (o11 * o21 + o12 * o22)
``` | vfc_19348 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1248/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n3\n1 3 2\n2\n0 3\n1\n1\n1\n1\n1\n2\n1\n1\n",
"output": "3\n1\n0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n2\n1000000000 0\n2\n1000000000 0\n2\n1 0\n2\n1000000000 0\n2\n1 0\n2\n1 0\n2\n1000000000 0\n2\n1 0\n1\n999999999\n1\n99999999\n1\n1000000000\n1\n999999999\n3\n1000000000 999999999 999999998\n7\n2 3 5 7 10 11 12\n",
"output": "4\n2\n2\n2\n1\n0\n10\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1\n664947340\n1\n254841583\n",
"output": "0\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2402 | Solve the following coding problem using the programming language python:
Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $n$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.
Suppose in the first round participant A took $x$-th place and in the second round — $y$-th place. Then the total score of the participant A is sum $x + y$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $i$ from $1$ to $n$ exactly one participant took $i$-th place in first round and exactly one participant took $i$-th place in second round.
Right after the end of the Olympiad, Nikolay was informed that he got $x$-th place in first round and $y$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 100$) — the number of test cases to solve.
Each of the following $t$ lines contains integers $n$, $x$, $y$ ($1 \leq n \leq 10^9$, $1 \le x, y \le n$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round.
-----Output-----
Print two integers — the minimum and maximum possible overall place Nikolay could take.
-----Examples-----
Input
1
5 1 3
Output
1 3
Input
1
6 3 4
Output
2 6
-----Note-----
Explanation for the first example:
Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: $\left. \begin{array}{|c|c|c|c|c|} \hline \text{Participant} & {\text{Round 1}} & {\text{Round 2}} & {\text{Total score}} & {\text{Place}} \\ \hline A & {1} & {3} & {4} & {1} \\ \hline B & {2} & {4} & {6} & {3} \\ \hline C & {3} & {5} & {8} & {5} \\ \hline D & {4} & {1} & {5} & {2} \\ \hline E & {5} & {2} & {7} & {4} \\ \hline \end{array} \right.$
However, the results of the Olympiad could also look like this: $\left. \begin{array}{|c|c|c|c|c|} \hline \text{Participant} & {\text{Round 1}} & {\text{Round 2}} & {\text{Total score}} & {\text{Place}} \\ \hline A & {1} & {3} & {4} & {3} \\ \hline B & {2} & {2} & {4} & {3} \\ \hline C & {3} & {1} & {4} & {3} \\ \hline D & {4} & {4} & {8} & {4} \\ \hline E & {5} & {5} & {10} & {5} \\ \hline \end{array} \right.$
In the first case Nikolay would have taken first place, and in the second — third place.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
t = int(input())
for _ in range(t):
n,x,y = map(int,input().split())
s = x+y
mx = min(s-1,n)
if s<=n:
mn = 1
else:
mn = min(s,s-n+1,n)
print(mn,mx)
``` | vfc_19356 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1313/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1\n5 1 3\n",
"output": "1 3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n6 3 4\n",
"output": "2 6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "25\n5 1 1\n5 2 3\n5 5 5\n5 5 1\n5 4 4\n10 1 2\n10 3 7\n10 10 10\n10 3 1\n10 7 6\n1000000 21232 1234\n999999 999999 999999\n1000000 300001 701321\n1000000 290001 679121\n500000 98765 123456\n100 1 1\n1000000000 1 1000000000\n1000000000 1 500000000\n100000000 1 12345678\n1000000000 1 499999999\n1000000000 1 1\n1000000000 727147900 673366700\n1000000000 525064181 561222627\n1000000000 198446857 735445702\n1000000000 1000000000 1000000000\n",
"output": "1 1\n1 4\n5 5\n2 5\n4 5\n1 2\n1 9\n10 10\n1 3\n4 10\n1 22465\n999999 999999\n1323 1000000\n1 969121\n1 222220\n1 1\n2 1000000000\n1 500000000\n1 12345678\n1 499999999\n1 1\n400514601 1000000000\n86286809 1000000000\n1 933892558\n1000000000 1000000000\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2403 | Solve the following coding problem using the programming language python:
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...
There are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \le i \le n$); Add $a_i$ to $ans$; If $b_i \neq -1$ then add $a_i$ to $a_{b_i}$.
What is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \le i \le n$) exactly once?
Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
-----Input-----
The first line contains the integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$ and $b$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($−10^6 \le a_i \le 10^6$).
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$).
Additional constraint: it's guaranteed that for any $i$ ($1 \le i \le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \ldots$ is not cyclic, in other words it will always end with $-1$.
-----Output-----
In the first line, print the maximum $ans$ you can get.
In the second line, print the order of operations: $n$ different integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.
-----Examples-----
Input
3
1 2 3
2 3 -1
Output
10
1 2 3
Input
2
-1 100
2 -1
Output
99
2 1
Input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
Output
-9
3 5 6 1 9 4 10 7 8 2
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n=int(input())
val=list(map(int,input().split()))
b=list(map(int,input().split()))
neig=[0]*n
for i in range(n):
neig[i]=[0]
inedges=[0]*n
for i in range(n):
if b[i]!=-1:
neig[i][0]+=1
neig[i].append(b[i]-1)
inedges[b[i]-1]+=1
ans=0
beg=[]
en=[]
tod=[]
for i in range(n):
if inedges[i]==0:
tod.append(i)
while len(tod)>0:
x=tod.pop()
ans+=val[x]
if val[x]>0:
beg.append(x+1)
else:
en.append(x+1)
if neig[x][0]==1:
inedges[neig[x][1]]-=1
if inedges[neig[x][1]]==0:
tod.append(neig[x][1])
if val[x]>0:
val[neig[x][1]]+=val[x]
print(ans)
print(*beg,end=" ")
en.reverse()
print(*en)
``` | vfc_19360 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1388/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n1 2 3\n2 3 -1\n",
"output": "10\n1 2 3 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n-1 100\n2 -1\n",
"output": "99\n2 1 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n-10 -1 2 2 5 -2 -3 -4 2 -6\n-1 -1 2 2 -1 5 5 7 7 9\n",
"output": "-9\n3 4 2 9 5 7 10 8 6 1 \n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2404 | Solve the following coding problem using the programming language python:
There was once young lass called Mary,
Whose jokes were occasionally scary.
On this April's Fool
Fixed limerick rules
Allowed her to trip the unwary.
Can she fill all the lines
To work at all times?
On juggling the words
Right around two-thirds
She nearly ran out of rhymes.
-----Input-----
The input contains a single integer $a$ ($4 \le a \le 998$). Not every integer in the range is a valid input for the problem; you are guaranteed that the input will be a valid integer.
-----Output-----
Output a single number.
-----Examples-----
Input
35
Output
57
Input
57
Output
319
Input
391
Output
1723
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
x = int(input())
for i in range(2, x):
if x % i == 0:
print(i, x // i, sep='')
break
``` | vfc_19364 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1331/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "35\n",
"output": "",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "57\n",
"output": "",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "391\n",
"output": "",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2406 | Solve the following coding problem using the programming language python:
Omkar is standing at the foot of Celeste mountain. The summit is $n$ meters away from him, and he can see all of the mountains up to the summit, so for all $1 \leq j \leq n$ he knows that the height of the mountain at the point $j$ meters away from himself is $h_j$ meters. It turns out that for all $j$ satisfying $1 \leq j \leq n - 1$, $h_j < h_{j + 1}$ (meaning that heights are strictly increasing).
Suddenly, a landslide occurs! While the landslide is occurring, the following occurs: every minute, if $h_j + 2 \leq h_{j + 1}$, then one square meter of dirt will slide from position $j + 1$ to position $j$, so that $h_{j + 1}$ is decreased by $1$ and $h_j$ is increased by $1$. These changes occur simultaneously, so for example, if $h_j + 2 \leq h_{j + 1}$ and $h_{j + 1} + 2 \leq h_{j + 2}$ for some $j$, then $h_j$ will be increased by $1$, $h_{j + 2}$ will be decreased by $1$, and $h_{j + 1}$ will be both increased and decreased by $1$, meaning that in effect $h_{j + 1}$ is unchanged during that minute.
The landslide ends when there is no $j$ such that $h_j + 2 \leq h_{j + 1}$. Help Omkar figure out what the values of $h_1, \dots, h_n$ will be after the landslide ends. It can be proven that under the given constraints, the landslide will always end in finitely many minutes.
Note that because of the large amount of input, it is recommended that your code uses fast IO.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^6$).
The second line contains $n$ integers $h_1, h_2, \dots, h_n$ satisfying $0 \leq h_1 < h_2 < \dots < h_n \leq 10^{12}$ — the heights.
-----Output-----
Output $n$ integers, where the $j$-th integer is the value of $h_j$ after the landslide has stopped.
-----Example-----
Input
4
2 6 7 8
Output
5 5 6 7
-----Note-----
Initially, the mountain has heights $2, 6, 7, 8$.
In the first minute, we have $2 + 2 \leq 6$, so $2$ increases to $3$ and $6$ decreases to $5$, leaving $3, 5, 7, 8$.
In the second minute, we have $3 + 2 \leq 5$ and $5 + 2 \leq 7$, so $3$ increases to $4$, $5$ is unchanged, and $7$ decreases to $6$, leaving $4, 5, 6, 8$.
In the third minute, we have $6 + 2 \leq 8$, so $6$ increases to $7$ and $8$ decreases to $7$, leaving $4, 5, 7, 7$.
In the fourth minute, we have $5 + 2 \leq 7$, so $5$ increases to $6$ and $7$ decreases to $6$, leaving $4, 6, 6, 7$.
In the fifth minute, we have $4 + 2 \leq 6$, so $4$ increases to $5$ and $6$ decreases to $5$, leaving $5, 5, 6, 7$.
In the sixth minute, nothing else can change so the landslide stops and our answer is $5, 5, 6, 7$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n = int(input());tot = sum(map(int, input().split()));extra = (n * (n - 1))//2;smol = (tot - extra) // n;out = [smol + i for i in range(n)]
for i in range(tot - sum(out)):out[i] += 1
print(' '.join(map(str,out)))
``` | vfc_19372 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1392/F",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n2 6 7 8\n",
"output": "5 5 6 7\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n0\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "100\n75 78 155 169 264 333 369 416 439 486 503 541 557 635 726 745 814 845 899 914 943 1016 1090 1132 1191 1199 1258 1323 1423 1491 1557 1562 1583 1663 1707 1711 1729 1767 1819 1894 1916 1926 1953 2022 2042 2055 2061 2125 2135 2170 2183 2215 2310 2338 2377 2420 2425 2473 2484 2537 2551 2576 2577 2593 2647 2717 2746 2757 2783 2854 2864 2918 2975 3075 3100 3159 3196 3218 3240 3336 3429 3501 3585 3612 3677 3738 3815 3851 3920 3980 4045 4098 4152 4224 4279 4349 4380 4385 4446 4504\n",
"output": "2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2407 | Solve the following coding problem using the programming language python:
Ivan plays an old action game called Heretic. He's stuck on one of the final levels of this game, so he needs some help with killing the monsters.
The main part of the level is a large corridor (so large and narrow that it can be represented as an infinite coordinate line). The corridor is divided into two parts; let's assume that the point $x = 0$ is where these parts meet.
The right part of the corridor is filled with $n$ monsters — for each monster, its initial coordinate $x_i$ is given (and since all monsters are in the right part, every $x_i$ is positive).
The left part of the corridor is filled with crusher traps. If some monster enters the left part of the corridor or the origin (so, its current coordinate becomes less than or equal to $0$), it gets instantly killed by a trap.
The main weapon Ivan uses to kill the monsters is the Phoenix Rod. It can launch a missile that explodes upon impact, obliterating every monster caught in the explosion and throwing all other monsters away from the epicenter. Formally, suppose that Ivan launches a missile so that it explodes in the point $c$. Then every monster is either killed by explosion or pushed away. Let some monster's current coordinate be $y$, then:
if $c = y$, then the monster is killed; if $y < c$, then the monster is pushed $r$ units to the left, so its current coordinate becomes $y - r$; if $y > c$, then the monster is pushed $r$ units to the right, so its current coordinate becomes $y + r$.
Ivan is going to kill the monsters as follows: choose some integer point $d$ and launch a missile into that point, then wait until it explodes and all the monsters which are pushed to the left part of the corridor are killed by crusher traps, then, if at least one monster is still alive, choose another integer point (probably the one that was already used) and launch a missile there, and so on.
What is the minimum number of missiles Ivan has to launch in order to kill all of the monsters? You may assume that every time Ivan fires the Phoenix Rod, he chooses the impact point optimally.
You have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 10^5$) — the number of queries.
The first line of each query contains two integers $n$ and $r$ ($1 \le n, r \le 10^5$) — the number of enemies and the distance that the enemies are thrown away from the epicenter of the explosion.
The second line of each query contains $n$ integers $x_i$ ($1 \le x_i \le 10^5$) — the initial positions of the monsters.
It is guaranteed that sum of all $n$ over all queries does not exceed $10^5$.
-----Output-----
For each query print one integer — the minimum number of shots from the Phoenix Rod required to kill all monsters.
-----Example-----
Input
2
3 2
1 3 5
4 1
5 2 3 5
Output
2
2
-----Note-----
In the first test case, Ivan acts as follows: choose the point $3$, the first monster dies from a crusher trap at the point $-1$, the second monster dies from the explosion, the third monster is pushed to the point $7$; choose the point $7$, the third monster dies from the explosion.
In the second test case, Ivan acts as follows: choose the point $5$, the first and fourth monsters die from the explosion, the second monster is pushed to the point $1$, the third monster is pushed to the point $2$; choose the point $2$, the first monster dies from a crusher trap at the point $0$, the second monster dies from the explosion.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
import math
from collections import defaultdict
from itertools import combinations
from itertools import permutations
input = lambda : sys.stdin.readline().rstrip()
read = lambda : map(int, input().split())
def write(*args, sep="\n"):
for i in args:
sys.stdout.write("{}".format(i) + sep)
INF = float('inf')
MOD = int(1e9 + 7)
for q in range(int(input())):
n, r = read()
arr = sorted(set(read()), reverse=True)
s = 0
ans = 0
for i in arr:
if s >= i:
break
s += r
ans += 1
write(ans)
``` | vfc_19376 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1238/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n3 2\n1 3 5\n4 1\n5 2 3 5\n",
"output": "2\n2\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2408 | Solve the following coding problem using the programming language python:
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $(x_i, y_i)$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 50$) — the number of electric poles.
Each of the following $n$ lines contains two integers $x_i$, $y_i$ ($-10^4 \le x_i, y_i \le 10^4$) — the coordinates of the poles.
It is guaranteed that all of these $n$ points are distinct.
-----Output-----
Print a single integer — the number of pairs of wires that are intersecting.
-----Examples-----
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
-----Note-----
In the first example: [Image]
In the second example: [Image]
Note that the three poles $(0, 0)$, $(0, 2)$ and $(0, 4)$ are connected by a single wire.
In the third example: [Image]
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n = int(input())
T = []
for i in range(n):
x, y = map(int, input().split())
T.append([x, y])
P = []
for i in range(n):
for j in range(i + 1, n):
x1, y1 = T[i]
x2, y2 = T[j]
a = y2 - y1
b = x1 - x2
c = -(a * x1 + b * y1)
P.append([a, b, c])
const = 10 ** 10 + 3
cnt = 0
newP = []
visit = []
for a, b, c in P:
if a != 0:
if [1, b / a, c / a] not in visit:
newP.append([1, b / a, c / a])
visit.append([1, b / a, c / a])
else:
if [0, 1, c / b] not in visit:
newP.append([0, 1, c / b])
visit.append([0, 1, c / b])
P = newP
for i in range(len(P)):
for j in range(i + 1, len(P)):
a1, b1, c1 = P[i]
a2, b2, c2 = P[j]
if a1 * b2 == a2 * b1:
pass
else:
x = (b1 * c2 - b2 * c1) / (a1 * b2 - b1 * a2)
y = (c1 * a2 - a1 * c2) / (a1 * b2 - b1 * a2)
cnt += 1
print(cnt)
``` | vfc_19380 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1163/C1",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n0 0\n1 1\n0 3\n1 2",
"output": "14\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n0 0\n0 2\n0 4\n2 0\n",
"output": "6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n-1 -1\n1 0\n3 1",
"output": "0\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2409 | Solve the following coding problem using the programming language python:
The only difference between easy and hard versions is on constraints. In this version constraints are higher. You can make hacks only if all versions of the problem are solved.
Koa the Koala is at the beach!
The beach consists (from left to right) of a shore, $n+1$ meters of sea and an island at $n+1$ meters from the shore.
She measured the depth of the sea at $1, 2, \dots, n$ meters from the shore and saved them in array $d$. $d_i$ denotes the depth of the sea at $i$ meters from the shore for $1 \le i \le n$.
Like any beach this one has tide, the intensity of the tide is measured by parameter $k$ and affects all depths from the beginning at time $t=0$ in the following way:
For a total of $k$ seconds, each second, tide increases all depths by $1$.
Then, for a total of $k$ seconds, each second, tide decreases all depths by $1$.
This process repeats again and again (ie. depths increase for $k$ seconds then decrease for $k$ seconds and so on ...).
Formally, let's define $0$-indexed array $p = [0, 1, 2, \ldots, k - 2, k - 1, k, k - 1, k - 2, \ldots, 2, 1]$ of length $2k$. At time $t$ ($0 \le t$) depth at $i$ meters from the shore equals $d_i + p[t \bmod 2k]$ ($t \bmod 2k$ denotes the remainder of the division of $t$ by $2k$). Note that the changes occur instantaneously after each second, see the notes for better understanding.
At time $t=0$ Koa is standing at the shore and wants to get to the island. Suppose that at some time $t$ ($0 \le t$) she is at $x$ ($0 \le x \le n$) meters from the shore:
In one second Koa can swim $1$ meter further from the shore ($x$ changes to $x+1$) or not swim at all ($x$ stays the same), in both cases $t$ changes to $t+1$.
As Koa is a bad swimmer, the depth of the sea at the point where she is can't exceed $l$ at integer points of time (or she will drown). More formally, if Koa is at $x$ ($1 \le x \le n$) meters from the shore at the moment $t$ (for some integer $t\ge 0$), the depth of the sea at this point — $d_x + p[t \bmod 2k]$ — can't exceed $l$. In other words, $d_x + p[t \bmod 2k] \le l$ must hold always.
Once Koa reaches the island at $n+1$ meters from the shore, she stops and can rest.
Note that while Koa swims tide doesn't have effect on her (ie. she can't drown while swimming). Note that Koa can choose to stay on the shore for as long as she needs and neither the shore or the island are affected by the tide (they are solid ground and she won't drown there).
Koa wants to know whether she can go from the shore to the island. Help her!
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains three integers $n$, $k$ and $l$ ($1 \le n \le 3 \cdot 10^5; 1 \le k \le 10^9; 1 \le l \le 10^9$) — the number of meters of sea Koa measured and parameters $k$ and $l$.
The second line of each test case contains $n$ integers $d_1, d_2, \ldots, d_n$ ($0 \le d_i \le 10^9$) — the depths of each meter of sea Koa measured.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case:
Print Yes if Koa can get from the shore to the island, and No otherwise.
You may print each letter in any case (upper or lower).
-----Example-----
Input
7
2 1 1
1 0
5 2 3
1 2 3 2 2
4 3 4
0 2 4 3
2 3 5
3 0
7 2 3
3 0 2 1 3 0 1
7 1 4
4 4 3 0 2 4 2
5 2 3
1 2 3 2 2
Output
Yes
No
Yes
Yes
Yes
No
No
-----Note-----
In the following $s$ denotes the shore, $i$ denotes the island, $x$ denotes distance from Koa to the shore, the underline denotes the position of Koa, and values in the array below denote current depths, affected by tide, at $1, 2, \dots, n$ meters from the shore.
In test case $1$ we have $n = 2, k = 1, l = 1, p = [ 0, 1 ]$.
Koa wants to go from shore (at $x = 0$) to the island (at $x = 3$). Let's describe a possible solution:
Initially at $t = 0$ the beach looks like this: $[\underline{s}, 1, 0, i]$. At $t = 0$ if Koa would decide to swim to $x = 1$, beach would look like: $[s, \underline{2}, 1, i]$ at $t = 1$, since $2 > 1$ she would drown. So Koa waits $1$ second instead and beach looks like $[\underline{s}, 2, 1, i]$ at $t = 1$. At $t = 1$ Koa swims to $x = 1$, beach looks like $[s, \underline{1}, 0, i]$ at $t = 2$. Koa doesn't drown because $1 \le 1$. At $t = 2$ Koa swims to $x = 2$, beach looks like $[s, 2, \underline{1}, i]$ at $t = 3$. Koa doesn't drown because $1 \le 1$. At $t = 3$ Koa swims to $x = 3$, beach looks like $[s, 1, 0, \underline{i}]$ at $t = 4$. At $t = 4$ Koa is at $x = 3$ and she made it!
We can show that in test case $2$ Koa can't get to the island.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
#
# ------------------------------------------------
# ____ _ Generatered using
# / ___| | |
# | | __ _ __| | ___ _ __ ______ _
# | | / _` |/ _` |/ _ \ '_ \|_ / _` |
# | |__| (_| | (_| | __/ | | |/ / (_| |
# \____\____|\____|\___|_| |_/___\____|
#
# GNU Affero General Public License v3.0
# ------------------------------------------------
# Author : prophet
# Created : 2020-07-24 11:19:20.229238
# UUID : dFs0Ek0q78tkOXbf
# ------------------------------------------------
#
production = True
import sys, math, collections
def input(input_format = 0, multi = 0):
if multi > 0: return [input(input_format) for i in range(multi)]
else:
next_line = sys.stdin.readline()[:-1]
if input_format >= 10:
use_list = False
input_format = int(str(input_format)[-1])
else: use_list = True
if input_format == 0: formatted_input = [next_line]
elif input_format == 1: formatted_input = list(map(int, next_line.split()))
elif input_format == 2: formatted_input = list(map(float, next_line.split()))
elif input_format == 3: formatted_input = list(next_line)
elif input_format == 4: formatted_input = list(map(int, list(next_line)))
elif input_format == 5: formatted_input = next_line.split()
else: formatted_input = [next_line]
return formatted_input if use_list else formatted_input[0]
def out(output_line, output_format = 0, newline = True):
formatted_output = ""
if output_format == 0: formatted_output = str(output_line)
elif output_format == 1: formatted_output = " ".join(map(str, output_line))
elif output_format == 2: formatted_output = "\n".join(map(str, output_line))
elif output_format == 3: formatted_output = "".join(map(str, output_line))
print(formatted_output, end = "\n" if newline else "")
def log(*args):
if not production:
print("$$$", end = "")
print(*args)
enu = enumerate
ter = lambda a, b, c: b if a else c
ceil = lambda a, b: -(-a // b)
flip = lambda a: (a + 1) & 1
def mapl(iterable, format = 0):
if format == 0: return list(map(int, iterable))
elif format == 1: return list(map(str, iterable))
elif format == 2: return list(map(list, iterable))
#
# >>>>>>>>>>>>>>> START OF SOLUTION <<<<<<<<<<<<<<
#
def solve():
n, k, l = input(1)
d = input(1)
log(k, l)
log(d)
f = [l - i for i in d]
log(f)
p = [(0, 2 * k - 1)]
for i in f:
a, b = p[-1]
if i >= k:
p.append((0, 2 * k - 1))
else:
fb = k + i
fa = max(a + 1, k - i)
log(i, fb, fa)
if fb < fa:
out("No")
return
p.append((fa, fb))
log(p)
else:
out("Yes")
log("")
return
for i in range(input(11)): solve()
#
# >>>>>>>>>>>>>>>> END OF SOLUTION <<<<<<<<<<<<<<<
#
``` | vfc_19384 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1384/B2",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7\n2 1 1\n1 0\n5 2 3\n1 2 3 2 2\n4 3 4\n0 2 4 3\n2 3 5\n3 0\n7 2 3\n3 0 2 1 3 0 1\n7 1 4\n4 4 3 0 2 4 2\n5 2 3\n1 2 3 2 2\n",
"output": "Yes\nNo\nYes\nYes\nYes\nNo\nNo\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n100 5 4\n0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 2 0 0 0\n",
"output": "No\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n1 1 1\n1\n1 1 1\n0\n1 1 1\n2\n1 1 1\n3\n1 1 1\n4\n2 2 2\n2 3\n2 1 2\n1 1\n2 1 2\n0 0\n14 3 9\n1 3 2 3 3 2 4 2 1 3 6 7 10 1\n7 1 1\n1 1 1 1 1 1 1\n",
"output": "Yes\nYes\nNo\nNo\nNo\nNo\nYes\nYes\nNo\nNo\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2410 | Solve the following coding problem using the programming language python:
Today, Yasser and Adel are at the shop buying cupcakes. There are $n$ cupcake types, arranged from $1$ to $n$ on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type $i$ is an integer $a_i$. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative.
Yasser, of course, wants to try them all, so he will buy exactly one cupcake of each type.
On the other hand, Adel will choose some segment $[l, r]$ $(1 \le l \le r \le n)$ that does not include all of cupcakes (he can't choose $[l, r] = [1, n]$) and buy exactly one cupcake of each of types $l, l + 1, \dots, r$.
After that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice.
For example, let the tastinesses of the cupcakes be $[7, 4, -1]$. Yasser will buy all of them, the total tastiness will be $7 + 4 - 1 = 10$. Adel can choose segments $[7], [4], [-1], [7, 4]$ or $[4, -1]$, their total tastinesses are $7, 4, -1, 11$ and $3$, respectively. Adel can choose segment with tastiness $11$, and as $10$ is not strictly greater than $11$, Yasser won't be happy :(
Find out if Yasser will be happy after visiting the shop.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains $n$ ($2 \le n \le 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$), where $a_i$ represents the tastiness of the $i$-th type of cupcake.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print "YES", if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice. Otherwise, print "NO".
-----Example-----
Input
3
4
1 2 3 4
3
7 4 -1
3
5 -5 5
Output
YES
NO
NO
-----Note-----
In the first example, the total tastiness of any segment Adel can choose is less than the total tastiness of all cupcakes.
In the second example, Adel will choose the segment $[1, 2]$ with total tastiness $11$, which is not less than the total tastiness of all cupcakes, which is $10$.
In the third example, Adel can choose the segment $[3, 3]$ with total tastiness of $5$. Note that Yasser's cupcakes' total tastiness is also $5$, so in that case, the total tastiness of Yasser's cupcakes isn't strictly greater than the total tastiness of Adel's cupcakes.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
input()
l = list(map(int,input().split()))
bad = False
curr = 0
for v in l:
curr += v
if curr <= 0:
bad = True
break
l.reverse()
curr = 0
for v in l:
curr += v
if curr <= 0:
bad = True
break
if bad:
print('NO')
else:
print('YES')
``` | vfc_19388 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1285/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n4\n1 2 3 4\n3\n7 4 -1\n3\n5 -5 5\n",
"output": "YES\nNO\nNO\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "12\n3\n7 -4 5\n3\n7 -5 4\n3\n4 -5 7\n3\n5 -4 7\n4\n5 -5 -6 6\n5\n100 100 100 -50 50\n10\n10 5 -12 7 -10 20 30 -10 50 60\n10\n10 5 -14 7 -7 20 30 -50 50 60\n2\n1 1\n2\n-1 -1\n3\n1000000000 1000000000 1000000000\n3\n-1000000000 -1000000000 -1000000000\n",
"output": "YES\nNO\nNO\nYES\nNO\nNO\nNO\nYES\nYES\nNO\nYES\nNO\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n2\n0 0\n",
"output": "NO\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2411 | Solve the following coding problem using the programming language python:
This problem is same as the previous one, but has larger constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $(x_i, y_i)$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 1000$) — the number of electric poles.
Each of the following $n$ lines contains two integers $x_i$, $y_i$ ($-10^4 \le x_i, y_i \le 10^4$) — the coordinates of the poles.
It is guaranteed that all of these $n$ points are distinct.
-----Output-----
Print a single integer — the number of pairs of wires that are intersecting.
-----Examples-----
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
-----Note-----
In the first example:
[Image]
In the second example:
[Image]
Note that the three poles $(0, 0)$, $(0, 2)$ and $(0, 4)$ are connected by a single wire.
In the third example:
[Image]
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
#Bhargey Mehta (Sophomore)
#DA-IICT, Gandhinagar
import sys, math, queue
#sys.stdin = open("input.txt", "r")
MOD = 10**9+7
n = int(input())
p = []
for i in range(n):
x, y = map(int, input().split())
p.append((x, y))
d = {}
for i in range(n):
x1, y1 = p[i]
for j in range(i+1, n):
x2, y2 = p[j]
if x1 != x2:
m = (y2-y1)/(x2-x1)
c = (y1*x2-x1*y2)/(x2-x1)
else:
m = 10**10
c = x1
if m in d:
if c in d[m]:
d[m][c] += 1
else:
d[m][c] = 1
else:
d[m] = {c: 1}
p = []
for m in d:
p.append(len(d[m]))
s = sum(p)
ans = 0
for x in p:
ans += x*(s-x)
print(ans//2)
``` | vfc_19392 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1163/C2",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n0 0\n1 1\n0 3\n1 2",
"output": "14\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2412 | Solve the following coding problem using the programming language python:
A telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388 and 80000011223388 are not.
You are given a string $s$ of length $n$, consisting of digits.
In one operation you can delete any character from string $s$. For example, it is possible to obtain strings 112, 111 or 121 from string 1121.
You need to determine whether there is such a sequence of operations (possibly empty), after which the string $s$ becomes a telephone number.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 100$) — the length of string $s$.
The second line of each test case contains the string $s$ ($|s| = n$) consisting of digits.
-----Output-----
For each test print one line.
If there is a sequence of operations, after which $s$ becomes a telephone number, print YES.
Otherwise, print NO.
-----Example-----
Input
2
13
7818005553535
11
31415926535
Output
YES
NO
-----Note-----
In the first test case you need to delete the first and the third digits. Then the string 7818005553535 becomes 88005553535.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
n = int(input())
s = input()
for i in range(len(s)-10):
if(s[i] == '8'):
print("YES")
break
else:
print("NO")
``` | vfc_19396 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1167/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n13\n7818005553535\n11\n31415926535\n",
"output": "YES\nNO\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n11\n80000000000\n",
"output": "YES\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n11\n83583640644\n",
"output": "YES\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2413 | Solve the following coding problem using the programming language python:
Nikolay lives in a two-storied house. There are $n$ rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between $1$ and $n$).
If Nikolay is currently in some room, he can move to any of the neighbouring rooms (if they exist). Rooms with numbers $i$ and $i+1$ on each floor are neighbouring, for all $1 \leq i \leq n - 1$. There may also be staircases that connect two rooms from different floors having the same numbers. If there is a staircase connecting the room $x$ on the first floor and the room $x$ on the second floor, then Nikolay can use it to move from one room to another.
$\text{floor}$ The picture illustrates a house with $n = 4$. There is a staircase between the room $2$ on the first floor and the room $2$ on the second floor, and another staircase between the room $4$ on the first floor and the room $4$ on the second floor. The arrows denote possible directions in which Nikolay can move. The picture corresponds to the string "0101" in the input.
Nikolay wants to move through some rooms in his house. To do this, he firstly chooses any room where he starts. Then Nikolay moves between rooms according to the aforementioned rules. Nikolay never visits the same room twice (he won't enter a room where he has already been).
Calculate the maximum number of rooms Nikolay can visit during his tour, if:
he can start in any room on any floor of his choice, and he won't visit the same room twice.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 100$) — the number of test cases in the input. Then test cases follow. Each test case consists of two lines.
The first line contains one integer $n$ $(1 \le n \le 1\,000)$ — the number of rooms on each floor.
The second line contains one string consisting of $n$ characters, each character is either a '0' or a '1'. If the $i$-th character is a '1', then there is a staircase between the room $i$ on the first floor and the room $i$ on the second floor. If the $i$-th character is a '0', then there is no staircase between the room $i$ on the first floor and the room $i$ on the second floor.
In hacks it is allowed to use only one test case in the input, so $t = 1$ should be satisfied.
-----Output-----
For each test case print one integer — the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won't visit the same room twice.
-----Example-----
Input
4
5
00100
8
00000000
5
11111
3
110
Output
6
8
10
6
-----Note-----
In the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then — to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then — to the fifth room on the second floor. So, Nikolay visits $6$ rooms.
There are no staircases in the second test case, so Nikolay can only visit all rooms on the same floor (if he starts in the leftmost or in the rightmost room).
In the third test case it is possible to visit all rooms: first floor, first room $\rightarrow$ second floor, first room $\rightarrow$ second floor, second room $\rightarrow$ first floor, second room $\rightarrow$ first floor, third room $\rightarrow$ second floor, third room $\rightarrow$ second floor, fourth room $\rightarrow$ first floor, fourth room $\rightarrow$ first floor, fifth room $\rightarrow$ second floor, fifth room.
In the fourth test case it is also possible to visit all rooms: second floor, third room $\rightarrow$ second floor, second room $\rightarrow$ second floor, first room $\rightarrow$ first floor, first room $\rightarrow$ first floor, second room $\rightarrow$ first floor, third room.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
# Contest: Codeforces Round #592 (Div. 2) (https://codeforces.com/contest/1244)
# Problem: B: Rooms and Staircases (https://codeforces.com/contest/1244/problem/B)
def rint():
return int(input())
def rints():
return list(map(int, input().split()))
t = rint()
for _ in range(t):
n = rint()
s = input()
mx = n
for i in range(n):
if s[i] == '0':
continue
mx = max(mx, 2 * max(i + 1, n - i))
print(mx)
``` | vfc_19400 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1244/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n5\n00100\n8\n00000000\n5\n11111\n3\n110\n",
"output": "6\n8\n10\n6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "14\n1\n0\n1\n1\n2\n00\n2\n01\n2\n10\n2\n11\n3\n000\n3\n001\n3\n010\n3\n011\n3\n100\n3\n101\n3\n110\n3\n111\n",
"output": "1\n2\n2\n4\n4\n4\n3\n6\n4\n6\n6\n6\n6\n6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n",
"output": "100\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2414 | Solve the following coding problem using the programming language python:
You are given two integers $a$ and $b$. Print $a+b$.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow.
Each test case is given as a line of two integers $a$ and $b$ ($-1000 \le a, b \le 1000$).
-----Output-----
Print $t$ integers — the required numbers $a+b$.
-----Example-----
Input
4
1 5
314 15
-99 99
123 987
Output
6
329
0
1110
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
ar = list(map(int, input().split()))
print(ar[0] + ar[1])
``` | vfc_19404 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1351/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 5\n314 15\n-99 99\n123 987\n",
"output": "6\n329\n0\n1110\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n147 555\n",
"output": "702\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n233 233\n",
"output": "466\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2415 | Solve the following coding problem using the programming language python:
-----Input-----
The input consists of a single string of uppercase letters A-Z. The length of the string is between 1 and 10 characters, inclusive.
-----Output-----
Output "YES" or "NO".
-----Examples-----
Input
GENIUS
Output
YES
Input
DOCTOR
Output
NO
Input
IRENE
Output
YES
Input
MARY
Output
NO
Input
SMARTPHONE
Output
NO
Input
REVOLVER
Output
YES
Input
HOLMES
Output
NO
Input
WATSON
Output
YES
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
k = '''
H
He
Li
Be
B
C
N
O
F
Ne
Na
Mg
Al
Si
P
S
Cl
Ar
K
Ca
Sc
Ti
V
Cr
Mn
Fe
Co
Ni
Cu
Zn
Ga
Ge
As
Se
Br
Kr
5
Rb
Sr
Y
Zr
Nb
Mo
Tc
Ru
Rh
Pd
Ag
Cd
In
Sn
Sb
Te
I
Xe
Cs
Ba
La
Ce
Pr
Nd
Pm
Sm
Eu
Gd
Tb
Dy
Ho
Er
Tm
Yb
Lu
Hf
Ta
W
Re
Os
Ir
Pt
Au
Hg
Tl
Pb
Bi
Po
At
Rn
Fr
Ra
Ac
Th
Pa
U
Np
Pu
Am
Cm
Bk
Cf
Es
Fm
Md
No
Lr
Rf
Db
Sg
Bh
Hs
Mt
Ds
Rg
Cn
Nh
Fl
Mc
Lv
Ts
Og
Uue
Ubn
Ubu
Ubb
Ubt
Ubq
Ubp
Ubh
Ubs
'''.upper().split()
s = '0' + input()
dp = [0] * (len(s))
dp[0] = 1
for i in range(1, len(s)):
for j in range(1, 3):
if s[i - j + 1: i + 1] in k: dp[i] |= dp[i - j]
print("YES" if dp[-1] else "NO")
``` | vfc_19408 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1331/F",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "GENIUS\n",
"output": "",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "DOCTOR\n",
"output": "",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "IRENE\n",
"output": "",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "MARY\n",
"output": "",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "SMARTPHONE\n",
"output": "",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2416 | Solve the following coding problem using the programming language python:
Ksenia has an array $a$ consisting of $n$ positive integers $a_1, a_2, \ldots, a_n$.
In one operation she can do the following: choose three distinct indices $i$, $j$, $k$, and then change all of $a_i, a_j, a_k$ to $a_i \oplus a_j \oplus a_k$ simultaneously, where $\oplus$ denotes the bitwise XOR operation.
She wants to make all $a_i$ equal in at most $n$ operations, or to determine that it is impossible to do so. She wouldn't ask for your help, but please, help her!
-----Input-----
The first line contains one integer $n$ ($3 \leq n \leq 10^5$) — the length of $a$.
The second line contains $n$ integers, $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — elements of $a$.
-----Output-----
Print YES or NO in the first line depending on whether it is possible to make all elements equal in at most $n$ operations.
If it is possible, print an integer $m$ ($0 \leq m \leq n$), which denotes the number of operations you do.
In each of the next $m$ lines, print three distinct integers $i, j, k$, representing one operation.
If there are many such operation sequences possible, print any. Note that you do not have to minimize the number of operations.
-----Examples-----
Input
5
4 2 1 7 2
Output
YES
1
1 3 4
Input
4
10 4 49 22
Output
NO
-----Note-----
In the first example, the array becomes $[4 \oplus 1 \oplus 7, 2, 4 \oplus 1 \oplus 7, 4 \oplus 1 \oplus 7, 2] = [2, 2, 2, 2, 2]$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def solve(n, arr):
xor_sum = arr[0]
for i in range(1, n):
xor_sum ^= arr[i]
if n % 2 == 0:
if xor_sum:
print("NO")
return
else:
n -= 1
if n == 3:
print(1)
print(1, 2, 3)
return
print("YES")
print(n-2)
for i in range(1, n-1, 2):
print(i, i+1, i+2)
for i in range(n-4, 0, -2):
print(i, i+1, i+2)
n = int(input())
arr = list(map(int, input().split()))
solve(n, arr)
``` | vfc_19412 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1438/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n4 2 1 7 2\n",
"output": "YES\n3\n1 2 3\n3 4 5\n1 2 3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n10 4 49 22\n",
"output": "NO\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2417 | Solve the following coding problem using the programming language python:
Consider a tunnel on a one-way road. During a particular day, $n$ cars numbered from $1$ to $n$ entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.
A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the tunnel exit. Perfectly balanced.
Thanks to the cameras, the order in which the cars entered and exited the tunnel is known. No two cars entered or exited at the same time.
Traffic regulations prohibit overtaking inside the tunnel. If car $i$ overtakes any other car $j$ inside the tunnel, car $i$ must be fined. However, each car can be fined at most once.
Formally, let's say that car $i$ definitely overtook car $j$ if car $i$ entered the tunnel later than car $j$ and exited the tunnel earlier than car $j$. Then, car $i$ must be fined if and only if it definitely overtook at least one other car.
Find the number of cars that must be fined.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 10^5$), denoting the number of cars.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$), denoting the ids of cars in order of entering the tunnel. All $a_i$ are pairwise distinct.
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$), denoting the ids of cars in order of exiting the tunnel. All $b_i$ are pairwise distinct.
-----Output-----
Output the number of cars to be fined.
-----Examples-----
Input
5
3 5 2 1 4
4 3 2 5 1
Output
2
Input
7
5 2 3 6 7 1 4
2 3 6 7 1 4 5
Output
6
Input
2
1 2
1 2
Output
0
-----Note-----
The first example is depicted below:
[Image]
Car $2$ definitely overtook car $5$, while car $4$ definitely overtook cars $1$, $2$, $3$ and $5$. Cars $2$ and $4$ must be fined.
In the second example car $5$ was definitely overtaken by all other cars.
In the third example no car must be fined.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n=int(input())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
fined=0
i=0
j=0
d1={}
while i<n and j<n:
if l1[i] in d1:
i+=1
continue
if l1[i]==l2[j]:
i+=1
j+=1
else :
while j<n and l2[j]!=l1[i]:
d1[l2[j]]=1
j+=1
fined+=1
print(fined)
``` | vfc_19416 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1237/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n3 5 2 1 4\n4 3 2 5 1\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n5 2 3 6 7 1 4\n2 3 6 7 1 4 5\n",
"output": "6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1 2\n1 2\n",
"output": "0\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2418 | Solve the following coding problem using the programming language python:
You are given a sequence of $n$ integers $a_1, a_2, \ldots, a_n$.
You have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\leq i\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1<i\leq n$, $b_i\geq b_{i-1}$ must hold $c$ is non-increasing, which means that for every $1<i\leq n$, $c_i\leq c_{i-1}$ must hold
You have to minimize $\max(b_i,c_i)$. In other words, you have to minimize the maximum number in sequences $b$ and $c$.
Also there will be $q$ changes, the $i$-th change is described by three integers $l,r,x$. You should add $x$ to $a_l,a_{l+1}, \ldots, a_r$.
You have to find the minimum possible value of $\max(b_i,c_i)$ for the initial sequence and for sequence after each change.
-----Input-----
The first line contains an integer $n$ ($1\leq n\leq 10^5$).
The secound line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq i\leq n$, $-10^9\leq a_i\leq 10^9$).
The third line contains an integer $q$ ($1\leq q\leq 10^5$).
Each of the next $q$ lines contains three integers $l,r,x$ ($1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$), desribing the next change.
-----Output-----
Print $q+1$ lines.
On the $i$-th ($1 \leq i \leq q+1$) line, print the answer to the problem for the sequence after $i-1$ changes.
-----Examples-----
Input
4
2 -1 7 3
2
2 4 -3
3 4 2
Output
5
5
6
Input
6
-9 -10 -9 -6 -5 4
3
2 6 -9
1 2 -10
4 6 -3
Output
3
3
3
1
Input
1
0
2
1 1 -1
1 1 -1
Output
0
0
-1
-----Note-----
In the first test: The initial sequence $a = (2, -1, 7, 3)$. Two sequences $b=(-3,-3,5,5),c=(5,2,2,-2)$ is a possible choice. After the first change $a = (2, -4, 4, 0)$. Two sequences $b=(-3,-3,5,5),c=(5,-1,-1,-5)$ is a possible choice. After the second change $a = (2, -4, 6, 2)$. Two sequences $b=(-4,-4,6,6),c=(6,0,0,-4)$ is a possible choice.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
from sys import stdout
import random
from fractions import Fraction
from math import *
def input():
return sys.stdin.readline().strip()
def iinput():
return int(input())
def finput():
return float(input())
def tinput():
return input().split()
def linput():
return list(input())
def rinput():
return list(map(int, tinput()))
def fiinput():
return list(map(float, tinput()))
def rlinput():
return list(map(int, input().split()))
def trinput():
return tuple(rinput())
def srlinput():
return sorted(list(map(int, input().split())))
def NOYES(fl):
if fl:
print("NO")
else:
print("YES")
def YESNO(fl):
if fl:
print("YES")
else:
print("NO")
def main():
def answer(res):
print((res + 1) // 2)
n = iinput()
#k = iinput()
#m = iinput()
#n = int(sys.stdin.readline().strip())
#n, k = rinput()
#n, m = rinput()
#m, k = rinput()
#n, k, m = rinput()
#n, m, k = rinput()
#k, n, m = rinput()
#k, m, n = rinput()
#m, k, n = rinput()
#m, n, k = rinput()
#q = srlinput()
#q = linput()
q = rlinput()
w = [0] + [q[i] - q[i - 1] for i in range(1, n)]
res = q[0] + sum([max(w[i], 0) for i in range(1, n)])
answer(res)
k = iinput()
for o in range(k):
l, r, x = rinput()
if l == 1:
res += x
else:
res = res - max(0, w[l - 1]) + max(0, w[l - 1] + x); w[l - 1] += x
if r != n:
res = res - max(0, w[r]) + max(0, w[r] - x); w[r] -= x
answer(res)
for i in range(1):
main()
``` | vfc_19420 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1406/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n2 -1 7 3\n2\n2 4 -3\n3 4 2\n",
"output": "5\n5\n6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n-9 -10 -9 -6 -5 4\n3\n2 6 -9\n1 2 -10\n4 6 -3\n",
"output": "3\n3\n3\n1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n0\n2\n1 1 -1\n1 1 -1\n",
"output": "0\n0\n-1\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2420 | Solve the following coding problem using the programming language python:
The Cybermen solved that first test much quicker than the Daleks. Luckily for us, the Daleks were angry (shocking!) and they destroyed some of the Cybermen.
After the fighting stopped, Heidi gave them another task to waste their time on.
There are $n$ points on a plane. Given a radius $r$, find the maximum number of points that can be covered by an $L^1$-ball with radius $r$.
An $L^1$-ball with radius $r$ and center $(x_0, y_0)$ in a 2D-plane is defined as the set of points $(x, y)$ such that the Manhattan distance between $(x_0, y_0)$ and $(x, y)$ is at most $r$.
Manhattan distance between $(x_0, y_0)$ and $(x, y)$ is defined as $|x - x_0| + |y - y_0|$.
-----Input-----
The first line contains two integers $n, r$ ($1 \le n \le 300\,000, 1 \le r \le 10^6$), the number of points and the radius of the ball, respectively.
Each of the next $n$ lines contains integers $x_i, y_i$ ($-10^6 \leq x_i, y_i \leq 10^6$), describing the coordinates of the $i$-th point.
It is guaranteed, that all points are distinct.
-----Output-----
Print one integer — the maximum number points that an $L^1$-ball with radius $r$ can cover.
-----Examples-----
Input
5 1
1 1
1 -1
-1 1
-1 -1
2 0
Output
3
Input
5 2
1 1
1 -1
-1 1
-1 -1
2 0
Output
5
-----Note-----
In the first example, a ball centered at $(1, 0)$ covers the points $(1, 1)$, $(1, -1)$, $(2, 0)$.
In the second example, a ball centered at $(0, 0)$ covers all the points.
Note that $x_0$ and $y_0$ need not be integer.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
NORM = 2000000
LIMIT = NORM * 2 + 1
class segmentTree:
def __init__(self, n):
self.n = n
self.t = [0] * (n * 2)
self.lazy = [0] * n
def apply(self, p, value):
self.t[p] += value
if p < self.n:
self.lazy[p] += value
def build(self, p):
while p > 1:
p >>= 1
self.t[p] = max(self.t[p * 2], self.t[p * 2 + 1]) + self.lazy[p]
def push(self, p):
log = 0
while (1 << log) <= self.n:
log += 1
for s in range(log, 0, -1):
parent = p >> s
if self.lazy[parent] != 0:
self.apply(parent * 2, self.lazy[parent])
self.apply(parent * 2 + 1, self.lazy[parent])
self.lazy[parent] = 0
def inc(self, l, r, value):
l += self.n
r += self.n
l0, r0 = l, r
while l < r:
if l & 1:
self.apply(l, value)
l += 1
if r & 1:
self.apply(r - 1, value)
r -= 1
l >>= 1
r >>= 1
self.build(l0)
self.build(r0 - 1)
def query(self, l, r):
l += self.n
r += self.n
self.push(l)
self.push(r - 1)
res = 0
while l < r:
if l & 1:
res = max(res, self.t[l])
l += 1
if r & 1:
res = max(res, self.t[r - 1])
r -= 1
l >>= 1
r >>= 1
return res
inp = [int(x) for x in sys.stdin.read().split()]
n, r = inp[0], inp[1]
inp_idx = 2
points = []
env = {}
for _ in range(n):
x, y = inp[inp_idx], inp[inp_idx + 1]
inp_idx += 2
new_x = x - y
new_y = x + y
new_x += NORM
new_y += NORM
if not new_y in env:
env[new_y] = []
env[new_y].append(new_x)
points.append([new_x, new_y])
sq_side = r * 2
tree = segmentTree(LIMIT)
ys = []
for y in list(env.keys()):
ys.append(y)
ys = sorted(ys)
ans = 0
last = 0
for i in range(len(ys)):
y = ys[i]
while i > last and ys[last] < y - sq_side:
low_y = ys[last]
for x in env[low_y]:
low_x = max(0, x - sq_side)
tree.inc(low_x, x + 1, -1)
last += 1
for x in env[y]:
low_x = max(0, x - sq_side)
tree.inc(low_x, x + 1, +1)
ans = max(ans, tree.query(0, LIMIT))
print(ans)
``` | vfc_19428 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1184/C2",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 1\n1 1\n1 -1\n-1 1\n-1 -1\n2 0\n",
"output": "3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 2\n1 1\n1 -1\n-1 1\n-1 -1\n2 0\n",
"output": "5\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1000000\n1000000 -1000000\n",
"output": "1\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2421 | Solve the following coding problem using the programming language python:
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below. [Image]
Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image]
Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows.
The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon.
The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
-----Output-----
For each testcase output the smallest cost of a path from the origin to the given cell.
-----Example-----
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
-----Note-----
The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
input = sys.stdin.readline
def main():
x, y = list(map(int, input().split()))
clst = list(map(int, input().split()))
clst[0] = min(clst[0], clst[5] + clst[1])
clst[1] = min(clst[1], clst[0] + clst[2])
clst[2] = min(clst[2], clst[1] + clst[3])
clst[3] = min(clst[3], clst[2] + clst[4])
clst[4] = min(clst[4], clst[3] + clst[5])
clst[5] = min(clst[5], clst[4] + clst[0])
directions = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
ans = 10 ** 20
for i in range(6):
dx1, dy1 = directions[i]
dx2, dy2 = directions[(i + 1) % 6]
a = (x * dy2 - y * dx2) // (dx1 * dy2 - dy1 * dx2)
b = (x * dy1 - y * dx1) // (dx2 * dy1 - dy2 * dx1)
if a < 0 or b < 0:
continue
ans = min(ans, clst[i] * a + clst[(i + 1) % 6] * b)
print(ans)
for _ in range(int(input())):
main()
``` | vfc_19432 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1421/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n-3 1\n1 3 5 7 9 11\n1000000000 1000000000\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n",
"output": "18\n1000000000000000000\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n0 0\n1 2 3 4 5 6\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n-1000000000 -1000000000\n10000 20000 30000 40000 50000 60000\n",
"output": "40000000000000\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2422 | Solve the following coding problem using the programming language python:
Recently a new building with a new layout was constructed in Monocarp's hometown. According to this new layout, the building consists of three types of apartments: three-room, five-room, and seven-room apartments. It's also known that each room of each apartment has exactly one window. In other words, a three-room apartment has three windows, a five-room — five windows, and a seven-room — seven windows.
Monocarp went around the building and counted $n$ windows. Now he is wondering, how many apartments of each type the building may have.
Unfortunately, Monocarp only recently has learned to count, so he is asking you to help him to calculate the possible quantities of three-room, five-room, and seven-room apartments in the building that has $n$ windows. If there are multiple answers, you can print any of them.
Here are some examples:
if Monocarp has counted $30$ windows, there could have been $2$ three-room apartments, $2$ five-room apartments and $2$ seven-room apartments, since $2 \cdot 3 + 2 \cdot 5 + 2 \cdot 7 = 30$; if Monocarp has counted $67$ windows, there could have been $7$ three-room apartments, $5$ five-room apartments and $3$ seven-room apartments, since $7 \cdot 3 + 5 \cdot 5 + 3 \cdot 7 = 67$; if Monocarp has counted $4$ windows, he should have mistaken since no building with the aforementioned layout can have $4$ windows.
-----Input-----
Th first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The only line of each test case contains one integer $n$ ($1 \le n \le 1000$) — the number of windows in the building.
-----Output-----
For each test case, if a building with the new layout and the given number of windows just can't exist, print $-1$.
Otherwise, print three non-negative integers — the possible number of three-room, five-room, and seven-room apartments. If there are multiple answers, print any of them.
-----Example-----
Input
4
30
67
4
14
Output
2 2 2
7 5 3
-1
0 0 2
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def solve():
n = int(input())
if n % 3 == 0:
print(n//3,0,0)
return 0
if n % 3 == 1:
if n < 7:
print(-1)
else:
print((n-7)//3, 0, 1)
return 0
if n < 5:
print(-1)
return 0
print((n-5)//3, 1, 0)
for i in range(int(input())):
solve()
``` | vfc_19436 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1430/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n30\n67\n4\n14\n",
"output": "10 0 0\n20 0 1\n-1\n3 1 0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n556\n556\n",
"output": "183 0 1\n183 0 1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1\n",
"output": "-1\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2423 | Solve the following coding problem using the programming language python:
The Resistance is trying to take control over all planets in a particular solar system. This solar system is shaped like a tree. More precisely, some planets are connected by bidirectional hyperspace tunnels in such a way that there is a path between every pair of the planets, but removing any tunnel would disconnect some of them.
The Resistance already has measures in place that will, when the time is right, enable them to control every planet that is not remote. A planet is considered to be remote if it is connected to the rest of the planets only via a single hyperspace tunnel.
How much work is there left to be done: that is, how many remote planets are there?
-----Input-----
The first line of the input contains an integer N (2 ≤ N ≤ 1000) – the number of planets in the galaxy.
The next N - 1 lines describe the hyperspace tunnels between the planets. Each of the N - 1 lines contains two space-separated integers u and v (1 ≤ u, v ≤ N) indicating that there is a bidirectional hyperspace tunnel between the planets u and v. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets.
-----Output-----
A single integer denoting the number of remote planets.
-----Examples-----
Input
5
4 1
4 2
1 3
1 5
Output
3
Input
4
1 2
4 3
1 4
Output
2
-----Note-----
In the first example, only planets 2, 3 and 5 are connected by a single tunnel.
In the second example, the remote planets are 2 and 3.
Note that this problem has only two versions – easy and medium.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
N = int(input())
cnt = [0] * N
for n in range(N - 1):
u, v = list(map(int, input().split()))
cnt[u - 1] += 1
cnt[v - 1] += 1
print(sum(x == 1 for x in cnt))
``` | vfc_19440 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/958/B1",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n4 1\n4 2\n1 3\n1 5\n",
"output": "3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n1 2\n4 3\n1 4\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n4 3\n2 6\n10 1\n5 7\n5 8\n10 6\n5 9\n9 3\n2 9\n",
"output": "4\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2424 | Solve the following coding problem using the programming language python:
Santa Claus has received letters from $n$ different kids throughout this year. Of course, each kid wants to get some presents from Santa: in particular, the $i$-th kid asked Santa to give them one of $k_i$ different items as a present. Some items could have been asked by multiple kids.
Santa is really busy, so he wants the New Year Bot to choose the presents for all children. Unfortunately, the Bot's algorithm of choosing presents is bugged. To choose a present for some kid, the Bot does the following:
choose one kid $x$ equiprobably among all $n$ kids; choose some item $y$ equiprobably among all $k_x$ items kid $x$ wants; choose a kid $z$ who will receive the present equipropably among all $n$ kids (this choice is independent of choosing $x$ and $y$); the resulting triple $(x, y, z)$ is called the decision of the Bot.
If kid $z$ listed item $y$ as an item they want to receive, then the decision valid. Otherwise, the Bot's choice is invalid.
Santa is aware of the bug, but he can't estimate if this bug is really severe. To do so, he wants to know the probability that one decision generated according to the aforementioned algorithm is valid. Can you help him?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 10^6$) — the number of kids who wrote their letters to Santa.
Then $n$ lines follow, the $i$-th of them contains a list of items wanted by the $i$-th kid in the following format: $k_i$ $a_{i, 1}$ $a_{i, 2}$ ... $a_{i, k_i}$ ($1 \le k_i, a_{i, j} \le 10^6$), where $k_i$ is the number of items wanted by the $i$-th kid, and $a_{i, j}$ are the items themselves. No item is contained in the same list more than once.
It is guaranteed that $\sum \limits_{i = 1}^{n} k_i \le 10^6$.
-----Output-----
Print the probatility that the Bot produces a valid decision as follows:
Let this probability be represented as an irreducible fraction $\frac{x}{y}$. You have to print $x \cdot y^{-1} \mod 998244353$, where $y^{-1}$ is the inverse element of $y$ modulo $998244353$ (such integer that $y \cdot y^{-1}$ has remainder $1$ modulo $998244353$).
-----Examples-----
Input
2
2 2 1
1 1
Output
124780545
Input
5
2 1 2
2 3 1
3 2 4 3
2 1 4
3 4 3 2
Output
798595483
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
readline = sys.stdin.readline
MOD = 998244353
N = int(readline())
wants = [tuple(map(int, readline().split()))[1:] for _ in range(N)]
Q = [0]*(10**6+1)
P = [0]*(10**6+1)
for i in range(N):
k = len(wants[i])
kinv = pow(k, MOD-2, MOD)
for w in wants[i]:
P[w] += 1
Q[w] = (Q[w] + kinv)%MOD
res = 0
for i in range(10**6+1):
res = (res+P[i]*Q[i])%MOD
print(pow(N**2, MOD-2, MOD)*res%MOD)
``` | vfc_19444 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1279/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n2 2 1\n1 1\n",
"output": "124780545\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n2 1 2\n2 3 1\n3 2 4 3\n2 1 4\n3 4 3 2\n",
"output": "798595483\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n5 48258 84644 992412 548310 132019\n5 132019 556600 548310 84644 992412\n6 132019 771663 523582 548310 463969 556600\n7 556600 132019 992412 523582 548310 70239 84644\n4 556600 548310 523582 463969\n7 548310 84644 771663 556600 132019 463969 48258\n5 556600 771663 463969 523582 84644\n6 48258 548310 84644 556600 523582 132019\n4 548310 523582 556600 132019\n5 992412 523582 556600 548310 84644\n",
"output": "820200343\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2425 | Solve the following coding problem using the programming language python:
Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.
Suppose you are given a positive integer $a$. You want to choose some integer $b$ from $1$ to $a - 1$ inclusive in such a way that the greatest common divisor (GCD) of integers $a \oplus b$ and $a \> \& \> b$ is as large as possible. In other words, you'd like to compute the following function:
$$f(a) = \max_{0 < b < a}{gcd(a \oplus b, a \> \& \> b)}.$$
Here $\oplus$ denotes the bitwise XOR operation, and $\&$ denotes the bitwise AND operation.
The greatest common divisor of two integers $x$ and $y$ is the largest integer $g$ such that both $x$ and $y$ are divided by $g$ without remainder.
You are given $q$ integers $a_1, a_2, \ldots, a_q$. For each of these integers compute the largest possible value of the greatest common divisor (when $b$ is chosen optimally).
-----Input-----
The first line contains an integer $q$ ($1 \le q \le 10^3$) — the number of integers you need to compute the answer for.
After that $q$ integers are given, one per line: $a_1, a_2, \ldots, a_q$ ($2 \le a_i \le 2^{25} - 1$) — the integers you need to compute the answer for.
-----Output-----
For each integer, print the answer in the same order as the integers are given in input.
-----Example-----
Input
3
2
3
5
Output
3
1
7
-----Note-----
For the first integer the optimal choice is $b = 1$, then $a \oplus b = 3$, $a \> \& \> b = 0$, and the greatest common divisor of $3$ and $0$ is $3$.
For the second integer one optimal choice is $b = 2$, then $a \oplus b = 1$, $a \> \& \> b = 2$, and the greatest common divisor of $1$ and $2$ is $1$.
For the third integer the optimal choice is $b = 2$, then $a \oplus b = 7$, $a \> \& \> b = 0$, and the greatest common divisor of $7$ and $0$ is $7$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
q = int(input())
def racunaj():
s = []
for i in range(27):
d = 2
n = 2**i - 1
z = 1
while d*d <= n:
if n % d == 0:
z = n // d
break
d += 1
s.append(z)
return s
najdel = racunaj()
# print(najdel)
for _q in range(q):
n = int(input())
z = 1
c = 1
while z < n:
z = 2*z+1
c += 1
if z > n:
print(z)
else:
print(najdel[c])
``` | vfc_19448 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1110/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n2\n3\n5\n",
"output": "3\n1\n7\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "9\n15\n7\n12\n122\n127\n99\n1999999\n255\n8388607\n",
"output": "5\n1\n15\n127\n1\n127\n2097151\n85\n178481\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n228\n",
"output": "255\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2426 | Solve the following coding problem using the programming language python:
You are given an array $a$ consisting of $n$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $2$) or determine that there is no such subset.
Both the given array and required subset may contain equal values.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 100$), number of test cases to solve. Descriptions of $t$ test cases follow.
A description of each test case consists of two lines. The first line contains a single integer $n$ ($1 \leq n \leq 100$), length of array $a$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 100$), elements of $a$. The given array $a$ can contain equal values (duplicates).
-----Output-----
For each test case output $-1$ if there is no such subset of elements. Otherwise output positive integer $k$, number of elements in the required subset. Then output $k$ distinct integers ($1 \leq p_i \leq n$), indexes of the chosen elements. If there are multiple solutions output any of them.
-----Example-----
Input
3
3
1 4 3
1
15
2
3 5
Output
1
2
-1
2
1 2
-----Note-----
There are three test cases in the example.
In the first test case, you can choose the subset consisting of only the second element. Its sum is $4$ and it is even.
In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.
In the third test case, the subset consisting of all array's elements has even sum.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
#list(map(int,input().split()))
t=int(input())
for i in range(t):
n=int(input())
it=list(map(int,input().split()))
if n==1 and it[0]%2==1:
print(-1)
else:
ind=-1
ind2=-1
ind3=-1
for j in range(n):
if it[j]%2==0:
ind=j
elif ind2==-1:
ind2=j
else:
ind3=j
if ind==-1:
print(2)
print(ind2+1,ind3+1)
else:
print(1)
print(ind+1)
``` | vfc_19452 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1323/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n3\n1 4 3\n1\n15\n2\n3 5\n",
"output": "1\n2\n-1\n2\n1 2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n2\n1 1\n",
"output": "2\n1 2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "14\n1\n2\n1\n1\n2\n2 2\n2\n2 1\n2\n1 2\n2\n1 1\n3\n2 2 2\n3\n2 2 1\n3\n2 1 2\n3\n2 1 1\n3\n1 2 2\n3\n1 2 1\n3\n1 1 2\n3\n1 1 1\n",
"output": "1\n1\n-1\n1\n1\n1\n1\n1\n2\n2\n1 2\n1\n1\n1\n1\n1\n1\n1\n1\n1\n2\n1\n2\n2\n1 2\n2\n1 2\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2427 | Solve the following coding problem using the programming language python:
Yurii is sure he can do everything. Can he solve this task, though?
He has an array $a$ consisting of $n$ positive integers. Let's call a subarray $a[l...r]$ good if the following conditions are simultaneously satisfied: $l+1 \leq r-1$, i. e. the subarray has length at least $3$; $(a_l \oplus a_r) = (a_{l+1}+a_{l+2}+\ldots+a_{r-2}+a_{r-1})$, where $\oplus$ denotes the bitwise XOR operation.
In other words, a subarray is good if the bitwise XOR of the two border elements is equal to the sum of the rest of the elements.
Yurii wants to calculate the total number of good subarrays. What is it equal to?
An array $c$ is a subarray of an array $d$ if $c$ can be obtained from $d$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
-----Input-----
The first line contains a single integer $n$ ($3 \leq n \leq 2\cdot 10^5$) — the length of $a$.
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \leq a_i \lt 2^{30}$) — elements of $a$.
-----Output-----
Output a single integer — the number of good subarrays.
-----Examples-----
Input
8
3 1 2 3 1 2 3 15
Output
6
Input
10
997230370 58052053 240970544 715275815 250707702 156801523 44100666 64791577 43523002 480196854
Output
2
-----Note-----
There are $6$ good subarrays in the example: $[3,1,2]$ (twice) because $(3 \oplus 2) = 1$; $[1,2,3]$ (twice) because $(1 \oplus 3) = 2$; $[2,3,1]$ because $(2 \oplus 1) = 3$; $[3,1,2,3,1,2,3,15]$ because $(3 \oplus 15) = (1+2+3+1+2+3)$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def solve(a):
seen = set()
for i in range(len(a)):
c = 0
for j in range(i+2,len(a)):
c += a[j-1]
if a[i]^a[j] == c:
seen.add((i,j))
if c >= 2*a[i]:
break
for i in range(len(a)-1,-1,-1):
c = 0
for j in range(i-2,-1,-1):
c += a[j+1]
if a[i]^a[j] == c:
seen.add((j,i))
if c >= 2 *a[i]:
break
print(len(seen))
n = int(input())
a = list(map(int,input().split()))
solve(a)
``` | vfc_19456 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1438/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "8\n3 1 2 3 1 2 3 15\n",
"output": "6",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n997230370 58052053 240970544 715275815 250707702 156801523 44100666 64791577 43523002 480196854\n",
"output": "2",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n512647258 29130422 386945218 423464437 1022947864 244293674 452 216 953 822\n",
"output": "0",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2428 | Solve the following coding problem using the programming language python:
You are given a string $s$. You can build new string $p$ from $s$ using the following operation no more than two times: choose any subsequence $s_{i_1}, s_{i_2}, \dots, s_{i_k}$ where $1 \le i_1 < i_2 < \dots < i_k \le |s|$; erase the chosen subsequence from $s$ ($s$ can become empty); concatenate chosen subsequence to the right of the string $p$ (in other words, $p = p + s_{i_1}s_{i_2}\dots s_{i_k}$).
Of course, initially the string $p$ is empty.
For example, let $s = \text{ababcd}$. At first, let's choose subsequence $s_1 s_4 s_5 = \text{abc}$ — we will get $s = \text{bad}$ and $p = \text{abc}$. At second, let's choose $s_1 s_2 = \text{ba}$ — we will get $s = \text{d}$ and $p = \text{abcba}$. So we can build $\text{abcba}$ from $\text{ababcd}$.
Can you build a given string $t$ using the algorithm above?
-----Input-----
The first line contains the single integer $T$ ($1 \le T \le 100$) — the number of test cases.
Next $2T$ lines contain test cases — two per test case. The first line contains string $s$ consisting of lowercase Latin letters ($1 \le |s| \le 400$) — the initial string.
The second line contains string $t$ consisting of lowercase Latin letters ($1 \le |t| \le |s|$) — the string you'd like to build.
It's guaranteed that the total length of strings $s$ doesn't exceed $400$.
-----Output-----
Print $T$ answers — one per test case. Print YES (case insensitive) if it's possible to build $t$ and NO (case insensitive) otherwise.
-----Example-----
Input
4
ababcd
abcba
a
b
defi
fed
xyz
x
Output
YES
NO
NO
YES
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def main():
T = int(input().strip())
for _ in range(T):
s = input().strip()
t = input().strip()
n = len(s)
find = [[n] * 26 for _ in range(n + 2)]
for i in range(n - 1, -1, -1):
find[i][:] = find[i + 1]
find[i][ord(s[i]) - ord("a")] = i
def interleaving(a, b):
dp = [n] * (len(b) + 1)
for i in range(len(a) + 1):
for j in range(len(b) + 1):
if i == j == 0:
dp[j] = -1
continue
res = n
if i > 0:
res = min(res, find[dp[j] + 1][ord(a[i - 1]) - ord("a")])
if j > 0:
res = min(res, find[dp[j - 1] + 1][ord(b[j - 1]) - ord("a")])
dp[j] = res
return dp[-1] < n
if any(interleaving(t[:i], t[i:]) for i in range(len(t))):
print("YES")
else:
print("NO")
main()
``` | vfc_19460 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1303/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\nababcd\nabcba\na\nb\ndefi\nfed\nxyz\nx\n",
"output": "YES\nNO\nNO\nYES\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2429 | Solve the following coding problem using the programming language python:
Phoenix has $n$ coins with weights $2^1, 2^2, \dots, 2^n$. He knows that $n$ is even.
He wants to split the coins into two piles such that each pile has exactly $\frac{n}{2}$ coins and the difference of weights between the two piles is minimized. Formally, let $a$ denote the sum of weights in the first pile, and $b$ denote the sum of weights in the second pile. Help Phoenix minimize $|a-b|$, the absolute value of $a-b$.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 100$) — the number of test cases.
The first line of each test case contains an integer $n$ ($2 \le n \le 30$; $n$ is even) — the number of coins that Phoenix has.
-----Output-----
For each test case, output one integer — the minimum possible difference of weights between the two piles.
-----Example-----
Input
2
2
4
Output
2
6
-----Note-----
In the first test case, Phoenix has two coins with weights $2$ and $4$. No matter how he divides the coins, the difference will be $4-2=2$.
In the second test case, Phoenix has four coins of weight $2$, $4$, $8$, and $16$. It is optimal for Phoenix to place coins with weights $2$ and $16$ in one pile, and coins with weights $4$ and $8$ in another pile. The difference is $(2+16)-(4+8)=6$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
n = int(input())
x1 = 0
x2 = 0
for i in range(1, n//2):
x1 += 2**i
for j in range(n//2, n):
x2 += 2**j
x1 += 2**n
print(abs(x1 - x2))
``` | vfc_19464 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1348/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n2\n4\n",
"output": "2\n6\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2430 | Solve the following coding problem using the programming language python:
Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is h_{i}. Liss wants to eat all nuts.
Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 ≤ i ≤ n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > h_{i} + 1.
Compute the minimal time (in seconds) required to eat all nuts.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 10^5) — the number of trees.
Next n lines contains the height of trees: i-th line contains an integer h_{i} (1 ≤ h_{i} ≤ 10^4) — the height of the tree with the number i.
-----Output-----
Print a single integer — the minimal time required to eat all nuts in seconds.
-----Examples-----
Input
2
1
2
Output
5
Input
5
2
1
2
1
1
Output
14
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
#python33
def program():
a=[]
t=0
n=int(input())
for i in range(n):
a.append(int(input()))
t=a[0]+1
for j in range(1,len(a)):
t+=abs(a[j]-a[j-1])+2
print (t)
program()
``` | vfc_19468 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/265/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1\n2\n",
"output": "5\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n2\n1\n2\n1\n1\n",
"output": "14\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1\n",
"output": "2\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2431 | Solve the following coding problem using the programming language python:
The Red Kingdom is attacked by the White King and the Black King!
The Kingdom is guarded by $n$ castles, the $i$-th castle is defended by $a_i$ soldiers. To conquer the Red Kingdom, the Kings have to eliminate all the defenders.
Each day the White King launches an attack on one of the castles. Then, at night, the forces of the Black King attack a castle (possibly the same one). Then the White King attacks a castle, then the Black King, and so on. The first attack is performed by the White King.
Each attack must target a castle with at least one alive defender in it. There are three types of attacks:
a mixed attack decreases the number of defenders in the targeted castle by $x$ (or sets it to $0$ if there are already less than $x$ defenders); an infantry attack decreases the number of defenders in the targeted castle by $y$ (or sets it to $0$ if there are already less than $y$ defenders); a cavalry attack decreases the number of defenders in the targeted castle by $z$ (or sets it to $0$ if there are already less than $z$ defenders).
The mixed attack can be launched at any valid target (at any castle with at least one soldier). However, the infantry attack cannot be launched if the previous attack on the targeted castle had the same type, no matter when and by whom it was launched. The same applies to the cavalry attack. A castle that was not attacked at all can be targeted by any type of attack.
The King who launches the last attack will be glorified as the conqueror of the Red Kingdom, so both Kings want to launch the last attack (and they are wise enough to find a strategy that allows them to do it no matter what are the actions of their opponent, if such strategy exists). The White King is leading his first attack, and you are responsible for planning it. Can you calculate the number of possible options for the first attack that allow the White King to launch the last attack? Each option for the first attack is represented by the targeted castle and the type of attack, and two options are different if the targeted castles or the types of attack are different.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Then, the test cases follow. Each test case is represented by two lines.
The first line contains four integers $n$, $x$, $y$ and $z$ ($1 \le n \le 3 \cdot 10^5$, $1 \le x, y, z \le 5$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^{18}$).
It is guaranteed that the sum of values of $n$ over all test cases in the input does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print the answer to it: the number of possible options for the first attack of the White King (or $0$, if the Black King can launch the last attack no matter how the White King acts).
-----Examples-----
Input
3
2 1 3 4
7 6
1 1 2 3
1
1 1 2 2
3
Output
2
3
0
Input
10
6 5 4 5
2 3 2 3 1 3
1 5 2 3
10
4 4 2 3
8 10 8 5
2 2 1 4
8 5
3 5 3 5
9 2 10
4 5 5 5
2 10 4 2
2 3 1 4
1 10
3 1 5 3
9 8 7
2 5 4 5
8 8
3 5 1 4
5 5 10
Output
0
2
1
2
5
12
5
0
0
2
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
input = sys.stdin.readline
G0=[[[[0]*5500 for i in range(6)] for j in range(6)] for k in range(6)]
G1=[[[[0]*5500 for i in range(6)] for j in range(6)] for k in range(6)]
G2=[[[[0]*5500 for i in range(6)] for j in range(6)] for k in range(6)]
for x in range(1,6):
for y in range(1,6):
for z in range(1,6):
for i in range(1,5500):
s=G0[x][y][z][max(0,i-x)]
t=G1[x][y][z][max(0,i-y)]
u=G2[x][y][z][max(0,i-z)]
for j in range(5):
if j==s or j==t or j==u:
continue
else:
G0[x][y][z][i]=j
break
for j in range(5):
if j==s or j==u:
continue
else:
G1[x][y][z][i]=j
break
for j in range(5):
if j==s or j==t:
continue
else:
G2[x][y][z][i]=j
break
def lcm(x, y):
return (x * y) // math.gcd(x, y)
t=int(input())
for tests in range(t):
n,x,y,z=list(map(int,input().split()))
B=list(map(int,input().split()))
A=[]
for a in B:
if a<=5400:
A.append(a)
else:
A.append(a%2520+2520)
XOR=0
for a in A:
XOR^=G0[x][y][z][a]
ANS=0
for a in A:
k=XOR^G0[x][y][z][a]
if G0[x][y][z][max(0,a-x)]==k:
ANS+=1
if G1[x][y][z][max(0,a-y)]==k:
ANS+=1
if G2[x][y][z][max(0,a-z)]==k:
ANS+=1
print(ANS)
``` | vfc_19472 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1312/F",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n2 1 3 4\n7 6\n1 1 2 3\n1\n1 1 2 2\n3\n",
"output": "2\n3\n0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n6 5 4 5\n2 3 2 3 1 3\n1 5 2 3\n10\n4 4 2 3\n8 10 8 5\n2 2 1 4\n8 5\n3 5 3 5\n9 2 10\n4 5 5 5\n2 10 4 2\n2 3 1 4\n1 10\n3 1 5 3\n9 8 7\n2 5 4 5\n8 8\n3 5 1 4\n5 5 10\n",
"output": "0\n2\n1\n2\n5\n12\n5\n0\n0\n2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n2 2 3 5\n4 5\n2 1 1 1\n1 2\n7 1 2 2\n3 5 3 1 4 5 5\n1 3 3 1\n2\n4 5 5 3\n1 3 4 3\n2 3 4 3\n4 2\n2 1 2 5\n3 1\n3 2 3 5\n2 1 3\n2 4 5 1\n4 4\n5 2 4 5\n5 5 5 2 1\n",
"output": "1\n6\n8\n2\n1\n2\n1\n2\n0\n3\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2432 | Solve the following coding problem using the programming language python:
-----Input-----
The input contains a single integer $a$ ($0 \le a \le 63$).
-----Output-----
Output a single number.
-----Examples-----
Input
2
Output
2
Input
5
Output
24
Input
35
Output
50
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n = int(input())
a = (n & 32) >> 5
b = (n & 16) >> 4
c = (n & 8) >> 3
d = (n & 4) >> 2
e = (n & 2) >> 1
f = n & 1
print(32 * a + 16 * f + 8 * d + 4 * c + 2 * e + b)
``` | vfc_19476 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1331/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n",
"output": "",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2433 | Solve the following coding problem using the programming language python:
There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburger you need two buns and a beef patty. To assemble a chicken burger you need two buns and a chicken cutlet.
You have $b$ buns, $p$ beef patties and $f$ chicken cutlets in your restaurant. You can sell one hamburger for $h$ dollars and one chicken burger for $c$ dollars. Calculate the maximum profit you can achieve.
You have to answer $t$ independent queries.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) – the number of queries.
The first line of each query contains three integers $b$, $p$ and $f$ ($1 \le b, ~p, ~f \le 100$) — the number of buns, beef patties and chicken cutlets in your restaurant.
The second line of each query contains two integers $h$ and $c$ ($1 \le h, ~c \le 100$) — the hamburger and chicken burger prices in your restaurant.
-----Output-----
For each query print one integer — the maximum profit you can achieve.
-----Example-----
Input
3
15 2 3
5 10
7 5 2
10 12
1 100 100
100 100
Output
40
34
0
-----Note-----
In first query you have to sell two hamburgers and three chicken burgers. Your income is $2 \cdot 5 + 3 \cdot 10 = 40$.
In second query you have to ell one hamburgers and two chicken burgers. Your income is $1 \cdot 10 + 2 \cdot 12 = 34$.
In third query you can not create any type of burgers because because you have only one bun. So your income is zero.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for i in range(int(input())):
a,b,c=map(int,input().split())
x,y=map(int,input().split())
ans=0
if x>y:
ans=ans+x*min(a//2,b)
a=(a-2*min(a//2,b))
ans=ans+y*min(a//2,c)
else :
ans=ans+y*min(a//2,c)
a=(a-2*min(a//2,c))
ans=ans+x*min(a//2,b)
print(ans)
``` | vfc_19480 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1207/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n15 2 3\n5 10\n7 5 2\n10 12\n1 100 100\n100 100\n",
"output": "40\n34\n0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n1 1 1\n1 1\n100 100 100\n100 100\n1 100 100\n100 100\n100 1 1\n100 100\n100 50 50\n100 99\n100 51 51\n100 99\n99 51 51\n100 99\n",
"output": "0\n5000\n0\n200\n5000\n5000\n4900\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n18 6 4\n3 4\n",
"output": "31\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2434 | Solve the following coding problem using the programming language python:
You are given two integers $n$ and $m$ ($m < n$). Consider a convex regular polygon of $n$ vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length). [Image] Examples of convex regular polygons
Your task is to say if it is possible to build another convex regular polygon with $m$ vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The next $t$ lines describe test cases. Each test case is given as two space-separated integers $n$ and $m$ ($3 \le m < n \le 100$) — the number of vertices in the initial polygon and the number of vertices in the polygon you want to build.
-----Output-----
For each test case, print the answer — "YES" (without quotes), if it is possible to build another convex regular polygon with $m$ vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon and "NO" otherwise.
-----Example-----
Input
2
6 3
7 3
Output
YES
NO
-----Note----- $0$ The first test case of the example
It can be shown that the answer for the second test case of the example is "NO".
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
t = int(input())
for tt in range(t):
x, y = map(int, input().split())
print('YES' if x % y == 0 else "NO")
``` | vfc_19484 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1312/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n6 3\n7 3\n",
"output": "YES\nNO\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n69 68\n",
"output": "NO\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n69 68\n11 9\n",
"output": "NO\nNO\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2435 | Solve the following coding problem using the programming language python:
You are given an array consisting of $n$ integers $a_1$, $a_2$, ..., $a_n$. Initially $a_x = 1$, all other elements are equal to $0$.
You have to perform $m$ operations. During the $i$-th operation, you choose two indices $c$ and $d$ such that $l_i \le c, d \le r_i$, and swap $a_c$ and $a_d$.
Calculate the number of indices $k$ such that it is possible to choose the operations so that $a_k = 1$ in the end.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Then the description of $t$ testcases follow.
The first line of each test case contains three integers $n$, $x$ and $m$ ($1 \le n \le 10^9$; $1 \le m \le 100$; $1 \le x \le n$).
Each of next $m$ lines contains the descriptions of the operations; the $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$).
-----Output-----
For each test case print one integer — the number of indices $k$ such that it is possible to choose the operations so that $a_k = 1$ in the end.
-----Example-----
Input
3
6 4 3
1 6
2 3
5 5
4 1 2
2 4
1 2
3 3 2
2 3
1 2
Output
6
2
3
-----Note-----
In the first test case, it is possible to achieve $a_k = 1$ for every $k$. To do so, you may use the following operations: swap $a_k$ and $a_4$; swap $a_2$ and $a_2$; swap $a_5$ and $a_5$.
In the second test case, only $k = 1$ and $k = 2$ are possible answers. To achieve $a_1 = 1$, you have to swap $a_1$ and $a_1$ during the second operation. To achieve $a_2 = 1$, you have to swap $a_1$ and $a_2$ during the second operation.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
t = int(input())
for _ in range(t):
n, x, m = list(map(int, input().split()))
left = x
right = x
for i in range(m):
l,r = list(map(int, input().split()))
if l < left <= r:
left = l
if r > right >= l:
right = r
print(right - left + 1)
``` | vfc_19488 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1366/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n6 4 3\n1 6\n2 3\n5 5\n4 1 2\n2 4\n1 2\n3 3 2\n2 3\n1 2\n",
"output": "6\n2\n3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "9\n100 50 6\n1 49\n51 100\n40 60\n30 70\n20 30\n70 80\n100 1 11\n1 1\n1 1\n1 1\n2 2\n2 100\n1 2\n2 3\n3 4\n5 6\n6 100\n4 5\n1 1 5\n1 1\n1 1\n1 1\n1 1\n1 1\n11 6 4\n6 6\n1 5\n7 11\n6 6\n3 3 2\n1 2\n2 3\n1000000000 1 1\n1 1000000000\n1000000000 1 1\n1 999999999\n1000000000 1 1\n2 1000000000\n1000000000 2 1\n1 1000000000\n",
"output": "61\n5\n1\n1\n2\n1000000000\n999999999\n1\n1000000000\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2436 | Solve the following coding problem using the programming language python:
Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.
She has $n$ friends who are also grannies (Maria is not included in this number). The $i$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $a_i$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $i$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $a_i$.
Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $1$). All the remaining $n$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $a_i$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $i$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $a_i$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance.
Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!
Consider an example: if $n=6$ and $a=[1,5,4,5,1,9]$, then: at the first step Maria can call grannies with numbers $1$ and $5$, each of them will see two grannies at the moment of going out into the yard (note that $a_1=1 \le 2$ and $a_5=1 \le 2$); at the second step, Maria can call grannies with numbers $2$, $3$ and $4$, each of them will see five grannies at the moment of going out into the yard (note that $a_2=5 \le 5$, $a_3=4 \le 5$ and $a_4=5 \le 5$); the $6$-th granny cannot be called into the yard — therefore, the answer is $6$ (Maria herself and another $5$ grannies).
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then test cases follow.
The first line of a test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of grannies (Maria is not included in this number).
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2\cdot10^5$).
It is guaranteed that the sum of the values $n$ over all test cases of the input does not exceed $10^5$.
-----Output-----
For each test case, print a single integer $k$ ($1 \le k \le n + 1$) — the maximum possible number of grannies in the courtyard.
-----Example-----
Input
4
5
1 1 2 2 1
6
2 3 4 5 6 7
6
1 5 4 5 1 9
5
1 2 3 5 6
Output
6
1
6
4
-----Note-----
In the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.
In the second test case in the example, no one can be in the yard, so Maria will remain there alone.
The third test case in the example is described in the details above.
In the fourth test case in the example, on the first step Maria can call grannies with numbers $1$, $2$ and $3$. If on the second step Maria calls $4$ or $5$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $4$-th granny or the $5$-th granny separately (one of them). If she calls both: $4$ and $5$, then when they appear, they will see $4+1=5$ grannies. Despite the fact that it is enough for the $4$-th granny, the $5$-th granny is not satisfied. So, Maria cannot call both the $4$-th granny and the $5$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
input = sys.stdin.readline
for f in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
a.sort()
mx=1
for i in range(n):
if a[i]<=i+1:
mx=i+2
print(mx)
``` | vfc_19492 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1358/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6\n",
"output": "6\n1\n6\n4\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "8\n1\n1\n1\n2\n1\n3\n1\n4\n1\n5\n1\n6\n1\n7\n1\n8\n",
"output": "2\n1\n1\n1\n1\n1\n1\n1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n2\n179 57\n2\n444 1329\n",
"output": "1\n1\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2438 | Solve the following coding problem using the programming language python:
The string $t_1t_2 \dots t_k$ is good if each letter of this string belongs to at least one palindrome of length greater than 1.
A palindrome is a string that reads the same backward as forward. For example, the strings A, BAB, ABBA, BAABBBAAB are palindromes, but the strings AB, ABBBAA, BBBA are not.
Here are some examples of good strings: $t$ = AABBB (letters $t_1$, $t_2$ belong to palindrome $t_1 \dots t_2$ and letters $t_3$, $t_4$, $t_5$ belong to palindrome $t_3 \dots t_5$); $t$ = ABAA (letters $t_1$, $t_2$, $t_3$ belong to palindrome $t_1 \dots t_3$ and letter $t_4$ belongs to palindrome $t_3 \dots t_4$); $t$ = AAAAA (all letters belong to palindrome $t_1 \dots t_5$);
You are given a string $s$ of length $n$, consisting of only letters A and B.
You have to calculate the number of good substrings of string $s$.
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) — the length of the string $s$.
The second line contains the string $s$, consisting of letters A and B.
-----Output-----
Print one integer — the number of good substrings of string $s$.
-----Examples-----
Input
5
AABBB
Output
6
Input
3
AAA
Output
3
Input
7
AAABABB
Output
15
-----Note-----
In the first test case there are six good substrings: $s_1 \dots s_2$, $s_1 \dots s_4$, $s_1 \dots s_5$, $s_3 \dots s_4$, $s_3 \dots s_5$ and $s_4 \dots s_5$.
In the second test case there are three good substrings: $s_1 \dots s_2$, $s_1 \dots s_3$ and $s_2 \dots s_3$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n = int(input())
s = input()
tab = []
count = 1
for i in range(1, n):
if s[i] == s[i-1]:
count += 1
else:
if count > 0:
tab.append(count)
count = 1
if count > 0:
tab.append(count)
dis = 0
k = len(tab)
if k == 0 or k == 1:
dis = 0
else:
dis += tab[1]
dis += tab[-2]
dis -= (k-1)
for i in range(1, k - 1):
dis += tab[i-1]
dis += tab[i+1]
print(n*(n-1)//2 - dis)
``` | vfc_19500 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1238/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\nAABBB\n",
"output": "6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\nAAA\n",
"output": "3\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2439 | Solve the following coding problem using the programming language python:
You are given an array of $n$ integers $a_1,a_2,\dots,a_n$.
You have to create an array of $n$ integers $b_1,b_2,\dots,b_n$ such that: The array $b$ is a rearrangement of the array $a$, that is, it contains the same values and each value appears the same number of times in the two arrays. In other words, the multisets $\{a_1,a_2,\dots,a_n\}$ and $\{b_1,b_2,\dots,b_n\}$ are equal.
For example, if $a=[1,-1,0,1]$, then $b=[-1,1,1,0]$ and $b=[0,1,-1,1]$ are rearrangements of $a$, but $b=[1,-1,-1,0]$ and $b=[1,0,2,-3]$ are not rearrangements of $a$. For all $k=1,2,\dots,n$ the sum of the first $k$ elements of $b$ is nonzero. Formally, for all $k=1,2,\dots,n$, it must hold $$b_1+b_2+\cdots+b_k\not=0\,.$$
If an array $b_1,b_2,\dots, b_n$ with the required properties does not exist, you have to print NO.
-----Input-----
Each test contains multiple test cases. The first line contains an integer $t$ ($1\le t \le 1000$) — the number of test cases. The description of the test cases follows.
The first line of each testcase contains one integer $n$ ($1\le n\le 50$) — the length of the array $a$.
The second line of each testcase contains $n$ integers $a_1,a_2,\dots, a_n$ ($-50\le a_i\le 50$) — the elements of $a$.
-----Output-----
For each testcase, if there is not an array $b_1,b_2,\dots,b_n$ with the required properties, print a single line with the word NO.
Otherwise print a line with the word YES, followed by a line with the $n$ integers $b_1,b_2,\dots,b_n$.
If there is more than one array $b_1,b_2,\dots,b_n$ satisfying the required properties, you can print any of them.
-----Example-----
Input
4
4
1 -2 3 -4
3
0 0 0
5
1 -1 1 -1 1
6
40 -31 -9 0 13 -40
Output
YES
1 -2 3 -4
NO
YES
1 1 -1 1 -1
YES
-40 13 40 0 -9 -31
-----Note-----
Explanation of the first testcase: An array with the desired properties is $b=[1,-2,3,-4]$. For this array, it holds: The first element of $b$ is $1$. The sum of the first two elements of $b$ is $-1$. The sum of the first three elements of $b$ is $2$. The sum of the first four elements of $b$ is $-2$.
Explanation of the second testcase: Since all values in $a$ are $0$, any rearrangement $b$ of $a$ will have all elements equal to $0$ and therefore it clearly cannot satisfy the second property described in the statement (for example because $b_1=0$). Hence in this case the answer is NO.
Explanation of the third testcase: An array with the desired properties is $b=[1, 1, -1, 1, -1]$. For this array, it holds: The first element of $b$ is $1$. The sum of the first two elements of $b$ is $2$. The sum of the first three elements of $b$ is $1$. The sum of the first four elements of $b$ is $2$. The sum of the first five elements of $b$ is $1$.
Explanation of the fourth testcase: An array with the desired properties is $b=[-40,13,40,0,-9,-31]$. For this array, it holds: The first element of $b$ is $-40$. The sum of the first two elements of $b$ is $-27$. The sum of the first three elements of $b$ is $13$. The sum of the first four elements of $b$ is $13$. The sum of the first five elements of $b$ is $4$. The sum of the first six elements of $b$ is $-27$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
from sys import stdin
input = stdin.readline
q = int(input())
for _ in range(q):
n = int(input())
l = list(map(int,input().split()))
if sum(l) == 0:
print("NO")
else:
l.sort()
if sum(l) > 0:
l.reverse()
print("YES")
print(*l)
``` | vfc_19504 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1427/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n4\n1 -2 3 -4\n3\n0 0 0\n5\n1 -1 1 -1 1\n6\n40 -31 -9 0 13 -40\n",
"output": "YES\n1 -2 3 -4\nNO\nYES\n1 1 -1 1 -1\nYES\n-40 13 40 0 -9 -31\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n50\n-27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -27 27 -6 27 -27 27 -27 27 -27 -4 -27 -47 -27 27\n",
"output": "YES\n-47 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -27 -6 -4 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n5\n1 1 2 -4 -6\n",
"output": "YES\n-6 -4 1 1 2 \n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2442 | Solve the following coding problem using the programming language python:
Given a set of integers (it can contain equal elements).
You have to split it into two subsets $A$ and $B$ (both of them can contain equal elements or be empty). You have to maximize the value of $mex(A)+mex(B)$.
Here $mex$ of a set denotes the smallest non-negative integer that doesn't exist in the set. For example: $mex(\{1,4,0,2,2,1\})=3$ $mex(\{3,3,2,1,3,0,0\})=4$ $mex(\varnothing)=0$ ($mex$ for empty set)
The set is splitted into two subsets $A$ and $B$ if for any integer number $x$ the number of occurrences of $x$ into this set is equal to the sum of the number of occurrences of $x$ into $A$ and the number of occurrences of $x$ into $B$.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1\leq t\leq 100$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer $n$ ($1\leq n\leq 100$) — the size of the set.
The second line of each testcase contains $n$ integers $a_1,a_2,\dots a_n$ ($0\leq a_i\leq 100$) — the numbers in the set.
-----Output-----
For each test case, print the maximum value of $mex(A)+mex(B)$.
-----Example-----
Input
4
6
0 2 1 5 0 1
3
0 1 2
4
0 2 0 1
6
1 2 3 4 5 6
Output
5
3
4
0
-----Note-----
In the first test case, $A=\left\{0,1,2\right\},B=\left\{0,1,5\right\}$ is a possible choice.
In the second test case, $A=\left\{0,1,2\right\},B=\varnothing$ is a possible choice.
In the third test case, $A=\left\{0,1,2\right\},B=\left\{0\right\}$ is a possible choice.
In the fourth test case, $A=\left\{1,3,5\right\},B=\left\{2,4,6\right\}$ is a possible choice.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
n = int(input())
arr = list(map(int,input().split()))
a,b = 0,0
arr.sort()
for i in arr:
if a==i:
a+=1
elif b==i:
b+=1
print(a+b)
``` | vfc_19516 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1406/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n6\n0 2 1 5 0 1\n3\n0 1 2\n4\n0 2 0 1\n6\n1 2 3 4 5 6\n",
"output": "5\n3\n4\n0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "15\n1\n0\n1\n1\n2\n0 0\n2\n0 1\n2\n1 1\n2\n0 2\n2\n1 2\n3\n0 0 0\n3\n0 1 0\n3\n0 1 2\n4\n0 1 2 3\n4\n1 1 2 3\n4\n0 1 0 1\n4\n0 0 0 1\n5\n0 0 0 0 0\n",
"output": "1\n0\n2\n2\n0\n1\n0\n2\n3\n3\n4\n0\n4\n3\n2\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2443 | Solve the following coding problem using the programming language python:
You are given an integer m, and a list of n distinct integers between 0 and m - 1.
You would like to construct a sequence satisfying the properties: Each element is an integer between 0 and m - 1, inclusive. All prefix products of the sequence modulo m are distinct. No prefix product modulo m appears as an element of the input list. The length of the sequence is maximized.
Construct any sequence satisfying the properties above.
-----Input-----
The first line of input contains two integers n and m (0 ≤ n < m ≤ 200 000) — the number of forbidden prefix products and the modulus.
If n is non-zero, the next line of input contains n distinct integers between 0 and m - 1, the forbidden prefix products. If n is zero, this line doesn't exist.
-----Output-----
On the first line, print the number k, denoting the length of your sequence.
On the second line, print k space separated integers, denoting your sequence.
-----Examples-----
Input
0 5
Output
5
1 2 4 3 0
Input
3 10
2 9 1
Output
6
3 9 2 9 8 0
-----Note-----
For the first case, the prefix products of this sequence modulo m are [1, 2, 3, 4, 0].
For the second case, the prefix products of this sequence modulo m are [3, 7, 4, 6, 8, 0].
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import math
def gcdExtended(a, b):
# Base Case
if a == 0 :
return b, 0, 1
gcd, x1, y1 = gcdExtended(b%a, a)
# Update x and y using results of recursive
# call
x = y1 - (b//a) * x1
y = x1
return gcd, x, y
def rev_elem(x, m):
return (gcdExtended(x, m)[1] % m + m) % m
n, m = map(int, input().split())
a = []
if n > 0:
a = [int(i) for i in input().split()]
banned = [False] * (m + 5)
for i in a:
banned[i] = True
cycle = [[] for i in range(m + 5)]
d, dp, p = [], [], []
for i in range(m):
cycle[math.gcd(m, i)].append(i)
cycle = [[i for i in j if not banned[i]] for j in cycle]
d = [i for i in range(1, m + 1) if m % i == 0]
dp = [len(cycle[i]) for i in d]
p = [-1 for i in d]
ans, lst = -1, -1
for i in range(len(d)):
if dp[i] > ans:
ans, lst = dp[i], i
for j in range(i + 1, len(d)):
if d[j] % d[i] != 0 or dp[j] > dp[i] + len(cycle[d[j]]):
continue
dp[j] = dp[i] + len(cycle[d[j]])
p[j] = i
print(ans)
pos, dpos, pref = [], [], []
cur = lst
while cur != -1:
dpos.append(d[cur])
cur = p[cur]
dpos.reverse()
for i in dpos:
pref += cycle[i]
cur = 1
for i in pref:
ad = 1
if math.gcd(i, m) != math.gcd(cur, m):
ad = ((cur * math.gcd(i, m) // math.gcd(cur, math.gcd(i, m))) // cur) % m
ncur = (cur * ad) % m
ad *= i // math.gcd(ncur, m) * (rev_elem(ncur // math.gcd(ncur, m), m // math.gcd(ncur, m)))
ad %= m
cur = (cur * ad) % m
pos.append(ad)
print(*pos)
``` | vfc_19520 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/772/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "0 5\n",
"output": "5\n1 2 4 3 0\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2444 | Solve the following coding problem using the programming language python:
There are $n$ seats in the train's car and there is exactly one passenger occupying every seat. The seats are numbered from $1$ to $n$ from left to right. The trip is long, so each passenger will become hungry at some moment of time and will go to take boiled water for his noodles. The person at seat $i$ ($1 \leq i \leq n$) will decide to go for boiled water at minute $t_i$.
Tank with a boiled water is located to the left of the $1$-st seat. In case too many passengers will go for boiled water simultaneously, they will form a queue, since there can be only one passenger using the tank at each particular moment of time. Each passenger uses the tank for exactly $p$ minutes. We assume that the time it takes passengers to go from their seat to the tank is negligibly small.
Nobody likes to stand in a queue. So when the passenger occupying the $i$-th seat wants to go for a boiled water, he will first take a look on all seats from $1$ to $i - 1$. In case at least one of those seats is empty, he assumes that those people are standing in a queue right now, so he would be better seating for the time being. However, at the very first moment he observes that all seats with numbers smaller than $i$ are busy, he will go to the tank.
There is an unspoken rule, that in case at some moment several people can go to the tank, than only the leftmost of them (that is, seating on the seat with smallest number) will go to the tank, while all others will wait for the next moment.
Your goal is to find for each passenger, when he will receive the boiled water for his noodles.
-----Input-----
The first line contains integers $n$ and $p$ ($1 \leq n \leq 100\,000$, $1 \leq p \leq 10^9$) — the number of people and the amount of time one person uses the tank.
The second line contains $n$ integers $t_1, t_2, \dots, t_n$ ($0 \leq t_i \leq 10^9$) — the moments when the corresponding passenger will go for the boiled water.
-----Output-----
Print $n$ integers, where $i$-th of them is the time moment the passenger on $i$-th seat will receive his boiled water.
-----Example-----
Input
5 314
0 310 942 628 0
Output
314 628 1256 942 1570
-----Note-----
Consider the example.
At the $0$-th minute there were two passengers willing to go for a water, passenger $1$ and $5$, so the first passenger has gone first, and returned at the $314$-th minute. At this moment the passenger $2$ was already willing to go for the water, so the passenger $2$ has gone next, and so on. In the end, $5$-th passenger was last to receive the boiled water.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
from heapq import heappop, heappush, heapify
from collections import deque
class SWAG_Stack():
def __init__(self, F):
self.stack1 = deque()
self.stack2 = deque()
self.F = F
self.len = 0
def push(self, x):
if self.stack2:
self.stack2.append((x, self.F(self.stack2[-1][1], x)))
else:
self.stack2.append((x, x))
self.len += 1
def pop(self):
if not self.stack1:
while self.stack2:
x, _ = self.stack2.pop()
if self.stack1:
self.stack1.appendleft((x, self.F(x, self.stack1[0][1])))
else:
self.stack1.appendleft((x, x))
x, _ = self.stack1.popleft()
self.len -= 1
return x
def sum_all(self):
if self.stack1 and self.stack2:
return self.F(self.stack1[0][1], self.stack2[-1][1])
elif self.stack1:
return self.stack1[0][1]
elif self.stack2:
return self.stack2[-1][1]
else:
return float("inf")
n,p = map(int, input().split())
t = list((j, i) for i,j in enumerate(map(int, input().split())))
heapify(t)
stack = SWAG_Stack(min)
heap = []
cur = 0
ans = [-1]*n
hoge = 0
# 今追加できるやつで最も小さいものを追加
# ここでなにもなかったら?
# 時間を変更する
# 次の時間までに追加できるものを追加
# 清算
while hoge != n:
if heap and stack.sum_all() > heap[0]:
j = heappop(heap)
stack.push(j)
if stack.len==0 and t:
cur = max(cur, t[0][0])
while t and t[0][0] <= cur+p:
ti, i = heappop(t)
if ti == cur+p:
# 後回し
heappush(heap, i)
else:
# いま追加できるか確認
if stack.sum_all() > i:
stack.push(i)
else:
# 後回し
heappush(heap, i)
if stack.len:
j = stack.pop()
cur += p
ans[j] = cur
hoge += 1
print(*ans)
``` | vfc_19524 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1248/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 314\n0 310 942 628 0\n",
"output": "314 628 1256 942 1570 \n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2445 | Solve the following coding problem using the programming language python:
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal.
The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $1\times1$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks.
The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.
Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!
Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.
The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
-----Input-----
The first line contains a single integer $T$ ($1 \le T \le 100$): the number of grids of the games.
Each of the next $T$ lines contains a single integer $n$ ($1 \le n \le 10^9$): the size of the side of the grid of the game.
-----Output-----
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
-----Example-----
Input
2
3
4
Output
2
3
-----Note-----
For $3\times3$ grid ponies can make two following moves: [Image]
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
n = int(input())
print(n // 2 + 1)
``` | vfc_19528 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1393/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n3\n4\n",
"output": "2\n3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n10000001\n",
"output": "5000001\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n69\n",
"output": "35\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2446 | Solve the following coding problem using the programming language python:
Given a sequence of integers a_1, ..., a_{n} and q queries x_1, ..., x_{q} on it. For each query x_{i} you have to count the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and gcd(a_{l}, a_{l} + 1, ..., a_{r}) = x_{i}.
$\operatorname{gcd}(v_{1}, v_{2}, \ldots, v_{n})$ is a greatest common divisor of v_1, v_2, ..., v_{n}, that is equal to a largest positive integer that divides all v_{i}.
-----Input-----
The first line of the input contains integer n, (1 ≤ n ≤ 10^5), denoting the length of the sequence. The next line contains n space separated integers a_1, ..., a_{n}, (1 ≤ a_{i} ≤ 10^9).
The third line of the input contains integer q, (1 ≤ q ≤ 3 × 10^5), denoting the number of queries. Then follows q lines, each contain an integer x_{i}, (1 ≤ x_{i} ≤ 10^9).
-----Output-----
For each query print the result in a separate line.
-----Examples-----
Input
3
2 6 3
5
1
2
3
4
6
Output
1
2
2
0
1
Input
7
10 20 3 15 1000 60 16
10
1
2
3
4
5
6
10
20
60
1000
Output
14
0
2
2
2
0
2
2
1
1
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
from collections import defaultdict
from math import gcd, sqrt
MAX = pow(10, 5)
# stdin = open("testdata.txt", "r")
ip = sys.stdin
n = int(ip.readline())
a = list(map(int, ip.readline().split()))
gcd_count = defaultdict(int)
main_gcd = defaultdict(int)
main_gcd[a[0]] = 1
gcd_count[a[0]] = 1
for i in range(1, n):
ele = a[i]
temp_gcd = defaultdict(int)
temp_gcd[ele] = 1
gcd_count[ele] += 1
for k, v in main_gcd.items():
temp = gcd(ele, k)
temp_gcd[temp] += v
gcd_count[temp] += v
main_gcd = temp_gcd
q = int(ip.readline())
for _ in range(q):
k = int(ip.readline())
print(gcd_count[k])
``` | vfc_19532 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/475/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n2 6 3\n5\n1\n2\n3\n4\n6\n",
"output": "1\n2\n2\n0\n1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n10 20 3 15 1000 60 16\n10\n1\n2\n3\n4\n5\n6\n10\n20\n60\n1000\n",
"output": "14\n0\n2\n2\n2\n0\n2\n2\n1\n1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "10\n2 2 4 3 2 4 4 2 4 2\n104\n3\n3\n1\n4\n1\n1\n4\n1\n1\n3\n1\n1\n4\n1\n1\n1\n4\n3\n1\n1\n4\n1\n1\n1\n1\n1\n4\n1\n1\n1\n4\n1\n1\n4\n1\n1\n1\n1\n1\n4\n4\n1\n3\n1\n4\n1\n1\n1\n4\n1\n2\n4\n1\n4\n1\n4\n1\n4\n3\n1\n2\n2\n4\n2\n1\n1\n2\n4\n4\n1\n2\n3\n1\n1\n4\n4\n4\n4\n4\n2\n2\n4\n1\n1\n1\n1\n4\n2\n1\n1\n4\n1\n4\n3\n4\n4\n1\n2\n3\n1\n2\n1\n3\n1\n",
"output": "1\n1\n27\n5\n27\n27\n5\n27\n27\n1\n27\n27\n5\n27\n27\n27\n5\n1\n27\n27\n5\n27\n27\n27\n27\n27\n5\n27\n27\n27\n5\n27\n27\n5\n27\n27\n27\n27\n27\n5\n5\n27\n1\n27\n5\n27\n27\n27\n5\n27\n22\n5\n27\n5\n27\n5\n27\n5\n1\n27\n22\n22\n5\n22\n27\n27\n22\n5\n5\n27\n22\n1\n27\n27\n5\n5\n5\n5\n5\n22\n22\n5\n27\n27\n27\n27\n5\n22\n27\n27\n5\n27\n5\n1\n5\n5\n27\n22\n1\n27\n22\n27\n1\n27\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2448 | Solve the following coding problem using the programming language python:
Let $n$ be a positive integer. Let $a, b, c$ be nonnegative integers such that $a + b + c = n$.
Alice and Bob are gonna play rock-paper-scissors $n$ times. Alice knows the sequences of hands that Bob will play. However, Alice has to play rock $a$ times, paper $b$ times, and scissors $c$ times.
Alice wins if she beats Bob in at least $\lceil \frac{n}{2} \rceil$ ($\frac{n}{2}$ rounded up to the nearest integer) hands, otherwise Alice loses.
Note that in rock-paper-scissors:
rock beats scissors; paper beats rock; scissors beat paper.
The task is, given the sequence of hands that Bob will play, and the numbers $a, b, c$, determine whether or not Alice can win. And if so, find any possible sequence of hands that Alice can use to win.
If there are multiple answers, print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases.
Then, $t$ testcases follow, each consisting of three lines: The first line contains a single integer $n$ ($1 \le n \le 100$). The second line contains three integers, $a, b, c$ ($0 \le a, b, c \le n$). It is guaranteed that $a + b + c = n$. The third line contains a string $s$ of length $n$. $s$ is made up of only 'R', 'P', and 'S'. The $i$-th character is 'R' if for his $i$-th Bob plays rock, 'P' if paper, and 'S' if scissors.
-----Output-----
For each testcase: If Alice cannot win, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes). Also, print a string $t$ of length $n$ made up of only 'R', 'P', and 'S' — a sequence of hands that Alice can use to win. $t$ must contain exactly $a$ 'R's, $b$ 'P's, and $c$ 'S's. If there are multiple answers, print any of them.
The "YES" / "NO" part of the output is case-insensitive (i.e. "yEs", "no" or "YEs" are all valid answers). Note that 'R', 'P' and 'S' are case-sensitive.
-----Example-----
Input
2
3
1 1 1
RPS
3
3 0 0
RPS
Output
YES
PSR
NO
-----Note-----
In the first testcase, in the first hand, Alice plays paper and Bob plays rock, so Alice beats Bob. In the second hand, Alice plays scissors and Bob plays paper, so Alice beats Bob. In the third hand, Alice plays rock and Bob plays scissors, so Alice beats Bob. Alice beat Bob 3 times, and $3 \ge \lceil \frac{3}{2} \rceil = 2$, so Alice wins.
In the second testcase, the only sequence of hands that Alice can play is "RRR". Alice beats Bob only in the last hand, so Alice can't win. $1 < \lceil \frac{3}{2} \rceil = 2$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
t = int(input())
for _ in range(t):
n = int(input())
a, b, c = map(int, input().split())
s = input()
t = ["X"] * n
wins = 0
for i, en in enumerate(s):
if en=="R" and b:
b-=1
t[i] = "P"
wins += 1
elif en=="P" and c:
c-=1
t[i] = "S"
wins += 1
elif en=="S" and a:
a-=1
t[i] = "R"
wins += 1
if wins < n/2:
print("NO")
else:
print("YES")
for i, my in enumerate(t):
if my=="X":
if a:a-=1;t[i]="R"
elif b:b-=1;t[i]="P"
elif c:c-=1;t[i]="S"
print("".join(t))
``` | vfc_19540 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1245/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n3\n1 1 1\nRPS\n3\n3 0 0\nRPS\n",
"output": "YES\nPSR\nNO\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "7\n2\n1 1 0\nRR\n2\n2 0 0\nSS\n2\n2 0 0\nRR\n3\n1 2 0\nRRR\n3\n2 1 0\nRRR\n1\n1 0 0\nP\n1\n1 0 0\nS\n",
"output": "YES\nPR\nYES\nRR\nNO\nYES\nPPR\nNO\nNO\nYES\nR\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n19\n2 8 9\nRRPPPPPPPPSSSSSSSSS\n",
"output": "YES\nPPSSSSSSSSRRPPPPPPS\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2449 | Solve the following coding problem using the programming language python:
You are given an integer m.
Let M = 2^{m} - 1.
You are also given a set of n integers denoted as the set T. The integers will be provided in base 2 as n binary strings of length m.
A set of integers S is called "good" if the following hold. If $a \in S$, then [Image]. If $a, b \in S$, then $a \text{AND} b \in S$ $T \subseteq S$ All elements of S are less than or equal to M.
Here, $XOR$ and $\text{AND}$ refer to the bitwise XOR and bitwise AND operators, respectively.
Count the number of good sets S, modulo 10^9 + 7.
-----Input-----
The first line will contain two integers m and n (1 ≤ m ≤ 1 000, 1 ≤ n ≤ min(2^{m}, 50)).
The next n lines will contain the elements of T. Each line will contain exactly m zeros and ones. Elements of T will be distinct.
-----Output-----
Print a single integer, the number of good sets modulo 10^9 + 7.
-----Examples-----
Input
5 3
11010
00101
11000
Output
4
Input
30 2
010101010101010010101010101010
110110110110110011011011011011
Output
860616440
-----Note-----
An example of a valid set S is {00000, 00101, 00010, 00111, 11000, 11010, 11101, 11111}.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
MOD = 10**9 + 7
m, N = list(map(int, input().split()))
binom = [[1] + [0 for i in range(m)] for j in range(m + 1)]
for n in range(1, m + 1):
for k in range(1, n + 1):
binom[n][k] = (binom[n - 1][k] + binom[n - 1][k - 1]) % MOD
bell = [0 for n in range(m + 1)]
bell[0] = bell[1] = 1
for n in range(1, m):
for k in range(n + 1):
bell[n + 1] += bell[k] * binom[n][k]
bell[n + 1] %= MOD
#print(bell)
bags = [0 for i in range(m)]
for it in range(N):
for i, z in enumerate(input()):
if z == '1':
bags[i] |= (1 << it)
difs = set(bags)
sol = 1
for mask in difs:
sol = sol * bell[bags.count(mask)] % MOD
print(sol)
``` | vfc_19544 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/908/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 3\n11010\n00101\n11000\n",
"output": "4\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2450 | Solve the following coding problem using the programming language python:
You might have remembered Theatre square from the problem 1A. Now it's finally getting repaved.
The square still has a rectangular shape of $n \times m$ meters. However, the picture is about to get more complicated now. Let $a_{i,j}$ be the $j$-th square in the $i$-th row of the pavement.
You are given the picture of the squares: if $a_{i,j} = $ "*", then the $j$-th square in the $i$-th row should be black; if $a_{i,j} = $ ".", then the $j$-th square in the $i$-th row should be white.
The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: $1 \times 1$ tiles — each tile costs $x$ burles and covers exactly $1$ square; $1 \times 2$ tiles — each tile costs $y$ burles and covers exactly $2$ adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into $1 \times 1$ tiles.
You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles.
What is the smallest total price of the tiles needed to cover all the white squares?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of testcases. Then the description of $t$ testcases follow.
The first line of each testcase contains four integers $n$, $m$, $x$ and $y$ ($1 \le n \le 100$; $1 \le m \le 1000$; $1 \le x, y \le 1000$) — the size of the Theatre square, the price of the $1 \times 1$ tile and the price of the $1 \times 2$ tile.
Each of the next $n$ lines contains $m$ characters. The $j$-th character in the $i$-th line is $a_{i,j}$. If $a_{i,j} = $ "*", then the $j$-th square in the $i$-th row should be black, and if $a_{i,j} = $ ".", then the $j$-th square in the $i$-th row should be white.
It's guaranteed that the sum of $n \times m$ over all testcases doesn't exceed $10^5$.
-----Output-----
For each testcase print a single integer — the smallest total price of the tiles needed to cover all the white squares in burles.
-----Example-----
Input
4
1 1 10 1
.
1 2 10 1
..
2 1 10 1
.
.
3 3 3 7
..*
*..
.*.
Output
10
1
20
18
-----Note-----
In the first testcase you are required to use a single $1 \times 1$ tile, even though $1 \times 2$ tile is cheaper. So the total price is $10$ burles.
In the second testcase you can either use two $1 \times 1$ tiles and spend $20$ burles or use a single $1 \times 2$ tile and spend $1$ burle. The second option is cheaper, thus the answer is $1$.
The third testcase shows that you can't rotate $1 \times 2$ tiles. You still have to use two $1 \times 1$ tiles for the total price of $20$.
In the fourth testcase the cheapest way is to use $1 \times 1$ tiles everywhere. The total cost is $6 \cdot 3 = 18$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
inp=lambda :map(int,input().split())
def sol():
x,y,on,tw=inp()
res=0
for n in range(x):
s=input()
sm=0
for n in s:
if n=='.':
sm+=1
else:
a=sm*on
b=(sm//2)*tw+(sm%2)*on
res+=min(a,b)
sm=0
a=sm*on
b=(sm//2)*tw+(sm%2)*on
res+=min(a,b)
print(res)
for n in range(int(input())):
sol()
``` | vfc_19548 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1359/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 1 10 1\n.\n1 2 10 1\n..\n2 1 10 1\n.\n.\n3 3 3 7\n..*\n*..\n.*.\n",
"output": "10\n1\n20\n18\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n3 3 2 4\n**.\n***\n***\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n9 9 9 9\n.........\n.........\n.........\n.........\n.........\n.........\n.........\n.........\n.........\n",
"output": "405\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2451 | Solve the following coding problem using the programming language python:
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.
The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building.
[Image]
The picture illustrates the first example.
You have given k pairs of locations (t_{a}, f_{a}), (t_{b}, f_{b}): floor f_{a} of tower t_{a} and floor f_{b} of tower t_{b}. For each pair you need to determine the minimum walking time between these locations.
-----Input-----
The first line of the input contains following integers:
n: the number of towers in the building (1 ≤ n ≤ 10^8), h: the number of floors in each tower (1 ≤ h ≤ 10^8), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h), k: total number of queries (1 ≤ k ≤ 10^4).
Next k lines contain description of the queries. Each description consists of four integers t_{a}, f_{a}, t_{b}, f_{b} (1 ≤ t_{a}, t_{b} ≤ n, 1 ≤ f_{a}, f_{b} ≤ h). This corresponds to a query to find the minimum travel time between f_{a}-th floor of the t_{a}-th tower and f_{b}-th floor of the t_{b}-th tower.
-----Output-----
For each query print a single integer: the minimum walking time between the locations in minutes.
-----Example-----
Input
3 6 2 3 3
1 2 1 3
1 4 3 4
1 2 2 3
Output
1
4
2
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n, h, a, b, k = map(int, input().split())
for i in range(k):
t1, f1, t2, f2 = map(int, input().split())
if t1 == t2:
print(abs(f1 - f2))
elif f1 >= a and f1 <= b:
print(abs(t2 - t1) + abs(f2 - f1))
elif f1 < a:
print(abs(a - f1) + abs(t2 - t1) + abs(f2 - a))
elif f1 > b:
print(abs(b - f1) + abs(t2 - t1) + abs(f2 - b))
``` | vfc_19552 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1020/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3\n",
"output": "1\n4\n2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1 1 1 1\n1 1 1 1\n",
"output": "0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "100000000 100000000 100000000 100000000 1\n100000000 100000000 100000000 1\n",
"output": "99999999\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2452 | Solve the following coding problem using the programming language python:
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
For a positive integer $n$, we call a permutation $p$ of length $n$ good if the following condition holds for every pair $i$ and $j$ ($1 \le i \le j \le n$) — $(p_i \text{ OR } p_{i+1} \text{ OR } \ldots \text{ OR } p_{j-1} \text{ OR } p_{j}) \ge j-i+1$, where $\text{OR}$ denotes the bitwise OR operation.
In other words, a permutation $p$ is good if for every subarray of $p$, the $\text{OR}$ of all elements in it is not less than the number of elements in that subarray.
Given a positive integer $n$, output any good permutation of length $n$. We can show that for the given constraints such a permutation always exists.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows.
The first and only line of every test case contains a single integer $n$ ($1 \le n \le 100$).
-----Output-----
For every test, output any good permutation of length $n$ on a separate line.
-----Example-----
Input
3
1
3
7
Output
1
3 1 2
4 3 5 2 7 1 6
-----Note-----
For $n = 3$, $[3,1,2]$ is a good permutation. Some of the subarrays are listed below. $3\text{ OR }1 = 3 \geq 2$ $(i = 1,j = 2)$ $3\text{ OR }1\text{ OR }2 = 3 \geq 3$ $(i = 1,j = 3)$ $1\text{ OR }2 = 3 \geq 2$ $(i = 2,j = 3)$ $1 \geq 1$ $(i = 2,j = 2)$
Similarly, you can verify that $[4,3,5,2,7,1,6]$ is also good.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
n = int(input())
print(*list(range(1, n+1)))
``` | vfc_19556 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1391/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n1\n3\n7\n",
"output": "1 \n1 2 3 \n1 2 3 4 5 6 7 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n77\n",
"output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n57\n",
"output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 \n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2453 | Solve the following coding problem using the programming language python:
You are given $n$ segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.
Your task is the following: for every $k \in [1..n]$, calculate the number of points with integer coordinates such that the number of segments that cover these points equals $k$. A segment with endpoints $l_i$ and $r_i$ covers point $x$ if and only if $l_i \le x \le r_i$.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of segments.
The next $n$ lines contain segments. The $i$-th line contains a pair of integers $l_i, r_i$ ($0 \le l_i \le r_i \le 10^{18}$) — the endpoints of the $i$-th segment.
-----Output-----
Print $n$ space separated integers $cnt_1, cnt_2, \dots, cnt_n$, where $cnt_i$ is equal to the number of points such that the number of segments that cover these points equals to $i$.
-----Examples-----
Input
3
0 3
1 3
3 8
Output
6 2 1
Input
3
1 3
2 4
5 7
Output
5 2 0
-----Note-----
The picture describing the first example:
[Image]
Points with coordinates $[0, 4, 5, 6, 7, 8]$ are covered by one segment, points $[1, 2]$ are covered by two segments and point $[3]$ is covered by three segments.
The picture describing the second example:
[Image]
Points $[1, 4, 5, 6, 7]$ are covered by one segment, points $[2, 3]$ are covered by two segments and there are no points covered by three segments.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
3
def solve(N, A):
cnt = [0] * (N + 1)
evd = {}
xs = []
for a, b in A:
if a not in evd:
evd[a] = [0, 0]
xs.append(a)
if b not in evd:
evd[b] = [0, 0]
xs.append(b)
evd[a][0] += 1
evd[b][1] += 1
xs.sort()
px = xs[0] - 1
pop = 0
for x in xs:
cnt[pop] += x - px - 1
cnt[pop + evd[x][0]] += 1
pop -= evd[x][1]
pop += evd[x][0]
px = x
return cnt[1:]
def main():
N = int(input())
A = [tuple([int(e) for e in input().split(' ')]) for _ in range(N)]
print(*solve(N, A))
def __starting_point():
main()
__starting_point()
``` | vfc_19560 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1000/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n0 3\n1 3\n3 8\n",
"output": "6 2 1 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1 3\n2 4\n5 7\n",
"output": "5 2 0 \n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2454 | Solve the following coding problem using the programming language python:
John has just bought a new car and is planning a journey around the country. Country has N cities, some of which are connected by bidirectional roads. There are N - 1 roads and every city is reachable from any other city. Cities are labeled from 1 to N.
John first has to select from which city he will start his journey. After that, he spends one day in a city and then travels to a randomly choosen city which is directly connected to his current one and which he has not yet visited. He does this until he can't continue obeying these rules.
To select the starting city, he calls his friend Jack for advice. Jack is also starting a big casino business and wants to open casinos in some of the cities (max 1 per city, maybe nowhere). Jack knows John well and he knows that if he visits a city with a casino, he will gamble exactly once before continuing his journey.
He also knows that if John enters a casino in a good mood, he will leave it in a bad mood and vice versa. Since he is John's friend, he wants him to be in a good mood at the moment when he finishes his journey. John is in a good mood before starting the journey.
In how many ways can Jack select a starting city for John and cities where he will build casinos such that no matter how John travels, he will be in a good mood at the end? Print answer modulo 10^9 + 7.
-----Input-----
In the first line, a positive integer N (1 ≤ N ≤ 100000), the number of cities.
In the next N - 1 lines, two numbers a, b (1 ≤ a, b ≤ N) separated by a single space meaning that cities a and b are connected by a bidirectional road.
-----Output-----
Output one number, the answer to the problem modulo 10^9 + 7.
-----Examples-----
Input
2
1 2
Output
4
Input
3
1 2
2 3
Output
10
-----Note-----
Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go back to city 1. If Jack selects city 2 for start, everything is symmetrical, so the answer is 4.
Example 2: If Jack tells John to start from city 1, he can either build casinos in 0 or 2 cities (total 4 possibilities). If he tells him to start from city 2, then John's journey will either contain cities 2 and 1 or 2 and 3. Therefore, Jack will either have to build no casinos, or build them in all three cities. With other options, he risks John ending his journey unhappy. Starting from 3 is symmetric to starting from 1, so in total we have 4 + 2 + 4 = 10 options.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n=int(input())
a = [0]*(n+1)
for i in range(n-1):
for i in input().split():
a[int(i)]+=1
l = a.count(1)
print ((l*2**(n-l+1)+(n-l)*(2**(n-l)))%(10**9+7))
``` | vfc_19564 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/852/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1 2\n",
"output": "4\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2455 | Solve the following coding problem using the programming language python:
There always is something to choose from! And now, instead of "Noughts and Crosses", Inna choose a very unusual upgrade of this game. The rules of the game are given below:
There is one person playing the game. Before the beginning of the game he puts 12 cards in a row on the table. Each card contains a character: "X" or "O". Then the player chooses two positive integers a and b (a·b = 12), after that he makes a table of size a × b from the cards he put on the table as follows: the first b cards form the first row of the table, the second b cards form the second row of the table and so on, the last b cards form the last (number a) row of the table. The player wins if some column of the table contain characters "X" on all cards. Otherwise, the player loses.
Inna has already put 12 cards on the table in a row. But unfortunately, she doesn't know what numbers a and b to choose. Help her win the game: print to her all the possible ways of numbers a, b that she can choose and win.
-----Input-----
The first line of the input contains integer t (1 ≤ t ≤ 100). This value shows the number of sets of test data in the input. Next follows the description of each of the t tests on a separate line.
The description of each test is a string consisting of 12 characters, each character is either "X", or "O". The i-th character of the string shows the character that is written on the i-th card from the start.
-----Output-----
For each test, print the answer to the test on a single line. The first number in the line must represent the number of distinct ways to choose the pair a, b. Next, print on this line the pairs in the format axb. Print the pairs in the order of increasing first parameter (a). Separate the pairs in the line by whitespaces.
-----Examples-----
Input
4
OXXXOXOOXOOX
OXOXOXOXOXOX
XXXXXXXXXXXX
OOOOOOOOOOOO
Output
3 1x12 2x6 4x3
4 1x12 2x6 3x4 6x2
6 1x12 2x6 3x4 4x3 6x2 12x1
0
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
T=int(input())
for t in range(T):
s=input()
ans=0
L=[]
for i in [1,2,3,4,6,12]:
x=i
y=12//x
E=[]
for j in range(12):
if(j%y==0):
E.append("")
E[-1]+=s[j]
for j in range(y):
c=0
for z in range(i):
if(E[z][j]=='X'):
c+=1
if(c==i):
ans+=1
L.append(i)
break
print(ans,end=" ")
for item in L:
print(item,end="x")
print(12//item,end=" ")
print()
``` | vfc_19568 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/400/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\nOXXXOXOOXOOX\nOXOXOXOXOXOX\nXXXXXXXXXXXX\nOOOOOOOOOOOO\n",
"output": "3 1x12 2x6 4x3\n4 1x12 2x6 3x4 6x2\n6 1x12 2x6 3x4 4x3 6x2 12x1\n0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\nOOOOOOOOOOOO\nXXXXXXXXXXXX\n",
"output": "0\n6 1x12 2x6 3x4 4x3 6x2 12x1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "13\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\n",
"output": "6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2456 | Solve the following coding problem using the programming language python:
A competitive eater, Alice is scheduling some practices for an eating contest on a magical calendar. The calendar is unusual because a week contains not necessarily $7$ days!
In detail, she can choose any integer $k$ which satisfies $1 \leq k \leq r$, and set $k$ days as the number of days in a week.
Alice is going to paint some $n$ consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day's cell is the leftmost cell in the next (under) row.
She wants to make all of the painted cells to be connected by side. It means, that for any two painted cells there should exist at least one sequence of painted cells, started in one of these cells, and ended in another, such that any two consecutive cells in this sequence are connected by side.
Alice is considering the shape of the painted cells. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar's sides.
For example, in the picture, a week has $4$ days and Alice paints $5$ consecutive days. [1] and [2] are different shapes, but [1] and [3] are equal shapes. [Image]
Alice wants to know how many possible shapes exists if she set how many days a week has and choose consecutive $n$ days and paints them in calendar started in one of the days of the week. As was said before, she considers only shapes, there all cells are connected by side.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Next $t$ lines contain descriptions of test cases.
For each test case, the only line contains two integers $n$, $r$ ($1 \le n \le 10^9, 1 \le r \le 10^9$).
-----Output-----
For each test case, print a single integer — the answer to the problem.
Please note, that the answer for some test cases won't fit into $32$-bit integer type, so you should use at least $64$-bit integer type in your programming language.
-----Example-----
Input
5
3 4
3 2
3 1
13 7
1010000 9999999
Output
4
3
1
28
510049495001
-----Note-----
In the first test case, Alice can set $1,2,3$ or $4$ days as the number of days in a week.
There are $6$ possible paintings shown in the picture, but there are only $4$ different shapes. So, the answer is $4$. Notice that the last example in the picture is an invalid painting because all cells are not connected by sides. [Image]
In the last test case, be careful with the overflow issue, described in the output format.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def solve():
n, r = list(map(int, input().split()))
k = min(r, n - 1)
print(k * (k + 1) // 2 + (r >= n))
for i in range(int(input())):
solve()
``` | vfc_19572 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1371/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n3 4\n3 2\n3 1\n13 7\n1010000 9999999\n",
"output": "4\n3\n1\n28\n510049495001\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1 1\n",
"output": "1\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2457 | Solve the following coding problem using the programming language python:
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.
In total, Nastya dropped $n$ grains. Nastya read that each grain weighs some integer number of grams from $a - b$ to $a + b$, inclusive (numbers $a$ and $b$ are known), and the whole package of $n$ grains weighs from $c - d$ to $c + d$ grams, inclusive (numbers $c$ and $d$ are known). The weight of the package is the sum of the weights of all $n$ grains in it.
Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $i$-th grain weighs some integer number $x_i$ $(a - b \leq x_i \leq a + b)$, and in total they weigh from $c - d$ to $c + d$, inclusive ($c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$).
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ $(1 \leq t \leq 1000)$ — the number of test cases.
The next $t$ lines contain descriptions of the test cases, each line contains $5$ integers: $n$ $(1 \leq n \leq 1000)$ — the number of grains that Nastya counted and $a, b, c, d$ $(0 \leq b < a \leq 1000, 0 \leq d < c \leq 1000)$ — numbers that determine the possible weight of one grain of rice (from $a - b$ to $a + b$) and the possible total weight of the package (from $c - d$ to $c + d$).
-----Output-----
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $n$ grains with masses from $a - b$ to $a + b$ cannot make a package with a total mass from $c - d$ to $c + d$.
-----Example-----
Input
5
7 20 3 101 18
11 11 10 234 2
8 9 7 250 122
19 41 21 321 10
3 10 8 6 1
Output
Yes
No
Yes
No
Yes
-----Note-----
In the first test case of the example, we can assume that each grain weighs $17$ grams, and a pack $119$ grams, then really Nastya could collect the whole pack.
In the third test case of the example, we can assume that each grain weighs $16$ grams, and a pack $128$ grams, then really Nastya could collect the whole pack.
In the fifth test case of the example, we can be assumed that $3$ grains of rice weigh $2$, $2$, and $3$ grams, and a pack is $7$ grams, then really Nastya could collect the whole pack.
In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
n, a, b, c, d = tuple(map(int, input().split()))
if (a - b) * n > c + d or (a + b) * n < c - d:
print('No')
else:
print('Yes')
``` | vfc_19576 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1341/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1\n",
"output": "Yes\nNo\nYes\nNo\nYes\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n9 2 1 16 0\n",
"output": "Yes\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n244 15 12 853 57\n",
"output": "Yes\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2458 | Solve the following coding problem using the programming language python:
We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.
But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.
Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (10^9 + 7).
-----Input-----
Input contains several test cases.
The first line contains two integers t and k (1 ≤ t, k ≤ 10^5), where t represents the number of test cases.
The next t lines contain two integers a_{i} and b_{i} (1 ≤ a_{i} ≤ b_{i} ≤ 10^5), describing the i-th test.
-----Output-----
Print t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between a_{i} and b_{i} flowers at dinner modulo 1000000007 (10^9 + 7).
-----Examples-----
Input
3 2
1 3
2 3
4 4
Output
6
5
5
-----Note----- For K = 2 and length 1 Marmot can eat (R). For K = 2 and length 2 Marmot can eat (RR) and (WW). For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR). For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can't eat (WWWR).
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
m = 1000000007
(t, k) = [int(i) for i in input().split(' ')]
n = 100000
dp=[0]*(n+1)
dp[0] = 1
for i in range(1, n+1):
dp[i] = (dp[i-1] + (0 if i-k < 0 else dp[i-k])) % m
s = [0]*(n+1)
s[1] = dp[1]
s[0] = 0
for i in range(2, n+1):
s[i] = (s[i-1] + dp[i]) % m
for _ in range(t):
(a, b) = [int(i) for i in input().split(' ')]
print((s[b] - s[a-1] + m) % m)
``` | vfc_19580 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/474/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 2\n1 3\n2 3\n4 4\n",
"output": "6\n5\n5\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1\n1 3\n",
"output": "14\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 2\n64329 79425\n",
"output": "0\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2459 | Solve the following coding problem using the programming language python:
You are given an array a of size n, and q queries to it. There are queries of two types: 1 l_{i} r_{i} — perform a cyclic shift of the segment [l_{i}, r_{i}] to the right. That is, for every x such that l_{i} ≤ x < r_{i} new value of a_{x} + 1 becomes equal to old value of a_{x}, and new value of a_{l}_{i} becomes equal to old value of a_{r}_{i}; 2 l_{i} r_{i} — reverse the segment [l_{i}, r_{i}].
There are m important indices in the array b_1, b_2, ..., b_{m}. For each i such that 1 ≤ i ≤ m you have to output the number that will have index b_{i} in the array after all queries are performed.
-----Input-----
The first line contains three integer numbers n, q and m (1 ≤ n, q ≤ 2·10^5, 1 ≤ m ≤ 100).
The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
Then q lines follow. i-th of them contains three integer numbers t_{i}, l_{i}, r_{i}, where t_{i} is the type of i-th query, and [l_{i}, r_{i}] is the segment where this query is performed (1 ≤ t_{i} ≤ 2, 1 ≤ l_{i} ≤ r_{i} ≤ n).
The last line contains m integer numbers b_1, b_2, ..., b_{m} (1 ≤ b_{i} ≤ n) — important indices of the array.
-----Output-----
Print m numbers, i-th of which is equal to the number at index b_{i} after all queries are done.
-----Example-----
Input
6 3 5
1 2 3 4 5 6
2 1 3
2 3 6
1 1 6
2 2 1 5 3
Output
3 3 1 5 2
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
# https://codeforces.com/contest/863/problem/D
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
# solve the reversed problem
n, q, m = map(int, input().split())
a = list(map(int, input().split()))
ops = [list(map(int, input().split())) for _ in range(q)]
b = list(map(int, input().split()))
def solve(index, ops):
def _solve(index, op):
t, l, r = op
if index < l or index > r:
return index
if t == 1:
if index == l:
return r
else:
return index - 1
else:
return l + r - index
for op in ops[::-1]:
index = _solve(index, op)
return index
b = list(map(lambda x: solve(x, ops), b))
for i in b:
print(str(a[i-1])+" ")
# Cartesian tree:
# https://codeforces.com/contest/863/submission/30693678
``` | vfc_19584 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/863/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3\n",
"output": "3 3 1 5 2 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5 2 5\n64 3 4 665 2\n1 1 3\n2 1 5\n1 2 3 4 5\n",
"output": "2 665 3 64 4 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1 1\n474812122\n2 1 1\n1\n",
"output": "474812122 \n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2460 | Solve the following coding problem using the programming language python:
Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5.
Lyft has become so popular so that it is now used by all $m$ taxi drivers in the city, who every day transport the rest of the city residents — $n$ riders.
Each resident (including taxi drivers) of Palo-Alto lives in its unique location (there is no such pair of residents that their coordinates are the same).
The Lyft system is very clever: when a rider calls a taxi, his call does not go to all taxi drivers, but only to the one that is the closest to that person. If there are multiple ones with the same distance, then to taxi driver with a smaller coordinate is selected.
But one morning the taxi drivers wondered: how many riders are there that would call the given taxi driver if they were the first to order a taxi on that day? In other words, you need to find for each taxi driver $i$ the number $a_{i}$ — the number of riders that would call the $i$-th taxi driver when all drivers and riders are at their home?
The taxi driver can neither transport himself nor other taxi drivers.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$) — number of riders and taxi drivers.
The second line contains $n + m$ integers $x_1, x_2, \ldots, x_{n+m}$ ($1 \le x_1 < x_2 < \ldots < x_{n+m} \le 10^9$), where $x_i$ is the coordinate where the $i$-th resident lives.
The third line contains $n + m$ integers $t_1, t_2, \ldots, t_{n+m}$ ($0 \le t_i \le 1$). If $t_i = 1$, then the $i$-th resident is a taxi driver, otherwise $t_i = 0$.
It is guaranteed that the number of $i$ such that $t_i = 1$ is equal to $m$.
-----Output-----
Print $m$ integers $a_1, a_2, \ldots, a_{m}$, where $a_i$ is the answer for the $i$-th taxi driver. The taxi driver has the number $i$ if among all the taxi drivers he lives in the $i$-th smallest coordinate (see examples for better understanding).
-----Examples-----
Input
3 1
1 2 3 10
0 0 1 0
Output
3
Input
3 2
2 3 4 5 6
1 0 0 0 1
Output
2 1
Input
1 4
2 4 6 10 15
1 1 1 1 0
Output
0 0 0 1
-----Note-----
In the first example, we have only one taxi driver, which means an order from any of $n$ riders will go to him.
In the second example, the first taxi driver lives at the point with the coordinate $2$, and the second one lives at the point with the coordinate $6$. Obviously, the nearest taxi driver to the rider who lives on the $3$ coordinate is the first one, and to the rider who lives on the coordinate $5$ is the second one. The rider who lives on the $4$ coordinate has the same distance to the first and the second taxi drivers, but since the first taxi driver has a smaller coordinate, the call from this rider will go to the first taxi driver.
In the third example, we have one rider and the taxi driver nearest to him is the fourth one.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
import string
from math import gcd
import getpass
import math
from decimal import Decimal
import pprint
def ria():
return [int(i) for i in input().split()]
if getpass.getuser() != 'frohenk':
filename = 'half'
# sys.stdin = open('input.txt')
# sys.stdout = open('output.txt', 'w')
else:
sys.stdin = open('input.txt')
# sys.stdin.close()
# sys.stdout = open('output.txt', 'w')
n, m = ria()
ar = ria()
arc = []
art = []
res = []
for n, i in enumerate(ria()):
if i == 1:
art.append(ar[n])
res.append(0)
else:
arc.append(ar[n])
nt = 0
for i in arc:
while nt != len(art) - 1 and abs(art[nt] - i) > abs(art[nt + 1] - i):
nt += 1
res[nt] += 1
for i in res:
print(i,end= ' ')
``` | vfc_19588 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1075/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 1\n1 2 3 10\n0 0 1 0\n",
"output": "3 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 2\n2 3 4 5 6\n1 0 0 0 1\n",
"output": "2 1 ",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2461 | Solve the following coding problem using the programming language python:
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to a_{i}.
Ilya believes that the beauty of the vertex x is the greatest common divisor of all numbers written on the vertices on the path from the root to x, including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to 0 or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.
For each vertex the answer must be considered independently.
The beauty of the root equals to number written on it.
-----Input-----
First line contains one integer number n — the number of vertices in tree (1 ≤ n ≤ 2·10^5).
Next line contains n integer numbers a_{i} (1 ≤ i ≤ n, 1 ≤ a_{i} ≤ 2·10^5).
Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ n, x ≠ y), which means that there is an edge (x, y) in the tree.
-----Output-----
Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.
-----Examples-----
Input
2
6 2
1 2
Output
6 6
Input
3
6 2 3
1 2
1 3
Output
6 6 6
Input
1
10
Output
10
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
from sys import stdin
from fractions import gcd
n = int(stdin.readline().strip())
v = list(map(int, stdin.readline().strip().split()))
adj = [[] for _ in range(n)]
for _ in range(n-1):
x, y = list(map(int, stdin.readline().strip().split()))
adj[x-1].append(y-1)
adj[y-1].append(x-1)
root_divisors = []
cnt = [0]*200001
d = 1
while d*d <= v[0]:
if v[0] % d == 0:
root_divisors.append(d)
cnt[d] += 1
if v[0]//d != d:
root_divisors.append(v[0]//d)
cnt[v[0]//d] += 1
d += 1
s = [0]
visited = [False]*n
visited[0] = True
level = [1]*n
res1 = [0]*n
res2 = [0]*n
res1[0] = v[0]
d = 1
while s:
x = s[-1]
any_more = False
while adj[x]:
y = adj[x].pop()
if not visited[y]:
visited[y] = True
any_more = True
s.append(y)
level[y] = level[x]+1
res2[y] = gcd(res2[x], v[y])
for d in root_divisors:
if v[y] % d == 0:
cnt[d] += 1
if cnt[d] == level[y] or cnt[d] == level[y]-1:
res1[y] = max(res1[y], res2[y], d)
break
if not any_more:
s.pop()
for d in root_divisors:
if v[x] % d == 0:
cnt[d] -= 1
print(' '.join(list(map(str, res1))))
``` | vfc_19592 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/842/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n6 2\n1 2",
"output": "6 6",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n6 2 3\n1 2\n1 3",
"output": "6 6 6",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n10",
"output": "10",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2462 | Solve the following coding problem using the programming language python:
Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task.
Recently, out of blue Captain Flint has been interested in math and even defined a new class of integers. Let's define a positive integer $x$ as nearly prime if it can be represented as $p \cdot q$, where $1 < p < q$ and $p$ and $q$ are prime numbers. For example, integers $6$ and $10$ are nearly primes (since $2 \cdot 3 = 6$ and $2 \cdot 5 = 10$), but integers $1$, $3$, $4$, $16$, $17$ or $44$ are not.
Captain Flint guessed an integer $n$ and asked you: can you represent it as the sum of $4$ different positive integers where at least $3$ of them should be nearly prime.
Uncle Bogdan easily solved the task and joined the crew. Can you do the same?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per line. The first and only line of each test case contains the single integer $n$ $(1 \le n \le 2 \cdot 10^5)$ — the number Flint guessed.
-----Output-----
For each test case print: YES and $4$ different positive integers such that at least $3$ of them are nearly prime and their sum is equal to $n$ (if there are multiple answers print any of them); NO if there is no way to represent $n$ as the sum of $4$ different positive integers where at least $3$ of them are nearly prime. You can print each character of YES or NO in any case.
-----Example-----
Input
7
7
23
31
36
44
100
258
Output
NO
NO
YES
14 10 6 1
YES
5 6 10 15
YES
6 7 10 21
YES
2 10 33 55
YES
10 21 221 6
-----Note-----
In the first and second test cases, it can be proven that there are no four different positive integers such that at least three of them are nearly prime.
In the third test case, $n=31=2 \cdot 7 + 2 \cdot 5 + 2 \cdot 3 + 1$: integers $14$, $10$, $6$ are nearly prime.
In the fourth test case, $n=36=5 + 2 \cdot 3 + 2 \cdot 5 + 3 \cdot 5$: integers $6$, $10$, $15$ are nearly prime.
In the fifth test case, $n=44=2 \cdot 3 + 7 + 2 \cdot 5 + 3 \cdot 7$: integers $6$, $10$, $21$ are nearly prime.
In the sixth test case, $n=100=2 + 2 \cdot 5 + 3 \cdot 11 + 5 \cdot 11$: integers $10$, $33$, $55$ are nearly prime.
In the seventh test case, $n=258=2 \cdot 5 + 3 \cdot 7 + 13 \cdot 17 + 2 \cdot 3$: integers $10$, $21$, $221$, $6$ are nearly prime.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
# for _ in range(1):
for _ in range(int(input())):
# a, b = map(int, input().split())
n = int(input())
# arr = list(map(int, input().split()))
# s = input()
x = 6 + 10 + 14
if x + 1 > n:
print('NO')
continue
y = n - x
if y not in (6, 10, 14):
print('YES')
print(6, 10, 14, y)
continue
x += 1
y -= 1
if y == 0:
print('NO')
else:
print('YES')
print(6, 10, 15, y)
``` | vfc_19596 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1388/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7\n7\n23\n31\n36\n44\n100\n258\n",
"output": "NO\nNO\nYES\n6 10 14 1\nYES\n6 10 15 5\nYES\n6 10 15 13\nYES\n6 10 14 70\nYES\n6 10 14 228\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n15239\n",
"output": "YES\n6 10 14 15209\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n78788\n",
"output": "YES\n6 10 14 78758\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2463 | Solve the following coding problem using the programming language python:
This is the easy version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
-----Input-----
The first line contains a single integer $n$ $(1 \le n \le 10^5)$ — the number of ice spheres in the shop.
The second line contains $n$ different integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$ — the prices of ice spheres.
-----Output-----
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
-----Example-----
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
-----Note-----
In the example it's not possible to place ice spheres in any order so that Sage would buy $3$ of them. If the ice spheres are placed like this $(3, 1, 4, 2, 5)$, then Sage will buy two spheres: one for $1$ and one for $2$, because they are cheap.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def mi():
return map(int, input().split())
'''
5
1 2 3 4 5
'''
for _ in range(1):
n = int(input())
a = list(mi())
a.sort()
k = 0
out = [0]*n
for i in range(1,n,2):
out[i] = a[k]
k+=1
for i in range(0,n,2):
out[i] = a[k]
k+=1
ans = 0
a = out
for i in range(1,n-1):
if a[i]<a[i-1] and a[i]<a[i+1]:
ans+=1
print (ans)
print (*out)
``` | vfc_19600 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1419/D1",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n1 2 3 4 5\n",
"output": "2\n3 1 4 2 5 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1000000000\n",
"output": "0\n1000000000 ",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2\n1000000000 1\n",
"output": "0\n1000000000 1 ",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2465 | Solve the following coding problem using the programming language python:
You are given an angle $\text{ang}$.
The Jury asks You to find such regular $n$-gon (regular polygon with $n$ vertices) that it has three vertices $a$, $b$ and $c$ (they can be non-consecutive) with $\angle{abc} = \text{ang}$ or report that there is no such $n$-gon. [Image]
If there are several answers, print the minimal one. It is guarantied that if answer exists then it doesn't exceed $998244353$.
-----Input-----
The first line contains single integer $T$ ($1 \le T \le 180$) — the number of queries.
Each of the next $T$ lines contains one integer $\text{ang}$ ($1 \le \text{ang} < 180$) — the angle measured in degrees.
-----Output-----
For each query print single integer $n$ ($3 \le n \le 998244353$) — minimal possible number of vertices in the regular $n$-gon or $-1$ if there is no such $n$.
-----Example-----
Input
4
54
50
2
178
Output
10
18
90
180
-----Note-----
The answer for the first query is on the picture above.
The answer for the second query is reached on a regular $18$-gon. For example, $\angle{v_2 v_1 v_6} = 50^{\circ}$.
The example angle for the third query is $\angle{v_{11} v_{10} v_{12}} = 2^{\circ}$.
In the fourth query, minimal possible $n$ is $180$ (not $90$).
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def gcd(a,b):
return a if b==0 else gcd(b, a%b)
for T in range(int(input())):
de=int(input())
g=gcd(de, 180)
if int(de/g)==int(180/g)-1:
print(int(180/g)*2)
else:
print(int(180/g))
``` | vfc_19608 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1096/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n54\n50\n2\n178\n",
"output": "10\n18\n90\n180\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n179\n",
"output": "360\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "69\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n",
"output": "180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n180\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2527 | Solve the following coding problem using the programming language python:
Dilku and Bhopu live in Artland. Dilku loves Bhopu and he writes a message : “iloveyou”
on paper and wishes to send it to Bhopu. As Dilku is busy making an artwork, he asks his friend Raj to send the message to Bhopu. However Raj has a condition that he may add/remove some characters and jumble the letters of the message.
As Bhopu understands Dilku, she can read “iloveyou” from the message if all the characters of the string “iloveyou” are in the message received by her. Bhopu is happy if she can read “iloveyou” from the message. Otherwise, she is sad. Tell whether Bhopu is happy or sad.
-----Input-----
Input contains a string S, where S is the message received by Bhopu. String S consists of only lowercase letters.
-----Output-----
Output “happy” if Bhopu is happy and “sad” if Bhopu is sad.
-----Constraints-----
1 ≤ |S| ≤ 100
Where |S| denotes length of message string S
-----Example-----
Input 1:
iloveyou
Output 1:
happy
Input 2:
ulrvysioqjifo
Output 2:
sad
Input 3:
abcvleouioydef
Output 3:
happy
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
#!/usr/bin/python
a = input()
x='iloveyou'
for i in x:
if i not in a:
print("sad")
return
if a.count('o') < 2:
print("sad")
return
print("happy")
return
``` | vfc_19841 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://www.codechef.com/IPC15P3A/problems/DMSG",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "iloveyou\n",
"output": "happy\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "ulrvysioqjifo\n",
"output": "sad\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "abcvleouioydef\n",
"output": "happy\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2528 | Solve the following coding problem using the programming language python:
Chef loves research! Now he is looking for subarray of maximal length with non-zero product.
Chef has an array A with N elements: A1, A2, ..., AN.
Subarray Aij of array A is elements from index i to index j: Ai, Ai+1, ..., Aj.
Product of subarray Aij is product of all its elements (from ith to jth).
-----Input-----
- First line contains sinlge integer N denoting the number of elements.
- Second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- In a single line print single integer - the maximal length of subarray with non-zero product.
-----Constraints-----
- 1 ≤ N ≤ 100000
- 0 ≤ Ai ≤ 10000
-----Example-----
Input:
6
1 0 2 3 0 4
Output:
2
Input:
1
0
Output:
0
Input:
3
1 0 1
Output:
1
-----Explanation-----
For the first sample subarray is: {2, 3}.
For the second sample there are no subbarays with non-zero product.
For the third sample subbarays is {1}, (the first element, or the third one).
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
# cook your dish here
n = int(input())
l = list(map(int, input().strip().split()))
m = 0
f = []
for e in l:
if(e != 0):
m = m+1
else:
f.append(m)
m = 0
f.append(m)
print(max(f))
``` | vfc_19845 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://www.codechef.com/problems/CHEFZOT",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6\n1 0 2 3 0 4\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n0\n",
"output": "0\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2529 | Solve the following coding problem using the programming language python:
Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US.
Calculate Pooja's account balance after an attempted transaction.
-----Input-----
Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.
Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance.
-----Output-----
Output the account balance after the attempted transaction, given as a number with two digits of precision. If there is not enough money in the account to complete the transaction, output the current bank balance.
-----Example - Successful Transaction-----
Input:
30 120.00
Output:
89.50
-----Example - Incorrect Withdrawal Amount (not multiple of 5)-----
Input:
42 120.00
Output:
120.00
-----Example - Insufficient Funds-----
Input:
300 120.00
Output:
120.00
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
x = list(map(float, input(" ").split()))
amount=int(x[0])
bal=float(x[1])
if amount+0.5 > bal or amount % 5 != 0:
print("{:.2f}".format(bal))
else:
bal = bal - amount-0.5
print("{:.2f}".format(bal))
``` | vfc_19849 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://www.codechef.com/problems/ATM",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "30 120.00\n\n\n",
"output": "89.50\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "42 120.00\n\n\n",
"output": "120.00\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "300 120.00\n\n\n",
"output": "120.00\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2530 | Solve the following coding problem using the programming language python:
Chefs from all over the globe gather each year for an international convention. Each chef represents some country. Please, note that more than one chef can represent a country.
Each of them presents their best dish to the audience. The audience then sends emails to a secret and secure mail server, with the subject being the name of the chef whom they wish to elect as the "Chef of the Year".
You will be given the list of the subjects of all the emails. Find the country whose chefs got the most number of votes, and also the chef who got elected as the "Chef of the Year" (the chef who got the most number of votes).
Note 1
If several countries got the maximal number of votes, consider the country with the lexicographically smaller name among them to be a winner. Similarly if several chefs got the maximal number of votes, consider the chef with the lexicographically smaller name among them to be a winner.
Note 2
The string A = a1a2...an is called lexicographically smaller then the string B = b1b2...bm in the following two cases:
- there exists index i ≤ min{n, m} such that aj = bj for 1 ≤ j < i and ai < bi;
- A is a proper prefix of B, that is, n < m and aj = bj for 1 ≤ j ≤ n.
The characters in strings are compared by their ASCII codes.
Refer to function strcmp in C or to standard comparator < for string data structure in C++ for details.
-----Input-----
The first line of the input contains two space-separated integers N and M denoting the number of chefs and the number of emails respectively. Each of the following N lines contains two space-separated strings, denoting the name of the chef and his country respectively. Each of the following M lines contains one string denoting the subject of the email.
-----Output-----
Output should consist of two lines. The first line should contain the name of the country whose chefs got the most number of votes. The second line should contain the name of the chef who is elected as the "Chef of the Year".
-----Constraints-----
- 1 ≤ N ≤ 10000 (104)
- 1 ≤ M ≤ 100000 (105)
- Each string in the input contains only letters of English alphabets (uppercase or lowercase)
- Each string in the input has length not exceeding 10
- All chef names will be distinct
- Subject of each email will coincide with the name of one of the chefs
-----Example 1-----
Input:
1 3
Leibniz Germany
Leibniz
Leibniz
Leibniz
Output:
Germany
Leibniz
-----Example 2-----
Input:
4 5
Ramanujan India
Torricelli Italy
Gauss Germany
Lagrange Italy
Ramanujan
Torricelli
Torricelli
Ramanujan
Lagrange
Output:
Italy
Ramanujan
-----Example 3-----
Input:
2 2
Newton England
Euclid Greece
Newton
Euclid
Output:
England
Euclid
-----Explanation-----
Example 1. Here we have only one chef Leibniz and he is from Germany. Clearly, all votes are for him. So Germany is the country-winner and Leibniz is the "Chef of the Year".
Example 2. Here we have chefs Torricelli and Lagrange from Italy, chef Ramanujan from India and chef Gauss from Germany. Torricelli got 2 votes, while Lagrange got one vote. Hence the Italy got 3 votes in all. Ramanujan got also 2 votes. And so India got 2 votes in all. Finally Gauss got no votes leaving Germany without votes. So the country-winner is Italy without any ties. But we have two chefs with 2 votes: Torricelli and Ramanujan. But since the string "Ramanujan" is lexicographically smaller than "Torricelli", then Ramanujan is the "Chef of the Year".
Example 3. Here we have two countries with 1 vote: England and Greece. Since the string "England" is lexicographically smaller than "Greece", then England is the country-winner. Next, we have two chefs with 1 vote: Newton and Euclid. Since the string "Euclid" is lexicographically smaller than "Newton", then Euclid is the "Chef of the Year".
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
N,M = [int(i) for i in input().split()]
get_country = {}
for i in range(N):
name,country = input().split()
get_country[name] = country
country_votes = {}
name_votes = {}
for i in range(M):
name = input()
country_votes[get_country[name]] = country_votes.get(get_country[name],0) + 1
name_votes[name] = name_votes.get(name,0) + 1
win_country = None
votes = -float('inf')
for i,j in country_votes.items():
if(j>votes):
win_country = i
votes = j
elif(j==votes and i<win_country):
win_country = i
win_name = None
votes = -float('inf')
for i,j in name_votes.items():
if(j>votes):
win_name = i
votes = j
elif(j==votes and i<win_name):
win_name = i
print(win_country)
print(win_name)
``` | vfc_19853 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://www.codechef.com/problems/CVOTE",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1 3\nLeibniz Germany\nLeibniz\nLeibniz\nLeibniz\n",
"output": "Germany\nLeibniz\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 5\nRamanujan India\nTorricelli Italy\nGauss Germany\nLagrange Italy\nRamanujan\nTorricelli\nTorricelli\nRamanujan\nLagrange\n",
"output": "Italy\nRamanujan\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2531 | Solve the following coding problem using the programming language python:
You are given a sequence of integers $a_1, a_2, ..., a_N$. An element ak is said to be an average element if there are indices $i, j$ (with $i \neq j$) such that $a_k = \frac{a_i + a_j}{2}$.
In the sequence
371022171537102217153 \quad 7 \quad 10 \quad 22 \quad 17 \quad 15
for $i=1, j=5$ and $k=3$, we get $a_k = \frac{a_i + a_j}{2}$. Thus $a_3 = 10$ is an average element in this sequence. You can check that $a_3$ is the only average element in this sequence.
Consider the sequence
371031837103183 \quad 7 \quad 10 \quad 3 \quad 18
With $i=1, j=4$ and $k=1$ we get $a_k = \frac{a_i + a_j}{2}$. Thus $a_1=3$ is an average element. We could also choose $i=1, j=4$ and $k=4$ and get $a_k = \frac{a_i + a_j}{2}$. You can check that $a_1$ and $a_4$ are the only average elements of this sequence.
On the other hand, the sequence
38111730381117303 \quad 8 \quad 11 \quad 17 \quad 30
has no average elements.
Your task is to count the number of average elements in the given sequence.
-----Input:-----
The first line contains a single integer $N$ indicating the number of elements in the sequence. This is followed by $N$ lines containing one integer each (Line $i+1$ contains $a_i$). (You may assume that $a_i + a_j$ would not exceed MAXINT for any $i$ and $j$).
-----Output:-----
The output must consist of a single line containing a single integer $k$ indicating the number of average elements in the given sequence.
-----Constraints:-----
- You may assume that $1 \leq N \leq 10000$.
- In $30 \%$ of the inputs $1 \leq N \leq 200$.
- In $60 \%$ of the inputs $1 \leq N \leq 5000$.
-----Sample Input 1:-----
6
3
7
10
17
22
15
-----Sample Output 1:-----
1
-----Sample Input 2:-----
5
3
7
10
3
18
-----Sample Output 2:-----
2
-----Sample Input 3;-----
5
3
8
11
17
30
-----Sample Output 3:-----
0
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
l=[]
for _ in range(int(input())):
l.append(int(input()))
avg=0;l.sort()
for i in range(0,len(l)):
j=0;n=len(l)-1
while j<n:
s=l[j]+l[n]
if s>2*l[i]:
n-=1
elif s<2*l[i]:
j+=1
else:
avg+=1;break
print(avg)
``` | vfc_19857 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://www.codechef.com/IARCSJUD/problems/AVERAGE",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6\n3\n7\n10\n17\n22\n15\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n3\n7\n10\n3\n18\n",
"output": "2\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n3\n8\n11\n17\n30\n",
"output": "0\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2534 | Solve the following coding problem using the programming language python:
Our Chef is very happy that his son was selected for training in one of the finest culinary schools of the world.
So he and his wife decide to buy a gift for the kid as a token of appreciation.
Unfortunately, the Chef hasn't been doing good business lately, and is in no mood on splurging money.
On the other hand, the boy's mother wants to buy something big and expensive.
To settle the matter like reasonable parents, they play a game.
They spend the whole day thinking of various gifts and write them down in a huge matrix.
Each cell of the matrix contains the gift's cost.
Then they decide that the mother will choose a row number r while the father will choose a column number c,
the item from the corresponding cell will be gifted to the kid in a couple of days.
The boy observes all of this secretly.
He is smart enough to understand that his parents will ultimately choose a gift whose cost is smallest in its row,
but largest in its column.
If no such gift exists, then our little chef has no option but to keep guessing.
As the matrix is huge, he turns to you for help.
He knows that sometimes the gift is not determined uniquely even if a gift exists whose cost is smallest in its row,
but largest in its column.
However, since the boy is so smart, he realizes that the gift's cost is determined uniquely.
Your task is to tell him the gift's cost which is smallest in its row,
but largest in its column, or to tell him no such gift exists.
-----Input-----
First line contains two integers R and C, the number of rows and columns in the matrix respectively. Then follow R lines, each containing C space separated integers - the costs of different gifts.
-----Output-----
Print a single integer - a value in the matrix that is smallest in its row but highest in its column. If no such value exists, then print "GUESS" (without quotes of course)
-----Constraints-----
1 <= R, C <= 100
All gift costs are positive and less than 100000000 (10^8)
-----Example 1-----
Input:
2 3
9 8 8
2 6 11
Output:
8
-----Example 2-----
Input:
3 3
9 8 11
2 6 34
5 9 11
Output:
GUESS
-----Example 3-----
Input:
2 2
10 10
10 10
Output:
10
-----Explanation of Sample Cases-----
Example 1: The first row contains 9, 8, 8. Observe that both 8 are the minimum. Considering the first 8, look at the corresponding column (containing 8 and 6). Here, 8 is the largest element in that column. So it will be chosen.
Example 2: There is no value in the matrix that is smallest in its row but largest in its column.
Example 3: The required gift in matrix is not determined uniquely, but the required cost is determined uniquely.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
try:
r, c = map(int, input().split())
mat = []
for i in range(r):
arr=list(map(int, input().split()))
mat.append(arr)
flag=0
arr1=[]
for x in range(c):
arr = []
for j in range(r):
arr.append(mat[j][x])
arr1.append(arr)
i=0
for i in range(r):
for j in range(c):
if mat[i][j]==max(arr1[j]) and mat[i][j]==min(mat[i]):
flag=1
print(mat[i][j])
break
if flag==1:
break
if flag==0:
print("GUESS")
except:
pass
``` | vfc_19869 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://www.codechef.com/problems/SAD",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2 3\n9 8 8\n2 6 11\n",
"output": "8\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 3\n9 8 11\n2 6 34\n5 9 11\n",
"output": "GUESS\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "2 2\n10 10\n10 10\n",
"output": "10\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2535 | Solve the following coding problem using the programming language python:
A daily train consists of N cars. Let's consider one particular car. It has 54 places numbered consecutively from 1 to 54, some of which are already booked and some are still free. The places are numbered in the following fashion:
The car is separated into 9 compartments of 6 places each, as shown in the picture. So, the 1st compartment consists of places 1, 2, 3, 4, 53 and 54, the 2nd compartment consists of places 5, 6, 7, 8, 51 and 52, and so on.
A group of X friends wants to buy tickets for free places, all of which are in one compartment (it's much funnier to travel together). You are given the information about free and booked places in each of the N cars. Find the number of ways to sell the friends exactly X tickets in one compartment (note that the order in which the tickets are sold doesn't matter).
-----Input-----
The first line of the input contains two integers X and N (1 ≤ X ≤ 6, 1 ≤ N ≤ 10) separated by a single space. Each of the following N lines contains the information about one car which is a string of length 54 consisting of '0' and '1'. The i-th character (numbered from 1) is '0' if place i in the corresponding car is free, and is '1' if place i is already booked.
-----Output-----
Output just one integer -- the requested number of ways.
-----Example-----
Input:
1 3
100101110000001011000001111110010011110010010111000101
001010000000101111100000000000000111101010101111111010
011110011110000001010100101110001011111010001001111010
Output:
85
Input:
6 3
100101110000001011000001111110010011110010010111000101
001010000000101111100000000000000111101010101111111010
011110011110000001010100101110001011111010001001111010
Output:
1
Input:
3 2
000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000
Output:
360
Explanation:
In the first test case, any of the free places can be sold. In the second test case, the only free compartment in the train is compartment 3 in the first car (places 9, 10, 11, 12, 49 and 50 are all free). In the third test case, the train is still absolutely free; as there are 20 ways to sell 3 tickets in an empty compartment, the answer is 2 * 9 * 20 = 360.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
from re import findall
def f(num):
if num == 0:
return 1
else:
return num*f(num-1)
dic = {1: [0,1,2,3,53,52],2: [4,5,6,7,50,51],3: [8,9,10,11,48,49],
4: [12,13,14,15,47,46], 5: [16,17,18,19,44,45],6:[20,21,22,23,42,43],
7:[24,25,26,27,40,41],8:[28,29,30,31,38,39],9:[32,33,34,35,36,37]}
def test():
nonlocal inp,x
net = 0
for com in list(dic.values()):
count = 0
for xx in com:
if inp[xx] == '0':
count += 1
if count >= x:
net += f(count)/f(x)/f(count-x)
return net
x,n = [int(x) for x in findall("\d+",input())]
count,arr = 0,[]
for i in range(n):
inp = input()
count += test()
print(count)
``` | vfc_19873 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://www.codechef.com/COOK19/problems/DAILY",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "1 3\n100101110000001011000001111110010011110010010111000101\n001010000000101111100000000000000111101010101111111010\n011110011110000001010100101110001011111010001001111010\n",
"output": "85\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 3\n100101110000001011000001111110010011110010010111000101\n001010000000101111100000000000000111101010101111111010\n011110011110000001010100101110001011111010001001111010\n",
"output": "1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 2\n000000000000000000000000000000000000000000000000000000\n000000000000000000000000000000000000000000000000000000\n",
"output": "360\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2538 | Solve the following coding problem using the programming language python:
You play your favourite game yet another time. You chose the character you didn't play before. It has $str$ points of strength and $int$ points of intelligence. Also, at start, the character has $exp$ free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by $1$ or raise intelligence by $1$).
Since you'd like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).
Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.
-----Input-----
The first line contains the single integer $T$ ($1 \le T \le 100$) — the number of queries. Next $T$ lines contain descriptions of queries — one per line.
This line contains three integers $str$, $int$ and $exp$ ($1 \le str, int \le 10^8$, $0 \le exp \le 10^8$) — the initial strength and intelligence of the character and the number of free points, respectively.
-----Output-----
Print $T$ integers — one per query. For each query print the number of different character builds you can create.
-----Example-----
Input
4
5 3 4
2 1 0
3 5 5
4 10 6
Output
3
1
2
0
-----Note-----
In the first query there are only three appropriate character builds: $(str = 7, int = 5)$, $(8, 4)$ and $(9, 3)$. All other builds are either too smart or don't use all free points.
In the second query there is only one possible build: $(2, 1)$.
In the third query there are two appropriate builds: $(7, 6)$, $(8, 5)$.
In the fourth query all builds have too much brains.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for TT in range(1, int(input()) + 1):
a, b, c = map(int, input().split())
l = max(-1, (b + c - a) // 2)
r = c
print(r - l if r >= l else 0)
``` | vfc_19885 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1217/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n5 3 4\n2 1 0\n3 5 5\n4 10 6\n",
"output": "3\n1\n2\n0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n1 1 100000000\n100000000 100000000 100000000\n100000000 1 100000000\n1 100000000 100000000\n100000000 100000000 1\n",
"output": "50000000\n50000000\n100000000\n1\n1\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2539 | Solve the following coding problem using the programming language python:
Let's denote as L(x, p) an infinite sequence of integers y such that gcd(p, y) = 1 and y > x (where gcd is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of L(x, p) are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of L(7, 22), respectively.
You have to process t queries. Each query is denoted by three integers x, p and k, and the answer to this query is k-th element of L(x, p).
-----Input-----
The first line contains one integer t (1 ≤ t ≤ 30000) — the number of queries to process.
Then t lines follow. i-th line contains three integers x, p and k for i-th query (1 ≤ x, p, k ≤ 10^6).
-----Output-----
Print t integers, where i-th integer is the answer to i-th query.
-----Examples-----
Input
3
7 22 1
7 22 2
7 22 3
Output
9
13
15
Input
5
42 42 42
43 43 43
44 44 44
45 45 45
46 46 46
Output
187
87
139
128
141
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
prime_factors = [[] for _ in range(10**6 + 1)]
primes=[True for i in range(10**6+1)]
def generate_primes(n):
for p in range(2,n+1):
if primes[p]:
prime_factors[p].append(p)
for i in range(2 * p,n+1,p):
primes[i] = False
prime_factors[i].append(p)
generate_primes(10**6)
result = []
for _ in range(int(input())):
x, p, k = map(int, input().split())
arr = prime_factors[p]
to_add = []
to_subtract = []
for i in range(1, 1 << len(arr)):
mul = 1
count = 0
for j in range(len(arr)):
if (1 << j) & i:
count += 1
mul *= arr[j]
if count%2:
to_add.append(mul)
else:
to_subtract.append(mul)
count_before = 0
for num in to_add:
count_before += x//num
for num in to_subtract:
count_before -= x//num
k += (x - count_before)
low = 0
high = 10**9
answer = high
while low <= high:
mid = (high + low)//2
temp_count = 0
for num in to_add:
temp_count += mid // num
for num in to_subtract:
temp_count -= mid // num
temp_count = mid - temp_count
if temp_count >= k:
answer = min(answer, mid)
high = mid - 1
else:
low = mid + 1
result.append(answer)
print(*result, sep = "\n")
``` | vfc_19889 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/920/G",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n7 22 1\n7 22 2\n7 22 3\n",
"output": "9\n13\n15\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n42 42 42\n43 43 43\n44 44 44\n45 45 45\n46 46 46\n",
"output": "187\n87\n139\n128\n141\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2540 | Solve the following coding problem using the programming language python:
You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour.
Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it's possible that two or more colours will be dominating in the subtree of some vertex.
The subtree of vertex v is the vertex v and all other vertices that contains vertex v in each path to the root.
For each vertex v find the sum of all dominating colours in the subtree of vertex v.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 10^5) — the number of vertices in the tree.
The second line contains n integers c_{i} (1 ≤ c_{i} ≤ n), c_{i} — the colour of the i-th vertex.
Each of the next n - 1 lines contains two integers x_{j}, y_{j} (1 ≤ x_{j}, y_{j} ≤ n) — the edge of the tree. The first vertex is the root of the tree.
-----Output-----
Print n integers — the sums of dominating colours for each vertex.
-----Examples-----
Input
4
1 2 3 4
1 2
2 3
2 4
Output
10 9 3 4
Input
15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13
Output
6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
from collections import defaultdict
class SumDefaultdict(defaultdict):
def __init__(self, *args, **kwargs) -> None:
super().__init__(int, *args, **kwargs)
self.mx = max(self.values())
self.mx_sum = sum(c for c, v in list(self.items()) if v == self.mx)
def sumadd(self, map):
for bb, val in list(map.items()):
if val > 0:
self[bb] += val
if self[bb] > self.mx:
self.mx = self[bb]
self.mx_sum = bb
elif self[bb] == self.mx:
self.mx_sum += bb
def go():
n = int(input())
c = list(map(int, input().split()))
edges = [[] for _ in range(n)]
for _ in range(n - 1):
a, b = [int(x) - 1 for x in input().split()]
edges[a].append(b)
edges[b].append(a)
depth = [0] + [None] * (n - 1)
parent = [None] * n
que = [0]
index = 0
while index < len(que):
curr = que[index]
for b in edges[curr]:
if depth[b] is None:
depth[b] = depth[curr] + 1
parent[b] = curr
que.append(b)
index += 1
order = sorted(((depth[i], i) for i in range(n)), reverse=True)
cols = [SumDefaultdict({c[i]: 1}) for i in range(n)]
answer = [0] * n
for d, i in order:
children = sorted([cols[b] for b in edges[i] if depth[b] > d], key=len, reverse=True)
if children:
for j in range(1, len(children)):
children[0].sumadd(children[j])
children[0].sumadd({c[i]: 1})
cols[i] = children[0]
# max_val = max(cols[i].values())
answer[i] = cols[i].mx_sum
print(' '.join(map(str, answer)))
go()
``` | vfc_19893 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/600/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 2 3 4\n1 2\n2 3\n2 4\n",
"output": "10 9 3 4\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2541 | Solve the following coding problem using the programming language python:
You're given Q queries of the form (L, R).
For each query you have to find the number of such x that L ≤ x ≤ R and there exist integer numbers a > 0, p > 1 such that x = a^{p}.
-----Input-----
The first line contains the number of queries Q (1 ≤ Q ≤ 10^5).
The next Q lines contains two integers L, R each (1 ≤ L ≤ R ≤ 10^18).
-----Output-----
Output Q lines — the answers to the queries.
-----Example-----
Input
6
1 4
9 9
5 7
12 29
137 591
1 1000000
Output
2
1
0
3
17
1111
-----Note-----
In query one the suitable numbers are 1 and 4.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
import math
from math import sqrt
import bisect
small = 10**5+1
big = 10**18+1
pos = []
f = pos.append
#pos.add(1)
j = 2
while True:
#if sqrt(j).is_integer():
# j+=1
# continue
j2 = j*j
i = j*j2
if i>=big:
break
#if int(i**0.5+0.5)**2==i:
# j+=1
# continue
while i<big:
f(i)
i *= j2
j += 1
pos2 = [p for p in pos if not sqrt(p).is_integer()]
pos2.sort()
prev = -1
pos = []
f = pos.append
for p in pos2:
if p==prev:
continue
f(p)
prev = p
q = int(input())
#inp = [int(x) for line in sys.stdin for x in line.split()]
#ii = 0
out = []
for line in sys.stdin:
L,R = line.split()
L = int(L)
R = int(R)
#ii+=2
a = -1
b = len(pos)
while b-a>1:
m = (a+b)//2
if pos[m]<L:
a=m
else:
b=m
under = a
#under = bisect.bisect_left(pos,L)
#if under<len(pos) and pos[under]>=L:
# under -= 1
a = -1
b = len(pos)
while b-a>1:
m = (a+b)//2
if pos[m]<=R:
a = m
else:
b = m
upper = a
#upper = bisect.bisect_left(pos,R+1)-1
Lupp = max(int(sqrt(L)-1),0)
while Lupp*Lupp<L:
Lupp+=1
Rdown = int(sqrt(R))+1
while Rdown*Rdown>R:
Rdown-=1
count = max(0,Rdown-Lupp+1)
out.append(str(upper-under+count))
print('\n'.join(out))
``` | vfc_19897 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/955/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6\n1 4\n9 9\n5 7\n12 29\n137 591\n1 1000000\n",
"output": "2\n1\n0\n3\n17\n1111\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "20\n862 928\n758 964\n541 789\n622 943\n328 900\n14 764\n217 972\n461 847\n442 468\n900 986\n518 529\n938 993\n549 851\n690 944\n484 601\n320 910\n98 868\n816 915\n765 880\n551 770\n",
"output": "1\n4\n5\n6\n14\n32\n20\n9\n0\n2\n1\n1\n6\n4\n4\n15\n26\n2\n2\n4\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2542 | Solve the following coding problem using the programming language python:
Let's call left cyclic shift of some string $t_1 t_2 t_3 \dots t_{n - 1} t_n$ as string $t_2 t_3 \dots t_{n - 1} t_n t_1$.
Analogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \dots t_{n - 1}$.
Let's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift.
You are given string $s$ which consists of digits 0–9.
What is the minimum number of characters you need to erase from $s$ to make it good?
-----Input-----
The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contains test cases — one per line. The first and only line of each test case contains string $s$ ($2 \le |s| \le 2 \cdot 10^5$). Each character $s_i$ is digit 0–9.
It's guaranteed that the total length of strings doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the minimum number of characters you need to erase from $s$ to make it good.
-----Example-----
Input
3
95831
100120013
252525252525
Output
3
5
0
-----Note-----
In the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good.
In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good.
In the third test case, the given string $s$ is already good.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def det(s, i, j):
ans = 0
curr = i
for a in s:
if a == curr:
ans += 1
if curr == i: curr = j
else: curr = i
if i == j: return ans
return ans // 2 * 2
for t in range(int(input())):
s = list(map(int, list(input())))
ans = 0
for i in range(10):
for j in range(10):
ans = max(ans, det(s, i, j))
print(len(s)-ans)
``` | vfc_19901 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1389/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n95831\n100120013\n252525252525\n",
"output": "3\n5\n0\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2543 | Solve the following coding problem using the programming language python:
Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array a of length n and array b of length m. He introduced a function f(j) which is defined for integers j, which satisfy 0 ≤ j ≤ m - n. Suppose, c_{i} = a_{i} - b_{i} + j. Then f(j) = |c_1 - c_2 + c_3 - c_4... c_{n}|. More formally, $f(j) =|\sum_{i = 1}^{n}(- 1)^{i - 1} *(a_{i} - b_{i + j})|$.
Dr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid j. They found it a bit easy, so Dr. Evil made their task harder. He will give them q update queries. During each update they should add an integer x_{i} to all elements in a in range [l_{i};r_{i}] i.e. they should add x_{i} to a_{l}_{i}, a_{l}_{i} + 1, ... , a_{r}_{i} and then they should calculate the minimum value of f(j) for all valid j.
Please help Mahmoud and Ehab.
-----Input-----
The first line contains three integers n, m and q (1 ≤ n ≤ m ≤ 10^5, 1 ≤ q ≤ 10^5) — number of elements in a, number of elements in b and number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_{n}. ( - 10^9 ≤ a_{i} ≤ 10^9) — elements of a.
The third line contains m integers b_1, b_2, ..., b_{m}. ( - 10^9 ≤ b_{i} ≤ 10^9) — elements of b.
Then q lines follow describing the queries. Each of them contains three integers l_{i} r_{i} x_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n, - 10^9 ≤ x ≤ 10^9) — range to be updated and added value.
-----Output-----
The first line should contain the minimum value of the function f before any update.
Then output q lines, the i-th of them should contain the minimum value of the function f after performing the i-th update .
-----Example-----
Input
5 6 3
1 2 3 4 5
1 2 3 4 5 6
1 1 10
1 1 -9
1 5 -1
Output
0
9
0
0
-----Note-----
For the first example before any updates it's optimal to choose j = 0, f(0) = |(1 - 1) - (2 - 2) + (3 - 3) - (4 - 4) + (5 - 5)| = |0| = 0.
After the first update a becomes {11, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(11 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6) = |9| = 9.
After the second update a becomes {2, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(2 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6)| = |0| = 0.
After the third update a becomes {1, 1, 2, 3, 4} and it's optimal to choose j = 0, f(0) = |(1 - 1) - (1 - 2) + (2 - 3) - (3 - 4) + (4 - 5)| = |0| = 0.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def read(): return list(map(int, input().split(' ')))
n, m, q = read()
aa = read()
bb = read()
reqs = [read() for _ in range(q)]
asum = 0
bsum = 0
for i, (a, b) in enumerate(zip(aa, bb)):
asum += a if i % 2 == 0 else -a
bsum += b if i % 2 == 0 else -b
bpos = [bsum]
for i in range(len(aa), len(bb)):
b = bb[i]
rempos = i - len(aa)
bsum += b if i % 2 == 0 else -b
bsum -= bb[rempos] if rempos % 2 == 0 else -bb[rempos]
bpos += [bsum if rempos % 2 == 1 else -bsum]
bpos = sorted(set(bpos))
def closest(arr, value):
l = 0
r = len(arr)
while l + 1 < r:
m = (l + r) // 2
if arr[m] <= value:
l = m
else:
r = m
res = arr[l]
if l + 1 < len(arr) and abs(arr[l + 1] - value) < abs(arr[l] - value):
res = arr[l + 1]
return res
print(abs(asum - closest(bpos, asum)))
for req in reqs:
l, r, x = req
l -= 1
if (r - l) % 2 != 0:
asum += x if l % 2 == 0 else -x
print(abs(asum - closest(bpos, asum)))
``` | vfc_19905 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/862/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 6 3\n1 2 3 4 5\n1 2 3 4 5 6\n1 1 10\n1 1 -9\n1 5 -1\n",
"output": "0\n9\n0\n0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1 1 1\n937982044\n179683049\n1 1 821220804\n",
"output": "758298995\n1579519799\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2545 | Solve the following coding problem using the programming language python:
You are given two integers $a$ and $b$. You may perform any number of operations on them (possibly zero).
During each operation you should choose any positive integer $x$ and set $a := a - x$, $b := b - 2x$ or $a := a - 2x$, $b := b - x$. Note that you may choose different values of $x$ in different operations.
Is it possible to make $a$ and $b$ equal to $0$ simultaneously?
Your program should answer $t$ independent test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases.
Then the test cases follow, each test case is represented by one line containing two integers $a$ and $b$ for this test case ($0 \le a, b \le 10^9$).
-----Output-----
For each test case print the answer to it — YES if it is possible to make $a$ and $b$ equal to $0$ simultaneously, and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
-----Example-----
Input
3
6 9
1 1
1 2
Output
YES
NO
YES
-----Note-----
In the first test case of the example two operations can be used to make both $a$ and $b$ equal to zero: choose $x = 4$ and set $a := a - x$, $b := b - 2x$. Then $a = 6 - 4 = 2$, $b = 9 - 8 = 1$; choose $x = 1$ and set $a := a - 2x$, $b := b - x$. Then $a = 2 - 2 = 0$, $b = 1 - 1 = 0$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
t=int(input())
for nt in range(t):
a,b=[int(k) for k in input().strip().split(" ")]
if (a+b)%3 or 2*a<b or 2*b<a:
print("NO")
else:
print("YES")
``` | vfc_19913 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1260/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n6 9\n1 1\n1 2\n",
"output": "YES\nNO\nYES\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n69 69\n",
"output": "YES\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2546 | Solve the following coding problem using the programming language python:
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).
You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$.
It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$.
Note that you don't have to spend all your $s$ dollars on salaries.
You have to answer $t$ test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases.
The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.
The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$).
It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$.
-----Output-----
For each test case print one integer — the maximum median salary that you can obtain.
-----Example-----
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
-----Note-----
In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.
In the second test case, you have to pay $1337$ dollars to the only employee.
In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
input = sys.stdin.readline
t=int(input())
for test in range(t):
n,s=list(map(int,input().split()))
LR=[tuple(map(int,input().split())) for i in range(n)]
LR.sort(reverse=True)
R=[r for l,r in LR]
R.sort()
#print(LR,R)
MIN=LR[n//2][0]
MAX=R[n//2]
OK=(n+1)//2
while MIN!=MAX:
mid=(MIN+MAX+1)//2
#print(MIN,MAX,mid)
count=0
money=0
for l,r in LR:
if count<OK:
if r>=mid:
money+=max(l,mid)
count+=1
else:
money+=l
else:
money+=l
if count>=OK and money<=s:
MIN=mid
else:
MAX=mid-1
print(MIN)
``` | vfc_19917 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1251/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n3 26\n10 12\n1 4\n10 11\n1 1337\n1 1000000000\n5 26\n4 4\n2 4\n6 8\n5 6\n2 7\n",
"output": "11\n1337\n6\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2547 | Solve the following coding problem using the programming language python:
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read exactly $m$ books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ — the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not).
So they need to choose exactly $m$ books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these $m$ books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
-----Input-----
The first line of the input contains three integers $n$, $m$ and $k$ ($1 \le k \le m \le n \le 2 \cdot 10^5$).
The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ — the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise.
-----Output-----
If there is no solution, print only one integer -1.
If the solution exists, print $T$ in the first line — the minimum total reading time of the suitable set of books. In the second line print $m$ distinct integers from $1$ to $n$ in any order — indices of books which are in the set you found.
If there are several answers, print any of them.
-----Examples-----
Input
6 3 1
6 0 0
11 1 0
9 0 1
21 1 1
10 1 0
8 0 1
Output
24
6 5 1
Input
6 3 2
6 0 0
11 1 0
9 0 1
21 1 1
10 1 0
8 0 1
Output
39
4 6 5
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
# input = raw_input
# range = xrange
import sys
inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
seg = [0]*200000
def offset(x):
return x + 100000
def encode(x, y):
return x*200002 + y
def decode(x):
return x//200002, x%200002
def upd(node, L, R, pos, val):
while L < R:
seg[node] += val
seg[offset(node)] += val*pos
if L+1 == R:
break
M = (L+R)//2
node <<= 1
if pos < M:
R = M
else:
L = M
node += 1
def query(node, L, R, k):
ret = 0
while L < R:
if k == 0:
return ret
if seg[node] == k:
return ret + seg[offset(node)]
if L+1 == R:
return ret + k*L
M = (L+R)//2
node <<= 1
if seg[node] >= k:
R = M
else:
ret += seg[offset(node)]
k -= seg[node]
L = M
node += 1
return ret
n, m, k = inp[ii:ii+3]; ii += 3
A, B, both, neither = [], [], [], []
for i in range(n):
t, a, b = inp[ii:ii+3]; ii += 3
if a == 0 and b == 0:
neither.append(encode(t, i+1))
if a == 1 and b == 0:
A.append(encode(t, i+1))
if a == 0 and b == 1:
B.append(encode(t, i+1))
if a == 1 and b == 1:
both.append(encode(t, i+1))
upd(1, 0, 10001, t, 1)
A.sort(); B.sort(); both.sort()
p1 = min(k, len(both))
p2 = k - p1
if 2*k - p1 > m or p2 > min(len(A), len(B)):
print(-1)
return
sum, ans, ch = 0, 2**31, p1
for i in range(p1):
sum += both[i]//200002
upd(1, 0, 10001, both[i]//200002, -1)
for i in range(p2):
sum += A[i]//200002 + B[i]//200002
upd(1, 0, 10001, A[i]//200002, -1)
upd(1, 0, 10001, B[i]//200002, -1)
ans = query(1, 0, 10001, m-2*k+p1) + sum
while p1 > 0:
if p2 == min(len(A), len(B)):
break
upd(1, 0, 10001, A[p2]//200002, -1); sum += A[p2]//200002
upd(1, 0, 10001, B[p2]//200002, -1); sum += B[p2]//200002
upd(1, 0, 10001, both[p1-1]//200002, 1); sum -= both[p1-1]//200002
p2 += 1
p1 -= 1
if m - 2*k + p1 < 0:
break
Q = query(1, 0, 10001, m-2*k+p1)
if ans > sum + Q:
ans = sum + Q
ch = p1
print(ans)
ind = [both[i]%200002 for i in range(ch)] + [A[i]%200002 for i in range(k-ch)] + [B[i]%200002 for i in range(k-ch)]
st = neither + [both[i] for i in range(ch, len(both))] + [A[i] for i in range(k-ch, len(A))] + [B[i] for i in range(k-ch, len(B))]
st.sort()
ind += [st[i]%200002 for i in range(m-2*k+ch)]
print (' '.join(str(x) for x in ind))
``` | vfc_19921 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1374/E2",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1",
"output": "24\n6 5 1",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6 3 2\n\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1",
"output": "39\n4 6 5",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2548 | Solve the following coding problem using the programming language python:
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
n = int(input())
s = input()
d = {0: 1}
summa, cnt = 0, 0
ans = 0
for i in s:
summa += int(i)
cnt += 1
k = cnt - summa
if k not in d:
d[k] = 0
ans += d[k]
d[k] += 1
print(ans)
``` | vfc_19925 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1398/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n3\n120\n5\n11011\n6\n600005\n",
"output": "3\n6\n1\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2549 | Solve the following coding problem using the programming language python:
You are playing a computer game. In this game, you have to fight $n$ monsters.
To defend from monsters, you need a shield. Each shield has two parameters: its current durability $a$ and its defence rating $b$. Each monster has only one parameter: its strength $d$.
When you fight a monster with strength $d$ while having a shield with current durability $a$ and defence $b$, there are three possible outcomes: if $a = 0$, then you receive $d$ damage; if $a > 0$ and $d \ge b$, you receive no damage, but the current durability of the shield decreases by $1$; if $a > 0$ and $d < b$, nothing happens.
The $i$-th monster has strength $d_i$, and you will fight each of the monsters exactly once, in some random order (all $n!$ orders are equiprobable). You have to consider $m$ different shields, the $i$-th shield has initial durability $a_i$ and defence rating $b_i$. For each shield, calculate the expected amount of damage you will receive if you take this shield and fight the given $n$ monsters in random order.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of monsters and the number of shields, respectively.
The second line contains $n$ integers $d_1$, $d_2$, ..., $d_n$ ($1 \le d_i \le 10^9$), where $d_i$ is the strength of the $i$-th monster.
Then $m$ lines follow, the $i$-th of them contains two integers $a_i$ and $b_i$ ($1 \le a_i \le n$; $1 \le b_i \le 10^9$) — the description of the $i$-th shield.
-----Output-----
Print $m$ integers, where the $i$-th integer represents the expected damage you receive with the $i$-th shield as follows: it can be proven that, for each shield, the expected damage is an irreducible fraction $\dfrac{x}{y}$, where $y$ is coprime with $998244353$. You have to print the value of $x \cdot y^{-1} \bmod 998244353$, where $y^{-1}$ is the inverse element for $y$ ($y \cdot y^{-1} \bmod 998244353 = 1$).
-----Examples-----
Input
3 2
1 3 1
2 1
1 2
Output
665496237
1
Input
3 3
4 2 6
3 1
1 2
2 3
Output
0
8
665496236
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys,bisect
input = sys.stdin.readline
MOD = 998244353
def frac(a,b):
inv = pow(b, MOD - 2, MOD)
return (a * inv) % MOD
n, m = list(map(int, input().split()))
d = list(map(int, input().split()))
d.sort()
tot = sum(d)
pref = [0]
for v in d:
pref.append(pref[-1] + v)
out = [0] * m
for _ in range(m):
a, b = list(map(int, input().split()))
ind = bisect.bisect_left(d, b)
sum_before = pref[ind]
rest = tot - pref[ind]
if a <= (n - ind):
out[_] += sum_before * frac(n - ind - a + 1 ,n - ind + 1)
out[_] += rest * frac(n - ind - a ,n - ind)
out[_] %= MOD
print('\n'.join(map(str,out)))
``` | vfc_19929 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1418/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3 2\n1 3 1\n2 1\n1 2\n",
"output": "665496237\n1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3 3\n4 2 6\n3 1\n1 2\n2 3\n",
"output": "0\n8\n665496236\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2550 | Solve the following coding problem using the programming language python:
$n$ students are taking an exam. The highest possible score at this exam is $m$. Let $a_{i}$ be the score of the $i$-th student. You have access to the school database which stores the results of all students.
You can change each student's score as long as the following conditions are satisfied: All scores are integers $0 \leq a_{i} \leq m$ The average score of the class doesn't change.
You are student $1$ and you would like to maximize your own score.
Find the highest possible score you can assign to yourself such that all conditions are satisfied.
-----Input-----
Each test contains multiple test cases.
The first line contains the number of test cases $t$ ($1 \le t \le 200$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \leq n \leq 10^{3}$, $1 \leq m \leq 10^{5}$) — the number of students and the highest possible score respectively.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($ 0 \leq a_{i} \leq m$) — scores of the students.
-----Output-----
For each testcase, output one integer — the highest possible score you can assign to yourself such that both conditions are satisfied._
-----Example-----
Input
2
4 10
1 2 3 4
4 5
1 2 3 4
Output
10
5
-----Note-----
In the first case, $a = [1,2,3,4] $, with average of $2.5$. You can change array $a$ to $[10,0,0,0]$. Average remains $2.5$, and all conditions are satisfied.
In the second case, $0 \leq a_{i} \leq 5$. You can change $a$ to $[5,1,1,3]$. You cannot increase $a_{1}$ further as it will violate condition $0\le a_i\le m$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
n, m = map(int, input().split())
l1 = list(map(int,input().split()))
print(min(sum(l1),m))
``` | vfc_19933 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1316/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n4 10\n1 2 3 4\n4 5\n1 2 3 4\n",
"output": "10\n5\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "27\n1 1\n0\n1 2\n0\n1 5\n0\n1 10\n0\n1 50\n0\n1 100\n0\n1 500\n0\n1 1000\n0\n1 5000\n0\n1 10000\n0\n1 50000\n0\n1 100000\n0\n1 5\n4\n1 5\n5\n1 10\n9\n1 10\n10\n1 100000\n9999\n1 100000\n100000\n1 4999\n386\n1 100000\n1\n4 5\n1 0 0 0\n4 5\n0 1 0 0\n4 5\n0 0 0 0\n4 5\n5 5 5 5\n4 5\n4 4 5 5\n4 5\n5 4 4 4\n4 5\n4 0 0 0\n",
"output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n4\n5\n9\n10\n9999\n100000\n386\n1\n1\n1\n0\n5\n5\n5\n4\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2552 | Solve the following coding problem using the programming language python:
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are $n$ cities and $n−1$ undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from $1$ to $n$ and the city $1$ is a capital. In other words, the country has a tree structure.
There are $m$ citizens living in the country. A $p_i$ people live in the $i$-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the $i$-th city calculates a happiness index $h_i$ as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city $i$ the happiness index will be equal exactly to $h_i$".
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^5$; $0 \le m \le 10^9$) — the number of cities and citizens.
The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_{n}$ ($0 \le p_i \le m$; $p_1 + p_2 + \ldots + p_{n} = m$), where $p_i$ is the number of people living in the $i$-th city.
The third line contains $n$ integers $h_1, h_2, \ldots, h_{n}$ ($-10^9 \le h_i \le 10^9$), where $h_i$ is the calculated happiness index of the $i$-th city.
Next $n − 1$ lines contain description of the roads, one per line. Each line contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \neq y_i$), where $x_i$ and $y_i$ are cities connected by the $i$-th road.
It's guaranteed that the sum of $n$ from all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES, if the collected data is correct, or NO — otherwise. You can print characters in YES or NO in any case.
-----Examples-----
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
-----Note-----
Let's look at the first test case of the first sample: [Image]
At first, all citizens are in the capital. Let's describe one of possible scenarios: a person from city $1$: he lives in the capital and is in good mood; a person from city $4$: he visited cities $1$ and $4$, his mood was ruined between cities $1$ and $4$; a person from city $3$: he visited cities $1$ and $3$ in good mood; a person from city $6$: he visited cities $1$, $3$ and $6$, his mood was ruined between cities $1$ and $3$; In total, $h_1 = 4 - 0 = 4$, $h_2 = 0$, $h_3 = 1 - 1 = 0$, $h_4 = 0 - 1 = -1$, $h_5 = 0$, $h_6 = 0 - 1 = -1$, $h_7 = 0$.
The second case of the first test: $\text{of}_{0}$
All people have already started in bad mood in the capital — this is the only possible scenario.
The first case of the second test: $\text{of} 0$
The second case of the second test: [Image]
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
input = sys.stdin.readline
T = int(input())
for testcase in range(1,T+1):
n,m = map(int,input().split())
p = tuple(map(int,input().split()))
h = tuple(map(int,input().split()))
a = [0]*n
b = [0]*n
edge = [[] for i in range(n)]
for _ in range(n-1):
x,y = map(int,input().split())
edge[x-1].append(y-1)
edge[y-1].append(x-1)
par = [-1]*n
tank = [0]
order = []
while tank:
now = tank.pop()
order.append(now)
for e in edge[now]:
if par[now] != e:
par[e] = now
tank.append(e)
flag = True
for e in order[::-1]:
if (a[e]-b[e]-p[e] <= h[e] <= a[e]+b[e]+p[e]) and (h[e]+a[e]+b[e]+p[e])%2 == 0:
if e != 0:
a[par[e]] += (h[e]+a[e]+b[e]+p[e])//2
b[par[e]] += (h[e]+a[e]+b[e]+p[e])//2 - h[e]
else:
flag = False
break
if flag:
print("YES")
else:
print("NO")
``` | vfc_19941 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1388/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n7 4\n1 0 1 1 0 1 0\n4 0 0 -1 0 -1 0\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n5 11\n1 2 5 2 1\n-11 -2 -6 -2 -1\n1 2\n1 3\n1 4\n3 5\n",
"output": "YES\nYES\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2553 | Solve the following coding problem using the programming language python:
Shubham has an array $a$ of size $n$, and wants to select exactly $x$ elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
-----Input-----
The first line of the input contains a single integer $t$ $(1\le t \le 100)$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $x$ $(1 \le x \le n \le 1000)$ — the length of the array and the number of elements you need to choose.
The next line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 1000)$ — elements of the array.
-----Output-----
For each test case, print "Yes" or "No" depending on whether it is possible to choose $x$ elements such that their sum is odd.
You may print every letter in any case you want.
-----Example-----
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
-----Note-----
For $1$st case: We must select element $999$, and the sum is odd.
For $2$nd case: We must select element $1000$, so overall sum is not odd.
For $3$rd case: We can select element $51$.
For $4$th case: We must select both elements $50$ and $51$ — so overall sum is odd.
For $5$th case: We must select all elements — but overall sum is not odd.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
q = int(input())
for ii in range(q):
n,x = map(int,input().split())
l = list(map(int,input().split()))
par = 0
npar = 0
for i in l:
if i%2 == 1:
npar += 1
else:
par += 1
if x == n:
if npar%2 == 1:
print("Yes")
else:
print("No")
else:
if npar > 0 and par > 0:
print("Yes")
else:
if par == 0:
if x%2 == 1:
print("Yes")
else:
print("No")
else:
print("No")
``` | vfc_19945 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1363/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5\n1 1\n999\n1 1\n1000\n2 1\n51 50\n2 2\n51 50\n3 3\n101 102 103\n",
"output": "Yes\nNo\nYes\nYes\nNo\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n3 1\n16 11 12\n10 3\n13 12 9 1 9 8 4 19 16 19\n3 2\n3 2 6\n9 7\n11 14 1 6 3 12 3 20 16\n",
"output": "Yes\nYes\nYes\nYes\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2554 | Solve the following coding problem using the programming language python:
You are given an array $a$ consisting of $n$ integers. Indices of the array start from zero (i. e. the first element is $a_0$, the second one is $a_1$, and so on).
You can reverse at most one subarray (continuous subsegment) of this array. Recall that the subarray of $a$ with borders $l$ and $r$ is $a[l; r] = a_l, a_{l + 1}, \dots, a_{r}$.
Your task is to reverse such a subarray that the sum of elements on even positions of the resulting array is maximized (i. e. the sum of elements $a_0, a_2, \dots, a_{2k}$ for integer $k = \lfloor\frac{n-1}{2}\rfloor$ should be maximum possible).
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. The second line of the test case contains $n$ integers $a_0, a_1, \dots, a_{n-1}$ ($1 \le a_i \le 10^9$), where $a_i$ is the $i$-th element of $a$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer on the separate line — the maximum possible sum of elements on even positions after reversing at most one subarray (continuous subsegment) of $a$.
-----Example-----
Input
4
8
1 7 3 4 7 6 2 9
5
1 2 1 2 1
10
7 8 4 5 7 6 8 9 7 3
4
3 1 2 1
Output
26
5
37
5
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def subsolve(a):
c = 0
m = 0
for i in a:
c += i
if c < 0:
c = 0
m = max(m, c)
return m
def solve():
n = int(input())
a = list(map(int, input().split()))
s = 0
for i in range(0, n, 2):
s += a[i]
u = subsolve([a[i] - a[i - 1] for i in range(1, n, 2)])
v = subsolve([a[i - 1] - a[i] for i in range(2, n, 2)])
print(max(u, v) + s)
t = int(input())
for _ in range(t):
solve()
``` | vfc_19949 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1373/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n8\n1 7 3 4 7 6 2 9\n5\n1 2 1 2 1\n10\n7 8 4 5 7 6 8 9 7 3\n4\n3 1 2 1\n",
"output": "26\n5\n37\n5\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n5\n17 6 4 4 4\n7\n19 5 13 11 12 13 5\n1\n213567876\n",
"output": "27\n57\n213567876\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2555 | Solve the following coding problem using the programming language python:
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved.
Pikachu is a cute and friendly pokémon living in the wild pikachu herd.
But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist.
First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct.
As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$.
The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$.
Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon.
Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation.
Help Andrew and the pokémon, or team R will realize their tricky plan!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively.
The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon.
$i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap.
-----Example-----
Input
3
3 1
1 3 2
1 2
2 2
1 2
1 2
1 2
7 5
1 2 5 4 3 6 7
1 2
6 7
3 4
1 2
2 3
Output
3
4
2
2
2
9
10
10
10
9
11
-----Note-----
Let's look at the third test case:
Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$.
After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$.
After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$.
After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$.
After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$.
After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
input = lambda: sys.stdin.readline().rstrip()
ANS = []
T = int(input())
for _ in range(T):
N, Q = list(map(int, input().split()))
A = [int(a) for a in input().split()]
ans = sum(max(b - a, 0) for a, b in zip([0] + A, A))
ANS.append(ans)
for _ in range(Q):
l, r = list(map(int, input().split()))
l, r = l-1, r-1
if l == r:
ANS.append(ans)
continue
ans -= max(A[l] - A[l-1], 0) if l else A[l]
if l < N - 1: ans -= max(A[l+1] - A[l], 0)
if r > l + 1: ans -= max(A[r] - A[r-1], 0)
if r < N - 1: ans -= max(A[r+1] - A[r], 0)
A[l], A[r] = A[r], A[l]
ans += max(A[l] - A[l-1], 0) if l else A[l]
if l < N - 1: ans += max(A[l+1] - A[l], 0)
if r > l + 1: ans += max(A[r] - A[r-1], 0)
if r < N - 1: ans += max(A[r+1] - A[r], 0)
ANS.append(ans)
print("\n".join(map(str, ANS)))
``` | vfc_19953 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1420/C2",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n3 1\n1 3 2\n1 2\n2 2\n1 2\n1 2\n1 2\n7 5\n1 2 5 4 3 6 7\n1 2\n6 7\n3 4\n1 2\n2 3\n",
"output": "3\n4\n2\n2\n2\n9\n10\n10\n10\n9\n11\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "5\n2 0\n1 2\n2 1\n1 2\n1 2\n2 1\n2 1\n1 2\n3 9\n1 2 3\n1 2\n1 3\n2 3\n1 2\n1 3\n2 3\n1 2\n1 3\n2 3\n3 0\n3 2 1\n",
"output": "2\n2\n2\n2\n2\n3\n4\n4\n3\n3\n3\n3\n4\n4\n3\n3\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2556 | Solve the following coding problem using the programming language python:
Several days ago you bought a new house and now you are planning to start a renovation. Since winters in your region can be very cold you need to decide how to heat rooms in your house.
Your house has $n$ rooms. In the $i$-th room you can install at most $c_i$ heating radiators. Each radiator can have several sections, but the cost of the radiator with $k$ sections is equal to $k^2$ burles.
Since rooms can have different sizes, you calculated that you need at least $sum_i$ sections in total in the $i$-th room.
For each room calculate the minimum cost to install at most $c_i$ radiators with total number of sections not less than $sum_i$.
-----Input-----
The first line contains single integer $n$ ($1 \le n \le 1000$) — the number of rooms.
Each of the next $n$ lines contains the description of some room. The $i$-th line contains two integers $c_i$ and $sum_i$ ($1 \le c_i, sum_i \le 10^4$) — the maximum number of radiators and the minimum total number of sections in the $i$-th room, respectively.
-----Output-----
For each room print one integer — the minimum possible cost to install at most $c_i$ radiators with total number of sections not less than $sum_i$.
-----Example-----
Input
4
1 10000
10000 1
2 6
4 6
Output
100000000
1
18
10
-----Note-----
In the first room, you can install only one radiator, so it's optimal to use the radiator with $sum_1$ sections. The cost of the radiator is equal to $(10^4)^2 = 10^8$.
In the second room, you can install up to $10^4$ radiators, but since you need only one section in total, it's optimal to buy one radiator with one section.
In the third room, there $7$ variants to install radiators: $[6, 0]$, $[5, 1]$, $[4, 2]$, $[3, 3]$, $[2, 4]$, $[1, 5]$, $[0, 6]$. The optimal variant is $[3, 3]$ and it costs $3^2+ 3^2 = 18$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
"""This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead."""
n = int(input())
for _ in range(n):
c, s = list(map(int, input().split()))
a = s // c
r = s % c
print((c - r) * a ** 2 + r * (a + 1) ** 2)
``` | vfc_19957 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1260/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1 10000\n10000 1\n2 6\n4 6\n",
"output": "100000000\n1\n18\n10\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "69\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n2 6\n",
"output": "18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2557 | Solve the following coding problem using the programming language python:
Shuseki Kingdom is the world's leading nation for innovation and technology. There are n cities in the kingdom, numbered from 1 to n.
Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city x to city y cannot be used to travel from city y to city x. The transportation within each city is extremely developed, therefore if a pipe from city x to city y and a pipe from city y to city z are both constructed, people will be able to travel from city x to city z instantly.
Mr. Kitayuta is also involved in national politics. He considers that the transportation between the m pairs of city (a_{i}, b_{i}) (1 ≤ i ≤ m) is important. He is planning to construct teleportation pipes so that for each important pair (a_{i}, b_{i}), it will be possible to travel from city a_{i} to city b_{i} by using one or more teleportation pipes (but not necessarily from city b_{i} to city a_{i}). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities.
-----Input-----
The first line contains two space-separated integers n and m (2 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^5), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively.
The following m lines describe the important pairs. The i-th of them (1 ≤ i ≤ m) contains two space-separated integers a_{i} and b_{i} (1 ≤ a_{i}, b_{i} ≤ n, a_{i} ≠ b_{i}), denoting that it must be possible to travel from city a_{i} to city b_{i} by using one or more teleportation pipes (but not necessarily from city b_{i} to city a_{i}). It is guaranteed that all pairs (a_{i}, b_{i}) are distinct.
-----Output-----
Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta's purpose.
-----Examples-----
Input
4 5
1 2
1 3
1 4
2 3
2 4
Output
3
Input
4 6
1 2
1 4
2 3
2 4
3 2
3 4
Output
4
-----Note-----
For the first sample, one of the optimal ways to construct pipes is shown in the image below: [Image]
For the second sample, one of the optimal ways is shown below: [Image]
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def main():
n, m = list(map(int, input().split()))
n += 1
cluster, dest, ab = list(range(n)), [0] * n, [[] for _ in range(n)]
def root(x):
if x != cluster[x]:
cluster[x] = x = root(cluster[x])
return x
for _ in range(m):
a, b = list(map(int, input().split()))
ab[a].append(b)
dest[b] += 1
cluster[root(a)] = root(b)
pool = [a for a, f in enumerate(dest) if not f]
for a in pool:
for b in ab[a]:
dest[b] -= 1
if not dest[b]:
pool.append(b)
ab = [True] * n
for a, f in enumerate(dest):
if f:
ab[root(a)] = False
print(n - sum(f and a == c for a, c, f in zip(list(range(n)), cluster, ab)))
def __starting_point():
from sys import setrecursionlimit
setrecursionlimit(100500)
main()
__starting_point()
``` | vfc_19961 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/505/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4 5\n1 2\n1 3\n1 4\n2 3\n2 4",
"output": "3\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4",
"output": "4\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2558 | Solve the following coding problem using the programming language python:
Meka-Naruto plays a computer game. His character has the following ability: given an enemy hero, deal $a$ instant damage to him, and then heal that enemy $b$ health points at the end of every second, for exactly $c$ seconds, starting one second after the ability is used. That means that if the ability is used at time $t$, the enemy's health decreases by $a$ at time $t$, and then increases by $b$ at time points $t + 1$, $t + 2$, ..., $t + c$ due to this ability.
The ability has a cooldown of $d$ seconds, i. e. if Meka-Naruto uses it at time moment $t$, next time he can use it is the time $t + d$. Please note that he can only use the ability at integer points in time, so all changes to the enemy's health also occur at integer times only.
The effects from different uses of the ability may stack with each other; that is, the enemy which is currently under $k$ spells gets $k\cdot b$ amount of heal this time. Also, if several health changes occur at the same moment, they are all counted at once.
Now Meka-Naruto wonders if he can kill the enemy by just using the ability each time he can (that is, every $d$ seconds). The enemy is killed if their health points become $0$ or less. Assume that the enemy's health is not affected in any way other than by Meka-Naruto's character ability. What is the maximal number of health points the enemy can have so that Meka-Naruto is able to kill them?
-----Input-----
The first line contains an integer $t$ ($1\leq t\leq 10^5$) standing for the number of testcases.
Each test case is described with one line containing four numbers $a$, $b$, $c$ and $d$ ($1\leq a, b, c, d\leq 10^6$) denoting the amount of instant damage, the amount of heal per second, the number of heals and the ability cooldown, respectively.
-----Output-----
For each testcase in a separate line print $-1$ if the skill can kill an enemy hero with an arbitrary number of health points, otherwise print the maximal number of health points of the enemy that can be killed.
-----Example-----
Input
7
1 1 1 1
2 2 2 2
1 2 3 4
4 3 2 1
228 21 11 3
239 21 11 3
1000000 1 1000000 1
Output
1
2
1
5
534
-1
500000500000
-----Note-----
In the first test case of the example each unit of damage is cancelled in a second, so Meka-Naruto cannot deal more than 1 damage.
In the fourth test case of the example the enemy gets: $4$ damage ($1$-st spell cast) at time $0$; $4$ damage ($2$-nd spell cast) and $3$ heal ($1$-st spell cast) at time $1$ (the total of $5$ damage to the initial health); $4$ damage ($3$-nd spell cast) and $6$ heal ($1$-st and $2$-nd spell casts) at time $2$ (the total of $3$ damage to the initial health); and so on.
One can prove that there is no time where the enemy gets the total of $6$ damage or more, so the answer is $5$. Please note how the health is recalculated: for example, $8$-health enemy would not die at time $1$, as if we first subtracted $4$ damage from his health and then considered him dead, before adding $3$ heal.
In the sixth test case an arbitrarily healthy enemy can be killed in a sufficient amount of time.
In the seventh test case the answer does not fit into a 32-bit integer type.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
# sys.stdin = open('in.txt')
s = sys.stdin.read().split()
p = 0
def getSm(k, a, b, c, d):
return (k + 1) * a - (k * (k + 1) >> 1) * b * d
t = int(s[p])
p += 1
res = []
for _ in range(t):
a = int(s[p])
p += 1
b = int(s[p])
p += 1
c = int(s[p])
p += 1
d = int(s[p])
p += 1
if a - b * c > 0:
res.append(-1)
elif d >= c:
res.append(a)
else:
dn = 0
up = int(1e6) + 1
while up - dn > 1:
md = (up + dn) >> 1
if getSm(md, a, b, c, d) < getSm(md + 1, a, b, c, d):
dn = md
else:
up = md
ans = max(a, getSm(dn, a, b, c, d), getSm(up, a, b, c, d))
res.append(ans)
print('\n'.join(map(str, res)))
``` | vfc_19965 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1413/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7\n1 1 1 1\n2 2 2 2\n1 2 3 4\n4 3 2 1\n228 21 11 3\n239 21 11 3\n1000000 1 1000000 1\n",
"output": "1\n2\n1\n5\n534\n-1\n500000500000\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1000000 1000000 1 1000000\n",
"output": "1000000\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2559 | Solve the following coding problem using the programming language python:
Welcome! Everything is fine.
You have arrived in The Medium Place, the place between The Good Place and The Bad Place. You are assigned a task that will either make people happier or torture them for eternity.
You have a list of $k$ pairs of people who have arrived in a new inhabited neighborhood. You need to assign each of the $2k$ people into one of the $2k$ houses. Each person will be the resident of exactly one house, and each house will have exactly one resident.
Of course, in the neighborhood, it is possible to visit friends. There are $2k - 1$ roads, each of which connects two houses. It takes some time to traverse a road. We will specify the amount of time it takes in the input. The neighborhood is designed in such a way that from anyone's house, there is exactly one sequence of distinct roads you can take to any other house. In other words, the graph with the houses as vertices and the roads as edges is a tree.
The truth is, these $k$ pairs of people are actually soulmates. We index them from $1$ to $k$. We denote by $f(i)$ the amount of time it takes for the $i$-th pair of soulmates to go to each other's houses.
As we have said before, you will need to assign each of the $2k$ people into one of the $2k$ houses. You have two missions, one from the entities in The Good Place and one from the entities of The Bad Place. Here they are: The first mission, from The Good Place, is to assign the people into the houses such that the sum of $f(i)$ over all pairs $i$ is minimized. Let's define this minimized sum as $G$. This makes sure that soulmates can easily and efficiently visit each other; The second mission, from The Bad Place, is to assign the people into the houses such that the sum of $f(i)$ over all pairs $i$ is maximized. Let's define this maximized sum as $B$. This makes sure that soulmates will have a difficult time to visit each other.
What are the values of $G$ and $B$?
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
The first line of each test case contains a single integer $k$ denoting the number of pairs of people ($1 \le k \le 10^5$). The next $2k - 1$ lines describe the roads; the $i$-th of them contains three space-separated integers $a_i, b_i, t_i$ which means that the $i$-th road connects the $a_i$-th and $b_i$-th houses with a road that takes $t_i$ units of time to traverse ($1 \le a_i, b_i \le 2k$, $a_i \neq b_i$, $1 \le t_i \le 10^6$). It is guaranteed that the given roads define a tree structure.
It is guaranteed that the sum of the $k$ in a single file is at most $3 \cdot 10^5$.
-----Output-----
For each test case, output a single line containing two space-separated integers $G$ and $B$.
-----Example-----
Input
2
3
1 2 3
3 2 4
2 4 3
4 5 6
5 6 5
2
1 2 1
1 3 2
1 4 3
Output
15 33
6 6
-----Note-----
For the sample test case, we have a minimum sum equal to $G = 15$. One way this can be achieved is with the following assignment: The first pair of people get assigned to houses $5$ and $6$, giving us $f(1) = 5$; The second pair of people get assigned to houses $1$ and $4$, giving us $f(2) = 6$; The third pair of people get assigned to houses $3$ and $2$, giving us $f(3) = 4$.
Note that the sum of the $f(i)$ is $5 + 6 + 4 = 15$.
We also have a maximum sum equal to $B = 33$. One way this can be achieved is with the following assignment: The first pair of people get assigned to houses $1$ and $4$, giving us $f(1) = 6$; The second pair of people get assigned to houses $6$ and $2$, giving us $f(2) = 14$; The third pair of people get assigned to houses $3$ and $5$, giving us $f(3) = 13$.
Note that the sum of the $f(i)$ is $6 + 14 + 13 = 33$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import sys
# inf = open('input.txt', 'r')
# reader = (map(int, line.split()) for line in inf)
reader = (list(map(int, line.split())) for line in sys.stdin)
def minmaxPairs(g, costDict, n):
G = B = 0
s = 1
stack = [s]
traversal = []
visited = [False] * (n + 1)
subtreeSize = [1 for _ in range(n + 1)]
while stack:
v = stack.pop()
if not visited[v]:
visited[v] = True
for to in g[v]:
if not visited[to]:
stack.append(v)
stack.append(to)
else:
to = traversal[-1]
if (v, to) in costDict:
cost = costDict[(v, to)]
else:
cost = costDict[(to, v)]
toSize = subtreeSize[to]
subtreeSize[v] += toSize
minComp = min(toSize, n - toSize)
G += (minComp % 2) * cost
B += minComp * cost
traversal.append(v)
return G, B
t, = next(reader)
for _ in range(t):
k, = next(reader)
n = 2 * k
g = [[] for i in range(n + 1)]
costDict = {}
for i in range(n - 1):
v, to, cost = next(reader)
costDict[(v, to)] = cost
g[v].append(to)
g[to].append(v)
G, B = minmaxPairs(g, costDict, n)
print(G, B)
# inf.close()
``` | vfc_19969 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1281/E",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n3\n1 2 3\n3 2 4\n2 4 3\n4 5 6\n5 6 5\n2\n1 2 1\n1 3 2\n1 4 3\n",
"output": "15 33\n6 6\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n1\n2 1 4\n",
"output": "4 4\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2560 | Solve the following coding problem using the programming language python:
Alexey, a merry Berland entrant, got sick of the gray reality and he zealously wants to go to university. There are a lot of universities nowadays, so Alexey is getting lost in the diversity — he has not yet decided what profession he wants to get. At school, he had bad grades in all subjects, and it's only thanks to wealthy parents that he was able to obtain the graduation certificate.
The situation is complicated by the fact that each high education institution has the determined amount of voluntary donations, paid by the new students for admission — n_{i} berubleys. He cannot pay more than n_{i}, because then the difference between the paid amount and n_{i} can be regarded as a bribe!
Each rector is wearing the distinctive uniform of his university. Therefore, the uniform's pockets cannot contain coins of denomination more than r_{i}. The rector also does not carry coins of denomination less than l_{i} in his pocket — because if everyone pays him with so small coins, they gather a lot of weight and the pocket tears. Therefore, a donation can be paid only by coins of denomination x berubleys, where l_{i} ≤ x ≤ r_{i} (Berland uses coins of any positive integer denomination). Alexey can use the coins of different denominations and he can use the coins of the same denomination any number of times. When Alexey was first confronted with such orders, he was puzzled because it turned out that not all universities can accept him! Alexey is very afraid of going into the army (even though he had long wanted to get the green uniform, but his dad says that the army bullies will beat his son and he cannot pay to ensure the boy's safety). So, Alexey wants to know for sure which universities he can enter so that he could quickly choose his alma mater.
Thanks to the parents, Alexey is not limited in money and we can assume that he has an unlimited number of coins of each type.
In other words, you are given t requests, each of them contains numbers n_{i}, l_{i}, r_{i}. For each query you need to answer, whether it is possible to gather the sum of exactly n_{i} berubleys using only coins with an integer denomination from l_{i} to r_{i} berubleys. You can use coins of different denominations. Coins of each denomination can be used any number of times.
-----Input-----
The first line contains the number of universities t, (1 ≤ t ≤ 1000) Each of the next t lines contain three space-separated integers: n_{i}, l_{i}, r_{i} (1 ≤ n_{i}, l_{i}, r_{i} ≤ 10^9; l_{i} ≤ r_{i}).
-----Output-----
For each query print on a single line: either "Yes", if Alexey can enter the university, or "No" otherwise.
-----Examples-----
Input
2
5 2 3
6 4 5
Output
Yes
No
-----Note-----
You can pay the donation to the first university with two coins: one of denomination 2 and one of denomination 3 berubleys. The donation to the second university cannot be paid.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
t = int(input())
for i in range(0, t) :
n, l, r = [int(s) for s in input().split()]
print('Yes' if n % l <= (r - l) * (n // l) else 'No')
``` | vfc_19973 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/397/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n5 2 3\n6 4 5\n",
"output": "Yes\nNo\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "50\n69 6 6\n22 1 1\n23 3 3\n60 13 13\n13 3 3\n7 4 7\n6 1 1\n49 7 9\n68 8 8\n20 2 2\n34 1 1\n79 5 5\n22 1 1\n77 58 65\n10 3 3\n72 5 5\n47 1 1\n82 3 3\n92 8 8\n34 1 1\n42 9 10\n63 14 14\n10 3 3\n38 2 2\n80 6 6\n79 5 5\n53 5 5\n44 7 7\n85 2 2\n24 2 2\n57 3 3\n95 29 81\n77 6 6\n24 1 1\n33 4 4\n93 6 6\n55 22 28\n91 14 14\n7 1 1\n16 1 1\n20 3 3\n43 3 3\n53 3 3\n49 3 3\n52 5 5\n2 1 1\n60 5 5\n76 57 68\n67 3 3\n61 52 61\n",
"output": "No\nYes\nNo\nNo\nNo\nYes\nYes\nYes\nNo\nYes\nYes\nNo\nYes\nNo\nNo\nNo\nYes\nNo\nNo\nYes\nNo\nNo\nNo\nYes\nNo\nNo\nNo\nNo\nNo\nYes\nYes\nYes\nNo\nYes\nNo\nNo\nYes\nNo\nYes\nYes\nNo\nNo\nNo\nNo\nNo\nYes\nYes\nNo\nNo\nYes\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2562 | Solve the following coding problem using the programming language python:
Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers.
He has created a method to know how strong his army is. Let the i-th soldier’s strength be a_{i}. For some k he calls i_1, i_2, ..., i_{k} a clan if i_1 < i_2 < i_3 < ... < i_{k} and gcd(a_{i}_1, a_{i}_2, ..., a_{i}_{k}) > 1 . He calls the strength of that clan k·gcd(a_{i}_1, a_{i}_2, ..., a_{i}_{k}). Then he defines the strength of his army by the sum of strengths of all possible clans.
Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (10^9 + 7).
Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army.
The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000000) — denoting the strengths of his soldiers.
-----Output-----
Print one integer — the strength of John Snow's army modulo 1000000007 (10^9 + 7).
-----Examples-----
Input
3
3 3 1
Output
12
Input
4
2 3 4 6
Output
39
-----Note-----
In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
n,N,MOD,ans=int(input()),int(1e6+5),int(1e9+7),0
cnt,pw,f=[0]*int(N),[1]*(n+1),[0]*int(N)
for i in range(n):
pw[i+1]=pw[i]*2%MOD
for i in input().split():
cnt[int(i)]+=1
for i in reversed(range(2,N)):
t=sum([cnt[j] for j in range(i,N,i)])
if t:
f[i]=t*pw[t-1]%MOD
for j in range(i*2,N,i):
f[i]=(f[i]-f[j])%MOD
ans+=i*f[i]%MOD
print(ans%MOD)
``` | vfc_19981 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/839/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n3 3 1\n",
"output": "12\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "4\n2 3 4 6\n",
"output": "39\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2563 | Solve the following coding problem using the programming language python:
You are given a huge integer $a$ consisting of $n$ digits ($n$ is between $1$ and $3 \cdot 10^5$, inclusive). It may contain leading zeros.
You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by $2$).
For example, if $a = 032867235$ you can get the following integers in a single operation: $302867235$ if you swap the first and the second digits; $023867235$ if you swap the second and the third digits; $032876235$ if you swap the fifth and the sixth digits; $032862735$ if you swap the sixth and the seventh digits; $032867325$ if you swap the seventh and the eighth digits.
Note, that you can't swap digits on positions $2$ and $4$ because the positions are not adjacent. Also, you can't swap digits on positions $3$ and $4$ because the digits have the same parity.
You can perform any number (possibly, zero) of such operations.
Find the minimum integer you can obtain.
Note that the resulting integer also may contain leading zeros.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The only line of each test case contains the integer $a$, its length $n$ is between $1$ and $3 \cdot 10^5$, inclusive.
It is guaranteed that the sum of all values $n$ does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print line — the minimum integer you can obtain.
-----Example-----
Input
3
0709
1337
246432
Output
0079
1337
234642
-----Note-----
In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): $0 \underline{\textbf{70}} 9 \rightarrow 0079$.
In the second test case, the initial integer is optimal.
In the third test case you can perform the following sequence of operations: $246 \underline{\textbf{43}} 2 \rightarrow 24 \underline{\textbf{63}}42 \rightarrow 2 \underline{\textbf{43}} 642 \rightarrow 234642$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for __ in range(int(input())):
a = list(map(int, input()))
ar1 = []
ar2 = []
for elem in a:
if elem % 2 == 0:
ar1.append(elem)
else:
ar2.append(elem)
ans = []
i = 0
j = 0
while i < len(ar1) and j < len(ar2):
if ar1[i] < ar2[j]:
ans.append(ar1[i])
i += 1
else:
ans.append(ar2[j])
j += 1
if i < len(ar1):
for h in range(i, len(ar1)):
ans.append(ar1[h])
if j < len(ar2):
for h in range(j, len(ar2)):
ans.append(ar2[h])
print(''.join(map(str, ans)))
``` | vfc_19985 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1251/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n0709\n1337\n246432\n",
"output": "0079\n1337\n234642\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2564 | Solve the following coding problem using the programming language python:
Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a "+=" operation that adds the right-hand side value to the left-hand side variable. For example, performing "a += b" when a = $2$, b = $3$ changes the value of a to $5$ (the value of b does not change).
In a prototype program Leo has two integer variables a and b, initialized with some positive values. He can perform any number of operations "a += b" or "b += a". Leo wants to test handling large integers, so he wants to make the value of either a or b strictly greater than a given value $n$. What is the smallest number of operations he has to perform?
-----Input-----
The first line contains a single integer $T$ ($1 \leq T \leq 100$) — the number of test cases.
Each of the following $T$ lines describes a single test case, and contains three integers $a, b, n$ ($1 \leq a, b \leq n \leq 10^9$) — initial values of a and b, and the value one of the variables has to exceed, respectively.
-----Output-----
For each test case print a single integer — the smallest number of operations needed. Separate answers with line breaks.
-----Example-----
Input
2
1 2 3
5 4 100
Output
2
7
-----Note-----
In the first case we cannot make a variable exceed $3$ in one operation. One way of achieving this in two operations is to perform "b += a" twice.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
from sys import stdin
input = stdin.readline
q = int(input())
for _ in range(q):
a,b,n = map(int,input().split())
kroki = 0
while max(a,b) <= n:
if a > b:
a,b = b,a
a = a+b
kroki += 1
print(kroki)
``` | vfc_19989 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1368/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n1 2 3\n5 4 100\n",
"output": "2\n7\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "16\n1 1 1\n3 4 7\n4 5 13\n456 123 7890123\n1 1 1000000000\n45 12 782595420\n1 1000000000 1000000000\n1 999999999 1000000000\n1 99999 676497416\n5 6 930234861\n8 9 881919225\n500000000 500000000 1000000000\n1000000000 1000000000 1000000000\n999999999 1000000000 1000000000\n666 999999 987405273\n5378 5378 652851553\n",
"output": "1\n2\n2\n21\n43\n36\n1\n2\n20\n40\n38\n2\n1\n1\n16\n24\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2565 | Solve the following coding problem using the programming language python:
You are given two sequences $a_1, a_2, \dots, a_n$ and $b_1, b_2, \dots, b_n$. Each element of both sequences is either $0$, $1$ or $2$. The number of elements $0$, $1$, $2$ in the sequence $a$ is $x_1$, $y_1$, $z_1$ respectively, and the number of elements $0$, $1$, $2$ in the sequence $b$ is $x_2$, $y_2$, $z_2$ respectively.
You can rearrange the elements in both sequences $a$ and $b$ however you like. After that, let's define a sequence $c$ as follows:
$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$
You'd like to make $\sum_{i=1}^n c_i$ (the sum of all elements of the sequence $c$) as large as possible. What is the maximum possible sum?
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
Each test case consists of two lines. The first line of each test case contains three integers $x_1$, $y_1$, $z_1$ ($0 \le x_1, y_1, z_1 \le 10^8$) — the number of $0$-s, $1$-s and $2$-s in the sequence $a$.
The second line of each test case also contains three integers $x_2$, $y_2$, $z_2$ ($0 \le x_2, y_2, z_2 \le 10^8$; $x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$) — the number of $0$-s, $1$-s and $2$-s in the sequence $b$.
-----Output-----
For each test case, print the maximum possible sum of the sequence $c$.
-----Example-----
Input
3
2 3 2
3 3 1
4 0 1
2 3 0
0 0 1
0 0 1
Output
4
2
0
-----Note-----
In the first sample, one of the optimal solutions is:
$a = \{2, 0, 1, 1, 0, 2, 1\}$
$b = \{1, 0, 1, 0, 2, 1, 0\}$
$c = \{2, 0, 0, 0, 0, 2, 0\}$
In the second sample, one of the optimal solutions is:
$a = \{0, 2, 0, 0, 0\}$
$b = \{1, 1, 0, 1, 0\}$
$c = \{0, 2, 0, 0, 0\}$
In the third sample, the only possible solution is:
$a = \{2\}$
$b = \{2\}$
$c = \{0\}$
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for _ in range(int(input())):
a,b,c = list(map(int, input().split()))
x,y,z = list(map(int, input().split()))
temp = min(c,y)
ans = 2*temp
c -= temp
y -= temp
temp = min(c,z)
c -= temp
z -= temp
temp = min(c,x)
c -= temp
x -= temp
temp = min(a,z)
a -= temp
z -= temp
temp = min(b,z)
ans -= 2*temp
b -= temp
z -= temp
print(ans)
``` | vfc_19993 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1401/B",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1\n",
"output": "4\n2\n0\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n45094381 58629867 47778466\n33028864 48902149 69571701\n",
"output": "46602292\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2567 | Solve the following coding problem using the programming language python:
A binary string is a string where each character is either 0 or 1. Two binary strings $a$ and $b$ of equal length are similar, if they have the same character in some position (there exists an integer $i$ such that $a_i = b_i$). For example: 10010 and 01111 are similar (they have the same character in position $4$); 10010 and 11111 are similar; 111 and 111 are similar; 0110 and 1001 are not similar.
You are given an integer $n$ and a binary string $s$ consisting of $2n-1$ characters. Let's denote $s[l..r]$ as the contiguous substring of $s$ starting with $l$-th character and ending with $r$-th character (in other words, $s[l..r] = s_l s_{l + 1} s_{l + 2} \dots s_r$).
You have to construct a binary string $w$ of length $n$ which is similar to all of the following strings: $s[1..n]$, $s[2..n+1]$, $s[3..n+2]$, ..., $s[n..2n-1]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 50$).
The second line of each test case contains the binary string $s$ of length $2n - 1$. Each character $s_i$ is either 0 or 1.
-----Output-----
For each test case, print the corresponding binary string $w$ of length $n$. If there are multiple such strings — print any of them. It can be shown that at least one string $w$ meeting the constraints always exists.
-----Example-----
Input
4
1
1
3
00000
4
1110000
2
101
Output
1
000
1010
00
-----Note-----
The explanation of the sample case (equal characters in equal positions are bold):
The first test case: $\mathbf{1}$ is similar to $s[1..1] = \mathbf{1}$.
The second test case: $\mathbf{000}$ is similar to $s[1..3] = \mathbf{000}$; $\mathbf{000}$ is similar to $s[2..4] = \mathbf{000}$; $\mathbf{000}$ is similar to $s[3..5] = \mathbf{000}$.
The third test case: $\mathbf{1}0\mathbf{10}$ is similar to $s[1..4] = \mathbf{1}1\mathbf{10}$; $\mathbf{1}01\mathbf{0}$ is similar to $s[2..5] = \mathbf{1}10\mathbf{0}$; $\mathbf{10}1\mathbf{0}$ is similar to $s[3..6] = \mathbf{10}0\mathbf{0}$; $1\mathbf{0}1\mathbf{0}$ is similar to $s[4..7] = 0\mathbf{0}0\mathbf{0}$.
The fourth test case: $0\mathbf{0}$ is similar to $s[1..2] = 1\mathbf{0}$; $\mathbf{0}0$ is similar to $s[2..3] = \mathbf{0}1$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
t=int(input())
for t in range(t):
n=int(input())
s=input()
a=[int(x) for x in s]
p=a[n-1]
print(str(p)*n)
``` | vfc_20001 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1400/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n1\n1\n3\n00000\n4\n1110000\n2\n101\n",
"output": "1\n000\n0000\n11\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "1\n7\n0000000000001\n",
"output": "0000000\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2568 | Solve the following coding problem using the programming language python:
You are given a string $s$ consisting only of characters + and -. You perform some process with this string. This process can be described by the following pseudocode: res = 0
for init = 0 to inf
cur = init
ok = true
for i = 1 to |s|
res = res + 1
if s[i] == '+'
cur = cur + 1
else
cur = cur - 1
if cur < 0
ok = false
break
if ok
break
Note that the $inf$ denotes infinity, and the characters of the string are numbered from $1$ to $|s|$.
You have to calculate the value of the $res$ after the process ends.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The only lines of each test case contains string $s$ ($1 \le |s| \le 10^6$) consisting only of characters + and -.
It's guaranteed that sum of $|s|$ over all test cases doesn't exceed $10^6$.
-----Output-----
For each test case print one integer — the value of the $res$ after the process ends.
-----Example-----
Input
3
--+-
---
++--+-
Output
7
9
6
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def solve():
s = input()
pm = 0
cur = 0
ans = len(s)
for i in range(len(s)):
if s[i] == '+':
cur += 1
else:
cur -= 1
if cur < pm:
pm = cur
ans += i + 1
print(ans)
t = int(input())
for _ in range(t):
solve()
``` | vfc_20005 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1373/C",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "3\n--+-\n---\n++--+-\n",
"output": "7\n9\n6\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2569 | Solve the following coding problem using the programming language python:
Lee tried so hard to make a good div.2 D problem to balance his recent contest, but it still doesn't feel good at all. Lee invented it so tediously slow that he managed to develop a phobia about div.2 D problem setting instead. And now he is hiding behind the bushes...
Let's define a Rooted Dead Bush (RDB) of level $n$ as a rooted tree constructed as described below.
A rooted dead bush of level $1$ is a single vertex. To construct an RDB of level $i$ we, at first, construct an RDB of level $i-1$, then for each vertex $u$: if $u$ has no children then we will add a single child to it; if $u$ has one child then we will add two children to it; if $u$ has more than one child, then we will skip it.
[Image] Rooted Dead Bushes of level $1$, $2$ and $3$.
Let's define a claw as a rooted tree with four vertices: one root vertex (called also as center) with three children. It looks like a claw:
[Image] The center of the claw is the vertex with label $1$.
Lee has a Rooted Dead Bush of level $n$. Initially, all vertices of his RDB are green.
In one move, he can choose a claw in his RDB, if all vertices in the claw are green and all vertices of the claw are children of its center, then he colors the claw's vertices in yellow.
He'd like to know the maximum number of yellow vertices he can achieve. Since the answer might be very large, print it modulo $10^9+7$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
Next $t$ lines contain test cases — one per line.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^6$) — the level of Lee's RDB.
-----Output-----
For each test case, print a single integer — the maximum number of yellow vertices Lee can make modulo $10^9 + 7$.
-----Example-----
Input
7
1
2
3
4
5
100
2000000
Output
0
0
4
4
12
990998587
804665184
-----Note-----
It's easy to see that the answer for RDB of level $1$ or $2$ is $0$.
The answer for RDB of level $3$ is $4$ since there is only one claw we can choose: $\{1, 2, 3, 4\}$.
The answer for RDB of level $4$ is $4$ since we can choose either single claw $\{1, 3, 2, 4\}$ or single claw $\{2, 7, 5, 6\}$. There are no other claws in the RDB of level $4$ (for example, we can't choose $\{2, 1, 7, 6\}$, since $1$ is not a child of center vertex $2$).
$\therefore$ Rooted Dead Bush of level 4.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
# import numpy as np
# import sys
# q=int(input())
mod=1000000007
dp=[(0,0),(0,0)]
for i in range(2,2000001):
dp.append(((max(dp[-1])+2*max(dp[-2]))%mod,(dp[-1][0]+2*dp[-2][0]+1)%mod))
for o in range(int(input())):
print(max(dp[int(input())-1])*4%mod)
``` | vfc_20009 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1369/D",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "7\n1\n2\n3\n4\n5\n100\n2000000\n",
"output": "0\n0\n4\n4\n12\n990998587\n804665184\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "3\n1234567\n1268501\n1268499\n",
"output": "788765312\n999997375\n999999350\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2570 | Solve the following coding problem using the programming language python:
You are given two arrays $a$ and $b$, each consisting of $n$ positive integers, and an integer $x$. Please determine if one can rearrange the elements of $b$ so that $a_i + b_i \leq x$ holds for each $i$ ($1 \le i \le n$).
-----Input-----
The first line of input contains one integer $t$ ($1 \leq t \leq 100$) — the number of test cases. $t$ blocks follow, each describing an individual test case.
The first line of each test case contains two integers $n$ and $x$ ($1 \leq n \leq 50$; $1 \leq x \leq 1000$) — the length of arrays $a$ and $b$, and the parameter $x$, described in the problem statement.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_1 \le a_2 \le \dots \le a_n \leq x$) — the elements of array $a$ in non-descending order.
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_1 \le b_2 \le \dots \le b_n \leq x$) — the elements of array $b$ in non-descending order.
Test cases are separated by a blank line.
-----Output-----
For each test case print Yes if one can rearrange the corresponding array $b$ so that $a_i + b_i \leq x$ holds for each $i$ ($1 \le i \le n$) or No otherwise.
Each character can be printed in any case.
-----Example-----
Input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
Output
Yes
Yes
No
No
-----Note-----
In the first test case, one can rearrange $b$ so it'll look like $[1, 2, 1]$. In this case, $1 + 1 \leq 4$; $2 + 2 \leq 4$; $3 + 1 \leq 4$.
In the second test case, one can set $b$ to $[5, 2]$, then $1 + 5 \leq 6$; $4 + 2 \leq 6$.
In the third test case, no matter how one shuffles array $b$, $a_4 + b_4 = 4 + b_4 > 4$.
In the fourth test case, there is only one rearrangement of array $b$ and it doesn't satisfy the condition since $5 + 5 > 5$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
def testcase():
n, x = list(map(int, input().split()))
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
arr.sort()
brr.sort(reverse=True)
for i in range(n):
if arr[i] + brr[i] > x:
print('No')
return
print('Yes')
return
import sys, os
if os.path.exists('input.txt'):
sys.stdin = open('input.txt', 'r')
sys.setrecursionlimit(10 ** 5)
t = int(input())
for _ in range(t - 1):
testcase()
input()
testcase()
``` | vfc_20013 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1445/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5\n",
"output": "Yes\nYes\nNo\nNo\n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2571 | Solve the following coding problem using the programming language python:
Naruto has sneaked into the Orochimaru's lair and is now looking for Sasuke. There are $T$ rooms there. Every room has a door into it, each door can be described by the number $n$ of seals on it and their integer energies $a_1$, $a_2$, ..., $a_n$. All energies $a_i$ are nonzero and do not exceed $100$ by absolute value. Also, $n$ is even.
In order to open a door, Naruto must find such $n$ seals with integer energies $b_1$, $b_2$, ..., $b_n$ that the following equality holds: $a_{1} \cdot b_{1} + a_{2} \cdot b_{2} + ... + a_{n} \cdot b_{n} = 0$. All $b_i$ must be nonzero as well as $a_i$ are, and also must not exceed $100$ by absolute value. Please find required seals for every room there.
-----Input-----
The first line contains the only integer $T$ ($1 \leq T \leq 1000$) standing for the number of rooms in the Orochimaru's lair. The other lines contain descriptions of the doors.
Each description starts with the line containing the only even integer $n$ ($2 \leq n \leq 100$) denoting the number of seals.
The following line contains the space separated sequence of nonzero integers $a_1$, $a_2$, ..., $a_n$ ($|a_{i}| \leq 100$, $a_{i} \neq 0$) denoting the energies of seals.
-----Output-----
For each door print a space separated sequence of nonzero integers $b_1$, $b_2$, ..., $b_n$ ($|b_{i}| \leq 100$, $b_{i} \neq 0$) denoting the seals that can open the door. If there are multiple valid answers, print any. It can be proven that at least one answer always exists.
-----Example-----
Input
2
2
1 100
4
1 2 3 6
Output
-100 1
1 1 1 -1
-----Note-----
For the first door Naruto can use energies $[-100, 1]$. The required equality does indeed hold: $1 \cdot (-100) + 100 \cdot 1 = 0$.
For the second door Naruto can use, for example, energies $[1, 1, 1, -1]$. The required equality also holds: $1 \cdot 1 + 2 \cdot 1 + 3 \cdot 1 + 6 \cdot (-1) = 0$.
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
for test in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
for i in range(0, n, 2):
a1 = a[i]
a2 = a[i + 1]
print(-a2, a1, end=" ")
print()
``` | vfc_20017 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1413/A",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "2\n2\n1 100\n4\n1 2 3 6\n",
"output": "-100 1 \n-2 1 -6 3 \n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "6\n6\n-1 1 1 1 1 1\n6\n1 -1 1 1 1 1\n6\n1 1 -1 1 1 1\n6\n1 1 1 -1 1 1\n6\n1 1 1 1 -1 1\n6\n1 1 1 1 1 -1\n",
"output": "-1 -1 -1 1 -1 1 \n1 1 -1 1 -1 1 \n-1 1 -1 -1 -1 1 \n-1 1 1 1 -1 1 \n-1 1 -1 1 -1 -1 \n-1 1 -1 1 1 1 \n",
"type": "stdin_stdout"
}
]
} |
apps | verifiable_code | 2573 | Solve the following coding problem using the programming language python:
You are given a chessboard consisting of $n$ rows and $n$ columns. Rows are numbered from bottom to top from $1$ to $n$. Columns are numbered from left to right from $1$ to $n$. The cell at the intersection of the $x$-th column and the $y$-th row is denoted as $(x, y)$. Furthermore, the $k$-th column is a special column.
Initially, the board is empty. There are $m$ changes to the board. During the $i$-th change one pawn is added or removed from the board. The current board is good if we can move all pawns to the special column by the followings rules: Pawn in the cell $(x, y)$ can be moved to the cell $(x, y + 1)$, $(x - 1, y + 1)$ or $(x + 1, y + 1)$; You can make as many such moves as you like; Pawns can not be moved outside the chessboard; Each cell can not contain more than one pawn.
The current board may not always be good. To fix it, you can add new rows to the board. New rows are added at the top, i. e. they will have numbers $n+1, n+2, n+3, \dots$.
After each of $m$ changes, print one integer — the minimum number of rows which you have to add to make the board good.
-----Input-----
The first line contains three integers $n$, $k$ and $m$ ($1 \le n, m \le 2 \cdot 10^5; 1 \le k \le n$) — the size of the board, the index of the special column and the number of changes respectively.
Then $m$ lines follow. The $i$-th line contains two integers $x$ and $y$ ($1 \le x, y \le n$) — the index of the column and the index of the row respectively. If there is no pawn in the cell $(x, y)$, then you add a pawn to this cell, otherwise — you remove the pawn from this cell.
-----Output-----
After each change print one integer — the minimum number of rows which you have to add to make the board good.
-----Example-----
Input
5 3 5
4 4
3 5
2 4
3 4
3 5
Output
0
1
2
2
1
The input will be stdin and you should print your solution to stdout
Now solve the problem and return the code. | ```python
import collections
import math
n, k, m = [int(i) for i in input().split()]
l = math.ceil((math.log(2*n) / math.log(2)))
p = 2 ** l
# print(p)
memo = [0] * (2 * p)
allres = [0] * (2 * p)
exist = set()
for _i in range(m):
x, y = [int(i) for i in input().split()]
l = abs(x - k) + y
index = l + p
if (x, y) in exist:
exist.remove((x, y))
while index != 0:
memo[index] -= 1
index = index // 2
else:
exist.add((x, y))
while index != 0:
memo[index] += 1
index = index // 2
index = (l + p) // 2
allres[l+p] = l + memo[l + p] - 1
if memo[l + p] == 0:
allres[l+p] = 0
while index != 0:
allres[index] = max(allres[index * 2] + memo[index * 2 + 1], allres[index * 2 + 1])
index = index // 2
# print('i', _i + 1, exist, allres, memo)
print(max(allres[1] - n, 0))
``` | vfc_20025 | {
"difficulty": "interview",
"memory_limit": null,
"memory_limit_bytes": null,
"problem_url": "https://codeforces.com/problemset/problem/1373/G",
"time_limit": "None"
} | {
"language": "python",
"test_cases": [
{
"fn_name": null,
"input": "5 3 5\n4 4\n3 5\n2 4\n3 4\n3 5\n",
"output": "0\n1\n2\n2\n1\n",
"type": "stdin_stdout"
},
{
"fn_name": null,
"input": "50 50 1\n1 50\n",
"output": "49\n",
"type": "stdin_stdout"
}
]
} |
Subsets and Splits