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
2128
Solve the following coding problem using the programming language python: Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the $1$-st mirror again. You need to calculate the expected number of days until Creatnx becomes happy. This number should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains one integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of mirrors. The second line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). -----Output----- Print the answer modulo $998244353$ in a single line. -----Examples----- Input 1 50 Output 2 Input 3 10 20 50 Output 112 -----Note----- In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability $\frac{1}{2}$. So, the expected number of days until Creatnx becomes happy is $2$. 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()) #x, y = map(int,input().split(' ')) #print (' '.join(list(map(str, s)))) def extended_gcd(aa, bb): lastremainder, remainder = abs(aa), abs(bb) x, lastx, y, lasty = 0, 1, 1, 0 while remainder: lastremainder, (quotient, remainder) = remainder, divmod(lastremainder, remainder) x, lastx = lastx - quotient*x, x y, lasty = lasty - quotient*y, y return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1) def modinv(a, m): g, x, y = extended_gcd(a, m) if g != 1: raise ValueError return x % m m = 998244353 n = int(input()) p = list(map(int,input().split(' '))) up = 0 low = 1 for i in range(n): up = up + low up = up * 100 % m low = low * p[i] % m print (up*modinv(low,m)%m) ```
vfc_18266
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1265/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n50\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n10 20 50\n", "output": "112\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n18 73\n", "output": "112435446\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2129
Solve the following coding problem using the programming language python: There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. -----Input----- The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one. -----Output----- For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. -----Example----- Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7 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 collections rlines = sys.stdin.readlines() lines = (l.strip() for l in rlines) def eucycle(n,m,adj): diredges = [] us = list(adj.keys()) for u in us: while adj[u]: v0 = u v1 = adj[v0].pop() adj[v1].remove(v0) diredges.append((v0,v1)) while v1 != u: v0 = v1 v1 = adj[v0].pop() adj[v1].remove(v0) diredges.append((v0,v1)) return diredges def solve(n,m,edges): adj = collections.defaultdict(set) diredges = [] for u,v in edges: adj[u].add(v) adj[v].add(u) odds = set(u for u in adj if len(adj[u])%2 == 1) ans = n - len(odds) assert(len(odds)%2 == 0) for o in odds: adj[n+1].add(o) adj[o].add(n+1) diredges = eucycle(n+1,m,adj) return str(ans) + '\n' + '\n'.join(str(u) + ' ' + str(v) for (u,v) in diredges\ if u != n+1 and v != n+1) t = int(next(lines)) for ti in range(t): n,m = [int(s) for s in next(lines).split()] edges = [] for ei in range(m): u,v = [int(s) for s in next(lines).split()] edges.append((u,v)) #print(edges) print(solve(n,m,edges)) ```
vfc_18270
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/723/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n5 5\n2 1\n4 5\n2 3\n1 3\n3 5\n7 2\n3 7\n4 2\n", "output": "3\n1 3\n3 5\n5 4\n3 2\n2 1\n3\n2 4\n3 7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n9 17\n3 6\n2 6\n6 9\n4 1\n2 8\n1 9\n7 9\n8 5\n1 7\n4 9\n6 7\n3 4\n9 3\n8 4\n2 1\n3 8\n2 7\n5 6\n2 5\n3 4\n1 3\n4 5\n5 3\n2 3\n12 8\n10 2\n9 2\n6 9\n10 6\n8 2\n4 10\n11 2\n4 11\n19 10\n6 2\n3 12\n17 7\n2 19\n17 4\n1 13\n7 1\n13 7\n6 8\n11 7\n", "output": "7\n1 9\n9 7\n7 6\n6 9\n5 8\n8 4\n4 9\n9 3\n3 8\n8 2\n2 7\n7 1\n1 4\n4 3\n3 6\n6 2\n2 1\n3\n5 4\n4 3\n3 5\n5 2\n2 3\n3 1\n10\n2 11\n11 4\n4 10\n10 6\n6 9\n9 2\n2 10\n8 2\n13\n1 13\n13 7\n7 17\n17 4\n11 7\n7 1\n2 19\n8 6\n6 2\n3 12\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2130
Solve the following coding problem using the programming language python: Vitya has learned that the answer for The Ultimate Question of Life, the Universe, and Everything is not the integer 54 42, but an increasing integer sequence $a_1, \ldots, a_n$. In order to not reveal the secret earlier than needed, Vitya encrypted the answer and obtained the sequence $b_1, \ldots, b_n$ using the following rules: $b_1 = a_1$; $b_i = a_i \oplus a_{i - 1}$ for all $i$ from 2 to $n$, where $x \oplus y$ is the bitwise XOR of $x$ and $y$. It is easy to see that the original sequence can be obtained using the rule $a_i = b_1 \oplus \ldots \oplus b_i$. However, some time later Vitya discovered that the integers $b_i$ in the cypher got shuffled, and it can happen that when decrypted using the rule mentioned above, it can produce a sequence that is not increasing. In order to save his reputation in the scientific community, Vasya decided to find some permutation of integers $b_i$ so that the sequence $a_i = b_1 \oplus \ldots \oplus b_i$ is strictly increasing. Help him find such a permutation or determine that it is impossible. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $b_1, \ldots, b_n$ ($1 \leq b_i < 2^{60}$). -----Output----- If there are no valid permutations, print a single line containing "No". Otherwise in the first line print the word "Yes", and in the second line print integers $b'_1, \ldots, b'_n$ — a valid permutation of integers $b_i$. The unordered multisets $\{b_1, \ldots, b_n\}$ and $\{b'_1, \ldots, b'_n\}$ should be equal, i. e. for each integer $x$ the number of occurrences of $x$ in the first multiset should be equal to the number of occurrences of $x$ in the second multiset. Apart from this, the sequence $a_i = b'_1 \oplus \ldots \oplus b'_i$ should be strictly increasing. If there are multiple answers, print any of them. -----Examples----- Input 3 1 2 3 Output No Input 6 4 7 7 12 31 61 Output Yes 4 12 7 31 7 61 -----Note----- In the first example no permutation is valid. In the second example the given answer lead to the sequence $a_1 = 4$, $a_2 = 8$, $a_3 = 15$, $a_4 = 16$, $a_5 = 23$, $a_6 = 42$. 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=[[] for i in range(60)] for b in list(map(int,input().split())): for i in range(59,-1,-1): if b>>i&1: s[i].append(b) break ans=[] cur=0 for i in range(n): fl=False for j in range(60): if s[j]!=[] and cur>>j&1==0: ans.append(s[j][-1]) cur^=s[j][-1] s[j].pop() fl=True break if not fl: print('No') return print('Yes') print(' '.join(str(i) for i in ans)) ```
vfc_18274
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/925/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 2 3\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n4 7 7 12 31 61\n", "output": "Yes\n4 12 7 31 7 61 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\n", "output": "Yes\n4 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2132
Solve the following coding problem using the programming language python: Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types. speed limit: this sign comes with a positive integer number — maximal speed of the car after the sign (cancel the action of the previous sign of this type); overtake is allowed: this sign means that after some car meets it, it can overtake any other car; no speed limit: this sign cancels speed limit if any (car can move with arbitrary speed after this sign); no overtake allowed: some car can't overtake any other car after this sign. Polycarp goes past the signs consequentially, each new sign cancels the action of all the previous signs of it's kind (speed limit/overtake). It is possible that two or more "no overtake allowed" signs go one after another with zero "overtake is allowed" signs between them. It works with "no speed limit" and "overtake is allowed" signs as well. In the beginning of the ride overtake is allowed and there is no speed limit. You are given the sequence of events in chronological order — events which happened to Polycarp during the ride. There are events of following types: Polycarp changes the speed of his car to specified (this event comes with a positive integer number); Polycarp's car overtakes the other car; Polycarp's car goes past the "speed limit" sign (this sign comes with a positive integer); Polycarp's car goes past the "overtake is allowed" sign; Polycarp's car goes past the "no speed limit"; Polycarp's car goes past the "no overtake allowed"; It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified). After the exam Polycarp can justify his rule violations by telling the driving instructor that he just didn't notice some of the signs. What is the minimal number of signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view? -----Input----- The first line contains one integer number n (1 ≤ n ≤ 2·10^5) — number of events. Each of the next n lines starts with integer t (1 ≤ t ≤ 6) — the type of the event. An integer s (1 ≤ s ≤ 300) follows in the query of the first and the third type (if it is the query of first type, then it's new speed of Polycarp's car, if it is the query of third type, then it's new speed limit). It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified). -----Output----- Print the minimal number of road signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view. -----Examples----- Input 11 1 100 3 70 4 2 3 120 5 3 120 6 1 150 4 3 300 Output 2 Input 5 1 100 3 200 2 4 5 Output 0 Input 7 1 20 2 6 4 6 6 2 Output 2 -----Note----- In the first example Polycarp should say he didn't notice the "speed limit" sign with the limit of 70 and the second "speed limit" sign with the limit of 120. In the second example Polycarp didn't make any rule violation. In the third example Polycarp should say he didn't notice both "no overtake allowed" that came after "overtake is allowed" sign. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python speeds = [1000000] overtakes = [True] count = 0 speed = 0 n = int(input()) for e in range(n): inp = list(map(int, input().split())) # print(inp) if inp[0] == 4: overtakes.append(True) elif inp[0] == 6: overtakes.append(False) elif inp[0] == 5: speeds.append(1000000) elif inp[0] == 3: speeds.append(inp[1]) while speed > speeds[-1]: count += 1 speeds.pop() elif inp[0] == 2: while not overtakes[-1]: count += 1 overtakes.pop() else: while inp[1] > speeds[-1]: count += 1 speeds.pop() speed = inp[1] print(count) """ Polycarp changes the speed of his car to specified (this event comes with a positive integer number); Polycarp's car overtakes the other car; Polycarp's car goes past the "speed limit" sign (this sign comes with a positive integer); Polycarp's car goes past the "overtake is allowed" sign; Polycarp's car goes past the "no speed limit"; Polycarp's car goes past the "no overtake allowed"; """ ```
vfc_18282
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/845/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "11\n1 100\n3 70\n4\n2\n3 120\n5\n3 120\n6\n1 150\n4\n3 300\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2134
Solve the following coding problem using the programming language python: Marcin is a coach in his university. There are $n$ students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from $1$ to $n$. Each of them can be described with two integers $a_i$ and $b_i$; $b_i$ is equal to the skill level of the $i$-th student (the higher, the better). Also, there are $60$ known algorithms, which are numbered with integers from $0$ to $59$. If the $i$-th student knows the $j$-th algorithm, then the $j$-th bit ($2^j$) is set in the binary representation of $a_i$. Otherwise, this bit is not set. Student $x$ thinks that he is better than student $y$ if and only if $x$ knows some algorithm which $y$ doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 7000$) — the number of students interested in the camp. The second line contains $n$ integers. The $i$-th of them is $a_i$ ($0 \leq a_i < 2^{60}$). The third line contains $n$ integers. The $i$-th of them is $b_i$ ($1 \leq b_i \leq 10^9$). -----Output----- Output one integer which denotes the maximum sum of $b_i$ over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. -----Examples----- Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 -----Note----- In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of $b_i$. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset. 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()) lst1=list(map(int,input().split()))[:n] lst2=list(map(int,input().split()))[:n] dict={} for a in lst1: if a in dict: dict[a]+=1 else: dict[a]=1 ans=0 grp=[] for k in dict: if(dict[k]>1): grp.append(k) for i in range(n): for k in grp: if(lst1[i]|k==k): ans +=lst2[i] break print(ans) ```
vfc_18290
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1210/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n3 2 3 6\n2 8 5 10\n", "output": "15\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 2 3\n1 2 3\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n1\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2135
Solve the following coding problem using the programming language python: They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? -----Input----- The first line of the input contains two integers h and w (1 ≤ h, w ≤ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' — denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of queries. Each of the next q lines contains four integers r1_{i}, c1_{i}, r2_{i}, c2_{i} (1 ≤ r1_{i} ≤ r2_{i} ≤ h, 1 ≤ c1_{i} ≤ c2_{i} ≤ w) — the i-th query. Numbers r1_{i} and c1_{i} denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2_{i} and c2_{i} denote the row and the column (respectively) of the bottom right cell of the rectangle. -----Output----- Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. -----Examples----- Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 -----Note----- A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python h, w = list(map(int, input().split())) field = [[c == "." for c in input()] + [False] for i in range(h)] field.append([False] * (w + 1)) subVerts = [[0] * (w + 1) for i in range(h + 1)] subHoriz = [[0] * (w + 1) for i in range(h + 1)] for y in range(h): subVerts[y][0] = 0 subHoriz[y][0] = 0 for x in range(w): subHoriz[0][x] = 0 subVerts[0][x] = 0 for y in range(h): for x in range(w): subVerts[y + 1][x + 1] = subVerts[y + 1][x] + subVerts[y][x + 1] - subVerts[y][x] if field[y][x] and field[y - 1][x]: subVerts[y + 1][x + 1] += 1 subHoriz[y + 1][x + 1] = subHoriz[y + 1][x] + subHoriz[y][x + 1] - subHoriz[y][x] if field[y][x] and field[y][x - 1]: subHoriz[y + 1][x + 1] += 1 q = int(input()) for i in range(q): y1, x1, y2, x2 = [int(x) - 1 for x in input().split()] ansHoriz = subHoriz[y2 + 1][x2 + 1] - subHoriz[y1][x2 + 1] - subHoriz[y2 + 1][x1 + 1] + subHoriz[y1][x1 + 1] ansVerts = subVerts[y2 + 1][x2 + 1] - subVerts[y1 + 1][x2 + 1] - subVerts[y2 + 1][x1] + subVerts[y1 + 1][x1] print(ansHoriz + ansVerts) ```
vfc_18294
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/611/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 8\n....#..#\n.#......\n##.#....\n##..#.##\n........\n4\n1 1 2 3\n4 1 4 1\n1 2 4 5\n2 5 5 8\n", "output": "4\n0\n10\n15\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 39\n.......................................\n.###..###..#..###.....###..###..#..###.\n...#..#.#..#..#.........#..#.#..#..#...\n.###..#.#..#..###.....###..#.#..#..###.\n.#....#.#..#....#.....#....#.#..#..#.#.\n.###..###..#..###.....###..###..#..###.\n.......................................\n6\n1 1 3 20\n2 10 6 30\n2 10 7 30\n2 2 7 7\n1 7 7 7\n1 8 7 8\n", "output": "53\n89\n120\n23\n0\n2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 20\n.#..................\n....................\n15\n1 3 1 13\n1 11 2 14\n1 17 1 20\n1 2 2 3\n1 7 1 10\n1 7 2 17\n1 4 1 9\n2 6 2 8\n1 8 2 20\n2 7 2 16\n1 4 2 16\n1 6 1 9\n1 4 2 7\n1 9 1 20\n2 2 2 12\n", "output": "10\n10\n3\n2\n3\n31\n5\n2\n37\n9\n37\n3\n10\n11\n10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "15 3\n...\n.#.\n.#.\n.#.\n..#\n...\n.#.\n.##\n.#.\n...\n...\n.##\n..#\n.#.\n#.#\n20\n1 1 10 1\n2 1 9 3\n1 2 15 3\n10 2 12 2\n4 1 8 1\n5 2 8 2\n10 1 12 3\n11 1 11 3\n7 2 14 3\n6 2 12 3\n8 1 11 2\n7 1 9 1\n2 1 6 2\n6 3 7 3\n7 1 10 2\n6 1 10 2\n1 1 2 2\n10 1 15 3\n1 1 11 1\n9 1 15 1\n", "output": "9\n14\n12\n1\n4\n1\n8\n2\n5\n7\n6\n2\n7\n1\n4\n6\n2\n11\n10\n5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 19\n.##.#.#.#....#.#...\n.#...##..........#.\n..#.........#..#.#.\n#.#....#....#......\n.#.#.#.#....###...#\n.....##.....#......\n..........#.#..#.#.\n10\n2 2 3 10\n4 10 5 16\n3 3 6 12\n2 12 6 14\n5 1 5 19\n3 11 3 13\n4 10 5 17\n1 13 4 19\n5 3 5 17\n4 15 7 19\n", "output": "15\n10\n43\n8\n5\n1\n13\n24\n4\n19\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n.\n1\n1 1 1 1\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2136
Solve the following coding problem using the programming language python: Pink Floyd are pulling a prank on Roger Waters. They know he doesn't like walls, he wants to be able to walk freely, so they are blocking him from exiting his room which can be seen as a grid. Roger Waters has a square grid of size $n\times n$ and he wants to traverse his grid from the upper left ($1,1$) corner to the lower right corner ($n,n$). Waters can move from a square to any other square adjacent by a side, as long as he is still in the grid. Also except for the cells ($1,1$) and ($n,n$) every cell has a value $0$ or $1$ in it. Before starting his traversal he will pick either a $0$ or a $1$ and will be able to only go to cells values in which are equal to the digit he chose. The starting and finishing cells ($1,1$) and ($n,n$) are exempt from this rule, he may go through them regardless of picked digit. Because of this the cell ($1,1$) takes value the letter 'S' and the cell ($n,n$) takes value the letter 'F'. For example, in the first example test case, he can go from ($1, 1$) to ($n, n$) by using the zeroes on this path: ($1, 1$), ($2, 1$), ($2, 2$), ($2, 3$), ($3, 3$), ($3, 4$), ($4, 4$) The rest of the band (Pink Floyd) wants Waters to not be able to do his traversal, so while he is not looking they will invert at most two cells in the grid (from $0$ to $1$ or vice versa). They are afraid they will not be quick enough and asked for your help in choosing the cells. Note that you cannot invert cells $(1, 1)$ and $(n, n)$. We can show that there always exists a solution for the given constraints. Also note that Waters will pick his digit of the traversal after the band has changed his grid, so he must not be able to reach ($n,n$) no matter what digit he picks. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 50$). Description of the test cases follows. The first line of each test case contains one integers $n$ ($3 \le n \le 200$). The following $n$ lines of each test case contain the binary grid, square ($1, 1$) being colored in 'S' and square ($n, n$) being colored in 'F'. The sum of values of $n$ doesn't exceed $200$. -----Output----- For each test case output on the first line an integer $c$ ($0 \le c \le 2$)  — the number of inverted cells. In $i$-th of the following $c$ lines, print the coordinates of the $i$-th cell you inverted. You may not invert the same cell twice. Note that you cannot invert cells $(1, 1)$ and $(n, n)$. -----Example----- Input 3 4 S010 0001 1000 111F 3 S10 101 01F 5 S0101 00000 01111 11111 0001F Output 1 3 4 2 1 2 2 1 0 -----Note----- For the first test case, after inverting the cell, we get the following grid: S010 0001 1001 111F 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(input()) for _ in range(n)] res = [] if arr[n-2][n-1] == arr[n-1][n-2]: if arr[0][1] == arr[n-2][n-1]: res.append((1, 2)) if arr[1][0] == arr[n-2][n-1]: res.append((2, 1)) elif arr[0][1] == arr[1][0]: if arr[n-2][n-1] == arr[0][1]: res.append((n-1, n)) if arr[n-1][n-2] == arr[0][1]: res.append((n, n-1)) else: if arr[0][1] == "1": res.append((1, 2)) if arr[1][0] == "1": res.append((2, 1)) if arr[n-2][n-1] == "0": res.append((n-1, n)) if arr[n-1][n-2] == "0": res.append((n, n-1)) print(len(res)) for e in res: print(*e) ```
vfc_18298
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1421/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n4\nS010\n0001\n1000\n111F\n3\nS10\n101\n01F\n5\nS0101\n00000\n01111\n11111\n0001F\n", "output": "1\n3 4\n2\n1 2\n2 1\n0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n3\nS11\n011\n01F\n", "output": "1\n1 2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2137
Solve the following coding problem using the programming language python: Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way. There are $n$ ghosts in the universe, they move in the $OXY$ plane, each one of them has its own velocity that does not change in time: $\overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j}$ where $V_{x}$ is its speed on the $x$-axis and $V_{y}$ is on the $y$-axis. A ghost $i$ has experience value $EX_i$, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time. As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind $GX = \sum_{i=1}^{n} EX_i$ will never increase. Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time $T$, and magically all the ghosts were aligned on a line of the form $y = a \cdot x + b$. You have to compute what will be the experience index of the ghost kind $GX$ in the indefinite future, this is your task for today. Note that when Tameem took the picture, $GX$ may already be greater than $0$, because many ghosts may have scared one another at any moment between $[-\infty, T]$. -----Input----- The first line contains three integers $n$, $a$ and $b$ ($1 \leq n \leq 200000$, $1 \leq |a| \leq 10^9$, $0 \le |b| \le 10^9$) — the number of ghosts in the universe and the parameters of the straight line. Each of the next $n$ lines contains three integers $x_i$, $V_{xi}$, $V_{yi}$ ($-10^9 \leq x_i \leq 10^9$, $-10^9 \leq V_{x i}, V_{y i} \leq 10^9$), where $x_i$ is the current $x$-coordinate of the $i$-th ghost (and $y_i = a \cdot x_i + b$). It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all $(i,j)$ $x_i \neq x_j$ for $i \ne j$. -----Output----- Output one line: experience index of the ghost kind $GX$ in the indefinite future. -----Examples----- Input 4 1 1 1 -1 -1 2 1 1 3 1 1 4 -1 -1 Output 8 Input 3 1 0 -1 1 0 0 0 -1 1 -1 -2 Output 6 Input 3 1 0 0 0 0 1 0 0 2 0 0 Output 0 -----Note----- There are four collisions $(1,2,T-0.5)$, $(1,3,T-1)$, $(2,4,T+1)$, $(3,4,T+0.5)$, where $(u,v,t)$ means a collision happened between ghosts $u$ and $v$ at moment $t$. At each collision, each ghost gained one experience point, this means that $GX = 4 \cdot 2 = 8$. In the second test, all points will collide when $t = T + 1$. [Image] The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, A, C = list(map(int, input().split())) def Ro(x, y): return A * x - y + C huh = [] for i in range(n): z, x, y = list(map(int, input().split())) huh.append((Ro(x + z, z * A + y), x)) huh = sorted(huh) anss = 0 c1 = 0 c2 = 0 prev = (-9999999999999, -999999999999999) g = [] huh.append((-9999999999999, -999999999999999)) #print(huh) for huhh in huh: if huhh[0] != prev[0]: g.append(c1) #print(g) for j in g: anss += (c2 - j) * j g = [] c1 = 1 c2 = 1 prev = (huhh[0], huhh[1]) continue c2 += 1 if huhh[1] != prev[1]: g.append(c1) c1 = 0 prev = (huhh[0], huhh[1]) c1 += 1 print(anss) ```
vfc_18302
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/975/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 1 1\n1 -1 -1\n2 1 1\n3 1 1\n4 -1 -1\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1 0\n-1 1 0\n0 0 -1\n1 -1 -2\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1 0\n0 0 0\n1 0 0\n2 0 0\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2139
Solve the following coding problem using the programming language python: The bear has a string s = s_1s_2... s_{|}s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = s_{i}s_{i} + 1... s_{j} contains at least one string "bear" as a substring. String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that s_{k} = b, s_{k} + 1 = e, s_{k} + 2 = a, s_{k} + 3 = r. Help the bear cope with the given problem. -----Input----- The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters. -----Output----- Print a single number — the answer to the problem. -----Examples----- Input bearbtear Output 6 Input bearaabearc Output 20 -----Note----- In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9). In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s=input() if(len(s)<=3): print(0) else: n=len(s) ans=0 A=0 for i in range(3,n): if(s[i-3]+s[i-2]+s[i-1]+s[i]=='bear'): ans+=((i-3)-A+1)*(n-i) A=i-2 print(ans) ```
vfc_18310
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/385/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "bearbtear\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "bearaabearc\n", "output": "20\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "pbearbearhbearzqbearjkterasjhy\n", "output": "291\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "pbearjbearbebearnbabcffbearbearwubearjezpiorrbearbearjbdlbearbearqbearjbearwipmsbearoaftrsebearzsnqb\n", "output": "4419\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "bear\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2140
Solve the following coding problem using the programming language python: Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer a_{i} and reversed a piece of string (a segment) from position a_{i} to position |s| - a_{i} + 1. It is guaranteed that 2·a_{i} ≤ |s|. You face the following task: determine what Pasha's string will look like after m days. -----Input----- The first line of the input contains Pasha's string s of length from 2 to 2·10^5 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 10^5) —  the number of days when Pasha changed his string. The third line contains m space-separated elements a_{i} (1 ≤ a_{i}; 2·a_{i} ≤ |s|) — the position from which Pasha started transforming the string on the i-th day. -----Output----- In the first line of the output print what Pasha's string s will look like after m days. -----Examples----- Input abcdef 1 2 Output aedcbf Input vwxyz 2 2 2 Output vwxyz Input abcdef 3 1 2 3 Output fbdcea The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s = list(input()) n = len(s) // 2 m = int(input()) a = [int(x) for x in input().split()] sums = [0] * (n + 1) for i in a: sums[i - 1] += 1 for i in range(1, len(sums)): sums[i] += sums[i-1] for i in range(n): if (sums[i] % 2 == 1): s[i], s[-i-1] = s[-i-1], s[i] print("".join(s)) ```
vfc_18314
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/525/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "abcdef\n1\n2\n", "output": "aedcbf\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "vwxyz\n2\n2 2\n", "output": "vwxyz\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "abcdef\n3\n1 2 3\n", "output": "fbdcea\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "jc\n5\n1 1 1 1 1\n", "output": "cj\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2141
Solve the following coding problem using the programming language python: You are given a chess board with $n$ rows and $n$ columns. Initially all cells of the board are empty, and you have to put a white or a black knight into each cell of the board. A knight is a chess piece that can attack a piece in cell ($x_2$, $y_2$) from the cell ($x_1$, $y_1$) if one of the following conditions is met: $|x_1 - x_2| = 2$ and $|y_1 - y_2| = 1$, or $|x_1 - x_2| = 1$ and $|y_1 - y_2| = 2$. Here are some examples of which cells knight can attack. In each of the following pictures, if the knight is currently in the blue cell, it can attack all red cells (and only them). [Image] A duel of knights is a pair of knights of different colors such that these knights attack each other. You have to put a knight (a white one or a black one) into each cell in such a way that the number of duels is maximum possible. -----Input----- The first line contains one integer $n$ ($3 \le n \le 100$) — the number of rows (and columns) in the board. -----Output----- Print $n$ lines with $n$ characters in each line. The $j$-th character in the $i$-th line should be W, if the cell ($i$, $j$) contains a white knight, or B, if it contains a black knight. The number of duels should be maximum possible. If there are multiple optimal answers, print any of them. -----Example----- Input 3 Output WBW BBB WBW -----Note----- In the first example, there are $8$ duels: the white knight in ($1$, $1$) attacks the black knight in ($3$, $2$); the white knight in ($1$, $1$) attacks the black knight in ($2$, $3$); the white knight in ($1$, $3$) attacks the black knight in ($3$, $2$); the white knight in ($1$, $3$) attacks the black knight in ($2$, $1$); the white knight in ($3$, $1$) attacks the black knight in ($1$, $2$); the white knight in ($3$, $1$) attacks the black knight in ($2$, $3$); the white knight in ($3$, $3$) attacks the black knight in ($1$, $2$); the white knight in ($3$, $3$) attacks the black knight in ($2$, $1$). 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()) for i in range(n): for j in range(n): if (i + j) % 2 == 0: print('W', end='') else: print('B', end='') print() ```
vfc_18318
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1221/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n", "output": "WBW\nBWB\nWBW\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2142
Solve the following coding problem using the programming language python: You are given two arrays of integers $a_1,\ldots,a_n$ and $b_1,\ldots,b_m$. Your task is to find a non-empty array $c_1,\ldots,c_k$ that is a subsequence of $a_1,\ldots,a_n$, and also a subsequence of $b_1,\ldots,b_m$. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, find any. If there are no such arrays, you should report about it. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) elements. For example, $[3,1]$ is a subsequence of $[3,2,1]$ and $[4,3,1]$, but not a subsequence of $[1,3,3,7]$ and $[3,10,4]$. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains two integers $n$ and $m$ ($1\le n,m\le 1000$)  — the lengths of the two arrays. The second line of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\le a_i\le 1000$)  — the elements of the first array. The third line of each test case contains $m$ integers $b_1,\ldots,b_m$ ($1\le b_i\le 1000$)  — the elements of the second array. It is guaranteed that the sum of $n$ and the sum of $m$ across all test cases does not exceed $1000$ ($\sum\limits_{i=1}^t n_i, \sum\limits_{i=1}^t m_i\le 1000$). -----Output----- For each test case, output "YES" if a solution exists, or "NO" otherwise. If the answer is "YES", on the next line output an integer $k$ ($1\le k\le 1000$)  — the length of the array, followed by $k$ integers $c_1,\ldots,c_k$ ($1\le c_i\le 1000$)  — the elements of the array. If there are multiple solutions with the smallest possible $k$, output any. -----Example----- Input 5 4 5 10 8 6 4 1 2 3 4 5 1 1 3 3 1 1 3 2 5 3 1000 2 2 2 3 3 1 5 5 5 1 2 3 4 5 1 2 3 4 5 Output YES 1 4 YES 1 3 NO YES 1 3 YES 1 2 -----Note----- In the first test case, $[4]$ is a subsequence of $[10, 8, 6, 4]$ and $[1, 2, 3, 4, 5]$. This array has length $1$, it is the smallest possible length of a subsequence of both $a$ and $b$. In the third test case, no non-empty subsequences of both $[3]$ and $[2]$ exist, so the answer is "NO". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for testcases in range(int(input())): n,m = list(map(int,input().split())) ar = list(map(int,input().split())) br = list(map(int, input().split())) flag = 0 ans = 0 for i in ar: for j in br: if i == j : ans = i flag = 1 break if flag == 0 : print("NO") else: print("YES") print(1,ans) ```
vfc_18322
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1382/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n4 5\n10 8 6 4\n1 2 3 4 5\n1 1\n3\n3\n1 1\n3\n2\n5 3\n1000 2 2 2 3\n3 1 5\n5 5\n1 2 3 4 5\n1 2 3 4 5\n", "output": "YES\n1 4\nYES\n1 3\nNO\nYES\n1 3\nYES\n1 5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2 3\n1 1\n1 2 3\n", "output": "YES\n1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2 2\n2 2\n2 2\n", "output": "YES\n1 2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2143
Solve the following coding problem using the programming language python: Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest children in that age to code. That is why he decided to give each child two sweets. Mike has $n$ sweets with sizes $a_1, a_2, \ldots, a_n$. All his sweets have different sizes. That is, there is no such pair $(i, j)$ ($1 \leq i, j \leq n$) such that $i \ne j$ and $a_i = a_j$. Since Mike has taught for many years, he knows that if he gives two sweets with sizes $a_i$ and $a_j$ to one child and $a_k$ and $a_p$ to another, where $(a_i + a_j) \neq (a_k + a_p)$, then a child who has a smaller sum of sizes will be upset. That is, if there are two children who have different sums of sweets, then one of them will be upset. Apparently, Mike does not want somebody to be upset. Mike wants to invite children giving each of them two sweets. Obviously, he can't give one sweet to two or more children. His goal is to invite as many children as he can. Since Mike is busy preparing to his first lecture in the elementary school, he is asking you to find the maximum number of children he can invite giving each of them two sweets in such way that nobody will be upset. -----Input----- The first line contains one integer $n$ ($2 \leq n \leq 1\,000$) — the number of sweets Mike has. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^5$) — the sizes of the sweets. It is guaranteed that all integers are distinct. -----Output----- Print one integer — the maximum number of children Mike can invite giving each of them two sweets in such way that nobody will be upset. -----Examples----- Input 8 1 8 3 11 4 9 2 7 Output 3 Input 7 3 1 7 11 9 2 12 Output 2 -----Note----- In the first example, Mike can give $9+2=11$ to one child, $8+3=11$ to another one, and $7+4=11$ to the third child. Therefore, Mike can invite three children. Note that it is not the only solution. In the second example, Mike can give $3+9=12$ to one child and $1+11$ to another one. Therefore, Mike can invite two children. Note that it is not the only solution. 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()) sweets = [int(x) for x in input().strip().split(" ")] dict = {} for i in range(n - 1): for j in range(i + 1, n): sum = sweets[i] + sweets[j] if sum in dict: dict[sum] += 1 else: dict[sum] = 1 print(max(dict.values())) ```
vfc_18326
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1121/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8\n1 8 3 11 4 9 2 7\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2144
Solve the following coding problem using the programming language python: You are given two integers $a$ and $m$. Calculate the number of integers $x$ such that $0 \le x < m$ and $\gcd(a, m) = \gcd(a + x, m)$. Note: $\gcd(a, b)$ is the greatest common divisor of $a$ and $b$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 50$) — the number of test cases. Next $T$ lines contain test cases — one per line. Each line contains two integers $a$ and $m$ ($1 \le a < m \le 10^{10}$). -----Output----- Print $T$ integers — one per test case. For each test case print the number of appropriate $x$-s. -----Example----- Input 3 4 9 5 10 42 9999999967 Output 6 1 9999999966 -----Note----- In the first test case appropriate $x$-s are $[0, 1, 3, 4, 6, 7]$. In the second test case the only appropriate $x$ is $0$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import gcd def Fenjie(n): k = {} if (n == 1): return {} a = 2 while (n >= 2): if (a*a > n): if (n in k): k[n] += 1 else: k[n] = 1 break b = n%a if (b == 0): if (a in k): k[a] += 1 else: k[a] = 1 n = n//a else: a += 1 return k def Euler(n): if (n == 1): return 1 k = Fenjie(n) m = n for i in k: m = m // i * (i-1) return m t = int(input()) for _ in range(t): a, b = list(map(int, input().split())) b = b//gcd(a,b) print(Euler(b)) ```
vfc_18330
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1295/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n4 9\n5 10\n42 9999999967\n", "output": "6\n1\n9999999966\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n1 8\n7 9\n8 10\n1 11\n", "output": "1\n2\n2\n4\n1\n6\n4\n6\n4\n10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n20 52\n48 53\n36 54\n23 55\n14 56\n54 57\n34 58\n33 59\n10 60\n21 61\n", "output": "12\n52\n2\n40\n2\n18\n28\n58\n2\n60\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2145
Solve the following coding problem using the programming language python: Recently Petya walked in the forest and found a magic stick. Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $a$ is even, then the spell will turn it into $\frac{3a}{2}$; If the chosen number $a$ is greater than one, then the spell will turn it into $a-1$. Note that if the number is even and greater than one, then Petya can choose which spell to apply. Petya now has only one number $x$. He wants to know if his favorite number $y$ can be obtained from $x$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $x$ as it is. -----Input----- The first line contains single 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 two integers $x$ and $y$ ($1 \le x, y \le 10^9$) — the current number and the number that Petya wants to get. -----Output----- For the $i$-th test case print the answer on it — YES if Petya can get the number $y$ from the number $x$ using known spells, 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 7 2 3 1 1 3 6 6 8 1 2 4 1 31235 6578234 Output YES YES NO YES NO YES YES 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(t): a, b = map(int, input().split()) if a == 1: if b == 1: print('YES') else: print('NO') continue if a <= 3: if b <= 3: print('YES') else: print('NO') continue print('YES') ```
vfc_18334
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1257/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234\n", "output": "YES\nYES\nNO\nYES\nNO\nYES\nYES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2 3\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n5 1000000000\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2146
Solve the following coding problem using the programming language python: Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city. City consists of n intersections numbered from 1 to n. Mike starts walking from his house located at the intersection number 1 and goes along some sequence of intersections. Walking from intersection number i to intersection j requires |i - j| units of energy. The total energy spent by Mike to visit a sequence of intersections p_1 = 1, p_2, ..., p_{k} is equal to $\sum_{i = 1}^{k - 1}|p_{i} - p_{i + 1}$ units of energy. Of course, walking would be boring if there were no shortcuts. A shortcut is a special path that allows Mike walking from one intersection to another requiring only 1 unit of energy. There are exactly n shortcuts in Mike's city, the i^{th} of them allows walking from intersection i to intersection a_{i} (i ≤ a_{i} ≤ a_{i} + 1) (but not in the opposite direction), thus there is exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequence p_1 = 1, p_2, ..., p_{k} then for each 1 ≤ i < k satisfying p_{i} + 1 = a_{p}_{i} and a_{p}_{i} ≠ p_{i} Mike will spend only 1 unit of energy instead of |p_{i} - p_{i} + 1| walking from the intersection p_{i} to intersection p_{i} + 1. For example, if Mike chooses a sequence p_1 = 1, p_2 = a_{p}_1, p_3 = a_{p}_2, ..., p_{k} = a_{p}_{k} - 1, he spends exactly k - 1 units of total energy walking around them. Before going on his adventure, Mike asks you to find the minimum amount of energy required to reach each of the intersections from his home. Formally, for each 1 ≤ i ≤ n Mike is interested in finding minimum possible total energy of some sequence p_1 = 1, p_2, ..., p_{k} = i. -----Input----- The first line contains an integer n (1 ≤ n ≤ 200 000) — the number of Mike's city intersection. The second line contains n integers a_1, a_2, ..., a_{n} (i ≤ a_{i} ≤ n , $a_{i} \leq a_{i + 1} \forall i < n)$, describing shortcuts of Mike's city, allowing to walk from intersection i to intersection a_{i} using only 1 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (from a_{i} to i). -----Output----- In the only line print n integers m_1, m_2, ..., m_{n}, where m_{i} denotes the least amount of total energy required to walk from intersection 1 to intersection i. -----Examples----- Input 3 2 2 3 Output 0 1 2 Input 5 1 2 3 4 5 Output 0 1 2 3 4 Input 7 4 4 4 4 7 7 7 Output 0 1 2 1 2 3 3 -----Note----- In the first sample case desired sequences are: 1: 1; m_1 = 0; 2: 1, 2; m_2 = 1; 3: 1, 3; m_3 = |3 - 1| = 2. In the second sample case the sequence for any intersection 1 < i is always 1, i and m_{i} = |1 - i|. In the third sample case — consider the following intersection sequences: 1: 1; m_1 = 0; 2: 1, 2; m_2 = |2 - 1| = 1; 3: 1, 4, 3; m_3 = 1 + |4 - 3| = 2; 4: 1, 4; m_4 = 1; 5: 1, 4, 5; m_5 = 1 + |4 - 5| = 2; 6: 1, 4, 6; m_6 = 1 + |4 - 6| = 3; 7: 1, 4, 5, 7; m_7 = 1 + |4 - 5| + 1 = 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()) a = list([int(x) - 1 for x in input().split()]) cnt = 0 curr = [0] v = [0 for _ in range(n)] r = [0 for _ in range(n)] lvl = 0 while cnt < n: nxt = [] for i in curr: if v[i]: continue v[i] = 1 r[i] = lvl cnt += 1 if i > 0 and not v[i-1]: nxt.append(i - 1) if i < n - 1 and not v[i+1]: nxt.append(i + 1) if not v[a[i]]: nxt.append(a[i]) curr = nxt lvl += 1 print(' '.join(map(str,r))) ```
vfc_18338
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/689/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n2 2 3\n", "output": "0 1 2 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2148
Solve the following coding problem using the programming language python: Carol is currently curling. She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10^100. She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed. -----Input----- The first line will contain two integers n and r (1 ≤ n, r ≤ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 1 000) — the x-coordinates of the disks. -----Output----- Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$ for all coordinates. -----Example----- Input 6 2 5 5 6 8 3 12 Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 -----Note----- The final positions of the disks will look as follows: [Image] In particular, note the position of the last disk. 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 n, r = map(int, input().split()) xs = list(map(int, input().split())) points = [] for x in xs: y = r for x0, y0 in points: if abs(x - x0) <= 2 * r: y = max(y, y0 + ((2*r)**2 - (x-x0)**2)**0.5) points.append((x, y)) print(" ".join("%.20f" % p[1] for p in points)) ```
vfc_18346
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/908/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 2\n5 5 6 8 3 12\n", "output": "2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n5\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 300\n939 465 129 611 532\n", "output": "300 667.864105343 1164.9596696 1522.27745533 2117.05388391\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 1\n416 387 336 116 81\n", "output": "1 1 1 1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 10\n1 100 1000\n", "output": "10 10 10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n2 20\n", "output": "1 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2149
Solve the following coding problem using the programming language python: Your program fails again. This time it gets "Wrong answer on test 233". This is the easier version of the problem. In this version $1 \le n \le 2000$. You can hack this problem only if you solve and lock both problems. The problem is about a test containing $n$ one-choice-questions. Each of the questions contains $k$ options, and only one of them is correct. The answer to the $i$-th question is $h_{i}$, and if your answer of the question $i$ is $h_{i}$, you earn $1$ point, otherwise, you earn $0$ points for this question. The values $h_1, h_2, \dots, h_n$ are known to you in this problem. However, you have a mistake in your program. It moves the answer clockwise! Consider all the $n$ answers are written in a circle. Due to the mistake in your program, they are shifted by one cyclically. Formally, the mistake moves the answer for the question $i$ to the question $i \bmod n + 1$. So it moves the answer for the question $1$ to question $2$, the answer for the question $2$ to the question $3$, ..., the answer for the question $n$ to the question $1$. We call all the $n$ answers together an answer suit. There are $k^n$ possible answer suits in total. You're wondering, how many answer suits satisfy the following condition: after moving clockwise by $1$, the total number of points of the new answer suit is strictly larger than the number of points of the old one. You need to find the answer modulo $998\,244\,353$. For example, if $n = 5$, and your answer suit is $a=[1,2,3,4,5]$, it will submitted as $a'=[5,1,2,3,4]$ because of a mistake. If the correct answer suit is $h=[5,2,2,3,4]$, the answer suit $a$ earns $1$ point and the answer suite $a'$ earns $4$ points. Since $4 > 1$, the answer suit $a=[1,2,3,4,5]$ should be counted. -----Input----- The first line contains two integers $n$, $k$ ($1 \le n \le 2000$, $1 \le k \le 10^9$) — the number of questions and the number of possible answers to each question. The following line contains $n$ integers $h_1, h_2, \dots, h_n$, ($1 \le h_{i} \le k)$ — answers to the questions. -----Output----- Output one integer: the number of answers suits satisfying the given condition, modulo $998\,244\,353$. -----Examples----- Input 3 3 1 3 1 Output 9 Input 5 5 1 1 4 2 2 Output 1000 -----Note----- For the first example, valid answer suits are $[2,1,1], [2,1,2], [2,1,3], [3,1,1], [3,1,2], [3,1,3], [3,2,1], [3,2,2], [3,2,3]$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python M=998244353 class Factorial: def __init__(self,n): self.f=f=[0]*(n+1) f[0]=b=1 for i in range(1,n+1):f[i]=b=b*i%M self.inv=inv=[0]*(n+1) inv[n]=b=pow(self.f[n],M-2,M) for i in range(n,0,-1):inv[i-1]=b=b*i%M def factorial(self,i): return self.f[i] def ifactorial(self,i): return self.inv[i] def comb(self,n,k): if n>=k:return self.f[n]*self.inv[n-k]*self.inv[k]%M else:return 0 def main(): n,k,*h=map(int,open(0).read().split()) m=sum(i!=j for i,j in zip(h,h[1:]+h[:1])) comb=Factorial(m).comb print((pow(k,m,M)-sum(comb(m,i)*comb(m-i,i)*pow(k-2,m-i-i,M)for i in range(m//2+1)))*pow(k,n-m,M)*pow(2,M-2,M)%M) main() ```
vfc_18350
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1227/F1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 3\n1 3 1\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n1 1 4 2 2\n", "output": "1000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 2\n1 1 2 2 1 1\n", "output": "16\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2150
Solve the following coding problem using the programming language python: Alicia has an array, $a_1, a_2, \ldots, a_n$, of non-negative integers. For each $1 \leq i \leq n$, she has found a non-negative integer $x_i = max(0, a_1, \ldots, a_{i-1})$. Note that for $i=1$, $x_i = 0$. For example, if Alicia had the array $a = \{0, 1, 2, 0, 3\}$, then $x = \{0, 0, 1, 2, 2\}$. Then, she calculated an array, $b_1, b_2, \ldots, b_n$: $b_i = a_i - x_i$. For example, if Alicia had the array $a = \{0, 1, 2, 0, 3\}$, $b = \{0-0, 1-0, 2-1, 0-2, 3-2\} = \{0, 1, 1, -2, 1\}$. Alicia gives you the values $b_1, b_2, \ldots, b_n$ and asks you to restore the values $a_1, a_2, \ldots, a_n$. Can you help her solve the problem? -----Input----- The first line contains one integer $n$ ($3 \leq n \leq 200\,000$) – the number of elements in Alicia's array. The next line contains $n$ integers, $b_1, b_2, \ldots, b_n$ ($-10^9 \leq b_i \leq 10^9$). It is guaranteed that for the given array $b$ there is a solution $a_1, a_2, \ldots, a_n$, for all elements of which the following is true: $0 \leq a_i \leq 10^9$. -----Output----- Print $n$ integers, $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^9$), such that if you calculate $x$ according to the statement, $b_1$ will be equal to $a_1 - x_1$, $b_2$ will be equal to $a_2 - x_2$, ..., and $b_n$ will be equal to $a_n - x_n$. It is guaranteed that there exists at least one solution for the given tests. It can be shown that the solution is unique. -----Examples----- Input 5 0 1 1 -2 1 Output 0 1 2 0 3 Input 3 1000 999999000 -1000000000 Output 1000 1000000000 0 Input 5 2 1 2 2 3 Output 2 3 5 7 10 -----Note----- The first test was described in the problem statement. In the second test, if Alicia had an array $a = \{1000, 1000000000, 0\}$, then $x = \{0, 1000, 1000000000\}$ and $b = \{1000-0, 1000000000-1000, 0-1000000000\} = \{1000, 999999000, -1000000000\}$. 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()) ls = list(map(int, input().split())) msf = 0 res = [] for e in ls: v = msf + e msf = max(msf, v) res.append(v) for e in res: print(e, end=' ') ```
vfc_18354
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1326/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n0 1 1 -2 1\n", "output": "0 1 2 0 3 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1000 999999000 -1000000000\n", "output": "1000 1000000000 0 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n2 1 2 2 3\n", "output": "2 3 5 7 10 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
2151
Solve the following coding problem using the programming language python: You are given a sequence $s$ consisting of $n$ digits from $1$ to $9$. You have to divide it into at least two segments (segment — is a consecutive sequence of elements) (in other words, you have to place separators between some digits of the sequence) in such a way that each element belongs to exactly one segment and if the resulting division will be represented as an integer numbers sequence then each next element of this sequence will be strictly greater than the previous one. More formally: if the resulting division of the sequence is $t_1, t_2, \dots, t_k$, where $k$ is the number of element in a division, then for each $i$ from $1$ to $k-1$ the condition $t_{i} < t_{i + 1}$ (using numerical comparing, it means that the integer representations of strings are compared) should be satisfied. For example, if $s=654$ then you can divide it into parts $[6, 54]$ and it will be suitable division. But if you will divide it into parts $[65, 4]$ then it will be bad division because $65 > 4$. If $s=123$ then you can divide it into parts $[1, 23]$, $[1, 2, 3]$ but not into parts $[12, 3]$. Your task is to find any suitable division for each of the $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 300$) — the number of queries. The first line of the $i$-th query contains one integer number $n_i$ ($2 \le n_i \le 300$) — the number of digits in the $i$-th query. The second line of the $i$-th query contains one string $s_i$ of length $n_i$ consisting only of digits from $1$ to $9$. -----Output----- If the sequence of digits in the $i$-th query cannot be divided into at least two parts in a way described in the problem statement, print the single line "NO" for this query. Otherwise in the first line of the answer to this query print "YES", on the second line print $k_i$ — the number of parts in your division of the $i$-th query sequence and in the third line print $k_i$ strings $t_{i, 1}, t_{i, 2}, \dots, t_{i, k_i}$ — your division. Parts should be printed in order of the initial string digits. It means that if you write the parts one after another without changing their order then you'll get the string $s_i$. See examples for better understanding. -----Example----- Input 4 6 654321 4 1337 2 33 4 2122 Output YES 3 6 54 321 YES 3 1 3 37 NO YES 2 21 22 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()) for x in range(n): l=int(input()) ar=input() a=int(ar[0]) b=int(ar[1:]) if(a<b): print("YES") print(2) print(a,b) else: print("NO") ```
vfc_18358
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1107/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n6\n654321\n4\n1337\n2\n33\n4\n2122\n", "output": "YES\n2\n6 54321\nYES\n2\n1 337\nNO\nYES\n2\n2 122\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\n3333\n", "output": "YES\n2\n3 333\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "21\n3\n123\n3\n123\n3\n123\n3\n123\n3\n123\n3\n213\n3\n123\n3\n123\n3\n123\n3\n321\n3\n123\n3\n321\n3\n132\n3\n322\n3\n321\n3\n341\n3\n123\n3\n421\n3\n421\n3\n421\n3\n421\n", "output": "YES\n2\n1 23\nYES\n2\n1 23\nYES\n2\n1 23\nYES\n2\n1 23\nYES\n2\n1 23\nYES\n2\n2 13\nYES\n2\n1 23\nYES\n2\n1 23\nYES\n2\n1 23\nYES\n2\n3 21\nYES\n2\n1 23\nYES\n2\n3 21\nYES\n2\n1 32\nYES\n2\n3 22\nYES\n2\n3 21\nYES\n2\n3 41\nYES\n2\n1 23\nYES\n2\n4 21\nYES\n2\n4 21\nYES\n2\n4 21\nYES\n2\n4 21\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n3\n333\n", "output": "YES\n2\n3 33\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2152
Solve the following coding problem using the programming language python: Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly a_{i} kilograms of meat. [Image] There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for p_{i} dollars per kilogram. Malek knows all numbers a_1, ..., a_{n} and p_1, ..., p_{n}. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future. Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days. -----Input----- The first line of input contains integer n (1 ≤ n ≤ 10^5), the number of days. In the next n lines, i-th line contains two integers a_{i} and p_{i} (1 ≤ a_{i}, p_{i} ≤ 100), the amount of meat Duff needs and the cost of meat in that day. -----Output----- Print the minimum money needed to keep Duff happy for n days, in one line. -----Examples----- Input 3 1 3 2 2 3 1 Output 10 Input 3 1 3 2 1 3 2 Output 8 -----Note----- In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day. In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day. 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()) bestP = 10**9 sol = 0 for i in range(0, n): a, p = list(map(int, input().split())) bestP = min(bestP, p) sol += a * bestP print(sol) ```
vfc_18362
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/588/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 3\n2 2\n3 1\n", "output": "10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 3\n2 1\n3 2\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n39 52\n", "output": "2028\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n25 56\n94 17\n", "output": "2998\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n39 21\n95 89\n73 90\n9 55\n85 32\n", "output": "6321\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "12\n70 11\n74 27\n32 11\n26 83\n57 18\n97 28\n75 43\n75 21\n84 29\n16 2\n89 63\n21 88\n", "output": "6742\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2153
Solve the following coding problem using the programming language python: There are $n$ beautiful skyscrapers in New York, the height of the $i$-th one is $h_i$. Today some villains have set on fire first $n - 1$ of them, and now the only safety building is $n$-th skyscraper. Let's call a jump from $i$-th skyscraper to $j$-th ($i < j$) discrete, if all skyscrapers between are strictly lower or higher than both of them. Formally, jump is discrete, if $i < j$ and one of the following conditions satisfied: $i + 1 = j$ $\max(h_{i + 1}, \ldots, h_{j - 1}) < \min(h_i, h_j)$ $\max(h_i, h_j) < \min(h_{i + 1}, \ldots, h_{j - 1})$. At the moment, Vasya is staying on the first skyscraper and wants to live a little longer, so his goal is to reach $n$-th skyscraper with minimal count of discrete jumps. Help him with calcualting this number. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 3 \cdot 10^5$) — total amount of skyscrapers. The second line contains $n$ integers $h_1, h_2, \ldots, h_n$ ($1 \le h_i \le 10^9$) — heights of skyscrapers. -----Output----- Print single number $k$ — minimal amount of discrete jumps. We can show that an answer always exists. -----Examples----- Input 5 1 3 1 4 5 Output 3 Input 4 4 2 2 4 Output 1 Input 2 1 1 Output 1 Input 5 100 1 100 1 100 Output 2 -----Note----- In the first testcase, Vasya can jump in the following way: $1 \rightarrow 2 \rightarrow 4 \rightarrow 5$. In the second and third testcases, we can reach last skyscraper in one jump. Sequence of jumps in the fourth testcase: $1 \rightarrow 3 \rightarrow 5$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python mod = 1000000007 eps = 10**-9 inf = 10**9 def main(): import sys input = sys.stdin.readline class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i def lower_bound(self, w): if w <= 0: return 0 x = 0 k = 1 << (self.size.bit_length() - 1) while k: if x + k <= self.size and self.tree[x + k] < w: w -= self.tree[x + k] x += k k >>= 1 return x + 1 N = int(input()) A = list(map(int, input().split())) adj = [[] for _ in range(N+1)] AA = sorted(list(set(A))) a2i = {a:i for i, a in enumerate(AA)} AI = [[] for _ in range(len(AA))] for i, a in enumerate(A): ii = a2i[a] AI[ii].append(i+1) bit_high = Bit(N) for i_list in AI: for i in i_list: bit_high.add(i, 1) for i in i_list: val = bit_high.sum(i) il = bit_high.lower_bound(val - 1) ir = bit_high.lower_bound(val + 1) if il > 0: adj[il].append(i) if ir <= N: adj[i].append(ir) bit_low = Bit(N) AI.reverse() for i_list in AI: for i in i_list: bit_low.add(i, 1) for i in i_list: val = bit_low.sum(i) il = bit_low.lower_bound(val - 1) ir = bit_low.lower_bound(val + 1) if il > 0: adj[il].append(i) if ir <= N: adj[i].append(ir) dp = [inf] * (N+1) dp[1] = 0 for i in range(1, N+1): for j in adj[i]: dp[j] = min(dp[j], dp[i] + 1) print(dp[N]) def __starting_point(): main() __starting_point() ```
vfc_18366
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1407/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 3 1 4 5\n", "output": "3", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n4 2 2 4\n", "output": "1", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1 1\n", "output": "1", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n100 1 100 1 100\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n2 3 4 2\n", "output": "1", "type": "stdin_stdout" } ] }
apps
verifiable_code
2155
Solve the following coding problem using the programming language python: Since Sonya has just learned the basics of matrices, she decided to play with them a little bit. Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so. The Manhattan distance between two cells ($x_1$, $y_1$) and ($x_2$, $y_2$) is defined as $|x_1 - x_2| + |y_1 - y_2|$. For example, the Manhattan distance between the cells $(5, 2)$ and $(7, 1)$ equals to $|5-7|+|2-1|=3$. [Image] Example of a rhombic matrix. Note that rhombic matrices are uniquely defined by $n$, $m$, and the coordinates of the cell containing the zero. She drew a $n\times m$ rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of $n\cdot m$ numbers). Note that Sonya will not give you $n$ and $m$, so only the sequence of numbers in this matrix will be at your disposal. Write a program that finds such an $n\times m$ rhombic matrix whose elements are the same as the elements in the sequence in some order. -----Input----- The first line contains a single integer $t$ ($1\leq t\leq 10^6$) — the number of cells in the matrix. The second line contains $t$ integers $a_1, a_2, \ldots, a_t$ ($0\leq a_i< t$) — the values in the cells in arbitrary order. -----Output----- In the first line, print two positive integers $n$ and $m$ ($n \times m = t$) — the size of the matrix. In the second line, print two integers $x$ and $y$ ($1\leq x\leq n$, $1\leq y\leq m$) — the row number and the column number where the cell with $0$ is located. If there are multiple possible answers, print any of them. If there is no solution, print the single integer $-1$. -----Examples----- Input 20 1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4 Output 4 5 2 2 Input 18 2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1 Output 3 6 2 3 Input 6 2 1 0 2 1 2 Output -1 -----Note----- You can see the solution to the first example in the legend. You also can choose the cell $(2, 2)$ for the cell where $0$ is located. You also can choose a $5\times 4$ matrix with zero at $(4, 2)$. In the second example, there is a $3\times 6$ matrix, where the zero is located at $(2, 3)$ there. In the third example, a solution does not exist. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import sqrt from collections import Counter def f(h, w, y, x): return h * w * (h + w - (x + y + 1) * 2) // 2 + h * x * (x + 1) + w * y * (y + 1) def check(h, w, y, x, cnt): for i in range(1, y + 1): for j in range(i + 1, i + x + 1): cnt[j] -= 1 for j in range(i, i + w - x): cnt[j] -= 1 for i in range(h - y): for j in range(i + 1, i + x + 1): cnt[j] -= 1 for j in range(i, i + w - x): cnt[j] -= 1 if any(cnt.values()): return print(f'{h} {w}\n{y+1} {int(x)+1}') raise TabError def getyx(h, w, tot, cnt): b = (w - 1) * .5 c = h * (tot - h * (w * w - 2 * w * (1 - h) - 1) * .25) for y in range((h + 3) // 2): d = (c - h * y * (w * y - h * w + w)) if d >= 0: x = b - sqrt(d) / h if x.is_integer() and x >= 0.: check(h, w, y, int(x), cnt.copy()) def main(): n, l = int(input()), list(map(int, input().split())) cnt, r, R, tot = Counter(l), 1, max(l), sum(l) for r in range(1, R + 1): if cnt[r] < r * 4: break try: for h in range(r * 2 - 1, int(sqrt(n)) + 1): if not n % h: w = n // h if f(h, w, h // 2, w // 2) <= tot <= f(h, w, 0, 0): getyx(h, w, tot, cnt) except TabError: return print(-1) def __starting_point(): main() __starting_point() ```
vfc_18374
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1004/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4\n", "output": "4 5\n2 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1\n", "output": "3 6\n2 3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n2 1 0 2 1 2\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n", "output": "1 1\n1 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2156
Solve the following coding problem using the programming language python: Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it. 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,deque import sys,heapq,bisect,math,itertools,string,queue,copy,time sys.setrecursionlimit(10**8) INF = float('inf') mod = 10**9+7 eps = 10**-7 def inp(): return int(input()) def inpl(): return list(map(int, input().split())) def inpl_str(): return list(input().split()) N = inp() aa = inpl() Q = inp() tmp = 0 ruiseki = [0] for a in aa: tmp += a ruiseki.append(tmp) for q in range(Q): l,r = inpl() print((ruiseki[r]-ruiseki[l-1])//10) ```
vfc_18378
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1189/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8\n8 7 3 1 7 0 9 4\n3\n1 8\n2 5\n7 7\n", "output": "3\n1\n0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n0 1 2 3 3 5\n3\n1 2\n1 4\n3 6\n", "output": "0\n0\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n8\n1\n1 1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n9 9\n2\n1 1\n1 2\n", "output": "0\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n8 5 6 6\n3\n2 3\n2 3\n3 4\n", "output": "1\n1\n1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2157
Solve the following coding problem using the programming language python: The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 3 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,q=list(map(int,input().split())) A=list(map(int,input().split())) C=[0]*n for i in range(q): l,r=list(map(int,input().split())) C[l-1]+=1 if(r!=n): C[r]-=1 for i in range(1,n): C[i]+=C[i-1] C.sort() A.sort() ans=0 for i in range(n): ans+=C[i]*A[i] print(int(ans)) ```
vfc_18382
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/276/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 3\n5 3 2\n1 2\n2 3\n1 3\n", "output": "25\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\n5 2 4 1 3\n1 5\n2 3\n2 3\n", "output": "33\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "34 21\n23 38 16 49 44 50 48 34 33 19 18 31 11 15 20 47 44 30 39 33 45 46 1 13 27 16 31 36 17 23 38 5 30 16\n8 16\n14 27\n8 26\n1 8\n5 6\n23 28\n4 33\n13 30\n12 30\n11 30\n9 21\n1 14\n15 22\n4 11\n5 24\n8 20\n17 33\n6 9\n3 14\n25 34\n10 17\n", "output": "9382\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2158
Solve the following coding problem using the programming language python: Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on. Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank? Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly. Jenny is given the number 0. -----Input----- The first line of the input contains the number of friends n (3 ≤ n ≤ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 ≤ u, v ≤ n - 1, 1 ≤ c ≤ 10^4), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c. It is guaranteed that the social network of the input forms a tree. -----Output----- Output a single integer – the maximum sum of costs. -----Examples----- Input 4 0 1 4 0 2 2 2 3 3 Output 5 Input 6 1 2 3 0 2 100 1 4 2 0 3 7 3 5 10 Output 105 Input 11 1 0 1664 2 0 881 3 2 4670 4 2 1555 5 1 1870 6 2 1265 7 2 288 8 7 2266 9 2 1536 10 6 3378 Output 5551 -----Note----- In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 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 sys.setrecursionlimit(10**6) ans = 0 def solve(): n = int(input()) Adj = [[] for i in range(n)] for i in range(n - 1): ai, bi, ci = map(int, sys.stdin.readline().split()) Adj[ai].append((bi, ci)) Adj[bi].append((ai, ci)) dfs(n, Adj, -1, 0, 0) print(ans) def dfs(n, Adj, p, u, cost): if u != 0 and len(Adj[u]) == 1: nonlocal ans ans = max(ans, cost) return for (v, c) in Adj[u]: if p == v: continue dfs(n, Adj, u, v, cost + c) def __starting_point(): solve() __starting_point() ```
vfc_18386
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/802/J", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n0 1 4\n0 2 2\n2 3 3\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 0 5987\n2 0 8891\n", "output": "8891\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2159
Solve the following coding problem using the programming language python: Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color t_{i}. For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant. There are $\frac{n \cdot(n + 1)}{2}$ non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant. -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — the number of balls. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ n) where t_{i} is the color of the i-th ball. -----Output----- Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color. -----Examples----- Input 4 1 2 1 2 Output 7 3 0 0 Input 3 1 1 1 Output 6 0 0 -----Note----- In the first sample, color 2 is dominant in three intervals: An interval [2, 2] contains one ball. This ball's color is 2 so it's clearly a dominant color. An interval [4, 4] contains one ball, with color 2 again. An interval [2, 4] contains two balls of color 2 and one ball of color 1. There are 7 more intervals and color 1 is dominant in all of them. 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=list(map(int,input().split())) r=[0]*(n+1) for i in range(n): d={} v=-1 for j in range(i,n): t=d.get(a[j],0)+1 d[a[j]]=t if t>v or t==v and a[j]<m: v=t m=a[j] r[m]+=1 print(' '.join(map(str,r[1:]))) ```
vfc_18390
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/643/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1 2 1 2\n", "output": "7 3 0 0 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 1 1\n", "output": "6 0 0 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n9 1 5 2 9 2 9 2 1 1\n", "output": "18 30 0 0 1 0 0 0 6 0 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "50\n17 13 19 19 19 34 32 24 24 13 34 17 19 19 7 32 19 13 13 30 19 34 34 28 41 24 24 47 22 34 21 21 30 7 22 21 32 19 34 19 34 22 7 28 6 13 19 30 13 30\n", "output": "0 0 0 0 0 22 40 0 0 0 0 0 98 0 0 0 5 0 675 0 165 9 0 61 0 0 0 5 0 6 0 4 0 183 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2160
Solve the following coding problem using the programming language python: Alice and Bob are playing a game on a line with $n$ cells. There are $n$ cells labeled from $1$ through $n$. For each $i$ from $1$ to $n-1$, cells $i$ and $i+1$ are adjacent. Alice initially has a token on some cell on the line, and Bob tries to guess where it is. Bob guesses a sequence of line cell numbers $x_1, x_2, \ldots, x_k$ in order. In the $i$-th question, Bob asks Alice if her token is currently on cell $x_i$. That is, Alice can answer either "YES" or "NO" to each Bob's question. At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions. Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all. You are given $n$ and Bob's questions $x_1, \ldots, x_k$. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions. Let $(a,b)$ denote a scenario where Alice starts at cell $a$ and ends at cell $b$. Two scenarios $(a_i, b_i)$ and $(a_j, b_j)$ are different if $a_i \neq a_j$ or $b_i \neq b_j$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \leq n,k \leq 10^5$) — the number of cells and the number of questions Bob asked. The second line contains $k$ integers $x_1, x_2, \ldots, x_k$ ($1 \leq x_i \leq n$) — Bob's questions. -----Output----- Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions. -----Examples----- Input 5 3 5 1 4 Output 9 Input 4 8 1 2 3 4 4 3 2 1 Output 0 Input 100000 1 42 Output 299997 -----Note----- The notation $(i,j)$ denotes a scenario where Alice starts at cell $i$ and ends at cell $j$. In the first example, the valid scenarios are $(1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5)$. For example, $(3,4)$ is valid since Alice can start at cell $3$, stay there for the first three questions, then move to cell $4$ after the last question. $(4,5)$ is valid since Alice can start at cell $4$, stay there for the first question, the move to cell $5$ for the next two questions. Note that $(4,5)$ is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once. In the second example, Alice has no valid scenarios. In the last example, all $(i,j)$ where $|i-j| \leq 1$ except for $(42, 42)$ are valid scenarios. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python nCells, nQueries = list(map(int, input().split())) queries = list(map(int, input().split())) seen = [False] * 100002 bad = set() for q in queries: if not seen[q]: seen[q] = True bad.add((q, q)) if seen[q - 1]: bad.add((q - 1, q)) if seen[q + 1]: bad.add((q + 1, q)) print((nCells - 1) * 2 + nCells - len(bad)) ```
vfc_18394
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1147/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 3\n5 1 4\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 8\n1 2 3 4 4 3 2 1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100000 1\n42\n", "output": "299997\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2\n1 1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "50 75\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "50000 30\n33549 17601 44000 7481 38819 15862 27683 21020 24720 399 14593 35601 41380 25049 46665 32822 24640 11058 26495 34522 49913 18477 12333 4947 30203 26721 3805 7259 42643 4522\n", "output": "149968\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2161
Solve the following coding problem using the programming language python: Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers. Vasya decided to organize information about the phone numbers of friends. You will be given n strings — all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record. Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account. The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once. Read the examples to understand statement and format of the output better. -----Input----- First line contains the integer n (1 ≤ n ≤ 20) — number of entries in Vasya's phone books. The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros. -----Output----- Print out the ordered information about the phone numbers of Vasya's friends. First output m — number of friends that are found in Vasya's phone books. The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend. Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order. -----Examples----- Input 2 ivan 1 00123 masha 1 00123 Output 2 masha 1 00123 ivan 1 00123 Input 3 karl 2 612 12 petr 1 12 katya 1 612 Output 3 katya 1 612 petr 1 12 karl 1 612 Input 4 ivan 3 123 123 456 ivan 2 456 456 ivan 8 789 3 23 6 56 9 89 2 dasha 2 23 789 Output 2 dasha 2 23 789 ivan 4 789 123 2 456 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()) friends = dict() for _ in range(n): line = input().strip().split() if line[0] not in friends: friends[line[0]] = set() for i in range(2, len(line)): friends[line[0]].add(line[i]) def order(S): S2 = [(len(e),e) for e in S] S2.sort() real = set() for i in range(len(S2)): d,e = S2[i] flag = True for j in range(i+1, len(S2)): x = S2[j][1] if x[-d:] == e: flag = False break if flag: real.add(e) return real print(len(friends)) for f in friends: nums = order(friends[f]) st = ' '.join(nums) print(f + ' ' + str(len(nums)) + ' ' + st) ```
vfc_18398
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/898/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\nivan 1 00123\nmasha 1 00123\n", "output": "2\nmasha 1 00123 \nivan 1 00123 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\nkarl 2 612 12\npetr 1 12\nkatya 1 612\n", "output": "3\nkatya 1 612 \npetr 1 12 \nkarl 1 612 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\nivan 3 123 123 456\nivan 2 456 456\nivan 8 789 3 23 6 56 9 89 2\ndasha 2 23 789\n", "output": "2\ndasha 2 789 23 \nivan 4 789 123 456 2 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\nnxj 6 7 6 6 7 7 7\nnxj 10 8 5 1 7 6 1 0 7 0 6\nnxj 2 6 5\nnxj 10 6 7 6 6 5 8 3 6 6 8\nnxj 10 6 1 7 6 7 1 8 7 8 6\nnxj 10 8 5 8 6 5 6 1 9 6 3\nnxj 10 8 1 6 4 8 0 4 6 0 1\nnxj 9 2 6 6 8 1 1 3 6 6\nnxj 10 8 9 0 9 1 3 2 3 2 3\nnxj 6 6 7 0 8 1 2\nnxj 7 7 7 8 1 3 6 9\nnxj 10 2 7 0 1 5 1 9 1 2 6\nnxj 6 9 6 9 6 3 7\nnxj 9 0 1 7 8 2 6 6 5 6\nnxj 4 0 2 3 7\nnxj 10 0 4 0 6 1 1 8 8 4 7\nnxj 8 4 6 2 6 6 1 2 7\nnxj 10 5 3 4 2 1 0 7 0 7 6\nnxj 10 9 6 0 6 1 6 2 1 9 6\nnxj 4 2 9 0 1\n", "output": "1\nnxj 10 0 3 2 1 4 7 8 5 9 6 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\nl 6 02 02 2 02 02 2\nl 8 8 8 8 2 62 13 31 3\ne 9 0 91 0 0 60 91 60 2 44\ne 9 69 2 1 44 2 91 66 1 70\nl 9 7 27 27 3 1 3 7 80 81\nl 9 2 1 13 7 2 10 02 3 92\ne 9 0 15 3 5 5 15 91 09 44\nl 7 2 50 4 5 98 31 98\nl 3 26 7 3\ne 6 7 5 0 62 65 91\nl 8 80 0 4 0 2 2 0 13\nl 9 19 13 02 2 1 4 19 26 02\nl 10 7 39 7 9 22 22 26 2 90 4\ne 7 65 2 36 0 34 57 9\ne 8 13 02 09 91 73 5 36 62\nl 9 75 0 10 8 76 7 82 8 34\nl 7 34 0 19 80 6 4 7\ne 5 4 2 5 7 2\ne 7 4 02 69 7 07 20 2\nl 4 8 2 1 63\n", "output": "2\ne 18 15 62 07 70 91 57 02 66 65 69 09 13 20 44 73 34 60 36 \nl 21 27 50 22 63 75 19 26 90 02 92 62 31 10 76 82 80 98 81 39 34 13 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\no 10 6 6 97 45 6 6 6 6 5 6\nl 8 5 5 5 19 59 5 8 5\nj 9 2 30 58 2 2 1 0 30 4\nc 10 1 1 7 51 7 7 51 1 1 1\no 9 7 97 87 70 2 19 2 14 6\ne 6 26 6 6 6 26 5\ng 9 3 3 3 3 3 78 69 8 9\nl 8 8 01 1 5 8 41 72 3\nz 10 1 2 2 2 9 1 9 1 6 7\ng 8 7 78 05 36 7 3 67 9\no 5 6 9 9 7 7\ne 10 30 2 1 1 2 5 04 0 6 6\ne 9 30 30 2 2 0 26 30 79 8\nt 10 2 2 9 29 7 7 7 9 2 9\nc 7 7 51 1 31 2 7 4\nc 9 83 1 6 78 94 74 54 8 32\ng 8 4 1 01 9 39 28 6 6\nt 7 9 2 01 4 4 9 58\nj 5 0 1 58 02 4\nw 10 80 0 91 91 06 91 9 9 27 7\n", "output": "9\nw 5 91 27 06 9 80 \nt 6 7 58 4 29 2 01 \ne 8 8 79 5 30 2 26 04 1 \nl 8 72 3 19 59 41 01 5 8 \nj 5 4 30 58 1 02 \nz 5 6 1 9 2 7 \ng 10 05 39 4 3 36 01 67 69 28 78 \no 8 14 87 97 6 19 70 45 2 \nc 10 83 51 31 54 74 32 7 94 78 6 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2162
Solve the following coding problem using the programming language python: A team of three programmers is going to play a contest. The contest consists of $n$ problems, numbered from $1$ to $n$. Each problem is printed on a separate sheet of paper. The participants have decided to divide the problem statements into three parts: the first programmer took some prefix of the statements (some number of first paper sheets), the third contestant took some suffix of the statements (some number of last paper sheets), and the second contestant took all remaining problems. But something went wrong — the statements were printed in the wrong order, so the contestants have received the problems in some random order. The first contestant has received problems $a_{1, 1}, a_{1, 2}, \dots, a_{1, k_1}$. The second one has received problems $a_{2, 1}, a_{2, 2}, \dots, a_{2, k_2}$. The third one has received all remaining problems ($a_{3, 1}, a_{3, 2}, \dots, a_{3, k_3}$). The contestants don't want to play the contest before they redistribute the statements. They want to redistribute them so that the first contestant receives some prefix of the problemset, the third contestant receives some suffix of the problemset, and the second contestant receives all the remaining problems. During one move, some contestant may give one of their problems to other contestant. What is the minimum number of moves required to redistribute the problems? It is possible that after redistribution some participant (or even two of them) will not have any problems. -----Input----- The first line contains three integers $k_1, k_2$ and $k_3$ ($1 \le k_1, k_2, k_3 \le 2 \cdot 10^5, k_1 + k_2 + k_3 \le 2 \cdot 10^5$) — the number of problems initially taken by the first, the second and the third participant, respectively. The second line contains $k_1$ integers $a_{1, 1}, a_{1, 2}, \dots, a_{1, k_1}$ — the problems initially taken by the first participant. The third line contains $k_2$ integers $a_{2, 1}, a_{2, 2}, \dots, a_{2, k_2}$ — the problems initially taken by the second participant. The fourth line contains $k_3$ integers $a_{3, 1}, a_{3, 2}, \dots, a_{3, k_3}$ — the problems initially taken by the third participant. It is guaranteed that no problem has been taken by two (or three) participants, and each integer $a_{i, j}$ meets the condition $1 \le a_{i, j} \le n$, where $n = k_1 + k_2 + k_3$. -----Output----- Print one integer — the minimum number of moves required to redistribute the problems so that the first participant gets the prefix of the problemset, the third participant gets the suffix of the problemset, and the second participant gets all of the remaining problems. -----Examples----- Input 2 1 2 3 1 4 2 5 Output 1 Input 3 2 1 3 2 1 5 4 6 Output 0 Input 2 1 3 5 6 4 1 2 3 Output 3 Input 1 5 1 6 5 1 2 4 7 3 Output 2 -----Note----- In the first example the third contestant should give the problem $2$ to the first contestant, so the first contestant has $3$ first problems, the third contestant has $1$ last problem, and the second contestant has $1$ remaining problem. In the second example the distribution of problems is already valid: the first contestant has $3$ first problems, the third contestant has $1$ last problem, and the second contestant has $2$ remaining problems. The best course of action in the third example is to give all problems to the third contestant. The best course of action in the fourth example is to give all problems to the second contestant. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = sum(list(map(int, input().split()))) A = [0 for i in range(n)] DP = [[0 for i in range(3)] for i in range(n)] for i in range(3): for j in map(int, input().split()): A[j - 1] = i DP[0] = [A[0] != i for i in range(3)] for i in range(1, n): DP[i][0] = DP[i - 1][0] + (A[i] != 0) DP[i][1] = min(DP[i - 1][0], DP[i - 1][1]) + (A[i] != 1) DP[i][2] = min(DP[i - 1]) + (A[i] != 2) print(min(DP[n - 1])) ```
vfc_18402
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1257/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 1 2\n3 1\n4\n2 5\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2 1\n3 2 1\n5 4\n6\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2164
Solve the following coding problem using the programming language python: This is the easy version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string $s$, consisting of lowercase English letters. Find the longest string, $t$, which satisfies the following conditions: The length of $t$ does not exceed the length of $s$. $t$ is a palindrome. There exists two strings $a$ and $b$ (possibly empty), such that $t = a + b$ ( "$+$" represents concatenation), and $a$ is prefix of $s$ while $b$ is suffix of $s$. -----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 each describe a test case. Each test case is a non-empty string $s$, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed $5000$. -----Output----- For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. -----Example----- Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba -----Note----- In the first test, the string $s = $"a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: Its length is $9$, which does not exceed the length of the string $s$, which equals $11$. It is a palindrome. "abcdfdcba" $=$ "abcdfdc" $+$ "ba", and "abcdfdc" is a prefix of $s$ while "ba" is a suffix of $s$. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" $=$ "c" $+$ "" and $a$ or $b$ can be empty. The other possible solution for this test is "s". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def ispalin(s): ans = True for i in range(len(s)): if s[i] != s[len(s)-1-i]: ans = False break if i > len(s)//2: break return ans for _ in range(int(input())): s = input() k = '' l = '' for j in range(len(s)): if s[j] == s[len(s)-1-j]: k += s[j] l = s[j] + l else: break if j != len(s)-1: t = '' y = '' for r in range(j,len(s)-j): t += s[r] if ispalin(t): y = t q = '' v = '' for r in range(len(s)-j-1,j-1,-1): q = s[r] + q if ispalin(q): v = q if len(v) > len(y): print(k+v+l) else: print(k+y+l) else: print(s) ```
vfc_18410
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1326/D1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\na\nabcdfdcecba\nabbaxyzyx\ncodeforces\nacbba\n", "output": "a\nabcdfdcba\nxyzyx\nc\nabba\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2166
Solve the following coding problem using the programming language python: Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work. The girl finds an important permutation for the research. The permutation contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n). She replaces some of permutation elements with -1 value as a revenge. When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element a_{k} which has value equal to k (a_{k} = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (10^9 + 7). -----Input----- The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1. It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation. -----Output----- Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (10^9 + 7). -----Examples----- Input 5 -1 -1 4 3 -1 Output 2 -----Note----- For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python input() t = list(map(int, input().split())) s, m = 0, 1000000007 p = {i for i, q in enumerate(t, 1) if q == -1} n, k = len(p), len(p - set(t)) d, c = 2 * (n & 1) - 1, 1 for j in range(n + 1): d = -d * max(1, j) % m if n - j <= k: s += c * d c = c * max(1, n - j) * pow(k - n + j + 1, m - 2, m) % m print(s % m) ```
vfc_18418
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/340/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n-1 -1 4 3 -1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n2 4 5 3 -1 8 -1 6\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n-1 -1 4 -1 7 1 6\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2167
Solve the following coding problem using the programming language python: Polycarpus has an array, consisting of n integers a_1, a_2, ..., a_{n}. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times: he chooses two elements of the array a_{i}, a_{j} (i ≠ j); he simultaneously increases number a_{i} by 1 and decreases number a_{j} by 1, that is, executes a_{i} = a_{i} + 1 and a_{j} = a_{j} - 1. The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times. Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the array size. The second line contains space-separated integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 10^4) — the original array. -----Output----- Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation. -----Examples----- Input 2 2 1 Output 1 Input 3 1 4 1 Output 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()) print(n - 1 if sum(map(int, input().split())) % n else n) ```
vfc_18422
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/246/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n2 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 4 1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n2 -7 -2 -6\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n2 0 -2 -1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n-1 1 0 0 -1 -1\n", "output": "5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2168
Solve the following coding problem using the programming language python: A conglomerate consists of $n$ companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left. But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal. To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase. Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one. -----Input----- The first line contains a single integer $n$ — the number of companies in the conglomerate ($1 \le n \le 2 \cdot 10^5$). Each of the next $n$ lines describes a company. A company description start with an integer $m_i$ — the number of its employees ($1 \le m_i \le 2 \cdot 10^5$). Then $m_i$ integers follow: the salaries of the employees. All salaries are positive and do not exceed $10^9$. The total number of employees in all companies does not exceed $2 \cdot 10^5$. -----Output----- Output a single integer — the minimal total increase of all employees that allows to merge all companies. -----Example----- Input 3 2 4 3 2 2 1 3 1 1 1 Output 13 -----Note----- One of the optimal merging strategies is the following. First increase all salaries in the second company by $2$, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries $[4, 3, 4, 3]$ and $[1, 1, 1]$. To merge them, increase the salaries in the second of those by $3$. The total increase is $2 + 2 + 3 + 3 + 3 = 13$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def ii(): return int(input()) def mi(): return list(map(int, input().split())) def li(): return list(mi()) # A. Company Merging n = ii() c = [] for i in range(n): a = li()[1:] c.append(a) mx = max(max(e) for e in c) ans = sum((mx - max(e)) * len(e) for e in c) print(ans) ```
vfc_18426
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1090/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n2 4 3\n2 2 1\n3 1 1 1\n", "output": "13\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2171
Solve the following coding problem using the programming language python: This morning Chef wants to jump a little. In a few minutes he will arrive at the point 0. Then he will perform a lot of jumps in such a sequence: 1-jump, 2-jump, 3-jump, 1-jump, 2-jump, 3-jump, 1-jump, and so on. 1-jump means that if Chef is at the point x, he will jump to the point x+1. 2-jump means that if Chef is at the point x, he will jump to the point x+2. 3-jump means that if Chef is at the point x, he will jump to the point x+3. Before the start Chef asks you: will he arrive at the point a after some number of jumps? -----Input----- The first line contains a single integer a denoting the point Chef asks about. -----Output----- Output "yes" without a quotes if Chef can arrive at point a or "no" without a quotes otherwise. -----Constraints----- - 0 ≤ a ≤ 1018 -----Example----- Input: 0 Output: yes Input: 1 Output: yes Input: 2 Output: no Input: 3 Output: yes Input: 6 Output: yes Input: 7 Output: yes Input: 10 Output: no -----Explanation----- The first reached points are: 0 (+1) 1 (+2) 3 (+3) 6 (+1) 7, and so on. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a = int(input()) if a%6 == 0 or a%6==1 or a%6 == 3: print("yes") else: print("no") ```
vfc_18438
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://www.codechef.com/problems/OJUMPS", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "0\n", "output": "yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n", "output": "yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "no\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n", "output": "yes\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2172
Solve the following coding problem using the programming language python: You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes. You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning. You can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language. You are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes. -----Input----- The first line contains two integers, n and m (1 ≤ n ≤ 3000, 1 ≤ m ≤ 3000) — the number of words in the professor's lecture and the number of words in each of these languages. The following m lines contain the words. The i-th line contains two strings a_{i}, b_{i} meaning that the word a_{i} belongs to the first language, the word b_{i} belongs to the second language, and these two words have the same meaning. It is guaranteed that no word occurs in both languages, and each word occurs in its language exactly once. The next line contains n space-separated strings c_1, c_2, ..., c_{n} — the text of the lecture. It is guaranteed that each of the strings c_{i} belongs to the set of strings {a_1, a_2, ... a_{m}}. All the strings in the input are non-empty, each consisting of no more than 10 lowercase English letters. -----Output----- Output exactly n words: how you will record the lecture in your notebook. Output the words of the lecture in the same order as in the input. -----Examples----- Input 4 3 codeforces codesecrof contest round letter message codeforces contest letter contest Output codeforces round letter round Input 5 3 joll wuqrd euzf un hbnyiyc rsoqqveh hbnyiyc joll joll euzf joll Output hbnyiyc joll joll un joll The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m = map(int, input().split()) d = { } for i in range(m): a, b = input().split() d[a] = b if len(b) < len(a) else a for word in input().split(): print(d[word], end=' ') print() ```
vfc_18442
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/499/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 3\ncodeforces codesecrof\ncontest round\nletter message\ncodeforces contest letter contest\n", "output": "codeforces round letter round\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\njoll wuqrd\neuzf un\nhbnyiyc rsoqqveh\nhbnyiyc joll joll euzf joll\n", "output": "hbnyiyc joll joll un joll\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\nqueyqj f\nb vn\ntabzvk qpfzoqx\nytnyonoc hnxsd\njpggvr lchinjmt\nqueyqj jpggvr b ytnyonoc b\n", "output": "f jpggvr b hnxsd b\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2173
Solve the following coding problem using the programming language python: One very well-known internet resource site (let's call it X) has come up with a New Year adventure. Specifically, they decided to give ratings to all visitors. There are n users on the site, for each user we know the rating value he wants to get as a New Year Present. We know that user i wants to get at least a_{i} rating units as a present. The X site is administered by very creative and thrifty people. On the one hand, they want to give distinct ratings and on the other hand, the total sum of the ratings in the present must be as small as possible. Help site X cope with the challenging task of rating distribution. Find the optimal distribution. -----Input----- The first line contains integer n (1 ≤ n ≤ 3·10^5) — the number of users on the site. The next line contains integer sequence a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- Print a sequence of integers b_1, b_2, ..., b_{n}. Number b_{i} means that user i gets b_{i} of rating as a present. The printed sequence must meet the problem conditions. If there are multiple optimal solutions, print any of them. -----Examples----- Input 3 5 1 1 Output 5 1 2 Input 1 1000000000 Output 1000000000 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys n = int(sys.stdin.readline()) l = [int(x) for x in sys.stdin.readline().split(' ')] ind = sorted(list(range(len(l))), key=lambda x: l[x]) h = l[ind[0]] - 1 for i in ind: if l[i] <= h: l[i] = h+1 h += 1 else: h = l[i] print(" ".join([str(x) for x in l])) ```
vfc_18446
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/379/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n5 1 1\n", "output": "5 1 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1000000000\n", "output": "1000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1 1 1 1 1 1 1 1 1 1\n", "output": "1 2 3 4 5 6 7 8 9 10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1 10 1 10 1 1 7 8 6 7\n", "output": "1 10 2 11 3 4 7 9 6 8\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2174
Solve the following coding problem using the programming language python: Permutation p is an ordered set of integers p_1, p_2, ..., p_{n}, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as p_{i}. We'll call number n the size or the length of permutation p_1, p_2, ..., p_{n}. You have a sequence of integers a_1, a_2, ..., a_{n}. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence. -----Input----- The first line contains integer n (1 ≤ n ≤ 3·10^5) — the size of the sought permutation. The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 2 3 0 Output 2 Input 3 -1 -1 2 Output 6 -----Note----- In the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1). In the second sample you need 6 moves to build permutation (1, 3, 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()) arr = sorted(list(map(int, input().split()))) ans = 0 for i in range(n): ans += abs (arr[i] - i - 1) print (ans) ```
vfc_18450
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/285/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n3 0\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n-1 -1 2\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n-3 5 -3 3 3\n", "output": "10\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2175
Solve the following coding problem using the programming language python: There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is a_{i} liters. [Image] Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor. Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries: Add x_{i} liters of water to the p_{i}-th vessel; Print the number of liters of water in the k_{i}-th vessel. When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels. -----Input----- The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·10^5). The second line contains n integers a_1, a_2, ..., a_{n} — the vessels' capacities (1 ≤ a_{i} ≤ 10^9). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·10^5). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 p_{i} x_{i}", the query of the second type is represented as "2 k_{i}" (1 ≤ p_{i} ≤ n, 1 ≤ x_{i} ≤ 10^9, 1 ≤ k_{i} ≤ n). -----Output----- For each query, print on a single line the number of liters of water in the corresponding vessel. -----Examples----- Input 2 5 10 6 1 1 4 2 1 1 2 5 1 1 4 2 1 2 2 Output 4 5 8 Input 3 5 10 8 6 1 1 12 2 2 1 1 6 1 3 2 2 2 2 3 Output 7 10 5 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()) + 1 a = list(map(int, input().split())) + [1 << 50] l, p, r = [0] * n, list(range(n)), [] for i in range(int(input())): t = list(map(int, input().split())) if t[0] == 2: r.append(l[t[1] - 1]) else: x = t[1] - 1 s, d = [x], t[2] while True: if p[x] != x: x = p[x] s.append(x) continue if l[x] + d < a[x]: l[x] += d break d -= a[x] - l[x] l[x] = a[x] x += 1 s.append(x) for j in s: p[j] = x print('\n'.join(map(str, r))) ```
vfc_18454
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/371/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n5 10\n6\n1 1 4\n2 1\n1 2 5\n1 1 4\n2 1\n2 2\n", "output": "4\n5\n8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n5 10 8\n6\n1 1 12\n2 2\n1 1 6\n1 3 2\n2 2\n2 3\n", "output": "7\n10\n5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n71 59 88 55 18 98 38 73 53 58\n20\n1 5 93\n1 7 69\n2 3\n1 1 20\n2 10\n1 6 74\n1 7 100\n1 9 14\n2 3\n2 4\n2 7\n1 3 31\n2 4\n1 6 64\n2 2\n2 2\n1 3 54\n2 9\n2 1\n1 6 86\n", "output": "0\n0\n0\n0\n38\n0\n0\n0\n53\n20\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2176
Solve the following coding problem using the programming language python: You are given a sequence of $n$ pairs of integers: $(a_1, b_1), (a_2, b_2), \dots , (a_n, b_n)$. This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: $s = [(1, 2), (3, 2), (3, 1)]$ is bad because the sequence of first elements is sorted: $[1, 3, 3]$; $s = [(1, 2), (3, 2), (1, 2)]$ is bad because the sequence of second elements is sorted: $[2, 2, 2]$; $s = [(1, 1), (2, 2), (3, 3)]$ is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; $s = [(1, 3), (3, 3), (2, 2)]$ is good because neither the sequence of first elements $([1, 3, 2])$ nor the sequence of second elements $([3, 3, 2])$ is sorted. Calculate the number of permutations of size $n$ such that after applying this permutation to the sequence $s$ it turns into a good sequence. A permutation $p$ of size $n$ is a sequence $p_1, p_2, \dots , p_n$ consisting of $n$ distinct integers from $1$ to $n$ ($1 \le p_i \le n$). If you apply permutation $p_1, p_2, \dots , p_n$ to the sequence $s_1, s_2, \dots , s_n$ you get the sequence $s_{p_1}, s_{p_2}, \dots , s_{p_n}$. For example, if $s = [(1, 2), (1, 3), (2, 3)]$ and $p = [2, 3, 1]$ then $s$ turns into $[(1, 3), (2, 3), (1, 2)]$. -----Input----- The first line contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$). The next $n$ lines contains description of sequence $s$. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le n$) — the first and second elements of $i$-th pair in the sequence. The sequence $s$ may contain equal elements. -----Output----- Print the number of permutations of size $n$ such that after applying this permutation to the sequence $s$ it turns into a good sequence. Print the answer modulo $998244353$ (a prime number). -----Examples----- Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 -----Note----- In first test case there are six permutations of size $3$: if $p = [1, 2, 3]$, then $s = [(1, 1), (2, 2), (3, 1)]$ — bad sequence (sorted by first elements); if $p = [1, 3, 2]$, then $s = [(1, 1), (3, 1), (2, 2)]$ — bad sequence (sorted by second elements); if $p = [2, 1, 3]$, then $s = [(2, 2), (1, 1), (3, 1)]$ — good sequence; if $p = [2, 3, 1]$, then $s = [(2, 2), (3, 1), (1, 1)]$ — good sequence; if $p = [3, 1, 2]$, then $s = [(3, 1), (1, 1), (2, 2)]$ — bad sequence (sorted by second elements); if $p = [3, 2, 1]$, then $s = [(3, 1), (2, 2), (1, 1)]$ — good sequence. 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 Counter def input(): return sys.stdin.readline()[:-1] MOD = 998244353 n = int(input()) fact = [1] for i in range(1, n+1): fact.append((fact[-1]*i)%MOD) seq = [] ca, cb = Counter(), Counter() for _ in range(n): a, b = map(int, input().split()) ca[a] += 1 cb[b] += 1 seq.append((a, b)) ans = fact[n] ans %= MOD #print(ans) res = 1 for v in ca.values(): res *= fact[v] res %= MOD ans -= res ans %= MOD #print(ans) res = 1 for v in cb.values(): res *= fact[v] res %= MOD ans -= res #print(ans) seq.sort(key=lambda x: (x[0], x[1])) cur = seq[0][0] res = 1 M = 1 ctmp = Counter() for i in range(n): if seq[i][0] == cur: ctmp[seq[i][1]] += 1 M = max(M, seq[i][1]) else: if seq[i][1] < M: res = 0 break tmp = 1 for v in ctmp.values(): tmp *= fact[v] tmp %= MOD res *= tmp res %= MOD ctmp = Counter() ctmp[seq[i][1]] += 1 cur = seq[i][0] M = max(M, seq[i][1]) tmp = 1 for v in ctmp.values(): tmp *= fact[v] tmp %= MOD res *= tmp res %= MOD ans += res ans %= MOD print(ans) ```
vfc_18458
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1207/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 1\n2 2\n3 1\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2177
Solve the following coding problem using the programming language python: You are given two integers $A$ and $B$, calculate the number of pairs $(a, b)$ such that $1 \le a \le A$, $1 \le b \le B$, and the equation $a \cdot b + a + b = conc(a, b)$ is true; $conc(a, b)$ is the concatenation of $a$ and $b$ (for example, $conc(12, 23) = 1223$, $conc(100, 11) = 10011$). $a$ and $b$ should not contain leading zeroes. -----Input----- The first line contains $t$ ($1 \le t \le 100$) — the number of test cases. Each test case contains two integers $A$ and $B$ $(1 \le A, B \le 10^9)$. -----Output----- Print one integer — the number of pairs $(a, b)$ such that $1 \le a \le A$, $1 \le b \le B$, and the equation $a \cdot b + a + b = conc(a, b)$ is true. -----Example----- Input 3 1 11 4 2 191 31415926 Output 1 0 1337 -----Note----- There is only one suitable pair in the first test case: $a = 1$, $b = 9$ ($1 + 9 + 1 \cdot 9 = 19$). 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): A, B = list(map(int, input().split())) b = -1 B += 1 while B: b += 1 B //= 10 print(A*b) ```
vfc_18462
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1288/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 11\n4 2\n191 31415926\n", "output": "1\n0\n1337\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2178
Solve the following coding problem using the programming language python: Vasya has got $n$ books, numbered from $1$ to $n$, arranged in a stack. The topmost book has number $a_1$, the next one — $a_2$, and so on. The book at the bottom of the stack has number $a_n$. All numbers are distinct. Vasya wants to move all the books to his backpack in $n$ steps. During $i$-th step he wants to move the book number $b_i$ into his backpack. If the book with number $b_i$ is in the stack, he takes this book and all the books above the book $b_i$, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order $[1, 2, 3]$ (book $1$ is the topmost), and Vasya moves the books in the order $[2, 1, 3]$, then during the first step he will move two books ($1$ and $2$), during the second step he will do nothing (since book $1$ is already in the backpack), and during the third step — one book (the book number $3$). Note that $b_1, b_2, \dots, b_n$ are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. -----Input----- The first line contains one integer $n~(1 \le n \le 2 \cdot 10^5)$ — the number of books in the stack. The second line contains $n$ integers $a_1, a_2, \dots, a_n~(1 \le a_i \le n)$ denoting the stack of books. The third line contains $n$ integers $b_1, b_2, \dots, b_n~(1 \le b_i \le n)$ denoting the steps Vasya is going to perform. All numbers $a_1 \dots a_n$ are distinct, the same goes for $b_1 \dots b_n$. -----Output----- Print $n$ integers. The $i$-th of them should be equal to the number of books Vasya moves to his backpack during the $i$-th step. -----Examples----- Input 3 1 2 3 2 1 3 Output 2 0 1 Input 5 3 1 4 2 5 4 5 1 3 2 Output 3 2 0 0 0 Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Output 1 1 2 0 1 1 -----Note----- The first example is described in the statement. In the second example, during the first step Vasya will move the books $[3, 1, 4]$. After that only books $2$ and $5$ remain in the stack ($2$ is above $5$). During the second step Vasya will take the books $2$ and $5$. After that the stack becomes empty, so during next steps Vasya won't move any books. 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 = list(map(int, input().split()))[::-1] b = list(map(int, input().split())) ans = [0] * n marked = [True] * (n + 1) for i in range(n): if marked[b[i]]: while True: marked[a[-1]] = False ans[i] += 1 if a[-1] == b[i]: a.pop() break a.pop() else: continue print(*ans) ```
vfc_18466
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1073/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 2 3\n2 1 3\n", "output": "2 0 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n3 1 4 2 5\n4 5 1 3 2\n", "output": "3 2 0 0 0 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n6 5 4 3 2 1\n6 5 3 4 2 1\n", "output": "1 1 2 0 1 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n1\n", "output": "1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n7 6 3 1 5 4 2\n2 3 1 6 4 7 5\n", "output": "7 0 0 0 0 0 0 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n4 7 10 2 1 8 3 5 9 6\n4 2 7 1 8 10 5 3 9 6\n", "output": "1 3 0 1 1 0 2 0 1 1 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2179
Solve the following coding problem using the programming language python: Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows. Let's assume that we are given a connected weighted undirected graph G = (V, E) (here V is the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graph G_1 = (V, E_1) that is a tree with the set of edges E_1 that is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to any vertex to G and to G_1 are the same. You are given a connected weighted undirected graph G and vertex u. Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 3·10^5, 0 ≤ m ≤ 3·10^5) — the number of vertices and edges of the graph, respectively. Next m lines contain three integers each, representing an edge — u_{i}, v_{i}, w_{i} — the numbers of vertices connected by an edge and the weight of the edge (u_{i} ≠ v_{i}, 1 ≤ w_{i} ≤ 10^9). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices. The last line of the input contains integer u (1 ≤ u ≤ n) — the number of the start vertex. -----Output----- In the first line print the minimum total weight of the edges of the tree. In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order. If there are multiple answers, print any of them. -----Examples----- Input 3 3 1 2 1 2 3 1 1 3 2 3 Output 2 1 2 Input 4 4 1 2 1 2 3 1 3 4 1 4 1 2 4 Output 4 2 3 4 -----Note----- In the first sample there are two possible shortest path trees: with edges 1 – 3 and 2 – 3 (the total weight is 3); with edges 1 – 2 and 2 – 3 (the total weight is 2); And, for example, a tree with edges 1 – 2 and 1 – 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math,string,itertools,fractions,heapq,collections,re,array,bisect from itertools import chain, dropwhile, permutations, combinations from collections import defaultdict def VI(): return list(map(int,input().split())) def I(): return int(input()) def LIST(n,m=None): return [0]*n if m is None else [[0]*m for i in range(n)] def ELIST(n): return [[] for i in range(n)] def MI(n=None,m=None): # input matrix of integers if n is None: n,m = VI() arr = LIST(n) for i in range(n): arr[i] = VI() return arr def MS(n=None,m=None): # input matrix of strings if n is None: n,m = VI() arr = LIST(n) for i in range(n): arr[i] = input() return arr def MIT(n=None,m=None): # input transposed matrix/array of integers if n is None: n,m = VI() # a = MI(n,m) arr = LIST(m,n) for i in range(n): v = VI() for j in range(m): arr[j][i] = v[j] # for i,l in enumerate(a): # for j,x in enumerate(l): # arr[j][i] = x return arr def run2(n,m,u,v,w,x): # correct, but time limit exceeded. g = ELIST(n+1) # list of vertices; Adjacency list for i in range(m): g[u[i]].append((v[i],w[i],i+1)) g[v[i]].append((u[i],w[i],i+1)) # index priority queue with deque and priorities pq = [] marked = [False] * (n+1) pq.append((0,0,x,0)) sg = [] wmax = -w[-1] # to fix the issue that start doesn't have edge weight while len(pq)!=0: wi,lw,i,ei = heapq.heappop(pq) if not marked[i]: marked[i] = True sg.append(ei) wmax += w[ei-1] #print(i,wi,ei, wmax) for j,wj,ej in g[i]: if not marked[j]: heapq.heappush(pq, (wi+wj,wj,j,ej)) sg = sg[1:] print(wmax) for i in sg: print(i,end=" ") print() def main2(info=0): n,m = VI() u,v,w = MIT(m,3) x = I() run(n,m,u,v,w,x) def run(n,m,g,x): pq = [(0,0,x,0)] marked = [False] * (n+1) sg = [] wtot = 0 while len(pq)!=0: wi,lw,i,ei = heapq.heappop(pq) if not marked[i]: marked[i] = True sg.append(str(ei)) wtot += lw for j,wj,ej in g[i]: if not marked[j]: heapq.heappush(pq, (wi+wj,wj,j,ej)) print(wtot) print(" ".join(sg[1:])) def main(info=0): n,m = VI() g = ELIST(n+1) for i in range(m): u,v,w = VI() g[u].append((v,w,i+1)) g[v].append((u,w,i+1)) x = I() run(n,m,g,x) def __starting_point(): main() __starting_point() ```
vfc_18470
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/545/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 3\n1 2 1\n2 3 1\n1 3 2\n3\n", "output": "2\n1 2 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 4\n1 2 1\n2 3 1\n3 4 1\n4 1 2\n4\n", "output": "4\n2 3 4 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 5\n1 2 1\n1 3 1\n2 4 1\n3 4 1\n2 3 10\n1\n", "output": "3\n1 2 3 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 8\n1 2 30\n1 3 20\n2 3 50\n4 2 100\n2 5 40\n3 5 10\n3 6 50\n5 6 60\n4\n", "output": "230\n1 4 5 6 7 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2180
Solve the following coding problem using the programming language python: Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1). Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. -----Input----- The first line contains an integer n (1 ≤ n ≤ 1000). -----Output----- On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. -----Examples----- Input 2 Output 2 C. .C 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()) print(n*n//2+n*n%2) for i in range(n): if i %2==1: print('.C'*(n//2)+'.'*(n%2)) else: print('C.'*(n//2)+'C'*(n%2)) ```
vfc_18474
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/384/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n", "output": "2\nC.\n.C\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "5\nC.C\n.C.\nC.C\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n", "output": "8\nC.C.\n.C.C\nC.C.\n.C.C\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n", "output": "50\nC.C.C.C.C.\n.C.C.C.C.C\nC.C.C.C.C.\n.C.C.C.C.C\nC.C.C.C.C.\n.C.C.C.C.C\nC.C.C.C.C.\n.C.C.C.C.C\nC.C.C.C.C.\n.C.C.C.C.C\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "15\n", "output": "113\nC.C.C.C.C.C.C.C\n.C.C.C.C.C.C.C.\nC.C.C.C.C.C.C.C\n.C.C.C.C.C.C.C.\nC.C.C.C.C.C.C.C\n.C.C.C.C.C.C.C.\nC.C.C.C.C.C.C.C\n.C.C.C.C.C.C.C.\nC.C.C.C.C.C.C.C\n.C.C.C.C.C.C.C.\nC.C.C.C.C.C.C.C\n.C.C.C.C.C.C.C.\nC.C.C.C.C.C.C.C\n.C.C.C.C.C.C.C.\nC.C.C.C.C.C.C.C\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n", "output": "1\nC\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2182
Solve the following coding problem using the programming language python: Bob is a competitive programmer. He wants to become red, and for that he needs a strict training regime. He went to the annual meeting of grandmasters and asked $n$ of them how much effort they needed to reach red. "Oh, I just spent $x_i$ hours solving problems", said the $i$-th of them. Bob wants to train his math skills, so for each answer he wrote down the number of minutes ($60 \cdot x_i$), thanked the grandmasters and went home. Bob could write numbers with leading zeroes — for example, if some grandmaster answered that he had spent $2$ hours, Bob could write $000120$ instead of $120$. Alice wanted to tease Bob and so she took the numbers Bob wrote down, and for each of them she did one of the following independently: rearranged its digits, or wrote a random number. This way, Alice generated $n$ numbers, denoted $y_1$, ..., $y_n$. For each of the numbers, help Bob determine whether $y_i$ can be a permutation of a number divisible by $60$ (possibly with leading zeroes). -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 418$) — the number of grandmasters Bob asked. Then $n$ lines follow, the $i$-th of which contains a single integer $y_i$ — the number that Alice wrote down. Each of these numbers has between $2$ and $100$ digits '0' through '9'. They can contain leading zeroes. -----Output----- Output $n$ lines. For each $i$, output the following. If it is possible to rearrange the digits of $y_i$ such that the resulting number is divisible by $60$, output "red" (quotes for clarity). Otherwise, output "cyan". -----Example----- Input 6 603 006 205 228 1053 0000000000000000000000000000000000000000000000 Output red red cyan cyan cyan red -----Note----- In the first example, there is one rearrangement that yields a number divisible by $60$, and that is $360$. In the second example, there are two solutions. One is $060$ and the second is $600$. In the third example, there are $6$ possible rearrangments: $025$, $052$, $205$, $250$, $502$, $520$. None of these numbers is divisible by $60$. In the fourth example, there are $3$ rearrangements: $228$, $282$, $822$. In the fifth example, none of the $24$ rearrangements result in a number divisible by $60$. In the sixth example, note that $000\dots0$ is a valid solution. 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()) for i in range(n): s=list(input()) s=[int(i) for i in s] try: s.remove(0) x=sum(s) if x%3==0: if len([i for i in s if i%2==0])>0: print("red") else: print("cyan") else: print("cyan") except: print("cyan") ```
vfc_18482
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1266/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n603\n006\n205\n228\n1053\n0000000000000000000000000000000000000000000000\n", "output": "red\nred\ncyan\ncyan\ncyan\nred\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "15\n5318008\n379009\n06\n79153975193751375917591379919397337913333535330\n791539751937513759175913799193973379133301353533\n123456789023487138475693874561834576138495713485673485364857638475673465873457346581495713641\n7915397519375137511917591379919397337913333535330\n3155355553535353532535535833353535\n1335353578125379138476139690013476834874\n817509834750913874591034\n0992\n102\n081\n00\n60\n", "output": "cyan\ncyan\nred\ncyan\ncyan\nred\ncyan\ncyan\ncyan\nred\ncyan\nred\nred\nred\nred\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "13\n023\n025\n027\n403\n407\n405\n036\n056\n076\n06777\n083\n085\n780\n", "output": "cyan\ncyan\nred\ncyan\ncyan\nred\nred\ncyan\ncyan\nred\ncyan\ncyan\nred\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n900\n", "output": "red\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n55500\n", "output": "red\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n300\n", "output": "red\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2183
Solve the following coding problem using the programming language python: Три брата договорились о встрече. Пронумеруем братьев следующим образом: пусть старший брат имеет номер 1, средний брат имеет номер 2, а младший брат — номер 3. Когда пришло время встречи, один из братьев опоздал. По заданным номерам двух братьев, которые пришли вовремя, вам предстоит определить номер опоздавшего брата. -----Входные данные----- В первой строке входных данных следуют два различных целых числа a и b (1 ≤ a, b ≤ 3, a ≠ b) — номера братьев, которые пришли на встречу вовремя. Номера даны в произвольном порядке. -----Выходные данные----- Выведите единственное целое число — номер брата, который опоздал на встречу. -----Пример----- Входные данные 3 1 Выходные данные 2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python print(6 - sum(map(int, input().split()))) ```
vfc_18486
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/646/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2184
Solve the following coding problem using the programming language python: You are given a boolean function of three variables which is defined by its truth table. You need to find an expression of minimum length that equals to this function. The expression may consist of: Operation AND ('&', ASCII code 38) Operation OR ('|', ASCII code 124) Operation NOT ('!', ASCII code 33) Variables x, y and z (ASCII codes 120-122) Parentheses ('(', ASCII code 40, and ')', ASCII code 41) If more than one expression of minimum length exists, you should find the lexicographically smallest one. Operations have standard priority. NOT has the highest priority, then AND goes, and OR has the lowest priority. The expression should satisfy the following grammar: E ::= E '|' T | T T ::= T '&' F | F F ::= '!' F | '(' E ')' | 'x' | 'y' | 'z' -----Input----- The first line contains one integer n — the number of functions in the input (1 ≤ n ≤ 10 000). The following n lines contain descriptions of functions, the i-th of them contains a string of length 8 that consists of digits 0 and 1 — the truth table of the i-th function. The digit on position j (0 ≤ j < 8) equals to the value of the function in case of $x = \lfloor \frac{j}{4} \rfloor$, $y = \lfloor \frac{j}{2} \rfloor \operatorname{mod} 2$ and $z = j \operatorname{mod} 2$. -----Output----- You should output n lines, the i-th line should contain the expression of minimum length which equals to the i-th function. If there is more than one such expression, output the lexicographically smallest of them. Expressions should satisfy the given grammar and shouldn't contain white spaces. -----Example----- Input 4 00110011 00000111 11110000 00011111 Output y (y|z)&x !x x|y&z -----Note----- The truth table for the second function: [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python f =['!x&x', '!(x|y|z)', '!x&!y&z', '!x&!y', '!x&!z&y', '!x&!z', '!(!y&!z|x|y&z)', '!(x|y&z)', '!x&y&z', '!(!y&z|!z&y|x)', '!x&z', '!(!z&y|x)', '!x&y', '!(!y&z|x)', '!x&(y|z)', '!x', '!y&!z&x', '!y&!z', '!(!x&!z|x&z|y)', '!(x&z|y)', '!(!x&!y|x&y|z)', '!(x&y|z)', '!(!x&!y|x&y|z)|!x&!y&z', '!((x|y)&z|x&y)', '!x&y&z|!y&!z&x', '!x&y&z|!y&!z', '!x&z|!y&!z&x', '!x&z|!y&!z', '!x&y|!y&!z&x', '!x&y|!y&!z', '!x&(y|z)|!y&!z&x', '!x|!y&!z', '!y&x&z', '!(!x&z|!z&x|y)', '!y&z', '!(!z&x|y)', '!x&!z&y|!y&x&z', '!x&!z|!y&x&z', '!x&!z&y|!y&z', '!x&!z|!y&z', '!x&y&z|!y&x&z', '!(!x&z|!z&x|y)|!x&y&z', '!(x&y)&z', '!(!z&x|y)|!x&z', '!x&y|!y&x&z', '!(!y&z|x)|!y&x&z', '!x&y|!y&z', '!x|!y&z', '!y&x', '!(!x&z|y)', '!y&(x|z)', '!y', '!x&!z&y|!y&x', '!x&!z|!y&x', '!x&!z&y|!y&(x|z)', '!x&!z|!y', '!x&y&z|!y&x', '!(!x&z|y)|!x&y&z', '!x&z|!y&x', '!x&z|!y', '!x&y|!y&x', '!(!x&!y&z|x&y)', '!x&(y|z)|!y&x', '!x|!y', '!z&x&y', '!(!x&y|!y&x|z)', '!x&!y&z|!z&x&y', '!x&!y|!z&x&y', '!z&y', '!(!y&x|z)', '!x&!y&z|!z&y', '!x&!y|!z&y', '!x&y&z|!z&x&y', '!(!x&y|!y&x|z)|!x&y&z', '!x&z|!z&x&y', '!(!z&y|x)|!z&x&y', '!(x&z)&y', '!(!y&x|z)|!x&y', '!x&z|!z&y', '!x|!z&y', '!z&x', '!(!x&y|z)', '!x&!y&z|!z&x', '!x&!y|!z&x', '!z&(x|y)', '!z', '!x&!y&z|!z&(x|y)', '!x&!y|!z', '!x&y&z|!z&x', '!(!x&y|z)|!x&y&z', '!x&z|!z&x', '!(!x&!z&y|x&z)', '!x&y|!z&x', '!x&y|!z', '!x&(y|z)|!z&x', '!x|!z', '!y&x&z|!z&x&y', '!(!x&y|!y&x|z)|!y&x&z', '!y&z|!z&x&y', '!(!z&x|y)|!z&x&y', '!y&x&z|!z&y', '!(!y&x|z)|!y&x&z', '!y&z|!z&y', '!(!y&!z&x|y&z)', '!x&y&z|!y&x&z|!z&x&y', '!(!x&y|!y&x|z)|!x&y&z|!y&x&z', '!(x&y)&z|!z&x&y', '!(!z&x|y)|!x&z|!z&x&y', '!(x&z)&y|!y&x&z', '!(!y&x|z)|!x&y|!y&x&z', '!(x&y)&z|!z&y', '!x|!y&z|!z&y', '!(y&z)&x', '!(!x&y|z)|!y&x', '!y&z|!z&x', '!y|!z&x', '!y&x|!z&y', '!y&x|!z', '!y&(x|z)|!z&y', '!y|!z', '!(y&z)&x|!x&y&z', '!(!x&y|z)|!x&y&z|!y&x', '!(x&y)&z|!z&x', '!x&z|!y|!z&x', '!(x&z)&y|!y&x', '!x&y|!y&x|!z', '!x&y|!y&z|!z&x', '!(x&y&z)', 'x&y&z', '!(x|y|z)|x&y&z', '!x&!y&z|x&y&z', '!x&!y|x&y&z', '!x&!z&y|x&y&z', '!x&!z|x&y&z', '!(!y&!z|x|y&z)|x&y&z', '!(x|y&z)|x&y&z', 'y&z', '!(x|y|z)|y&z', '!x&z|y&z', '!x&!y|y&z', '!x&y|y&z', '!x&!z|y&z', '!x&(y|z)|y&z', '!x|y&z', '!y&!z&x|x&y&z', '!y&!z|x&y&z', '!(!x&!z|x&z|y)|x&y&z', '!(x&z|y)|x&y&z', '!(!x&!y|x&y|z)|x&y&z', '!(x&y|z)|x&y&z', '!(!x&!y|x&y|z)|!x&!y&z|x&y&z', '!((x|y)&z|x&y)|x&y&z', '!y&!z&x|y&z', '!y&!z|y&z', '!x&z|!y&!z&x|y&z', '!(x&z|y)|y&z', '!x&y|!y&!z&x|y&z', '!(x&y|z)|y&z', '!x&(y|z)|!y&!z&x|y&z', '!x|!y&!z|y&z', 'x&z', '!(x|y|z)|x&z', '!y&z|x&z', '!x&!y|x&z', '!x&!z&y|x&z', '!x&!z|x&z', '!x&!z&y|!y&z|x&z', '!(x|y&z)|x&z', '(x|y)&z', '!(x|y|z)|(x|y)&z', 'z', '!x&!y|z', '!x&y|x&z', '!(!y&z|x)|x&z', '!x&y|z', '!x|z', '!y&x|x&z', '!y&!z|x&z', '!y&(x|z)|x&z', '!y|x&z', '!x&!z&y|!y&x|x&z', '!(x&y|z)|x&z', '!x&!z&y|!y&(x|z)|x&z', '!x&!z|!y|x&z', '!y&x|y&z', '!(!x&z|y)|y&z', '!y&x|z', '!y|z', '!x&y|!y&x|x&z', '!x&!z|!y&x|y&z', '!x&y|!y&x|z', '!x|!y|z', 'x&y', '!(x|y|z)|x&y', '!x&!y&z|x&y', '!x&!y|x&y', '!z&y|x&y', '!x&!z|x&y', '!x&!y&z|!z&y|x&y', '!(x|y&z)|x&y', '(x|z)&y', '!(x|y|z)|(x|z)&y', '!x&z|x&y', '!(!z&y|x)|x&y', 'y', '!x&!z|y', '!x&z|y', '!x|y', '!z&x|x&y', '!y&!z|x&y', '!x&!y&z|!z&x|x&y', '!(x&z|y)|x&y', '!z&(x|y)|x&y', '!z|x&y', '!x&!y&z|!z&(x|y)|x&y', '!x&!y|!z|x&y', '!z&x|y&z', '!(!x&y|z)|y&z', '!x&z|!z&x|x&y', '!x&!y|!z&x|y&z', '!z&x|y', '!z|y', '!x&z|!z&x|y', '!x|!z|y', '(y|z)&x', '!(x|y|z)|(y|z)&x', '!y&z|x&y', '!(!z&x|y)|x&y', '!z&y|x&z', '!(!y&x|z)|x&z', '!y&z|!z&y|x&y', '!x&!y|!z&y|x&z', '(x|y)&z|x&y', '!(x|y|z)|(x|y)&z|x&y', 'x&y|z', '!x&!y|x&y|z', 'x&z|y', '!x&!z|x&z|y', 'y|z', '!x|y|z', 'x', '!y&!z|x', '!y&z|x', '!y|x', '!z&y|x', '!z|x', '!y&z|!z&y|x', '!y|!z|x', 'x|y&z', '!y&!z|x|y&z', 'x|z', '!y|x|z', 'x|y', '!z|x|y', 'x|y|z', '!x|x'] n = int(input()) for i in range(n): s = input() a = 0 for j in range(8): a += int(s[j] == '1') << j print(f[a]) ```
vfc_18490
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/913/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n00110011\n00000111\n11110000\n00011111\n", "output": "y\n(y|z)&x\n!x\nx|y&z\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n11001110\n", "output": "!y|!z&x\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n11001110\n01001001\n", "output": "!y|!z&x\n!(!x&!z|x&z|y)|x&y&z\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n10001001\n10111011\n10111101\n", "output": "!y&!z|x&y&z\n!z|y\n!x&!z|!y&x|y&z\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2185
Solve the following coding problem using the programming language python: You're given two arrays $a[1 \dots n]$ and $b[1 \dots n]$, both of the same length $n$. In order to perform a push operation, you have to choose three integers $l, r, k$ satisfying $1 \le l \le r \le n$ and $k > 0$. Then, you will add $k$ to elements $a_l, a_{l+1}, \ldots, a_r$. For example, if $a = [3, 7, 1, 4, 1, 2]$ and you choose $(l = 3, r = 5, k = 2)$, the array $a$ will become $[3, 7, \underline{3, 6, 3}, 2]$. You can do this operation at most once. Can you make array $a$ equal to array $b$? (We consider that $a = b$ if and only if, for every $1 \le i \le n$, $a_i = b_i$) -----Input----- The first line contains a single integer $t$ ($1 \le t \le 20$) — the number of test cases in the input. The first line of each test case contains a single integer $n$ ($1 \le n \le 100\ 000$) — the number of elements in each array. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 1000$). The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 1000$). It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case, output one line containing "YES" if it's possible to make arrays $a$ and $b$ equal by performing at most once the described operation or "NO" if it's impossible. You can print each letter in any case (upper or lower). -----Example----- Input 4 6 3 7 1 4 1 2 3 7 3 6 3 2 5 1 1 1 1 1 1 2 1 3 1 2 42 42 42 42 1 7 6 Output YES NO YES NO -----Note----- The first test case is described in the statement: we can perform a push operation with parameters $(l=3, r=5, k=2)$ to make $a$ equal to $b$. In the second test case, we would need at least two operations to make $a$ equal to $b$. In the third test case, arrays $a$ and $b$ are already equal. In the fourth test case, it's impossible to make $a$ equal to $b$, because the integer $k$ has to be positive. 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 collections import deque from itertools import combinations from itertools import permutations input = lambda : sys.stdin.readline().rstrip() read = lambda : list(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 _ in range(int(input())): n = int(input()) a = list(read()) b = list(read()) arr = [] for i in range(n): if a[i] != b[i]: arr.append((i, b[i] - a[i])) flag = True s = [] for i, j in arr: if j <= 0: flag = False break s.append(j) for i in range(1, len(arr)): if arr[i][0] != arr[i-1][0] + 1: flag = False break if len(set(s)) >= 2: flag = False if flag: print("YES") else: print("NO") ```
vfc_18494
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1253/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n6\n3 7 1 4 1 2\n3 7 3 6 3 2\n5\n1 1 1 1 1\n1 2 1 3 1\n2\n42 42\n42 42\n1\n7\n6\n", "output": "YES\nNO\nYES\nNO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n3\n1 1 1\n1 1 3\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n6\n1 2 2 1 5 6\n1 4 3 4 7 6\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n3\n1 1 2\n1 1 1\n2\n1 1\n1 1\n", "output": "NO\nYES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2186
Solve the following coding problem using the programming language python: Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position". Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you. -----Input----- The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·10^5, 0 ≤ m ≤ 3·10^5) — the number of the initial strings and the number of queries, respectively. Next follow n non-empty strings that are uploaded to the memory of the mechanism. Next follow m non-empty strings that are the queries to the mechanism. The total length of lines in the input doesn't exceed 6·10^5. Each line consists only of letters 'a', 'b', 'c'. -----Output----- For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes). -----Examples----- Input 2 3 aaaaa acacaca aabaa ccacacc caaac Output YES NO NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys u = [] t = set() p1 = 127 m1 = 1000000007 p2 = 131 m2 = 1000000009 pow1 = [1] + [0] * 600005 pow2 = [1] + [0] * 600005 for i in range(1, 600005): pow1[i] = (pow1[i-1] * p1) % m1 pow2[i] = (pow2[i-1] * p2) % m2 def hash1(n): hsh = 0 for i in range(len(n)): hsh += pow1[i] * ord(n[i]) hsh %= m1 return hsh % m1 def hash2(n): hsh = 0 for i in range(len(n)): hsh += pow2[i] * ord(n[i]) hsh %= m2 return hsh % m2 a,b = list(map(int,sys.stdin.readline().split())) def trans(n): a = hash1(n) b = hash2(n) cyc = ['a', 'b', 'c'] for i in range(len(n)): for x in range(3): if cyc[x] == n[i]: h11 = a - ord(n[i]) * pow1[i] + ord(cyc[(x+1)%3]) * pow1[i] h12 = b - ord(n[i]) * pow2[i] + ord(cyc[(x+1)%3]) * pow2[i] h21 = a - ord(n[i]) * pow1[i] + ord(cyc[(x+2)%3]) * pow1[i] h22 = b - ord(n[i]) * pow2[i] + ord(cyc[(x+2)%3]) * pow2[i] t.add((h11%m1)*m2 + h12%m2) t.add((h21%m1)*m2 + h22%m2) for i in range(a): trans(sys.stdin.readline()) for j in range(b): inpt = sys.stdin.readline() if hash1(inpt)*m2 + hash2(inpt) in t: print("YES") else: print("NO") ```
vfc_18498
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/514/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 3\naaaaa\nacacaca\naabaa\nccacacc\ncaaac\n", "output": "YES\nNO\nNO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 5\nacbacbacb\ncbacbacb\nacbacbac\naacbacbacb\nacbacbacbb\nacbaabacb\n", "output": "NO\nNO\nNO\nNO\nYES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 0\n", "output": "", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 4\nab\ncacab\ncbabc\nacc\ncacab\nabc\naa\nacbca\ncb\n", "output": "YES\nYES\nNO\nYES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2187
Solve the following coding problem using the programming language python: Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible. Omkar currently has $n$ supports arranged in a line, the $i$-th of which has height $a_i$. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In $1$ operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add $1$ to each of their heights. Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide! An array $b$ is a subsegment of an array $c$ if $b$ can be obtained from $c$ by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end. An array $b_1, b_2, \dots, b_n$ is called nondecreasing if $b_i\le b_{i+1}$ for every $i$ from $1$ to $n-1$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 100$). Description of the test cases follows. The first line of each test case contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of supports Omkar has. The second line of each test case contains $n$ integers $a_{1},a_{2},...,a_{n}$ $(0 \leq a_{i} \leq 10^9)$ — the heights of the supports. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer — the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide. -----Example----- Input 3 4 5 3 2 5 5 1 2 3 5 3 3 1 1 1 Output 3 2 0 -----Note----- The subarray with which Omkar performs the operation is bolded. In the first test case: First operation: $[5, 3, \textbf{2}, 5] \to [5, 3, \textbf{3}, 5]$ Second operation: $[5, \textbf{3}, \textbf{3}, 5] \to [5, \textbf{4}, \textbf{4}, 5]$ Third operation: $[5, \textbf{4}, \textbf{4}, 5] \to [5, \textbf{5}, \textbf{5}, 5]$ In the third test case, the array is already nondecreasing, so Omkar does $0$ operations. 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): n = int(input()) out = 0 l = list(map(int, input().split())) for i in range(n - 1): out += max(0, l[i] - l[i + 1]) print(out) ```
vfc_18502
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1392/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n4\n5 3 2 5\n5\n1 2 3 5 3\n3\n1 1 1\n", "output": "3\n2\n0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n943795198\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n2\n1 3\n2\n3 1\n", "output": "0\n2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\n6 5 5 6\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2188
Solve the following coding problem using the programming language python: You are given $n$ pairs of integers $(a_1, b_1), (a_2, b_2), \ldots, (a_n, b_n)$. All of the integers in the pairs are distinct and are in the range from $1$ to $2 \cdot n$ inclusive. Let's call a sequence of integers $x_1, x_2, \ldots, x_{2k}$ good if either $x_1 < x_2 > x_3 < \ldots < x_{2k-2} > x_{2k-1} < x_{2k}$, or $x_1 > x_2 < x_3 > \ldots > x_{2k-2} < x_{2k-1} > x_{2k}$. You need to choose a subset of distinct indices $i_1, i_2, \ldots, i_t$ and their order in a way that if you write down all numbers from the pairs in a single sequence (the sequence would be $a_{i_1}, b_{i_1}, a_{i_2}, b_{i_2}, \ldots, a_{i_t}, b_{i_t}$), this sequence is good. What is the largest subset of indices you can choose? You also need to construct the corresponding index sequence $i_1, i_2, \ldots, i_t$. -----Input----- The first line contains single integer $n$ ($2 \leq n \leq 3 \cdot 10^5$) — the number of pairs. Each of the next $n$ lines contain two numbers — $a_i$ and $b_i$ ($1 \le a_i, b_i \le 2 \cdot n$) — the elements of the pairs. It is guaranteed that all integers in the pairs are distinct, that is, every integer from $1$ to $2 \cdot n$ is mentioned exactly once. -----Output----- In the first line print a single integer $t$ — the number of pairs in the answer. Then print $t$ distinct integers $i_1, i_2, \ldots, i_t$ — the indexes of pairs in the corresponding order. -----Examples----- Input 5 1 7 6 4 2 10 9 8 3 5 Output 3 1 5 3 Input 3 5 4 3 2 6 1 Output 3 3 2 1 -----Note----- The final sequence in the first example is $1 < 7 > 3 < 5 > 2 < 10$. The final sequence in the second example is $6 > 1 < 3 > 2 < 5 > 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 #sys.stdin=open("data.txt") input=sys.stdin.readline mii=lambda:list(map(int,input().split())) n=int(input()) l1=[] l2=[] for i in range(n): a,b=mii() if a<b: l1.append((-a,b,i)) else: l2.append((a,b,i)) if len(l1)>len(l2): l1.sort() print(len(l1)) print(" ".join([str(x[2]+1) for x in l1])) else: l2.sort() print(len(l2)) print(" ".join([str(x[2]+1) for x in l2])) ```
vfc_18506
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1148/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 7\n6 4\n2 10\n9 8\n3 5\n", "output": "3\n5 3 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n5 4\n3 2\n6 1\n", "output": "3\n3 2 1 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2190
Solve the following coding problem using the programming language python: You are given $n$ positive integers $a_1, \ldots, a_n$, and an integer $k \geq 2$. Count the number of pairs $i, j$ such that $1 \leq i < j \leq n$, and there exists an integer $x$ such that $a_i \cdot a_j = x^k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq n \leq 10^5$, $2 \leq k \leq 100$). The second line contains $n$ integers $a_1, \ldots, a_n$ ($1 \leq a_i \leq 10^5$). -----Output----- Print a single integer — the number of suitable pairs. -----Example----- Input 6 3 1 3 9 8 24 1 Output 5 -----Note----- In the sample case, the suitable pairs are: $a_1 \cdot a_4 = 8 = 2^3$; $a_1 \cdot a_6 = 1 = 1^3$; $a_2 \cdot a_3 = 27 = 3^3$; $a_3 \cdot a_5 = 216 = 6^3$; $a_4 \cdot a_6 = 8 = 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 f(n): Ans = [] d = 2 while d * d <= n: if n % d == 0: Ans.append(d) n //= d else: d += 1 if n > 1: Ans.append(n) return Ans n, k = list(map(int, input().split())) arr = list(map(int, input().split())) m = {} c = 0 for i in arr: r = {} d = 2 while d * d <= i: if i % d == 0: r[d] = (r.get(d, 0) + 1) % k i //= d else: d += 1 if i > 1: r[i] = (r.get(i, 0) + 1) % k r = tuple([x for x in list(r.items()) if x[1]]) r2 = tuple([(x[0], k - x[1]) for x in r]) c += m.get(r2, 0) m[r] = m.get(r, 0) + 1 print(c) ```
vfc_18514
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1225/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 3\n1 3 9 8 24 1\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n40 90\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 2\n7 4 10 9 2 8 8 7 3 7\n", "output": "7\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2193
Solve the following coding problem using the programming language python: Egor is a famous Russian singer, rapper, actor and blogger, and finally he decided to give a concert in the sunny Republic of Dagestan. There are $n$ cities in the republic, some of them are connected by $m$ directed roads without any additional conditions. In other words, road system of Dagestan represents an arbitrary directed graph. Egor will arrive to the city $1$, travel to the city $n$ by roads along some path, give a concert and fly away. As any famous artist, Egor has lots of haters and too annoying fans, so he can travel only by safe roads. There are two types of the roads in Dagestan, black and white: black roads are safe at night only, and white roads — in the morning. Before the trip Egor's manager's going to make a schedule: for each city he'll specify it's color, black or white, and then if during the trip they visit some city, the only time they can leave it is determined by the city's color: night, if it's black, and morning, if it's white. After creating the schedule Egor chooses an available path from $1$ to $n$, and for security reasons it has to be the shortest possible. Egor's manager likes Dagestan very much and wants to stay here as long as possible, so he asks you to make such schedule that there would be no path from $1$ to $n$ or the shortest path's length would be greatest possible. A path is one city or a sequence of roads such that for every road (excluding the first one) the city this road goes from is equal to the city previous road goes into. Egor can move only along paths consisting of safe roads only. The path length is equal to the number of roads in it. The shortest path in a graph is a path with smallest length. -----Input----- The first line contains two integers $n$, $m$ ($1 \leq n \leq 500000$, $0 \leq m \leq 500000$) — the number of cities and the number of roads. The $i$-th of next $m$ lines contains three integers — $u_i$, $v_i$ and $t_i$ ($1 \leq u_i, v_i \leq n$, $t_i \in \{0, 1\}$) — numbers of cities connected by road and its type, respectively ($0$ — night road, $1$ — morning road). -----Output----- In the first line output the length of the desired path (or $-1$, if it's possible to choose such schedule that there's no path from $1$ to $n$). In the second line output the desired schedule — a string of $n$ digits, where $i$-th digit is $0$, if the $i$-th city is a night one, and $1$ if it's a morning one. If there are multiple answers, print any. -----Examples----- Input 3 4 1 2 0 1 3 1 2 3 0 2 3 1 Output 2 011 Input 4 8 1 1 0 1 3 0 1 3 1 3 2 0 2 1 0 3 4 1 2 4 0 2 4 1 Output 3 1101 Input 5 10 1 2 0 1 3 1 1 4 0 2 3 0 2 3 1 2 5 0 3 4 0 3 4 1 4 2 1 4 5 0 Output -1 11111 -----Note----- For the first sample, if we paint city $1$ white, the shortest path is $1 \rightarrow 3$. Otherwise, it's $1 \rightarrow 2 \rightarrow 3$ regardless of other cities' colors. For the second sample, we should paint city $3$ black, and there are both black and white roads going from $2$ to $4$. Note that there can be a road connecting a city with itself. 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 from collections import deque n, m = list(map(int, input().split())) back = [[] for i in range(n)] for _ in range(m): u, v, w = list(map(int, input().split())) u -= 1 v -= 1 back[v].append((u,w)) out = [2] * n outl = [-1] * n outl[-1] = 0 q = deque([n - 1]) while q: v = q.popleft() for u, w in back[v]: if out[u] != w: out[u] = 1 - w else: if outl[u] == -1: outl[u] = outl[v] + 1 q.append(u) out = [v if v != 2 else 1 for v in out] print(outl[0]) print(''.join(map(str,out))) ```
vfc_18526
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1407/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 4\n1 2 0\n1 3 1\n2 3 0\n2 3 1\n", "output": "2\n011", "type": "stdin_stdout" } ] }
apps
verifiable_code
2194
Solve the following coding problem using the programming language python: You are given an array $a$ of length $2^n$. You should process $q$ queries on it. Each query has one of the following $4$ types: $Replace(x, k)$ — change $a_x$ to $k$; $Reverse(k)$ — reverse each subarray $[(i-1) \cdot 2^k+1, i \cdot 2^k]$ for all $i$ ($i \ge 1$); $Swap(k)$ — swap subarrays $[(2i-2) \cdot 2^k+1, (2i-1) \cdot 2^k]$ and $[(2i-1) \cdot 2^k+1, 2i \cdot 2^k]$ for all $i$ ($i \ge 1$); $Sum(l, r)$ — print the sum of the elements of subarray $[l, r]$. Write a program that can quickly process given queries. -----Input----- The first line contains two integers $n$, $q$ ($0 \le n \le 18$; $1 \le q \le 10^5$) — the length of array $a$ and the number of queries. The second line contains $2^n$ integers $a_1, a_2, \ldots, a_{2^n}$ ($0 \le a_i \le 10^9$). Next $q$ lines contains queries — one per line. Each query has one of $4$ types: "$1$ $x$ $k$" ($1 \le x \le 2^n$; $0 \le k \le 10^9$) — $Replace(x, k)$; "$2$ $k$" ($0 \le k \le n$) — $Reverse(k)$; "$3$ $k$" ($0 \le k < n$) — $Swap(k)$; "$4$ $l$ $r$" ($1 \le l \le r \le 2^n$) — $Sum(l, r)$. It is guaranteed that there is at least one $Sum$ query. -----Output----- Print the answer for each $Sum$ query. -----Examples----- Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 -----Note----- In the first sample, initially, the array $a$ is equal to $\{7,4,9,9\}$. After processing the first query. the array $a$ becomes $\{7,8,9,9\}$. After processing the second query, the array $a_i$ becomes $\{9,9,7,8\}$ Therefore, the answer to the third query is $9+7+8=24$. In the second sample, initially, the array $a$ is equal to $\{7,0,8,8,7,1,5,2\}$. What happens next is: $Sum(3, 7)$ $\to$ $8 + 8 + 7 + 1 + 5 = 29$; $Reverse(1)$ $\to$ $\{0,7,8,8,1,7,2,5\}$; $Swap(2)$ $\to$ $\{1,7,2,5,0,7,8,8\}$; $Sum(1, 6)$ $\to$ $1 + 7 + 2 + 5 + 0 + 7 = 22$; $Reverse(3)$ $\to$ $\{8,8,7,0,5,2,7,1\}$; $Replace(5, 16)$ $\to$ $\{8,8,7,0,16,2,7,1\}$; $Sum(8, 8)$ $\to$ $1$; $Swap(0)$ $\to$ $\{8,8,0,7,2,16,1,7\}$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python class BIT(): def __init__(self,n): self.BIT=[0]*(n+1) self.num=n def query(self,idx): res_sum = 0 while idx > 0: res_sum += self.BIT[idx] idx -= idx&(-idx) return res_sum #Ai += x O(logN) def update(self,idx,x): while idx <= self.num: self.BIT[idx] += x idx += idx&(-idx) return n,q=map(int,input().split()) a=list(map(int,input().split())) bit=BIT(2**n) for i in range(2**n): bit.update(i+1,a[i]) b=0 def Sum(r,xor): id=xor res=0 if r==-1: return res for i in range(n,-1,-1): if r>>i &1: L=(id>>i)<<i R=L+(1<<i)-1 res+=bit.query(R+1)-bit.query(L) id^=(1<<i) return res for _ in range(q): query=tuple(map(int,input().split())) if query[0]==1: g,x,k=query x-=1 x^=b bit.update(x+1,k-a[x]) a[x]=k elif query[0]==2: k=query[1] b^=2**k-1 elif query[0]==3: k=query[1] if k!=n: b^=2**k else: gl,l,r=query l-=1 test=Sum(r,b)-Sum(l,b) print(test) ```
vfc_18530
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1401/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 3\n7 4 9 9\n1 2 8\n3 1\n4 2 4\n", "output": "24\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 8\n7 0 8 8 7 1 5 2\n4 3 7\n2 1\n3 2\n4 1 6\n2 3\n1 5 16\n4 8 8\n3 0\n", "output": "29\n22\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 1\n102372403\n4 1 1\n", "output": "102372403\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 2\n394521962\n4 1 1\n2 0\n", "output": "394521962\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n765529946 133384866\n4 1 2\n", "output": "898914812\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n170229564 772959316 149804289 584342409\n4 2 4\n", "output": "1507106014\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2195
Solve the following coding problem using the programming language python: You are given two integers $x$ and $y$. You can perform two types of operations: Pay $a$ dollars and increase or decrease any of these integers by $1$. For example, if $x = 0$ and $y = 7$ there are four possible outcomes after this operation: $x = 0$, $y = 6$; $x = 0$, $y = 8$; $x = -1$, $y = 7$; $x = 1$, $y = 7$. Pay $b$ dollars and increase or decrease both integers by $1$. For example, if $x = 0$ and $y = 7$ there are two possible outcomes after this operation: $x = -1$, $y = 6$; $x = 1$, $y = 8$. Your goal is to make both given integers equal zero simultaneously, i.e. $x = y = 0$. There are no other requirements. In particular, it is possible to move from $x=1$, $y=0$ to $x=y=0$. Calculate the minimum amount of dollars you have to spend on it. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of testcases. The first line of each test case contains two integers $x$ and $y$ ($0 \le x, y \le 10^9$). The second line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- For each test case print one integer — the minimum amount of dollars you have to spend. -----Example----- Input 2 1 3 391 555 0 0 9 4 Output 1337 0 -----Note----- In the first test case you can perform the following sequence of operations: first, second, first. This way you spend $391 + 555 + 391 = 1337$ dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money. 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): x,y = map(int,input().split()) a,b = map(int,input().split()) wynik = 0 if b <= 2*a: c = min(x,y) wynik += b*c wynik += (max(x,y)-c)*a else: wynik = a*(x+y) print(wynik) ```
vfc_18534
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1342/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n1 3\n391 555\n0 0\n9 4\n", "output": "1337\n0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1 3\n391 555\n129 8437\n9 4\n", "output": "1337\n75288\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n321 654\n3 4\n", "output": "2283\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n129 8437\n5 3\n", "output": "41927\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n150 140\n1 1\n", "output": "150\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 3\n391 555\n129 8437\n9 4\n321 654\n3 4\n", "output": "1337\n75288\n2283\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2196
Solve the following coding problem using the programming language python: Ivan has got an array of n non-negative integers a_1, a_2, ..., a_{n}. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2^{a}_1, 2^{a}_2, ..., 2^{a}_{n} on a piece of paper. Now he wonders, what minimum number of integers of form 2^{b} (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2^{v} - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The second input line contains n space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 2·10^9). It is guaranteed that a_1 ≤ a_2 ≤ ... ≤ a_{n}. -----Output----- Print a single integer — the answer to the problem. -----Examples----- Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 -----Note----- In the first sample you do not need to add anything, the sum of numbers already equals 2^3 - 1 = 7. In the second sample you need to add numbers 2^0, 2^1, 2^2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python input() a = list(map(int, input().split())) b = [] i = j = 0 while i < len(a): while j < len(a) and a[j] == a[i]: j += 1 if (j - i) % 2 == 1: b += [a[i]] i = j - (j - i) // 2 for k in range(i, j): a[k] += 1 print(b[-1] - len(b) + 1) ```
vfc_18538
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/305/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n0 1 1 1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n3\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2000000000\n", "output": "2000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "26\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2\n", "output": "5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2197
Solve the following coding problem using the programming language python: Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim. Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game. Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above. -----Input----- The first line of the input contains two integers n (1 ≤ n ≤ 10^9) and x (1 ≤ x ≤ 100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1. -----Output----- Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10^{ - 6}. -----Example----- Input 2 2 0.500000 0.250000 0.250000 Output 0.62500000 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("data.txt") input=sys.stdin.readline n,x=map(int,input().split()) def mult(a,b): # compute a*b c=[0]*128 for i in range(128): for j in range(128): c[i^j]+=a[i]*b[j] return c def quickpow(a,b): # compute a**b if b==1: return a if b&1: return mult(quickpow(mult(a,a),b//2),a) return quickpow(mult(a,a),b//2) prob=list(map(float,input().split())) prob+=[0.0]*(128-len(prob)) print("%.9f"%(1-quickpow(prob,n)[0])) ```
vfc_18542
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/717/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2\n0.500000 0.250000 0.250000\n", "output": "0.62500000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2198
Solve the following coding problem using the programming language python: Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacement as the following sequence of steps: find a substring ".." (two consecutive periods) in string s, of all occurrences of the substring let's choose the first one, and replace this substring with string ".". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string s contains no two consecutive periods, then nothing happens. Let's define f(s) as the minimum number of operations of replacement to perform, so that the string does not have any two consecutive periods left. You need to process m queries, the i-th results in that the character at position x_{i} (1 ≤ x_{i} ≤ n) of string s is assigned value c_{i}. After each operation you have to calculate and output the value of f(s). Help Daniel to process all queries. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) the length of the string and the number of queries. The second line contains string s, consisting of n lowercase English letters and period signs. The following m lines contain the descriptions of queries. The i-th line contains integer x_{i} and c_{i} (1 ≤ x_{i} ≤ n, c_{i} — a lowercas English letter or a period sign), describing the query of assigning symbol c_{i} to position x_{i}. -----Output----- Print m numbers, one per line, the i-th of these numbers must be equal to the value of f(s) after performing the i-th assignment. -----Examples----- Input 10 3 .b..bz.... 1 h 3 c 9 f Output 4 3 1 Input 4 4 .cc. 2 . 3 . 2 a 1 a Output 1 3 1 1 -----Note----- Note to the first sample test (replaced periods are enclosed in square brackets). The original string is ".b..bz....". after the first query f(hb..bz....) = 4    ("hb[..]bz...." → "hb.bz[..].." → "hb.bz[..]." → "hb.bz[..]" → "hb.bz.") after the second query f(hbс.bz....) = 3    ("hbс.bz[..].." → "hbс.bz[..]." → "hbс.bz[..]" → "hbс.bz.") after the third query f(hbс.bz..f.) = 1    ("hbс.bz[..]f." → "hbс.bz.f.") Note to the second sample test. The original string is ".cc.". after the first query: f(..c.) = 1    ("[..]c." → ".c.") after the second query: f(....) = 3    ("[..].." → "[..]." → "[..]" → ".") after the third query: f(.a..) = 1    (".a[..]" → ".a.") after the fourth query: f(aa..) = 1    ("aa[..]" → "aa.") 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.setrecursionlimit(1000000) def solve(): n, m, = rv() s = list(input()) res = [0] * m #replace dot: #dot had nothing on left or right: nothing changes #dot had one on left or right: -1 #dot had two on left or right: -2 #replace char: #if had two chars on left and right: 0 # if had one char and one dot: +1 # if had two dots: +2 helper = list() for i in range(n): if s[i] == '.': if i == 0: helper.append(1) else: if s[i-1] == '.': helper[-1] += 1 else: helper.append(1) initval = 0 for val in helper: initval += val - 1 for query in range(m): index, replace = input().split() index = int(index) - 1 if (s[index] == '.' and replace == '.') or (s[index] != '.' and replace != '.'): res[query] = initval else: sidedots = 0 if index > 0: if s[index - 1] == '.': sidedots+=1 if index < n - 1: if s[index + 1] == '.': sidedots+=1 if s[index] == '.': res[query] = initval - sidedots initval -= sidedots else: res[query] = initval + sidedots initval += sidedots s[index] = replace print('\n'.join(map(str, res))) def rv(): return list(map(int, input().split())) def rl(n): return [list(map(int, input().split())) for _ in range(n)] if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve() ```
vfc_18546
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/570/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "10 3\n.b..bz....\n1 h\n3 c\n9 f\n", "output": "4\n3\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 4\n.cc.\n2 .\n3 .\n2 a\n1 a\n", "output": "1\n3\n1\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\n...\n1 .\n2 a\n3 b\n", "output": "2\n0\n0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2199
Solve the following coding problem using the programming language python: You are given a multiset S consisting of positive integers (initially empty). There are two kind of queries: Add a positive integer to S, the newly added integer is not less than any number in it. Find a subset s of the set S such that the value $\operatorname{max}(s) - \operatorname{mean}(s)$ is maximum possible. Here max(s) means maximum value of elements in s, $\operatorname{mean}(s)$ — the average value of numbers in s. Output this maximum possible value of $\operatorname{max}(s) - \operatorname{mean}(s)$. -----Input----- The first line contains a single integer Q (1 ≤ Q ≤ 5·10^5) — the number of queries. Each of the next Q lines contains a description of query. For queries of type 1 two integers 1 and x are given, where x (1 ≤ x ≤ 10^9) is a number that you should add to S. It's guaranteed that x is not less than any number in S. For queries of type 2, a single integer 2 is given. It's guaranteed that the first query has type 1, i. e. S is not empty when a query of type 2 comes. -----Output----- Output the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line. Your answer is considered correct, if each of your answers has absolute or relative error not greater than 10^{ - 6}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if $\frac{|a - b|}{\operatorname{max}(1,|b|)} \leq 10^{-6}$. -----Examples----- Input 6 1 3 2 1 4 2 1 8 2 Output 0.0000000000 0.5000000000 3.0000000000 Input 4 1 1 1 4 1 5 2 Output 2.0000000000 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()) s=input().split() a=[int(s[1])] sum1=a[0] pos=-1 mean=sum1 fin='' for i in range(q-1): n=len(a) s=input().split() if(s[0]=='1'): a.append(int(s[1])) sum1+=(a[-1]-a[-2]) mean=sum1/(pos+2) n=len(a) #print(sum1,pos,i+1) while(pos<n-2 and a[pos+1]<mean): pos+=1 sum1+=a[pos] mean=sum1/(pos+2) #print(sum1,pos,i+1) else: #print(sum1,pos) fin+=str(a[-1]-mean) + '\n' print(fin) ```
vfc_18550
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/939/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n1 3\n2\n1 4\n2\n1 8\n2\n", "output": "0.0000000000\n0.5000000000\n3.0000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 1\n1 4\n1 5\n2\n", "output": "2.0000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n1 7\n1 26\n1 40\n1 45\n1 64\n2\n1 88\n1 94\n", "output": "31.6666666667\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2200
Solve the following coding problem using the programming language python: Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Round 240 Div 1 Problem B Author : chaotic_iak Language: Python 3.3.4 """ class InputHandlerObject(object): inputs = [] def getInput(self, n = 0): res = "" inputs = self.inputs if not inputs: inputs.extend(input().split(" ")) if n == 0: res = inputs[:] inputs[:] = [] while n > len(inputs): inputs.extend(input().split(" ")) if n > 0: res = inputs[:n] inputs[:n] = [] return res InputHandler = InputHandlerObject() g = InputHandler.getInput ############################## SOLUTION ############################## n,a,b = g() n,a,b = int(n),int(a),int(b) c = [int(x) for x in g()] r = [] for i in c: r.append(str(((i*a) % b) // a)) print(" ".join(r)) ```
vfc_18554
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/415/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 1 4\n12 6 11 9 1\n", "output": "0 2 3 1 1 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
2201
Solve the following coding problem using the programming language python: Johnny drives a truck and must deliver a package from his hometown to the district center. His hometown is located at point 0 on a number line, and the district center is located at the point d. Johnny's truck has a gas tank that holds exactly n liters, and his tank is initially full. As he drives, the truck consumes exactly one liter per unit distance traveled. Moreover, there are m gas stations located at various points along the way to the district center. The i-th station is located at the point x_{i} on the number line and sells an unlimited amount of fuel at a price of p_{i} dollars per liter. Find the minimum cost Johnny must pay for fuel to successfully complete the delivery. -----Input----- The first line of input contains three space separated integers d, n, and m (1 ≤ n ≤ d ≤ 10^9, 1 ≤ m ≤ 200 000) — the total distance to the district center, the volume of the gas tank, and the number of gas stations, respectively. Each of the next m lines contains two integers x_{i}, p_{i} (1 ≤ x_{i} ≤ d - 1, 1 ≤ p_{i} ≤ 10^6) — the position and cost of gas at the i-th gas station. It is guaranteed that the positions of the gas stations are distinct. -----Output----- Print a single integer — the minimum cost to complete the delivery. If there is no way to complete the delivery, print -1. -----Examples----- Input 10 4 4 3 5 5 8 6 3 8 4 Output 22 Input 16 5 2 8 2 5 1 Output -1 -----Note----- In the first sample, Johnny's truck holds 4 liters. He can drive 3 units to the first gas station, buy 2 liters of gas there (bringing the tank to 3 liters total), drive 3 more units to the third gas station, buy 4 liters there to fill up his tank, and then drive straight to the district center. His total cost is 2·5 + 4·3 = 22 dollars. In the second sample, there is no way for Johnny to make it to the district center, as his tank cannot hold enough gas to take him from the latest gas station to the district center. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python destination, max_gas_tank_volume, gas_prices_number = list(map(int, input().split())) start_point = 0 gas_prices = {start_point:0} for i in range(gas_prices_number): coordinate, price = list(map(int, input().split())) gas_prices[coordinate] = price points = sorted(list(gas_prices.keys()), reverse = True) current_point = start_point count = 0 gas_tank_volume = max_gas_tank_volume reachable_points = [] while current_point != destination: farthest_reachable_point = current_point + max_gas_tank_volume while points and points[-1] <= farthest_reachable_point: reachable_points.append(points.pop()) if reachable_points: cheaper_reachable_points = sorted([point for point in reachable_points if gas_prices[point] < gas_prices[current_point]]) next_point = cheaper_reachable_points[0] if cheaper_reachable_points else min(reachable_points, key = lambda point: gas_prices[point]) if farthest_reachable_point >= destination and (current_point == start_point or gas_prices[next_point] >= gas_prices[current_point]): next_point = destination else: reachable_points = [point for point in reachable_points if point > next_point] else: if farthest_reachable_point >= destination: next_point = destination else: count = -1 break distantion = next_point - current_point if next_point != destination and gas_prices[current_point] <= gas_prices[next_point]: required_gas_volume = max_gas_tank_volume else: required_gas_volume = distantion if required_gas_volume > gas_tank_volume: count += (required_gas_volume - gas_tank_volume)*gas_prices[current_point] gas_tank_volume = required_gas_volume current_point = next_point gas_tank_volume -= distantion print(count) ```
vfc_18558
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/627/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "10 4 4\n3 5\n5 8\n6 3\n8 4\n", "output": "22\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "16 5 2\n8 2\n5 1\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "400000000 400000000 3\n1 139613\n19426 13509\n246298622 343529\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "229 123 2\n170 270968\n76 734741\n", "output": "50519939\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "153 105 1\n96 83995\n", "output": "4031760\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2202
Solve the following coding problem using the programming language python: Rebel spy Heidi has just obtained the plans for the Death Star from the Empire and, now on her way to safety, she is trying to break the encryption of the plans (of course they are encrypted – the Empire may be evil, but it is not stupid!). The encryption has several levels of security, and here is how the first one looks. Heidi is presented with a screen that shows her a sequence of integers A and a positive integer p. She knows that the encryption code is a single number S, which is defined as follows: Define the score of X to be the sum of the elements of X modulo p. Heidi is given a sequence A that consists of N integers, and also given an integer p. She needs to split A into 2 parts such that: Each part contains at least 1 element of A, and each part consists of contiguous elements of A. The two parts do not overlap. The total sum S of the scores of those two parts is maximized. This is the encryption code. Output the sum S, which is the encryption code. -----Input----- The first line of the input contains two space-separated integer N and p (2 ≤ N ≤ 100 000, 2 ≤ p ≤ 10 000) – the number of elements in A, and the modulo for computing scores, respectively. The second line contains N space-separated integers which are the elements of A. Each integer is from the interval [1, 1 000 000]. -----Output----- Output the number S as described in the problem statement. -----Examples----- Input 4 10 3 4 7 2 Output 16 Input 10 12 16 3 24 13 9 8 7 5 12 12 Output 13 -----Note----- In the first example, the score is maximized if the input sequence is split into two parts as (3, 4), (7, 2). It gives the total score of $(3 + 4 \operatorname{mod} 10) +(7 + 2 \operatorname{mod} 10) = 16$. In the second example, the score is maximized if the first part consists of the first three elements, and the second part consists of the rest. Then, the score is $(16 + 3 + 24 \operatorname{mod} 12) +(13 + 9 + 8 + 7 + 5 + 12 + 12 \operatorname{mod} 12) = 7 + 6 = 13$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #http://codeforces.com/contest/958/problem/C1 N,p=list(map(int,input().split())) A=list(map(int,input().split())) sum1=A[0] sum2=sum(A[1:]) ans=(sum1%p)+(sum2%p) for i in range(1,N-1): sum1+=A[i] sum2-=A[i] ans1=sum1%p ans2=sum2%p ans=max(ans1+ans2,ans) print(ans) ```
vfc_18562
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/958/C1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 10\n3 4 7 2\n", "output": "16\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 12\n16 3 24 13 9 8 7 5 12 12\n", "output": "13\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n9 9\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n8 8\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 50\n1 1 1 1 1\n", "output": "5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2203
Solve the following coding problem using the programming language python: Amr bought a new video game "Guess Your Way Out! II". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node. Let's index all the nodes of the tree such that The root is number 1 Each internal node i (i ≤ 2^{h} - 1 - 1) will have a left child with index = 2i and a right child with index = 2i + 1 The level of a node is defined as 1 for a root, or 1 + level of parent of the node otherwise. The vertices of the level h are called leaves. The exit to the maze is located at some leaf node n, the player doesn't know where the exit is so he has to guess his way out! In the new version of the game the player is allowed to ask questions on the format "Does the ancestor(exit, i) node number belong to the range [L, R]?". Here ancestor(v, i) is the ancestor of a node v that located in the level i. The game will answer with "Yes" or "No" only. The game is designed such that it doesn't always answer correctly, and sometimes it cheats to confuse the player!. Amr asked a lot of questions and got confused by all these answers, so he asked you to help him. Given the questions and its answers, can you identify whether the game is telling contradictory information or not? If the information is not contradictory and the exit node can be determined uniquely, output its number. If the information is not contradictory, but the exit node isn't defined uniquely, output that the number of questions is not sufficient. Otherwise output that the information is contradictory. -----Input----- The first line contains two integers h, q (1 ≤ h ≤ 50, 0 ≤ q ≤ 10^5), the height of the tree and the number of questions respectively. The next q lines will contain four integers each i, L, R, ans (1 ≤ i ≤ h, 2^{i} - 1 ≤ L ≤ R ≤ 2^{i} - 1, $\text{ans} \in \{0,1 \}$), representing a question as described in the statement with its answer (ans = 1 if the answer is "Yes" and ans = 0 if the answer is "No"). -----Output----- If the information provided by the game is contradictory output "Game cheated!" without the quotes. Else if you can uniquely identify the exit to the maze output its index. Otherwise output "Data not sufficient!" without the quotes. -----Examples----- Input 3 1 3 4 6 0 Output 7 Input 4 3 4 10 14 1 3 6 6 0 2 3 3 1 Output 14 Input 4 2 3 4 6 1 4 12 15 1 Output Data not sufficient! Input 4 2 3 4 5 1 2 3 3 1 Output Game cheated! -----Note----- Node u is an ancestor of node v if and only if u is the same node as v, u is the parent of node v, or u is an ancestor of the parent of node v. In the first sample test there are 4 leaf nodes 4, 5, 6, 7. The first question says that the node isn't in the range [4, 6] so the exit is node number 7. In the second sample test there are 8 leaf nodes. After the first question the exit is in the range [10, 14]. After the second and the third questions only node number 14 is correct. Check the picture below to fully understand. [Image] 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 * h,q=list(map(int,input().split())) d=defaultdict(lambda:0) d[2**h]=0 d[2**(h-1)]=0 for _ in range(q): i,l,r,a=list(map(int,input().split())) l,r=l*2**(h-i),(r+1)*2**(h-i) if a:d[1]+=1;d[l]-=1;d[r]+=1 else:d[l]+=1;d[r]-=1 s=0 l=0 D=sorted(d.items()) for (a,x),(b,_) in zip(D,D[1:]): s+=x if s==0:q=a;l+=b-a print(("Game cheated!",q,"Data not sufficient!")[min(l,2)]) ```
vfc_18566
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/558/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 1\n3 4 6 0\n", "output": "7", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 3\n4 10 14 1\n3 6 6 0\n2 3 3 1\n", "output": "14", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2\n3 4 6 1\n4 12 15 1\n", "output": "Data not sufficient!", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2\n3 4 5 1\n2 3 3 1\n", "output": "Game cheated!", "type": "stdin_stdout" } ] }
apps
verifiable_code
2204
Solve the following coding problem using the programming language python: Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly $n$ flowers. Vladimir went to a flower shop, and he was amazed to see that there are $m$ types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the $i$-th type happiness of his wife increases by $a_i$ and after receiving each consecutive flower of this type her happiness increases by $b_i$. That is, if among the chosen flowers there are $x_i > 0$ flowers of type $i$, his wife gets $a_i + (x_i - 1) \cdot b_i$ additional happiness (and if there are no flowers of type $i$, she gets nothing for this particular type). Please help Vladimir to choose exactly $n$ flowers to maximize the total happiness of his wife. -----Input----- The first line contains the only integer $t$ ($1 \leq t \leq 10\,000$), the number of test cases. It is followed by $t$ descriptions of the test cases. Each test case description starts with two integers $n$ and $m$ ($1 \le n \le 10^9$, $1 \le m \le 100\,000$), the number of flowers Vladimir needs to choose and the number of types of available flowers. The following $m$ lines describe the types of flowers: each line contains integers $a_i$ and $b_i$ ($0 \le a_i, b_i \le 10^9$) for $i$-th available type of flowers. The test cases are separated by a blank line. It is guaranteed that the sum of values $m$ among all test cases does not exceed $100\,000$. -----Output----- For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly $n$ flowers optimally. -----Example----- Input 2 4 3 5 0 1 4 2 2 5 3 5 2 4 2 3 1 Output 14 16 -----Note----- In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals $5 + (1 + 2 \cdot 4) = 14$. In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals $(5 + 1 \cdot 2) + (4 + 1 \cdot 2) + 3 = 16$. 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): if test: input() n, m = list(map(int, input().split())) a = [-1] * m b = [-1] * m for i in range(m): a[i], b[i] = list(map(int, input().split())) l = [(a[i],0,i) for i in range(m)] + [(b[i],1,i) for i in range(m)] l.sort(reverse = True) count = 0 tot = 0 best = 0 used = [False] * m for good, typ, ind in l: if typ == 0: count += 1 tot += good used[ind] = True if count == n: best = max(best, tot) break else: curr = tot curr += good * (n - count) if not used[ind]: curr -= good curr += a[ind] best = max(curr,best) print(best) ```
vfc_18570
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1379/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n4 3\n5 0\n1 4\n2 2\n\n5 3\n5 2\n4 2\n3 1\n", "output": "14\n16\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2205
Solve the following coding problem using the programming language python: People in the Tomskaya region like magic formulas very much. You can see some of them below. Imagine you are given a sequence of positive integer numbers p_1, p_2, ..., p_{n}. Lets write down some magic formulas:$q_{i} = p_{i} \oplus(i \operatorname{mod} 1) \oplus(i \operatorname{mod} 2) \oplus \cdots \oplus(i \operatorname{mod} n)$$Q = q_{1} \oplus q_{2} \oplus \ldots \oplus q_{n}$ Here, "mod" means the operation of taking the residue after dividing. The expression $x \oplus y$ means applying the bitwise xor (excluding "OR") operation to integers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by "^", in Pascal — by "xor". People in the Tomskaya region like magic formulas very much, but they don't like to calculate them! Therefore you are given the sequence p, calculate the value of Q. -----Input----- The first line of the input contains the only integer n (1 ≤ n ≤ 10^6). The next line contains n integers: p_1, p_2, ..., p_{n} (0 ≤ p_{i} ≤ 2·10^9). -----Output----- The only line of output should contain a single integer — the value of Q. -----Examples----- Input 3 1 2 3 Output 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()) p = [int(i) for i in input().split()] s = [0] * n for i in range(1,n): s[i] = s[i-1] ^ i q = 0 for i in range(n): q = q ^ p[i] if (n // (i+1)) % 2 == 1: q = q ^ s[i] q = q ^ s[n % (i+1)] print(q) ```
vfc_18574
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/424/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 2 3\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n65535 0\n", "output": "65534\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1356106972 165139648 978829595 410669403 873711167 287346624 117863440 228957745 835903650 1575323015\n", "output": "948506286\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\n1999581813 313463235 1733614990 662007911 1789348031 1120800519 196972430 1579897311 191001928 241720485 1426288783 1103088596 839698523 1974815116 77040208 904949865 840522850 1488919296 1027394709 857931762\n", "output": "1536068328\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2207
Solve the following coding problem using the programming language python: "The zombies are lurking outside. Waiting. Moaning. And when they come..." "When they come?" "I hope the Wall is high enough." Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for help. Go forth and explore the wall! Report how many disconnected segments there are. The wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to R bricks high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between, is C columns wide. -----Input----- The first line of the input consists of two space-separated integers R and C, 1 ≤ R, C ≤ 100. The next R lines provide a description of the columns as follows: each of the R lines contains a string of length C, the c-th character of line r is B if there is a brick in column c and row R - r + 1, and . otherwise. The input will contain at least one character B and it will be valid. -----Output----- The number of wall segments in the input configuration. -----Examples----- Input 3 7 ....... ....... .BB.B.. Output 2 Input 4 5 ..B.. ..B.. B.B.B BBB.B Output 2 Input 4 6 ..B... B.B.BB BBB.BB BBBBBB Output 1 Input 1 1 B Output 1 Input 10 7 ....... ....... ....... ....... ....... ....... ....... ....... ...B... B.BB.B. Output 3 Input 8 8 ........ ........ ........ ........ .B...... .B.....B .B.....B .BB...BB Output 2 -----Note----- In the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m = map(int, input().split()) a = [input() for i in range(n)] ans = 0 i = 0 while i < m: if a[n-1][i] == "B": ans += 1 while i < m and a[n-1][i] == "B": i += 1 i += 1 print(ans) ```
vfc_18582
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/690/D1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 7\n.......\n.......\n.BB.B..\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 5\n..B..\n..B..\nB.B.B\nBBB.B\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 6\n..B...\nB.B.BB\nBBB.BB\nBBBBBB\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2208
Solve the following coding problem using the programming language python: Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows? Every one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r), Mike can instantly tell the value of $\operatorname{max}_{i = l}^{r} a_{i}$ while !Mike can instantly tell the value of $\operatorname{min}_{i = l} b_{i}$. Now suppose a robot (you!) asks them all possible different queries of pairs of integers (l, r) (1 ≤ l ≤ r ≤ n) (so he will make exactly n(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs $\operatorname{max}_{i = l}^{r} a_{i} = \operatorname{min}_{i = l} b_{i}$ is satisfied. How many occasions will the robot count? -----Input----- The first line contains only integer n (1 ≤ n ≤ 200 000). The second line contains n integer numbers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9) — the sequence a. The third line contains n integer numbers b_1, b_2, ..., b_{n} ( - 10^9 ≤ b_{i} ≤ 10^9) — the sequence b. -----Output----- Print the only integer number — the number of occasions the robot will count, thus for how many pairs $\operatorname{max}_{i = l}^{r} a_{i} = \operatorname{min}_{i = l} b_{i}$ is satisfied. -----Examples----- Input 6 1 2 3 2 1 4 6 7 1 2 3 2 Output 2 Input 3 3 3 3 1 1 1 Output 0 -----Note----- The occasions in the first sample case are: 1.l = 4,r = 4 since max{2} = min{2}. 2.l = 4,r = 5 since max{2, 1} = min{2, 3}. There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from bisect import bisect HISENTINEL = 10**9 + 1 LOSENTINEL = -HISENTINEL def main(): length = int(input()) a = [int(fld) for fld in input().strip().split()] b = [int(fld) for fld in input().strip().split()] print(countmaxminsubseq(a, b)) def countmaxminsubseq(a, b): leq, lgt = getleftbounds(a, b, 0) req, rgt = getleftbounds(reversed(a), reversed(b), 1) req = reverseindex(req) rgt = reverseindex(rgt) count = 0 for i, (leq1, lgt1, req1, rgt1) in enumerate(zip(leq, lgt, req, rgt)): count += (leq1 - lgt1)*(rgt1 - i) + (i - leq1)*(rgt1 - req1) return count def getleftbounds(a, b, bias): astack = [(HISENTINEL, -1)] bstack = [(LOSENTINEL, -1)] leqarr, lgtarr = [], [] for i, (aelt, belt) in enumerate(zip(a, b)): while astack[-1][0] < aelt + bias: astack.pop() lgt = astack[-1][1] while bstack[-1][0] > belt: bstack.pop() if belt < aelt: leq = lgt = i elif belt == aelt: leq = i istack = bisect(bstack, (aelt, -2)) - 1 lgt = max(lgt, bstack[istack][1]) else: istack = bisect(bstack, (aelt, i)) - 1 val, pos = bstack[istack] if val < aelt: lgt = leq = max(lgt, pos) else: leq = pos istack = bisect(bstack, (aelt, -2)) - 1 val, pos = bstack[istack] lgt = max(lgt, pos) leq = max(leq, lgt) leqarr.append(leq) lgtarr.append(lgt) astack.append((aelt, i)) bstack.append((belt, i)) return leqarr, lgtarr def reverseindex(rind): pivot = len(rind) - 1 return [pivot - i for i in reversed(rind)] main() ```
vfc_18586
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/689/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n1 2 3 2 1 4\n6 7 1 2 3 2\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n3 3 3\n1 1 1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "17\n714413739 -959271262 714413739 -745891378 926207665 -404845105 -404845105 -959271262 -189641729 -670860364 714413739 -189641729 192457837 -745891378 -670860364 536388097 -959271262\n-417715348 -959271262 -959271262 714413739 -189641729 571055593 571055593 571055593 -417715348 -417715348 192457837 -745891378 536388097 571055593 -189641729 571055593 -670860364\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n509658558\n509658558\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n509658558\n-544591380\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 1 1\n2 2 2\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2209
Solve the following coding problem using the programming language python: Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import functools n = int(input()) arr = [input() for i in range(n)] def compare(s1, s2): a = s1.count('s') b = s2.count('s') if (a*len(s2)) < b*len(s1): return 1 return -1 arr = sorted(arr, key=functools.cmp_to_key(compare)) s = ''.join(arr) c = 0 t = 0 for char in s: if char == 's': c += 1 elif char == 'h': t += c print(t) ```
vfc_18590
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/922/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\nssh\nhs\ns\nhhhs\n", "output": "18\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\nh\ns\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\nh\ns\nhhh\nh\nssssss\ns\n", "output": "40\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\ns\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\nsshshss\nhssssssssh\nhhhhhh\nhhhs\nhshhh\nhhhhshsh\nhh\nh\nshs\nsshhshhss\n", "output": "613\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2210
Solve the following coding problem using the programming language python: Ayush and Ashish play a game on an unrooted tree consisting of $n$ nodes numbered $1$ to $n$. Players make the following move in turns: Select any leaf node in the tree and remove it together with any edge which has this node as one of its endpoints. A leaf node is a node with degree less than or equal to $1$. A tree is a connected undirected graph without cycles. There is a special node numbered $x$. The player who removes this node wins the game. Ayush moves first. Determine the winner of the game if each player plays optimally. -----Input----- The first line of the input contains a single integer $t$ $(1 \leq t \leq 10)$ — the number of testcases. The description of the test cases follows. The first line of each testcase contains two integers $n$ and $x$ $(1\leq n \leq 1000, 1 \leq x \leq n)$ — the number of nodes in the tree and the special node respectively. Each of the next $n-1$ lines contain two integers $u$, $v$ $(1 \leq u, v \leq n, \text{ } u \ne v)$, meaning that there is an edge between nodes $u$ and $v$ in the tree. -----Output----- For every test case, if Ayush wins the game, print "Ayush", otherwise print "Ashish" (without quotes). -----Examples----- Input 1 3 1 2 1 3 1 Output Ashish Input 1 3 2 1 2 1 3 Output Ayush -----Note----- For the $1$st test case, Ayush can only remove node $2$ or $3$, after which node $1$ becomes a leaf node and Ashish can remove it in his turn. For the $2$nd test case, Ayush can remove node $2$ in the first move itself. 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): n, x = list(map(int, input().split())) x -= 1 degree = [0] * n edges = [(0,0)]*(n-1) for i in range(n - 1): u, v = list(map(int, input().split())) edges[i] = (u-1,v-1) degree[u-1] += 1 degree[v-1] += 1 if degree[x] == 1 or degree[x] == 0: print('Ayush') else: print('Ashish' if n % 2 else 'Ayush') ```
vfc_18594
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1363/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n3 1\n2 1\n3 1\n", "output": "Ashish\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n3 2\n1 2\n1 3\n", "output": "Ayush\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n6 4\n3 2\n1 6\n4 6\n6 5\n1 3\n6 4\n3 5\n1 2\n6 5\n4 1\n6 1\n6 3\n6 2\n3 1\n4 6\n1 6\n3 5\n5 5\n4 5\n4 2\n3 5\n4 1\n4 1\n1 3\n2 1\n4 1\n4 2\n1 2\n3 2\n4 1\n6 6\n3 4\n5 6\n1 3\n6 2\n3 5\n3 1\n1 2\n1 3\n3 1\n3 1\n2 1\n3 2\n2 1\n2 3\n", "output": "Ayush\nAyush\nAyush\nAshish\nAyush\nAyush\nAyush\nAshish\nAshish\nAshish\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n3 3\n3 2\n1 2\n8 8\n5 6\n3 4\n8 1\n5 2\n2 4\n2 1\n7 4\n4 1\n1 2\n4 3\n3 1\n6 5\n3 4\n4 2\n4 6\n4 5\n5 1\n6 2\n1 3\n2 5\n6 4\n4 2\n4 1\n8 1\n7 5\n7 6\n4 2\n8 3\n2 8\n1 8\n1 7\n6 1\n4 1\n2 1\n1 3\n1 6\n5 1\n8 2\n5 1\n2 4\n3 6\n1 2\n6 8\n6 1\n7 3\n3 2\n2 1\n3 2\n3 2\n2 1\n2 3\n", "output": "Ayush\nAyush\nAyush\nAyush\nAyush\nAyush\nAyush\nAyush\nAshish\nAshish\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n8 2\n1 5\n7 4\n8 5\n5 6\n8 7\n2 8\n1 3\n10 5\n10 1\n4 7\n6 4\n4 3\n8 1\n1 2\n1 4\n5 1\n1 9\n2 2\n2 1\n9 3\n9 6\n1 7\n2 6\n7 3\n6 8\n1 6\n5 1\n6 4\n8 6\n3 1\n8 6\n2 4\n5 7\n8 4\n1 8\n5 6\n10 3\n5 9\n10 1\n5 10\n6 1\n4 10\n1 2\n3 7\n2 3\n10 8\n4 1\n1 4\n1 3\n2 1\n5 5\n4 5\n1 3\n1 5\n2 3\n9 4\n1 2\n8 6\n3 4\n7 5\n2 4\n1 8\n2 7\n9 5\n6 1\n1 4\n5 6\n5 3\n5 1\n2 5\n", "output": "Ayush\nAyush\nAyush\nAyush\nAyush\nAyush\nAyush\nAshish\nAshish\nAyush\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2211
Solve the following coding problem using the programming language python: Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string s. To determine if a string is good or not the game uses rules. Overall there are n rules. Each rule is described by a group of three (p, l, r), where p is a string and l and r (l ≤ r) are integers. We’ll say that string t complies with rule (p, l, r), if the number of occurrences of string t in string p lies between l and r, inclusive. For example, string "ab", complies with rules ("ab", 1, 2) and ("aab", 0, 1), but does not comply with rules ("cd", 1, 2) and ("abab", 0, 1). A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s_1s_2... s_{|}s| (|s| is a length of s) is string s_{l}s_{l} + 1... s_{r}. Consider a number of occurrences of string t in string p as a number of pairs of integers l, r (1 ≤ l ≤ r ≤ |p|) such that p[l... r] = t. We’ll say that string t is good if it complies with all n rules. Smart Beaver asks you to help him to write a program that can calculate the number of distinct good substrings of string s. Two substrings s[x... y] and s[z... w] are cosidered to be distinct iff s[x... y] ≠ s[z... w]. -----Input----- The first line contains string s. The second line contains integer n. Next n lines contain the rules, one per line. Each of these lines contains a string and two integers p_{i}, l_{i}, r_{i}, separated by single spaces (0 ≤ l_{i} ≤ r_{i} ≤ |p_{i}|). It is guaranteed that all the given strings are non-empty and only contain lowercase English letters. The input limits for scoring 30 points are (subproblem G1): 0 ≤ n ≤ 10. The length of string s and the maximum length of string p is ≤ 200. The input limits for scoring 70 points are (subproblems G1+G2): 0 ≤ n ≤ 10. The length of string s and the maximum length of string p is ≤ 2000. The input limits for scoring 100 points are (subproblems G1+G2+G3): 0 ≤ n ≤ 10. The length of string s and the maximum length of string p is ≤ 50000. -----Output----- Print a single integer — the number of good substrings of string s. -----Examples----- Input aaab 2 aa 0 0 aab 1 1 Output 3 Input ltntlnen 3 n 0 0 ttlneenl 1 4 lelllt 1 1 Output 2 Input a 0 Output 1 -----Note----- There are three good substrings in the first sample test: «aab», «ab» and «b». In the second test only substrings «e» and «t» are good. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def count(p, s): start = 0 c = 0 while True: try: pos = s.index(p, start) c += 1 start = pos + 1 except ValueError: return c s = input() n = int(input()) pravs = [] for i in range(n): p, l, r = input().split() l = int(l); r = int(r) pravs.append((p, l, r)) var = set() for l in range(len(s)): for r in range(l+1, len(s)+1): pods = s[l:r] for prav in pravs: if not prav[1] <= count(pods, prav[0]) <= prav[2]: break else: var.add(pods) print(len(var)) ```
vfc_18598
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/316/G1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "aaab\n2\naa 0 0\naab 1 1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "ltntlnen\n3\nn 0 0\nttlneenl 1 4\nlelllt 1 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "a\n0\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "nysnvneyavzcebsbsvrsbcvzsrcr\n5\nycaa 1 3\nzsayyyvseccsbcbvzrr 5 16\nznz 1 3\nbvnzrccvcb 4 7\nseznebzeevvrncccaabsbny 17 21\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2212
Solve the following coding problem using the programming language python: Find an n × n matrix with different numbers from 1 to n^2, so the sum in each row, column and both main diagonals are odd. -----Input----- The only line contains odd integer n (1 ≤ n ≤ 49). -----Output----- Print n lines with n integers. All the integers should be different and from 1 to n^2. The sum in each row, column and both main diagonals should be odd. -----Examples----- Input 1 Output 1 Input 3 Output 2 1 4 3 5 7 6 9 8 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()) magic=int((n-1)/2) x = [] for t in range(magic, -1, -1): x.append(t*'*'+'D'*(n-2*t)+t*'*') for u in range(1, magic+1): x.append(u*'*'+'D'*(n-2*u)+u*'*') no = 1 ne = 2 for i in range(n): for j in range(n): if (x[i][j] == 'D'): print(no, end = ' ') no += 2 else: print(ne, end = ' ') ne += 2 print() ```
vfc_18602
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/710/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "2 1 4\n3 5 7\n6 9 8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n", "output": "2 4 1 6 8\n10 3 5 7 12\n9 11 13 15 17\n14 19 21 23 16\n18 20 25 22 24\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2213
Solve the following coding problem using the programming language python: Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers. Iahub can choose a pair of distinct indices i and j (1 ≤ i, j ≤ m, i ≠ j). Then in each array the values at positions i and j are swapped only if the value at position i is strictly greater than the value at position j. Iahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the n arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most $\frac{m(m - 1)}{2}$ (at most $\frac{m(m - 1)}{2}$ pairs). Help Iahub, find any suitable array. -----Input----- The first line contains three integers n (1 ≤ n ≤ 1000), m (1 ≤ m ≤ 100) and k. Integer k is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line i of the next n lines contains m integers separated by a space, representing the i-th array. For each element x of the array i, 1 ≤ x ≤ 10^6 holds. -----Output----- On the first line of the output print an integer p, the size of the array (p can be at most $\frac{m(m - 1)}{2}$). Each of the next p lines must contain two distinct integers i and j (1 ≤ i, j ≤ m, i ≠ j), representing the chosen indices. If there are multiple correct answers, you can print any. -----Examples----- Input 2 5 0 1 3 2 5 4 1 4 3 2 5 Output 3 2 4 2 3 4 5 Input 3 2 1 1 2 2 3 3 4 Output 1 2 1 -----Note----- Consider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5]. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,m,k=map(int,input().split()) print(m*(m-1)//2) for i in range(n): a=list(map(int,input().split())) if k==0: for i in range(m): for j in range(i+1,m): print(i+1,j+1) else: for i in range(m): for j in range(i+1,m): print(j+1,i+1) ```
vfc_18606
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/384/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 5 0\n1 3 2 5 4\n1 4 3 2 5\n", "output": "3\n2 4\n2 3\n4 5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2 1\n1 2\n2 3\n3 4\n", "output": "1\n2 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 5 0\n836096 600367 472071 200387 79763\n714679 505282 233544 157810 152591\n", "output": "10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2214
Solve the following coding problem using the programming language python: A binary matrix is called good if every even length square sub-matrix has an odd number of ones. Given a binary matrix $a$ consisting of $n$ rows and $m$ columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all. All the terms above have their usual meanings — refer to the Notes section for their formal definitions. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \leq n \leq m \leq 10^6$ and $n\cdot m \leq 10^6$)  — the number of rows and columns in $a$, respectively. The following $n$ lines each contain $m$ characters, each of which is one of 0 and 1. If the $j$-th character on the $i$-th line is 1, then $a_{i,j} = 1$. Similarly, if the $j$-th character on the $i$-th line is 0, then $a_{i,j} = 0$. -----Output----- Output the minimum number of cells you need to change to make $a$ good, or output $-1$ if it's not possible at all. -----Examples----- Input 3 3 101 001 110 Output 2 Input 7 15 000100001010010 100111010110001 101101111100100 010000111111010 111010010100001 000011001111101 111111011010011 Output -1 -----Note----- In the first case, changing $a_{1,1}$ to $0$ and $a_{2,2}$ to $1$ is enough. You can verify that there is no way to make the matrix in the second case good. Some definitions — A binary matrix is one in which every element is either $1$ or $0$. A sub-matrix is described by $4$ parameters — $r_1$, $r_2$, $c_1$, and $c_2$; here, $1 \leq r_1 \leq r_2 \leq n$ and $1 \leq c_1 \leq c_2 \leq m$. This sub-matrix contains all elements $a_{i,j}$ that satisfy both $r_1 \leq i \leq r_2$ and $c_1 \leq j \leq c_2$. A sub-matrix is, further, called an even length square if $r_2-r_1 = c_2-c_1$ and $r_2-r_1+1$ is divisible by $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 n, m = list(map(int, input().split())) l = [] for _ in range(n): l.append(input().strip()) def witch(s): out = 0 if s[0] != s[1]: out += 2 if s[1] != s[2]: out += 1 return out if n >= 4 and m >= 4: print(-1) else: if n < m: n, m = m, n l = [''.join([l[j][i] for j in range(m)]) for i in range(n)] if m == 1: print(0) elif m == 2: even = 0 odd = 0 first = l.pop(0) if first == '00' or first == '11': odd += 1 else: even += 1 for nex in l: if nex == '00' or nex == '11': odd, even = even + 1, odd else: odd, even = even, odd + 1 print(min(even, odd)) elif m == 3: #ee, eo, oe, oo = [0, 0, 0 ,0] ll = [0, 0, 0, 0] for nex in l: ll.reverse() ll[witch(nex)] += 1 print(n - max(ll)) ```
vfc_18610
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1391/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 3\n101\n001\n110\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 15\n000100001010010\n100111010110001\n101101111100100\n010000111111010\n111010010100001\n000011001111101\n111111011010011\n", "output": "-1", "type": "stdin_stdout" } ] }
apps
verifiable_code
2215
Solve the following coding problem using the programming language python: Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition. There are $n$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $i$-th position. Thus each of $n$ positions should contain exactly one flower: a rose or a lily. She knows that exactly $m$ people will visit this exhibition. The $i$-th visitor will visit all flowers from $l_i$ to $r_i$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies. Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible. -----Input----- The first line contains two integers $n$ and $m$ ($1\leq n, m\leq 10^3$) — the number of flowers and visitors respectively. Each of the next $m$ lines contains two integers $l_i$ and $r_i$ ($1\leq l_i\leq r_i\leq n$), meaning that $i$-th visitor will visit all flowers from $l_i$ to $r_i$ inclusive. -----Output----- Print the string of $n$ characters. The $i$-th symbol should be «0» if you want to put a rose in the $i$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any. -----Examples----- Input 5 3 1 3 2 4 2 5 Output 01100 Input 6 3 5 6 1 4 4 6 Output 110010 -----Note----- In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; in the segment $[1\ldots3]$, there are one rose and two lilies, so the beauty is equal to $1\cdot 2=2$; in the segment $[2\ldots4]$, there are one rose and two lilies, so the beauty is equal to $1\cdot 2=2$; in the segment $[2\ldots5]$, there are two roses and two lilies, so the beauty is equal to $2\cdot 2=4$. The total beauty is equal to $2+2+4=8$. In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; in the segment $[5\ldots6]$, there are one rose and one lily, so the beauty is equal to $1\cdot 1=1$; in the segment $[1\ldots4]$, there are two roses and two lilies, so the beauty is equal to $2\cdot 2=4$; in the segment $[4\ldots6]$, there are two roses and one lily, so the beauty is equal to $2\cdot 1=2$. The total beauty is equal to $1+4+2=7$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m = map(int, input().split()) ans = ["0"] * n for i in range(1, n, 2): ans[i] = "1" print("".join(ans)) ```
vfc_18614
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1004/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 3\n1 3\n2 4\n2 5\n", "output": "01010\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 3\n5 6\n1 4\n4 6\n", "output": "010101\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 4\n3 3\n1 6\n9 9\n10 10\n", "output": "0101010101\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1 1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 2\n1 6\n1 4\n", "output": "010101\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n1 2\n", "output": "01\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2216
Solve the following coding problem using the programming language python: Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y). Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x_1, y_1), (x_2, y_2), ..., (x_{r}, y_{r}), that: r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |x_{i} - x_{i} + 1| + |y_{i} - y_{i} + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner. -----Input----- The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly. -----Output----- Print k lines. In the i-th line print the description of the i-th tube: first print integer r_{i} (the number of tube cells), then print 2r_{i} integers x_{i}1, y_{i}1, x_{i}2, y_{i}2, ..., x_{ir}_{i}, y_{ir}_{i} (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists. -----Examples----- Input 3 3 3 Output 3 1 1 1 2 1 3 3 2 1 2 2 2 3 3 3 1 3 2 3 3 Input 2 3 1 Output 6 1 1 1 2 1 3 2 3 2 2 2 1 -----Note----- Picture for the first sample: [Image] Picture for the second sample: [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Round 252 Div 2 Problem C Author : chaotic_iak Language: Python 3.3.4 """ def read(mode=2): # 0: String # 1: List of strings # 2: List of integers inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return [int(x) for x in inputs.split()] def write(s="\n"): if isinstance(s, list): s = " ".join(s) s = str(s) print(s, end="") ################################################### SOLUTION n,m,k = read() cells = [] for i in range(n): if not i%2: for j in range(m): cells.append((i+1,j+1)) else: for j in range(m-1, -1, -1): cells.append((i+1,j+1)) ct = 0 for i in range(k-1): print(2, cells[ct][0], cells[ct][1], cells[ct+1][0], cells[ct+1][1]) ct += 2 print(len(cells) - ct, end=" ") for i in cells[ct:]: print(i[0], i[1], end=" ") ```
vfc_18618
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/441/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 3 3\n", "output": "3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3 1\n", "output": "6 1 1 1 2 1 3 2 3 2 2 2 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3 1\n", "output": "6 1 1 1 2 1 3 2 3 2 2 2 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2 2\n", "output": "2 1 1 1 2\n2 2 2 2 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3 3\n", "output": "2 1 1 1 2\n2 1 3 2 3\n2 2 2 2 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2217
Solve the following coding problem using the programming language python: You are given a positive integer $D$. Let's build the following graph from it: each vertex is a divisor of $D$ (not necessarily prime, $1$ and $D$ itself are also included); two vertices $x$ and $y$ ($x > y$) have an undirected edge between them if $x$ is divisible by $y$ and $\frac x y$ is a prime; the weight of an edge is the number of divisors of $x$ that are not divisors of $y$. For example, here is the graph for $D=12$: [Image] Edge $(4,12)$ has weight $3$ because $12$ has divisors $[1,2,3,4,6,12]$ and $4$ has divisors $[1,2,4]$. Thus, there are $3$ divisors of $12$ that are not divisors of $4$ — $[3,6,12]$. There is no edge between $3$ and $2$ because $3$ is not divisible by $2$. There is no edge between $12$ and $3$ because $\frac{12}{3}=4$ is not a prime. Let the length of the path between some vertices $v$ and $u$ in the graph be the total weight of edges on it. For example, path $[(1, 2), (2, 6), (6, 12), (12, 4), (4, 2), (2, 6)]$ has length $1+2+2+3+1+2=11$. The empty path has length $0$. So the shortest path between two vertices $v$ and $u$ is the path that has the minimal possible length. Two paths $a$ and $b$ are different if there is either a different number of edges in them or there is a position $i$ such that $a_i$ and $b_i$ are different edges. You are given $q$ queries of the following form: $v$ $u$ — calculate the number of the shortest paths between vertices $v$ and $u$. The answer for each query might be large so print it modulo $998244353$. -----Input----- The first line contains a single integer $D$ ($1 \le D \le 10^{15}$) — the number the graph is built from. The second line contains a single integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $v$ and $u$ ($1 \le v, u \le D$). It is guaranteed that $D$ is divisible by both $v$ and $u$ (both $v$ and $u$ are divisors of $D$). -----Output----- Print $q$ integers — for each query output the number of the shortest paths between the two given vertices modulo $998244353$. -----Examples----- Input 12 3 4 4 12 1 3 4 Output 1 3 1 Input 1 1 1 1 Output 1 Input 288807105787200 4 46 482955026400 12556830686400 897 414 12556830686400 4443186242880 325 Output 547558588 277147129 457421435 702277623 -----Note----- In the first example: The first query is only the empty path — length $0$; The second query are paths $[(12, 4), (4, 2), (2, 1)]$ (length $3+1+1=5$), $[(12, 6), (6, 2), (2, 1)]$ (length $2+2+1=5$) and $[(12, 6), (6, 3), (3, 1)]$ (length $2+2+1=5$). The third query is only the path $[(3, 1), (1, 2), (2, 4)]$ (length $1+1+1=3$). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): import sys input=sys.stdin.readline mod=998244353 N=10**5+3 fac=[1]*(N+1) for i in range(1,N+1): fac[i]=fac[i-1]*i%mod inv_fac=[1]*(N+1) inv_fac[N]=pow(fac[N],mod-2,mod) for i in range(N-1,0,-1): inv_fac[i]=inv_fac[i+1]*(i+1)%mod D=int(input()) A=[] for i in range(2,int(D**.5)+1): c=0 while D%i==0: D//=i c+=1 if c!=0: A.append(i) if D>=2: A.append(D) l=len(A) q=int(input()) for _ in range(q): u,v=map(int,input().split()) l1=[0]*l l2=[0]*l l3=[0]*l for i in range(l): while u%A[i]==0: l1[i]+=1 u//=A[i] while v%A[i]==0: l2[i]+=1 v//=A[i] l3[i]=l1[i]-l2[i] ans1=1 ans2=1 s1=0 s2=0 for i in range(l): if l3[i]>=0: ans1=ans1*inv_fac[l3[i]]%mod s1+=l3[i] else: ans2=ans2*inv_fac[-l3[i]]%mod s2-=l3[i] ans1=ans1*fac[s1]%mod ans2=ans2*fac[s2]%mod print(ans1*ans2%mod) def __starting_point(): main() __starting_point() ```
vfc_18622
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1334/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "12\n3\n4 4\n12 1\n3 4\n", "output": "1\n3\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n1 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "288807105787200\n4\n46 482955026400\n12556830686400 897\n414 12556830686400\n4443186242880 325\n", "output": "547558588\n277147129\n457421435\n702277623\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n4\n1 1\n1 2\n2 1\n2 2\n", "output": "1\n1\n1\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "18\n36\n1 1\n1 2\n1 3\n1 6\n1 9\n1 18\n2 1\n2 2\n2 3\n2 6\n2 9\n2 18\n3 1\n3 2\n3 3\n3 6\n3 9\n3 18\n6 1\n6 2\n6 3\n6 6\n6 9\n6 18\n9 1\n9 2\n9 3\n9 6\n9 9\n9 18\n18 1\n18 2\n18 3\n18 6\n18 9\n18 18\n", "output": "1\n1\n1\n2\n1\n3\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n2\n2\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n3\n1\n2\n1\n1\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "17592186044416\n1\n17592186044416 1\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2218
Solve the following coding problem using the programming language python: General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier. On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers. Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $\frac{n(n + 1)}{2}$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. -----Output----- Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 ≤ c_{i} ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. -----Examples----- Input 3 3 1 2 3 Output 1 1 1 2 2 3 2 Input 2 1 7 12 Output 1 12 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import random def S(L): Ans=[] s=0 for item in L: s+=item Ans.append(s) return Ans n,k=map(int,input().split()) B=list(map(int,input().split())) Sums=[] s=0 for i in range(n): s+=B[i] Sums.append(s) if(k<=n): for i in range(k): print(i+1,end="") for j in range(i+1): print(" "+str(B[j]),end="") print() else: Ans=[] length=1 Taken={} Sums=S(B) while(len(Ans)<k): if(length>n): length=1 random.shuffle(B) Sums=S(B) for i in range(n): if(i+length-1>=n): break x=Sums[i+length-1]-Sums[i]+B[i] if(x in Taken): continue Taken[x]=True L=[length] done=True for j in range(i,i+length): if(B[j] in L[1:]): done=False break L.append(B[j]) if(done): Ans.append(list(L)) length+=1 for i in range(k): item=Ans[i] print(item[0],end="") for z in range(1,len(item)): print(" "+str(item[z]),end="") print() ```
vfc_18626
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/246/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 3\n1 2 3\n", "output": "1 1\n1 2\n2 3 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n7 12\n", "output": "1 12 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1000\n", "output": "1 1000 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 8\n10 3 8 31 20\n", "output": "1 31 \n1 20 \n1 10 \n1 8 \n1 3 \n2 31 20 \n2 31 10 \n2 31 8 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2219
Solve the following coding problem using the programming language python: You are given an integer $n$ and an integer $k$. In one step you can do one of the following moves: decrease $n$ by $1$; divide $n$ by $k$ if $n$ is divisible by $k$. For example, if $n = 27$ and $k = 3$ you can do the following steps: $27 \rightarrow 26 \rightarrow 25 \rightarrow 24 \rightarrow 8 \rightarrow 7 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0$. You are asked to calculate the minimum number of steps to reach $0$ from $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of queries. The only line of each query contains two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $2 \le k \le 10^{18}$). -----Output----- For each query print the minimum number of steps to reach $0$ from $n$ in single line. -----Example----- Input 2 59 3 1000000000000000000 10 Output 8 19 -----Note----- Steps for the first test case are: $59 \rightarrow 58 \rightarrow 57 \rightarrow 19 \rightarrow 18 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0$. In the second test case you have to divide $n$ by $k$ $18$ times and then decrease $n$ by $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 i in range(t): n, k = list(map(int, input().split())) ans = 0 while n != 0: if n % k == 0: ans += 1 n //= k else: ans += n % k n -= n % k print(ans) ```
vfc_18630
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1175/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n59 3\n1000000000000000000 10\n", "output": "8\n19\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "93\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n", "output": "2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2220
Solve the following coding problem using the programming language python: There are $n$ emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The $i$-th emote increases the opponent's happiness by $a_i$ units (we all know that emotes in this game are used to make opponents happy). You have time to use some emotes only $m$ times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than $k$ times in a row (otherwise the opponent will think that you're trolling him). Note that two emotes $i$ and $j$ ($i \ne j$) such that $a_i = a_j$ are considered different. You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness. -----Input----- The first line of the input contains three integers $n, m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le k \le m \le 2 \cdot 10^9$) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is value of the happiness of the $i$-th emote. -----Output----- Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement. -----Examples----- Input 6 9 2 1 3 3 7 4 2 Output 54 Input 3 1000000000 1 1000000000 987654321 1000000000 Output 1000000000000000000 -----Note----- In the first example you may use emotes in the following sequence: $4, 4, 5, 4, 4, 5, 4, 4, 5$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,m,k = map(int,input().split()) a = list(map(int,input().split())) a = sorted(a) b1 = a[-1] b2 = a[-2] k+=1 total = m//k score = 0 score += (m%k) * b1 score += b1 * (total*(k-1)) score += b2 * total print (score) ```
vfc_18634
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1117/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 9 2\n1 3 3 7 4 2\n", "output": "54\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1000000000 1\n1000000000 987654321 1000000000\n", "output": "1000000000000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "69 10 2\n2 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\n", "output": "697\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 7 2\n6 7\n", "output": "47\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2221
Solve the following coding problem using the programming language python: You a captain of a ship. Initially you are standing in a point $(x_1, y_1)$ (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point $(x_2, y_2)$. You know the weather forecast — the string $s$ of length $n$, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side $s_1$, the second day — $s_2$, the $n$-th day — $s_n$ and $(n+1)$-th day — $s_1$ again and so on. Ship coordinates change the following way: if wind blows the direction U, then the ship moves from $(x, y)$ to $(x, y + 1)$; if wind blows the direction D, then the ship moves from $(x, y)$ to $(x, y - 1)$; if wind blows the direction L, then the ship moves from $(x, y)$ to $(x - 1, y)$; if wind blows the direction R, then the ship moves from $(x, y)$ to $(x + 1, y)$. The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction U and the ship moves the direction L, then from point $(x, y)$ it will move to the point $(x - 1, y + 1)$, and if it goes the direction U, then it will move to the point $(x, y + 2)$. You task is to determine the minimal number of days required for the ship to reach the point $(x_2, y_2)$. -----Input----- The first line contains two integers $x_1, y_1$ ($0 \le x_1, y_1 \le 10^9$) — the initial coordinates of the ship. The second line contains two integers $x_2, y_2$ ($0 \le x_2, y_2 \le 10^9$) — the coordinates of the destination point. It is guaranteed that the initial coordinates and destination point coordinates are different. The third line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the string $s$. The fourth line contains the string $s$ itself, consisting only of letters U, D, L and R. -----Output----- The only line should contain the minimal number of days required for the ship to reach the point $(x_2, y_2)$. If it's impossible then print "-1". -----Examples----- Input 0 0 4 6 3 UUU Output 5 Input 0 3 0 0 3 UDD Output 3 Input 0 0 0 1 1 L Output -1 -----Note----- In the first example the ship should perform the following sequence of moves: "RRRRU". Then its coordinates will change accordingly: $(0, 0)$ $\rightarrow$ $(1, 1)$ $\rightarrow$ $(2, 2)$ $\rightarrow$ $(3, 3)$ $\rightarrow$ $(4, 4)$ $\rightarrow$ $(4, 6)$. In the second example the ship should perform the following sequence of moves: "DD" (the third day it should stay in place). Then its coordinates will change accordingly: $(0, 3)$ $\rightarrow$ $(0, 3)$ $\rightarrow$ $(0, 1)$ $\rightarrow$ $(0, 0)$. In the third example the ship can never reach the point $(0, 1)$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) n = int(input()) s = input() psx = [0 for i in range(n)] psy = [0 for i in range(n)] if s[0] == 'L': psx[0] = -1 elif s[0] == 'R': psx[0] = +1 if s[0] == 'D': psy[0] = -1 elif s[0] == 'U': psy[0] = +1 for i in range(1, n): psx[i] = psx[i-1] psy[i] = psy[i-1] if s[i] == 'L': psx[i] += -1 elif s[i] == 'R': psx[i] += +1 if s[i] == 'D': psy[i] += -1 elif s[i] == 'U': psy[i] += +1 def valid(step): cycle = step // n rem = step % n x = x1 y = y1 x += psx[n-1] * cycle y += psy[n-1] * cycle if rem > 0: x += psx[rem-1] y += psy[rem-1] ManhattanDistance = abs(x - x2) + abs(y - y2) return (ManhattanDistance <= step) def binsearch(top, bot): res = -1 while top <= bot: mid = (top + bot) // 2 if valid(mid): res = mid bot = mid - 1 else: top = mid + 1 return res print(binsearch(0, 1000000000000000000)) ```
vfc_18638
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1117/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "0 0\n4 6\n3\nUUU\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 3\n0 0\n3\nUDD\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 0\n0 1\n1\nL\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2223
Solve the following coding problem using the programming language python: You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ — the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$. 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 = int(input()) if n % 2 != 0: print(-1) return links = [[1, set()] for i in range(1, n+1)] W = 0 L = 1 i = 0 while i < n-1: i += 1 [a, b] = [int(x) for x in input().split()] links[a-1][L].add(b-1) links[b-1][L].add(a-1) count = 0 sear = 0 cur = 0 while sear < n: li = cur l = links[li] if len(l[L]) != 1: if sear == cur: sear += 1 cur = sear continue mi = l[L].pop() m = links[mi] if l[W] % 2 == 0: count += 1 else: m[W] += 1 m[L].remove(li) if mi < sear: cur = mi else: sear += 1 cur = sear print(count) main() ```
vfc_18646
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/982/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n2 4\n4 1\n3 1\n", "output": "1", "type": "stdin_stdout" } ] }
apps
verifiable_code
2225
Solve the following coding problem using the programming language python: Xenia the beginner programmer has a sequence a, consisting of 2^{n} non-negative integers: a_1, a_2, ..., a_2^{n}. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a. Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a_1 or a_2, a_3 or a_4, ..., a_2^{n} - 1 or a_2^{n}, consisting of 2^{n} - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v. Let's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations (1, 2, 3, 4) → (1 or 2 = 3, 3 or 4 = 7) → (3 xor 7 = 4). The result is v = 4. You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional m queries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment a_{p} = b. After each query, you need to print the new value v for the new sequence a. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 10^5). The next line contains 2^{n} integers a_1, a_2, ..., a_2^{n} (0 ≤ a_{i} < 2^30). Each of the next m lines contains queries. The i-th line contains integers p_{i}, b_{i} (1 ≤ p_{i} ≤ 2^{n}, 0 ≤ b_{i} < 2^30) — the i-th query. -----Output----- Print m integers — the i-th integer denotes value v for sequence a after the i-th query. -----Examples----- Input 2 4 1 6 3 5 1 4 3 4 1 2 1 2 Output 1 3 3 3 -----Note----- For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #!/usr/bin/pypy3 from sys import stdin,stderr import random import cProfile def readInts(): return map(int,stdin.readline().strip().split()) def print_err(*args,**kwargs): print(*args,file=stderr,**kwargs) def solve(vs): return None def generate_tree(n,ns): out = [0 for _ in range(2**(n+1))] def gt(nix,left,right,op): if left+1==right: out[nix] = ns[left] return out[nix] mid = (left+right)//2 nL = nix*2+1 nR = nix*2+2 vL = gt(nL,left,mid,not op) vR = gt(nR,mid,right,not op) if op: v = vL ^ vR else: v = vL | vR out[nix] = v return v gt(0,0,2**n,n%2==0) return out def alter_tree2(n,t,p,b): def at(nix,width,offp,op): if width==1: t[nix]=b return b width //= 2 nL = nix*2+1 nR = nix*2+2 vL = t[nL] vR = t[nR] if offp >= width: vR = at(nR,width,offp-width,not op) else: vL = at(nL,width,offp,not op) if op: v = vL ^ vR else: v = vL | vR t[nix] = v return v at(0,2**n,p,n%2==0) def alter_tree(n,t,p,b): width = 2**n s = [] nix = 0 op = (n%2==0) while width>1: width //= 2 #print(nix) if p >= width: nix2 = 2*nix+2 s.append( (nix,nix2-1) ) p -= width else: nix2 = 2*nix+1 s.append( (nix,nix2+1) ) nix = nix2 op = not op #print(nix) t[nix] = b v = b while s: nix,nixO = s.pop() if op: v |= t[nixO] else: v ^= t[nixO] t[nix] = v op = not op return def run(): n,m = readInts() axs = list(readInts()) t = generate_tree(n,axs) for _ in range(m): p,b = readInts() alter_tree(n,t,p-1,b) print(t[0]) def test(): n = 17 ns = [] vs100 = list(range(100)) for _ in range(2**17): ns.append(random.choice(vs100)) t = generate_tree(n,ns) t2 = generate_tree(n,ns) for _ in range(100000): v1 = random.choice(vs100) v2 = random.choice(vs100) alter_tree(n,t,v1,v2) alter_tree2(n,t2,v1,v2) print(all(map(lambda x: x[0]==x[1],zip(t,t2)))) print(t[0]==t2[0]) run() ```
vfc_18654
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/339/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 4\n1 6 3 5\n1 4\n3 4\n1 2\n1 2\n", "output": "1\n3\n3\n3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1 1\n1 1\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2226
Solve the following coding problem using the programming language python: You are given a simple weighted connected undirected graph, consisting of $n$ vertices and $m$ edges. A path in the graph of length $k$ is a sequence of $k+1$ vertices $v_1, v_2, \dots, v_{k+1}$ such that for each $i$ $(1 \le i \le k)$ the edge $(v_i, v_{i+1})$ is present in the graph. A path from some vertex $v$ also has vertex $v_1=v$. Note that edges and vertices are allowed to be included in the path multiple times. The weight of the path is the total weight of edges in it. For each $i$ from $1$ to $q$ consider a path from vertex $1$ of length $i$ of the maximum weight. What is the sum of weights of these $q$ paths? Answer can be quite large, so print it modulo $10^9+7$. -----Input----- The first line contains a three integers $n$, $m$, $q$ ($2 \le n \le 2000$; $n - 1 \le m \le 2000$; $m \le q \le 10^9$) — the number of vertices in the graph, the number of edges in the graph and the number of lengths that should be included in the answer. Each of the next $m$ lines contains a description of an edge: three integers $v$, $u$, $w$ ($1 \le v, u \le n$; $1 \le w \le 10^6$) — two vertices $v$ and $u$ are connected by an undirected edge with weight $w$. The graph contains no loops and no multiple edges. It is guaranteed that the given edges form a connected graph. -----Output----- Print a single integer — the sum of the weights of the paths from vertex $1$ of maximum weights of lengths $1, 2, \dots, q$ modulo $10^9+7$. -----Examples----- Input 7 8 25 1 2 1 2 3 10 3 4 2 1 5 2 5 6 7 6 4 15 5 3 1 1 7 3 Output 4361 Input 2 1 5 1 2 4 Output 60 Input 15 15 23 13 10 12 11 14 12 2 15 5 4 10 8 10 2 4 10 7 5 3 10 1 5 6 11 1 13 8 9 15 4 4 2 9 11 15 1 11 12 14 10 8 12 3 6 11 Output 3250 Input 5 10 10000000 2 4 798 1 5 824 5 2 558 4 1 288 3 4 1890 3 1 134 2 3 1485 4 5 284 3 5 1025 1 2 649 Output 768500592 -----Note----- Here is the graph for the first example: [Image] Some maximum weight paths are: length $1$: edges $(1, 7)$ — weight $3$; length $2$: edges $(1, 2), (2, 3)$ — weight $1+10=11$; length $3$: edges $(1, 5), (5, 6), (6, 4)$ — weight $2+7+15=24$; length $4$: edges $(1, 5), (5, 6), (6, 4), (6, 4)$ — weight $2+7+15+15=39$; $\dots$ So the answer is the sum of $25$ terms: $3+11+24+39+\dots$ In the second example the maximum weight paths have weights $4$, $8$, $12$, $16$ and $20$. 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 #lev contains height from root,lower neighbour, higher neighbours #lev[0] contains 0 (because it is the root), higher neighbours (=neighbours) n,m,q=map(int,input().split()) mod=1000000007 mxw=0 wgts=[0]*n neig=[0]*n for i in range(n): neig[i]=[0] for i in range(m): a,b,w=map(int,input().split()) a-=1 b-=1 neig[a][0]+=1 neig[b][0]+=1 neig[a].append([b,w]) neig[b].append([a,w]) mxw=max(mxw,w) wgts[a]=max(wgts[a],w) wgts[b]=max(wgts[b],w) poss=[-1]*n poss[0]=0 sol=0 curw=0 hasmxw=[False]*n for i in range(n): if wgts[i]==mxw: hasmxw[i]=True ov=False l=0 while l<q and not ov and l<3000: newposs=[-1]*n for i in range(n): if poss[i]>=0: for j in range(1,neig[i][0]+1): newposs[neig[i][j][0]]=max(newposs[neig[i][j][0]],poss[i]+neig[i][j][1]) curmx=0 for i in range(n): poss[i]=newposs[i] if poss[i]>curmx: curmx=poss[i] ov=hasmxw[i] else: if poss[i]==curmx and hasmxw[i]: ov=True curw=curmx sol+=curw sol%=mod l+=1 if l==q: print(sol) else: if ov: rem=q-l sol+=rem*curw sol%=mod sol+=mxw*((rem*(rem+1))//2) sol%=mod print(sol) else: rem=q-l while not ov: mx=0 for i in range(n): if poss[i]==curw: mx=max(mx,wgts[i]) gd=-1 for i in range(n): if wgts[i]>mx and poss[i]>=0: diff=wgts[i]-mx loss=curw-poss[i] loss+=diff-1 att=loss//diff if gd==-1: gd=att gd=min(att,gd) if gd==-1 or gd>rem: sol+=rem*curw sol+=mx*((rem*(rem+1))//2) sol%=mod ov=True else: sol+=(gd-1)*curw sol+=mx*((gd*(gd-1))//2) sol%=mod for i in range(n): poss[i]+=gd*wgts[i] curw=max(curw,poss[i]) sol+=curw rem-=gd print(sol) ```
vfc_18658
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1366/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7 8 25\n1 2 1\n2 3 10\n3 4 2\n1 5 2\n5 6 7\n6 4 15\n5 3 1\n1 7 3\n", "output": "4361\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1 5\n1 2 4\n", "output": "60\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "15 15 23\n13 10 12\n11 14 12\n2 15 5\n4 10 8\n10 2 4\n10 7 5\n3 10 1\n5 6 11\n1 13 8\n9 15 4\n4 2 9\n11 15 1\n11 12 14\n10 8 12\n3 6 11\n", "output": "3250\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2227
Solve the following coding problem using the programming language python: Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. -----Input----- Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 10^6 characters. -----Output----- Print exactly one number — the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 -----Note----- In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s=input() ans=0 s=s.replace("heavy","1") s=s.replace("metal","2") heavy=[] metal=[] n=len(s) for i in range(n): if(s[i]=="1"): heavy.append(i) elif(s[i]=="2"): metal.append(i) n=len(heavy) nn=len(metal) x=0 l=nn for item in heavy: for i in range(x,nn): if(metal[i]>item): ans+=(nn-i) l=i break x=l; print(ans) ```
vfc_18662
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/318/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "heavymetalisheavymetal\n", "output": "3", "type": "stdin_stdout" } ] }
apps
verifiable_code
2228
Solve the following coding problem using the programming language python: During one of the space missions, humans have found an evidence of previous life at one of the planets. They were lucky enough to find a book with birth and death years of each individual that had been living at this planet. What's interesting is that these years are in the range $(1, 10^9)$! Therefore, the planet was named Longlifer. In order to learn more about Longlifer's previous population, scientists need to determine the year with maximum number of individuals that were alive, as well as the number of alive individuals in that year. Your task is to help scientists solve this problem! -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) — the number of people. Each of the following $n$ lines contain two integers $b$ and $d$ ($1 \le b \lt d \le 10^9$) representing birth and death year (respectively) of each individual. -----Output----- Print two integer numbers separated by blank character, $y$  — the year with a maximum number of people alive and $k$  — the number of people alive in year $y$. In the case of multiple possible solutions, print the solution with minimum year. -----Examples----- Input 3 1 5 2 4 5 6 Output 2 2 Input 4 3 4 4 5 4 6 8 10 Output 4 2 -----Note----- You can assume that an individual living from $b$ to $d$ has been born at the beginning of $b$ and died at the beginning of $d$, and therefore living for $d$ - $b$ years. 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()) mp = {} for _ in range(n): a, b = map(int, input().split()) mp[a] = mp.get(a, 0) + 1 mp[b] = mp.get(b, 0) - 1 cur = 0 maxi = 0 maxiy = 0 for i in sorted(mp): cur += mp[i] if cur > maxi: maxi = cur maxiy = i print(maxiy, maxi) ```
vfc_18666
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1424/G", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 5\n2 4\n5 6\n", "output": "2 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n3 4\n4 5\n4 6\n8 10\n", "output": "4 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1 1000000000\n", "output": "1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1 2\n", "output": "1 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2229
Solve the following coding problem using the programming language python: Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same length such that: b is lexicographically greater than or equal to a. b_{i} ≥ 2. b is pairwise coprime: for every 1 ≤ i < j ≤ n, b_{i} and b_{j} are coprime, i. e. GCD(b_{i}, b_{j}) = 1, where GCD(w, z) is the greatest common divisor of w and z. Ehab wants to choose a special array so he wants the lexicographically minimal array between all the variants. Can you find it? An array x is lexicographically greater than an array y if there exists an index i such than x_{i} > y_{i} and x_{j} = y_{j} for all 1 ≤ j < i. An array x is equal to an array y if x_{i} = y_{i} for all 1 ≤ i ≤ n. -----Input----- The first line contains an integer n (1 ≤ n ≤ 10^5), the number of elements in a and b. The second line contains n integers a_1, a_2, ..., a_{n} (2 ≤ a_{i} ≤ 10^5), the elements of a. -----Output----- Output n space-separated integers, the i-th of them representing b_{i}. -----Examples----- Input 5 2 3 5 4 13 Output 2 3 5 7 11 Input 3 10 3 7 Output 10 3 7 -----Note----- Note that in the second sample, the array is already pairwise coprime so we printed it. 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 def sieve(n): prime_factors = [[] for i in range(n+1)] tmp = [True] * (n+1) tmp[0] = tmp[1] = False for i in range(2, n+1): if tmp[i]: for k in range(i, n+1, i): tmp[k] = False prime_factors[k].append(i) return prime_factors n = int(sys.stdin.readline().strip()) a = list(map(int, sys.stdin.readline().strip().split(' '))) b = [] forbidden_factors = set({}) prime_factors = sieve(2000005) is_greater = False min_available = 2 for ai in a: factors = prime_factors[ai] if not is_greater: curr = ai ok = True for f in factors: if f in forbidden_factors: ok = False break if ok: for f in factors: forbidden_factors.add(f) else: is_greater = True ok = False while not ok: curr += 1 ok = True factors = prime_factors[curr] for f in factors: if f in forbidden_factors: ok = False break for f in factors: forbidden_factors.add(f) else: curr = min_available ok = True factors = prime_factors[curr] for f in factors: if f in forbidden_factors: ok = False break while not ok: curr += 1 ok = True factors = prime_factors[curr] for f in factors: if f in forbidden_factors: ok = False break min_available = curr for f in factors: forbidden_factors.add(f) b.append(str(curr)) print(" ".join(b)) ```
vfc_18670
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/959/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n2 3 5 4 13\n", "output": "2 3 5 7 11 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n10 3 7\n", "output": "10 3 7 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n7 10 2 5 5\n", "output": "7 10 3 11 13 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n20 9 7 6 7 9 15\n", "output": "20 9 7 11 13 17 19 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
2230
Solve the following coding problem using the programming language python: Gerald has n younger brothers and their number happens to be even. One day he bought n^2 candy bags. One bag has one candy, one bag has two candies, one bag has three candies and so on. In fact, for each integer k from 1 to n^2 he has exactly one bag with k candies. Help him give n bags of candies to each brother so that all brothers got the same number of candies. -----Input----- The single line contains a single integer n (n is even, 2 ≤ n ≤ 100) — the number of Gerald's brothers. -----Output----- Let's assume that Gerald indexes his brothers with numbers from 1 to n. You need to print n lines, on the i-th line print n integers — the numbers of candies in the bags for the i-th brother. Naturally, all these numbers should be distinct and be within limits from 1 to n^2. You can print the numbers in the lines in any order. It is guaranteed that the solution exists at the given limits. -----Examples----- Input 2 Output 1 4 2 3 -----Note----- The sample shows Gerald's actions if he has two brothers. In this case, his bags contain 1, 2, 3 and 4 candies. He can give the bags with 1 and 4 candies to one brother and the bags with 2 and 3 to the other brother. 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()) l = 0 r = n * n - 1 for i in range(n): for k in range(n // 2): print(l + 1, end = ' ') print(r + 1, end = ' ') l += 1 r -= 1 print() ```
vfc_18674
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/334/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n", "output": "1 4\n2 3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n", "output": "1 16 2 15\n3 14 4 13\n5 12 6 11\n7 10 8 9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n", "output": "1 36 2 35 3 34\n4 33 5 32 6 31\n7 30 8 29 9 28\n10 27 11 26 12 25\n13 24 14 23 15 22\n16 21 17 20 18 19\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n", "output": "1 64 2 63 3 62 4 61\n5 60 6 59 7 58 8 57\n9 56 10 55 11 54 12 53\n13 52 14 51 15 50 16 49\n17 48 18 47 19 46 20 45\n21 44 22 43 23 42 24 41\n25 40 26 39 27 38 28 37\n29 36 30 35 31 34 32 33\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n", "output": "1 100 2 99 3 98 4 97 5 96\n6 95 7 94 8 93 9 92 10 91\n11 90 12 89 13 88 14 87 15 86\n16 85 17 84 18 83 19 82 20 81\n21 80 22 79 23 78 24 77 25 76\n26 75 27 74 28 73 29 72 30 71\n31 70 32 69 33 68 34 67 35 66\n36 65 37 64 38 63 39 62 40 61\n41 60 42 59 43 58 44 57 45 56\n46 55 47 54 48 53 49 52 50 51\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "12\n", "output": "1 144 2 143 3 142 4 141 5 140 6 139\n7 138 8 137 9 136 10 135 11 134 12 133\n13 132 14 131 15 130 16 129 17 128 18 127\n19 126 20 125 21 124 22 123 23 122 24 121\n25 120 26 119 27 118 28 117 29 116 30 115\n31 114 32 113 33 112 34 111 35 110 36 109\n37 108 38 107 39 106 40 105 41 104 42 103\n43 102 44 101 45 100 46 99 47 98 48 97\n49 96 50 95 51 94 52 93 53 92 54 91\n55 90 56 89 57 88 58 87 59 86 60 85\n61 84 62 83 63 82 64 81 65 80 66 79\n67 78 68 77 69 76 70 75 71 74 72 73\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2231
Solve the following coding problem using the programming language python: You have $n$ sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks. Let $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. The chosen rectangle should have the value $\frac{P^2}{S}$ minimal possible. The value is taken without any rounding. If there are multiple answers, print any of them. Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($T \ge 1$) — the number of lists of sticks in the testcase. Then $2T$ lines follow — lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \le n \le 10^6$) — the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_j \le 10^4$) — lengths of the sticks in the $i$-th list. It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle. The total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase. -----Output----- Print $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order. If there are multiple answers, print any of them. -----Example----- Input 3 4 7 2 2 7 8 2 8 1 4 8 2 1 5 5 5 5 5 5 5 Output 2 7 7 2 2 2 1 1 5 5 5 5 -----Note----- There is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\frac{18^2}{14} \approx 23.143$. The second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\frac{6^2}{2} = 18$, $\frac{20^2}{16} = 25$ and $\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$. You can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys rd = lambda : sys.stdin.readline().rstrip() t = int(rd()) for _ in range(t): n = int(rd()) a = list(map(int, rd().split())) b = [] res_a, res_b = 1, 1e18 a = sorted(a) i = 0 while i < n-1: if a[i] == a[i+1]: b.append(a[i]) i += 1 i += 1 p2s = lambda x, y : (x+y)**2/(x*y) for i in range(len(b)-1): if p2s(res_a, res_b) > p2s(b[i], b[i+1]): res_a, res_b = b[i], b[i+1] print(res_a, res_a, res_b, res_b) ```
vfc_18678
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1027/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5\n", "output": "2 2 7 7\n1 1 2 2\n5 5 5 5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\n1 1 10000 10000\n", "output": "1 1 10000 10000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n5\n10 4 10 4 4\n4\n7 14 14 7\n6\n13 3 13 3 5 5\n5\n8 6 8 17 6\n6\n6 13 6 6 6 13\n7\n12 11 5 5 11 12 12\n16\n5 12 2 5 2 5 8 5 8 2 2 8 12 5 2 8\n17\n3 9 9 1 1 1 1 15 15 1 1 1 1 1 1 3 1\n12\n9 10 13 11 13 14 13 13 11 10 14 9\n4\n15 17 17 15\n", "output": "4 4 10 10\n7 7 14 14\n3 3 5 5\n6 6 8 8\n6 6 6 6\n11 11 12 12\n2 2 2 2\n1 1 1 1\n13 13 13 13\n15 15 17 17\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n20\n10 14 10 14 10 7 10 10 14 10 7 9 10 8 14 10 10 8 14 9\n12\n7 2 2 2 2 16 16 7 18 17 17 18\n5\n7 13 17 13 7\n6\n10 10 9 9 10 10\n6\n17 19 17 19 14 14\n10\n10 13 5 18 13 13 13 10 18 5\n11\n3 4 16 4 4 3 4 13 4 16 13\n7\n17 19 1 17 17 1 19\n5\n17 17 19 17 19\n9\n13 15 13 18 13 18 13 15 13\n", "output": "10 10 10 10\n2 2 2 2\n7 7 13 13\n10 10 10 10\n17 17 19 19\n13 13 13 13\n4 4 4 4\n17 17 19 19\n17 17 19 19\n13 13 13 13\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2232
Solve the following coding problem using the programming language python: You are given an undirected unweighted tree consisting of $n$ vertices. An undirected tree is a connected undirected graph with $n - 1$ edges. Your task is to choose two pairs of vertices of this tree (all the chosen vertices should be distinct) $(x_1, y_1)$ and $(x_2, y_2)$ in such a way that neither $x_1$ nor $y_1$ belong to the simple path from $x_2$ to $y_2$ and vice versa (neither $x_2$ nor $y_2$ should not belong to the simple path from $x_1$ to $y_1$). It is guaranteed that it is possible to choose such pairs for the given tree. Among all possible ways to choose such pairs you have to choose one with the maximum number of common vertices between paths from $x_1$ to $y_1$ and from $x_2$ to $y_2$. And among all such pairs you have to choose one with the maximum total length of these two paths. It is guaranteed that the answer with at least two common vertices exists for the given tree. The length of the path is the number of edges in it. The simple path is the path that visits each vertex at most once. -----Input----- The first line contains an integer $n$ — the number of vertices in the tree ($6 \le n \le 2 \cdot 10^5$). Each of the next $n - 1$ lines describes the edges of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects ($1 \le u_i, v_i \le n$, $u_i \ne v_i$). It is guaranteed that the given edges form a tree. It is guaranteed that the answer with at least two common vertices exists for the given tree. -----Output----- Print any two pairs of vertices satisfying the conditions described in the problem statement. It is guaranteed that it is possible to choose such pairs for the given tree. -----Examples----- Input 7 1 4 1 5 1 6 2 3 2 4 4 7 Output 3 6 7 5 Input 9 9 3 3 5 1 2 4 3 4 7 1 7 4 6 3 8 Output 2 9 6 8 Input 10 6 8 10 3 3 7 5 8 1 7 7 2 2 9 2 8 1 4 Output 10 6 4 5 Input 11 1 2 2 3 3 4 1 5 1 6 6 7 5 8 5 9 4 10 4 11 Output 9 11 8 10 -----Note----- The picture corresponding to the first example: [Image] The intersection of two paths is $2$ (vertices $1$ and $4$) and the total length is $4 + 3 = 7$. The picture corresponding to the second example: [Image] The intersection of two paths is $2$ (vertices $3$ and $4$) and the total length is $5 + 3 = 8$. The picture corresponding to the third example: $88^{\circ}$ The intersection of two paths is $3$ (vertices $2$, $7$ and $8$) and the total length is $5 + 5 = 10$. The picture corresponding to the fourth example: $\$ 8$ The intersection of two paths is $5$ (vertices $1$, $2$, $3$, $4$ and $5$) and the total length is $6 + 6 = 12$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys n = int(sys.stdin.readline()) edges = [[] for _ in range(n)] for _ in range(n - 1): i, j = tuple(int(k) for k in sys.stdin.readline().split()) i -= 1 j -= 1 edges[i].append(j) edges[j].append(i) # Prunes the graph starting from the vertices with # only 1 edge until we reach a vertex with 3+ edges. # Stores the distance from each non-pruned vertex # to each of the leaves it reaches. def prune(): pruned = [False for _ in range(n)] leaves = [[] for _ in range(n)] todo = [] for i in range(n): if len(edges[i]) == 1: todo.append((0, i, i)) while len(todo) > 0: d, i, j = todo.pop() pruned[j] = True for k in edges[j]: if not pruned[k]: if len(edges[k]) < 3: todo.append((d + 1, i, k)) else: leaves[k].append((d + 1, i)) return pruned, leaves pruned, leaves = prune() # Returns the furthest non-pruned vertices # from another non-pruned vertex. def furthest(i): assert not pruned[i] visited = list(pruned) top_distance = 0 top_vertices = [i] todo = [(0, i)] while len(todo) > 0: d, i = todo.pop() visited[i] = True if d > top_distance: top_distance = d top_vertices = [] if d == top_distance: top_vertices.append(i) for j in edges[i]: if not visited[j]: todo.append((d + 1, j)) return top_distance, top_vertices # Single center topology. # Only 1 vertex with 3+ edges. def solve_single_center(i): l = list(reversed(sorted(leaves[i])))[:4] return list(l[j][1] for j in range(4)) # Scores non-pruned vertices according to the sum # of the distances to their two furthest leaves. def vertices_score(v): scores = [] for i in v: assert not pruned[i] l = list(reversed(sorted(leaves[i])))[:2] score = (l[0][0] + l[1][0]), l[0][1], l[1][1] scores.append(score) return list(reversed(sorted(scores))) # Single cluster topology. # 1 cluster of vertices, all equally far away from each other. def solve_single_cluster(v): scores = vertices_score(v)[:2] return scores[0][1], scores[1][1], scores[0][2], scores[1][2] # Double cluster topology. # 2 clusters of vertices, pairwise equally far away from each other. def solve_double_cluster(v1, v2): scores1 = vertices_score(v1)[:1] scores2 = vertices_score(v2)[:1] return scores1[0][1], scores2[0][1], scores1[0][2], scores2[0][2] def solve(): def start_vertex(): for i in range(n): if not pruned[i]: return i i = start_vertex() distance, v1 = furthest(i) if distance == 0: return solve_single_center(v1[0]) else: distance, v1 = furthest(v1[0]) distance, v2 = furthest(v1[0]) v = list(set(v1) | set(v2)) if len(v) < len(v1) + len(v2): return solve_single_cluster(v) else: return solve_double_cluster(v1, v2) a, b, c, d = solve() print(a + 1, b + 1) print(c + 1, d + 1) ```
vfc_18682
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1073/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7\n1 4\n1 5\n1 6\n2 3\n2 4\n4 7\n", "output": "3 6\n7 5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9\n9 3\n3 5\n1 2\n4 3\n4 7\n1 7\n4 6\n3 8\n", "output": "2 9\n6 8\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2233
Solve the following coding problem using the programming language python: This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems. After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first. Ujan has two distinct strings $s$ and $t$ of length $n$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $i$ and $j$ ($1 \le i,j \le n$, the values $i$ and $j$ can be equal or different), and swaps the characters $s_i$ and $t_j$. Can he succeed? Note that he has to perform this operation exactly once. He has to perform this operation. -----Input----- The first line contains a single integer $k$ ($1 \leq k \leq 10$), the number of test cases. For each of the test cases, the first line contains a single integer $n$ ($2 \leq n \leq 10^4$), the length of the strings $s$ and $t$. Each of the next two lines contains the strings $s$ and $t$, each having length exactly $n$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. -----Output----- For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 4 5 souse houhe 3 cat dog 2 aa az 3 abc bca Output Yes No No No -----Note----- In the first test case, Ujan can swap characters $s_1$ and $t_4$, obtaining the word "house". In the second test case, it is not possible to make the strings equal using exactly one swap of $s_i$ and $t_j$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python k = int(input()) for i in range(k): n = int(input()) s1 = input() s2 = input() q = None for i in range(n): if s1[i] != s2[i]: if q is None: q = s1[i], s2[i] elif q == (s1[i], s2[i]): q = 1 else: print('No') break else: if q == 1: print('Yes')#isinstance(q, tuple): else: print('No') ```
vfc_18686
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1243/B1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca\n", "output": "Yes\nNo\nNo\nNo\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n11\nartiovnldnp\nartiovsldsp\n2\naa\nzz\n2\naa\nxy\n2\nab\nba\n2\nza\nzz\n3\nabc\nbca\n16\naajjhdsjfdsfkadf\naajjhjsjfdsfkajf\n2\nix\nii\n2\noo\nqo\n2\npp\npa\n", "output": "Yes\nYes\nNo\nNo\nNo\nNo\nYes\nNo\nNo\nNo\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2\nab\ncd\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\naacd\nbbdc\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\naabb\ncccc\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2\nab\ncc\n", "output": "No\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2234
Solve the following coding problem using the programming language python: We have a point $A$ with coordinate $x = n$ on $OX$-axis. We'd like to find an integer point $B$ (also on $OX$-axis), such that the absolute difference between the distance from $O$ to $B$ and the distance from $A$ to $B$ is equal to $k$. [Image] The description of the first test case. Since sometimes it's impossible to find such point $B$, we can, in one step, increase or decrease the coordinate of $A$ by $1$. What is the minimum number of steps we should do to make such point $B$ exist? -----Input----- The first line contains one integer $t$ ($1 \le t \le 6000$) — the number of test cases. The only line of each test case contains two integers $n$ and $k$ ($0 \le n, k \le 10^6$) — the initial position of point $A$ and desirable absolute difference. -----Output----- For each test case, print the minimum number of steps to make point $B$ exist. -----Example----- Input 6 4 0 5 8 0 1000000 0 0 1 0 1000000 1000000 Output 0 3 1000000 0 1 0 -----Note----- In the first test case (picture above), if we set the coordinate of $B$ as $2$ then the absolute difference will be equal to $|(2 - 0) - (4 - 2)| = 0$ and we don't have to move $A$. So the answer is $0$. In the second test case, we can increase the coordinate of $A$ by $3$ and set the coordinate of $B$ as $0$ or $8$. The absolute difference will be equal to $|8 - 0| = 8$, so the answer is $3$. [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for nt in range(int(input())): n,k = map(int,input().split()) if k<n: if n%2==0: if k%2: print (1) else: print (0) continue else: if k%2: print (0) else: print (1) continue print (k-n) ```
vfc_18690
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1401/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000\n", "output": "0\n3\n1000000\n0\n1\n0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1000000 0\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2235
Solve the following coding problem using the programming language python: A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: a ticket for one trip costs 20 byteland rubles, a ticket for 90 minutes costs 50 byteland rubles, a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip. -----Input----- The first line of input contains integer number n (1 ≤ n ≤ 10^5) — the number of trips made by passenger. Each of the following n lines contains the time of trip t_{i} (0 ≤ t_{i} ≤ 10^9), measured in minutes from the time of starting the system. All t_{i} are different, given in ascending order, i. e. t_{i} + 1 > t_{i} holds for all 1 ≤ i < n. -----Output----- Output n integers. For each trip, print the sum the passenger is charged after it. -----Examples----- Input 3 10 20 30 Output 20 20 10 Input 10 13 45 46 60 103 115 126 150 256 516 Output 20 20 10 0 20 0 0 20 20 10 -----Note----- In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only. 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 bisect input = sys.stdin.readline trips = int(input()) dyn = [int(input()) for i in range(trips)] cmap = [0,20] + [0 for i in range(trips-1)] for i in range(2,trips+1): cmap[i] = min(cmap[i-1] + 20, cmap[bisect.bisect_left(dyn,dyn[i-1]-89)] + 50, cmap[bisect.bisect_left(dyn,dyn[i-1]-1439)] + 120) for i in range(1,trips+1): print(cmap[i]-cmap[i-1]) ```
vfc_18694
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/756/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n10\n20\n30\n", "output": "20\n20\n10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n13\n45\n46\n60\n103\n115\n126\n150\n256\n516\n", "output": "20\n20\n10\n0\n20\n0\n0\n20\n20\n10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n100\n138\n279\n308\n396\n412\n821\n", "output": "20\n20\n20\n20\n20\n20\n0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2236
Solve the following coding problem using the programming language python: There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself. Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank. There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation. Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero. -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks. The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0. -----Output----- Print the minimum number of operations required to change balance in each bank to zero. -----Examples----- Input 3 5 0 -5 Output 1 Input 4 -1 0 1 0 Output 2 Input 4 1 2 3 -6 Output 3 -----Note----- In the first sample, Vasya may transfer 5 from the first bank to the third. In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first. In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import collections n = int(input()) A = list(map(int, input().split())) S = [0]*n S[0] = A[0] for i in range(1, n) : S[i] = S[i-1] + A[i] C = collections.Counter(S) max_val = 0 for key in C : max_val = max(max_val, C[key]) ans = n - max_val print(ans) ```
vfc_18698
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/675/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n5 0 -5\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2237
Solve the following coding problem using the programming language python: Kuro has just learned about permutations and he is really excited to create a new permutation type. He has chosen $n$ distinct positive integers and put all of them in a set $S$. Now he defines a magical permutation to be: A permutation of integers from $0$ to $2^x - 1$, where $x$ is a non-negative integer. The bitwise xor of any two consecutive elements in the permutation is an element in $S$. Since Kuro is really excited about magical permutations, he wants to create the longest magical permutation possible. In other words, he wants to find the largest non-negative integer $x$ such that there is a magical permutation of integers from $0$ to $2^x - 1$. Since he is a newbie in the subject, he wants you to help him find this value of $x$ and also the magical permutation for that $x$. -----Input----- The first line contains the integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of elements in the set $S$. The next line contains $n$ distinct integers $S_1, S_2, \ldots, S_n$ ($1 \leq S_i \leq 2 \cdot 10^5$) — the elements in the set $S$. -----Output----- In the first line print the largest non-negative integer $x$, such that there is a magical permutation of integers from $0$ to $2^x - 1$. Then print $2^x$ integers describing a magical permutation of integers from $0$ to $2^x - 1$. If there are multiple such magical permutations, print any of them. -----Examples----- Input 3 1 2 3 Output 2 0 1 3 2 Input 2 2 3 Output 2 0 2 1 3 Input 4 1 2 3 4 Output 3 0 1 3 2 6 7 5 4 Input 2 2 4 Output 0 0 Input 1 20 Output 0 0 Input 1 1 Output 1 0 1 -----Note----- In the first example, $0, 1, 3, 2$ is a magical permutation since: $0 \oplus 1 = 1 \in S$ $1 \oplus 3 = 2 \in S$ $3 \oplus 2 = 1 \in S$ Where $\oplus$ denotes bitwise xor operation. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def size(k): return int(math.log2(k)) def v2(k): if k%2==1: return 0 else: return 1+v2(k//2) n=int(input()) s=list(map(int,input().split())) import math s.sort() used=[] use=0 found={0:1} good=0 for guy in s: big=size(guy) if guy not in found: used.append(guy) use+=1 new=[] for boi in found: new.append(boi^guy) for guy in new: found[guy]=1 if use==big+1: good=use if good==0: print(0) print(0) else: useful=used[:good] perm=["0"] curr=0 for i in range(2**good-1): curr^=useful[v2(i+1)] perm.append(str(curr)) print(good) print(" ".join(perm)) ```
vfc_18702
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1163/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 2 3", "output": "2\n0 1 3 2", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n2 3", "output": "2\n0 2 1 3", "type": "stdin_stdout" } ] }
apps
verifiable_code
2238
Solve the following coding problem using the programming language python: Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n > 1) is an n × n matrix with a diamond inscribed into it. You are given an odd integer n. You need to draw a crystal of size n. The diamond cells of the matrix should be represented by character "D". All other cells of the matrix should be represented by character "*". Look at the examples to understand what you need to draw. -----Input----- The only line contains an integer n (3 ≤ n ≤ 101; n is odd). -----Output----- Output a crystal of size n. -----Examples----- Input 3 Output *D* DDD *D* Input 5 Output **D** *DDD* DDDDD *DDD* **D** Input 7 Output ***D*** **DDD** *DDDDD* DDDDDDD *DDDDD* **DDD** ***D*** 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()) magic=int((n-1)/2) for t in range(magic, -1, -1): print(t*'*'+'D'*(n-2*t)+t*'*') for u in range(1, magic+1): print(u*'*'+'D'*(n-2*u)+u*'*') ```
vfc_18706
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/454/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n", "output": "*D*\nDDD\n*D*\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n", "output": "**D**\n*DDD*\nDDDDD\n*DDD*\n**D**\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n", "output": "***D***\n**DDD**\n*DDDDD*\nDDDDDDD\n*DDDDD*\n**DDD**\n***D***\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "11\n", "output": "*****D*****\n****DDD****\n***DDDDD***\n**DDDDDDD**\n*DDDDDDDDD*\nDDDDDDDDDDD\n*DDDDDDDDD*\n**DDDDDDD**\n***DDDDD***\n****DDD****\n*****D*****\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "15\n", "output": "*******D*******\n******DDD******\n*****DDDDD*****\n****DDDDDDD****\n***DDDDDDDDD***\n**DDDDDDDDDDD**\n*DDDDDDDDDDDDD*\nDDDDDDDDDDDDDDD\n*DDDDDDDDDDDDD*\n**DDDDDDDDDDD**\n***DDDDDDDDD***\n****DDDDDDD****\n*****DDDDD*****\n******DDD******\n*******D*******\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "21\n", "output": "**********D**********\n*********DDD*********\n********DDDDD********\n*******DDDDDDD*******\n******DDDDDDDDD******\n*****DDDDDDDDDDD*****\n****DDDDDDDDDDDDD****\n***DDDDDDDDDDDDDDD***\n**DDDDDDDDDDDDDDDDD**\n*DDDDDDDDDDDDDDDDDDD*\nDDDDDDDDDDDDDDDDDDDDD\n*DDDDDDDDDDDDDDDDDDD*\n**DDDDDDDDDDDDDDDDD**\n***DDDDDDDDDDDDDDD***\n****DDDDDDDDDDDDD****\n*****DDDDDDDDDDD*****\n******DDDDDDDDD******\n*******DDDDDDD*******\n********DDDDD********\n*********DDD*********\n**********D**********\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2239
Solve the following coding problem using the programming language python: Mishka got a six-faced dice. It has integer numbers from $2$ to $7$ written on its faces (all numbers on faces are different, so this is an almost usual dice). Mishka wants to get exactly $x$ points by rolling his dice. The number of points is just a sum of numbers written at the topmost face of the dice for all the rolls Mishka makes. Mishka doesn't really care about the number of rolls, so he just wants to know any number of rolls he can make to be able to get exactly $x$ points for them. Mishka is very lucky, so if the probability to get $x$ points with chosen number of rolls is non-zero, he will be able to roll the dice in such a way. Your task is to print this number. It is guaranteed that at least one answer exists. Mishka is also very curious about different number of points to score so you have to answer $t$ independent queries. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 100$) — the number of queries. Each of the next $t$ lines contains one integer each. The $i$-th line contains one integer $x_i$ ($2 \le x_i \le 100$) — the number of points Mishka wants to get. -----Output----- Print $t$ lines. In the $i$-th line print the answer to the $i$-th query (i.e. any number of rolls Mishka can make to be able to get exactly $x_i$ points for them). It is guaranteed that at least one answer exists. -----Example----- Input 4 2 13 37 100 Output 1 3 8 27 -----Note----- In the first query Mishka can roll a dice once and get $2$ points. In the second query Mishka can roll a dice $3$ times and get points $5$, $5$ and $3$ (for example). In the third query Mishka can roll a dice $8$ times and get $5$ points $7$ times and $2$ points with the remaining roll. In the fourth query Mishka can roll a dice $27$ times and get $2$ points $11$ times, $3$ points $6$ times and $6$ points $10$ times. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math, sys def mp(): return list(map(int, input().split())) def main(): n = int(input()) for i in range(n): x = int(input()) print(x // 2) deb = 0 if deb: file = open('input.txt', 'w') else: input = sys.stdin.readline main() ```
vfc_18710
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1093/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n2\n13\n37\n100\n", "output": "1\n6\n18\n50\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2240
Solve the following coding problem using the programming language python: One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a signal $s$ towards a faraway galaxy. Recently they've received a response $t$ which they believe to be a response from aliens! The scientists now want to check if the signal $t$ is similar to $s$. The original signal $s$ was a sequence of zeros and ones (everyone knows that binary code is the universe-wide language). The returned signal $t$, however, does not look as easy as $s$, but the scientists don't give up! They represented $t$ as a sequence of English letters and say that $t$ is similar to $s$ if you can replace all zeros in $s$ with some string $r_0$ and all ones in $s$ with some other string $r_1$ and obtain $t$. The strings $r_0$ and $r_1$ must be different and non-empty. Please help Arkady's friend and find the number of possible replacements for zeros and ones (the number of pairs of strings $r_0$ and $r_1$) that transform $s$ to $t$. -----Input----- The first line contains a string $s$ ($2 \le |s| \le 10^5$) consisting of zeros and ones — the original signal. The second line contains a string $t$ ($1 \le |t| \le 10^6$) consisting of lowercase English letters only — the received signal. It is guaranteed, that the string $s$ contains at least one '0' and at least one '1'. -----Output----- Print a single integer — the number of pairs of strings $r_0$ and $r_1$ that transform $s$ to $t$. In case there are no such pairs, print $0$. -----Examples----- Input 01 aaaaaa Output 4 Input 001 kokokokotlin Output 2 -----Note----- In the first example, the possible pairs $(r_0, r_1)$ are as follows: "a", "aaaaa" "aa", "aaaa" "aaaa", "aa" "aaaaa", "a" The pair "aaa", "aaa" is not allowed, since $r_0$ and $r_1$ must be different. In the second example, the following pairs are possible: "ko", "kokotlin" "koko", "tlin" 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 math import * def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def add(a,b): return (a+b)%1000000007 def sub(a,b): return (a+1000000007-b)%1000000007 def mul(a,b): return (a*b)%1000000007 p = 102367 s = list(map(int,minp())) t = list(map(ord,minp())) h = [0]*(len(t)+1) pp = [1]*(len(t)+1) for i in range(len(t)): h[i+1] = add(mul(h[i], p), t[i]) pp[i+1] = mul(pp[i], p) def cmp(a, b, l): if a > b: a, b = b, a h1 = sub(h[a+l], mul(h[a], pp[l])) h2 = sub(h[b+l], mul(h[b], pp[l])) return h2 == h1 c = [0,0] idx = [-1,-1] for i in range(len(s)): c[s[i]] += 1 if idx[s[i]] < 0: idx[s[i]] = i Mv = max(c) mv = min(c) Mi = c.index(Mv) mi = (Mi^1) lt = len(t) sp = [0,0] res = 0 for k in range(1,lt//Mv+1): l = [0,0] x = (lt-k*Mv)//mv if x > 0 and x*mv + k*Mv == lt: l[Mi] = k l[mi] = x if idx[0] < idx[1]: sp[0] = 0 sp[1] = idx[1]*l[0] else: sp[1] = 0 sp[0] = idx[0]*l[1] ok = True j = 0 for i in range(len(s)): if not cmp(sp[s[i]], j, l[s[i]]): ok = False break j += l[s[i]] if l[0] == l[1] and cmp(sp[0], sp[1], l[0]): ok = False if ok: res += 1 print(res) ```
vfc_18714
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1056/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "01\naaaaaa\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2241
Solve the following coding problem using the programming language python: Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much. A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ a_{i}; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal b_{i}. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes x_{i} and y_{i} (x_{i} + y_{i} = b_{i}) correspondingly, Sereja's joy rises by x_{i}·y_{i}. Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song! -----Input----- The first line of the input contains integer n (1 ≤ n ≤ 10^5) — the number of notes in the song. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6). The third line contains n integers b_{i} (1 ≤ b_{i} ≤ 10^6). -----Output----- In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song. -----Examples----- Input 3 1 1 2 2 2 3 Output 4 Input 1 2 5 Output -1 -----Note----- In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4. In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, a, b = input(), map(int, input().split()), map(int, input().split()) print(sum((y // 2) * (y - y // 2) if y > 1 and 2 * x >= y else -1 for x, y in zip(a, b))) ```
vfc_18718
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/390/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 1 2\n2 2 3\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2245
Solve the following coding problem using the programming language python: Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one). Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the chip is currently in the cell i, the player can move it into cell i - 1, i - 2 or i - k). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it k cells to the left if the current cell has number i < k. The player who can't make a move loses the game. Who wins if both participants play optimally? Alice and Bob would like to play several games, so you should determine the winner in each game. -----Input----- The first line contains the single integer T (1 ≤ T ≤ 100) — the number of games. Next T lines contain one game per line. All games are independent. Each of the next T lines contains two integers n and k (0 ≤ n ≤ 10^9, 3 ≤ k ≤ 10^9) — the length of the strip and the constant denoting the third move, respectively. -----Output----- For each game, print Alice if Alice wins this game and Bob otherwise. -----Example----- Input 4 0 3 3 3 3 4 4 4 Output Bob Alice Bob Alice The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys T = int(sys.stdin.readline().strip()) for t in range(0, T): n, k = list(map(int, sys.stdin.readline().strip().split())) if k % 3 != 0: if n % 3 == 0: print("Bob") else: print("Alice") else: n = n % (k + 1) if n == k: print("Alice") elif n % 3 == 0: print("Bob") else: print("Alice") ```
vfc_18734
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1194/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n0 3\n3 3\n3 4\n4 4\n", "output": "Bob\nAlice\nBob\nAlice\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n73 18\n", "output": "Alice\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n25 6\n", "output": "Alice\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "24\n19 9\n20 9\n21 9\n22 9\n23 9\n24 9\n25 9\n26 9\n27 9\n28 9\n29 9\n30 9\n31 9\n32 9\n1000000000 999999999\n999999996 999999999\n999999997 999999999\n1000000000 6\n1000000000 3\n1000000000 9\n999999999 6\n999999996 6\n999999992 6\n999999999 9\n", "output": "Alice\nBob\nAlice\nAlice\nBob\nAlice\nAlice\nBob\nAlice\nAlice\nAlice\nBob\nAlice\nAlice\nBob\nBob\nAlice\nAlice\nBob\nBob\nAlice\nAlice\nAlice\nAlice\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n24 6\n", "output": "Bob\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
2247
Solve the following coding problem using the programming language python: There is a special offer in Vasya's favourite supermarket: if the customer buys $a$ chocolate bars, he or she may take $b$ additional bars for free. This special offer can be used any number of times. Vasya currently has $s$ roubles, and he wants to get as many chocolate bars for free. Each chocolate bar costs $c$ roubles. Help Vasya to calculate the maximum possible number of chocolate bars he can get! -----Input----- The first line contains one integer $t$ ($1 \le t \le 100$) — the number of testcases. Each of the next $t$ lines contains four integers $s, a, b, c~(1 \le s, a, b, c \le 10^9)$ — the number of roubles Vasya has, the number of chocolate bars you have to buy to use the special offer, the number of bars you get for free, and the cost of one bar, respectively. -----Output----- Print $t$ lines. $i$-th line should contain the maximum possible number of chocolate bars Vasya can get in $i$-th test. -----Example----- Input 2 10 3 1 1 1000000000 1 1000000000 1 Output 13 1000000001000000000 -----Note----- In the first test of the example Vasya can buy $9$ bars, get $3$ for free, buy another bar, and so he will get $13$ bars. In the second test Vasya buys $1000000000$ bars and gets $1000000000000000000$ for free. So he has $1000000001000000000$ bars. 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())): s, a, b, c = list(map(int, input().split())) x = s // c x += b * (x // a) print(x) ```
vfc_18742
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1065/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n10 3 1 1\n1000000000 1 1000000000 1\n", "output": "13\n1000000001000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n19260817 1 1 1\n", "output": "38521634\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "16\n1 1 1 1\n1 1 1 1000000000\n1 1 1000000000 1\n1 1 1000000000 1000000000\n1 1000000000 1 1\n1 1000000000 1 1000000000\n1 1000000000 1000000000 1\n1 1000000000 1000000000 1000000000\n1000000000 1 1 1\n1000000000 1 1 1000000000\n1000000000 1 1000000000 1\n1000000000 1 1000000000 1000000000\n1000000000 1000000000 1 1\n1000000000 1000000000 1 1000000000\n1000000000 1000000000 1000000000 1\n1000000000 1000000000 1000000000 1000000000\n", "output": "2\n0\n1000000001\n0\n1\n0\n1\n0\n2000000000\n2\n1000000001000000000\n1000000001\n1000000001\n1\n2000000000\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n19260818 1 1 1\n", "output": "38521636\n", "type": "stdin_stdout" } ] }