id
stringlengths
14
117
description
stringlengths
29
13k
code
stringlengths
10
49.8k
test_samples
dict
source
class label
3 classes
prompt
stringlengths
391
104k
1084_E. The Fair Nut and Strings_24588
Recently, the Fair Nut has written k strings of length n, consisting of letters "a" and "b". He calculated c — the number of strings that are prefixes of at least one of the written strings. Every string was counted only one time. Then, he lost his sheet with strings. He remembers that all written strings were lexicog...
def solve(n, k, start, end): tot = 0 lvl = 1 for i in range(n): lvl += lvl - int(start[i] == 'b') - int(end[i] == 'a') if lvl >= k: tot += k * (n-i) break else: tot += lvl return tot n, k = map(int, input().split()) start = input() end =...
{ "input": [ "2 4\naa\nbb\n", "4 5\nabbb\nbaaa\n", "3 3\naba\nbba\n", "100 10000\nababbabaaabababbabbaaabbbaaabbbbbbaabbbbaabbabbaabbbaaaaaaababaabaaaaaaaababbaabbbaabaababbbaababbba\nbbbbabbabbbbbabbbbbbbbabaaabbbbababbababaabaaabbbabbababbabaabbababbabbaaaaabbababaaababbaabaaaababb\n", "10 100\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recently, the Fair Nut has written k strings of length n, consisting of letters "a" and "b". He calculated c — the number of strings that are prefixes of at least one of the written s...
1104_B. Game with string_24592
Two people are playing a game with a string s, consisting of lowercase latin letters. On a player's turn, he should choose two consecutive equal letters in the string and delete them. For example, if the string is equal to "xaax" than there is only one possible turn: delete "aa", so the string will become "xx". A p...
s = input() stack = [] l = "" t = 0 for x in s: if x == l: t += 1 stack.pop(-1) if len(stack) > 0: l = stack[-1] else: l = "" else: stack.append(x) l = x if t % 2 == 0: print("No") else: print("Yes")
{ "input": [ "iiq\n", "abba\n", "abacaba\n", "aa\n", "axxxxbbaaa\n", "pogfjssywv\n", "caabbcd\n", "baaaab\n", "cxxbbc\n", "a\n", "caabbc\n", "abbbbbabba\n", "`a\n", "aaabbxxxxa\n", "pogfjssywu\n", "dcbbaac\n", "baaabb\n", "cbbxxc\n", "`\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Two people are playing a game with a string s, consisting of lowercase latin letters. On a player's turn, he should choose two consecutive equal letters in the string and delete the...
1152_C. Neko does Maths_24598
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers a and b. His goal is to find a non-negative integer k such that the least common multiple of a+k and b+k is the smallest possible. If there are multiple optimal integers k, he needs ...
a, b = map(int, input().split()) q = abs(a-b) if q == 0: print(0) else: dq = [] dq2 = [] for i in range(1, int(q**0.5)+1): if not q%i: dq.append(i) if i != q//i: dq2 = [q//i] + dq2 dq = dq + dq2 dq = dq[::-1] if a < b: a, b = b, a d...
{ "input": [ "6 10\n", "5 10\n", "21 31\n", "823409948 63146277\n", "509022792 57335\n", "173726711 47100867\n", "55138 453739731\n", "986866706 981520552\n", "24 9\n", "2 3\n", "21 1\n", "904189980 653467036\n", "21184942 66889\n", "999999973 800000007\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers a and b. His goal is to find a non-negative i...
1174_A. Ehab Fails to Be Thanos_24602
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements? Input The first line contains an integer n (1 ≤ n ≤ 1000), where 2n is the number of elements in the array a. The second line contains 2n space-separat...
n = int(input()) a = list(map(int, input().split())) a.sort() if sum(a[:n]) < sum(a[n:]): print(*a) else: print(-1)
{ "input": [ "3\n1 2 2 1 3 1\n", "1\n1 1\n", "1\n2 1\n", "4\n1 1 1 1 3 3 3 3\n", "2\n2 3 4 3\n", "2\n5 9 4 8\n", "10\n859983 998985 909455 4688 549610 325108 492856 2751 734117 162579 97740 703983 356830 17839 881224 458713 233429 269974 351504 595389\n", "2\n2 2 2 3\n", "97\n7 8 5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements? Input The first...
1191_F. Tokitsukaze and Strange Rectangle_24606
There are n points on the plane, the i-th of which is at (x_i, y_i). Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area. The strange area is enclosed by three lines, x = l, y = a and x = r, as its left side, its bottom side and its right side respectively, where l, r and a can be ...
from sys import stdin, stdout from collections import defaultdict n = int(input()) pts = [] for i in range(n): x, y = map(int, stdin.readline().split()) pts.append((x, y)) pts.sort() cts = [0]*(n+1) x_map = {} ctr = 0 for p in pts: if p[0] not in x_map: ctr += 1 x_map[p[0]] = ctr cts[c...
{ "input": [ "3\n1 1\n1 2\n1 3\n", "4\n2 1\n2 2\n3 1\n3 2\n", "3\n1 1\n2 1\n3 1\n", "30\n9 1\n1 6\n2 10\n6 8\n5 5\n5 10\n8 6\n10 8\n7 7\n8 5\n6 4\n1 5\n8 4\n4 6\n7 3\n5 1\n9 2\n5 6\n5 9\n10 9\n7 8\n5 2\n6 3\n10 5\n9 8\n10 2\n3 5\n7 10\n10 6\n2 6\n", "35\n8 1\n6 8\n4 1\n1 2\n1 4\n5 10\n10 8\n9 1\n9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n points on the plane, the i-th of which is at (x_i, y_i). Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area. The strange area is enc...
1230_C. Anadi and Domino_24612
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi ha...
n, m = map(int, input().split()) edges = [[int(x) - 1 for x in input().split()] for _ in range(m)] p = [6 ** i for i in range(n)] res = 0 for mask in range(6 ** n): colors = [0] + [(mask // p[i]) % 6 for i in range(n)] used = set(tuple(sorted([colors[u], colors[v]])) for u, v in edges) res = max(res, len(us...
{ "input": [ "7 21\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n2 3\n2 4\n2 5\n2 6\n2 7\n3 4\n3 5\n3 6\n3 7\n4 5\n4 6\n4 7\n5 6\n5 7\n6 7\n", "7 0\n", "4 4\n1 2\n2 3\n3 4\n4 1\n", "3 1\n1 3\n", "2 0\n", "5 10\n1 2\n3 4\n1 3\n2 3\n5 4\n5 1\n4 1\n5 3\n5 2\n2 4\n", "3 2\n2 3\n1 3\n", "7 9\n2 6\n7 4\n2 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≤ a ≤ b ≤ 6, there is exactly one domino with a dots on one ha...
1271_E. Common Number_24618
At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$ We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventua...
n, k = map(int,input().split()) p = 1 w = 0 while p * 2 <= k: p *= 2 w += 1 kk = k - p #2**w * tr <= n - kk odd = (n-kk)//2**w if odd % 2 == 0: odd -= 1 p = 1 w = 0 while 2 * (2*p - 1) < k: p *= 2 w += 1 poz = k - 2*(p-1) if poz == 0: #2**(w-1) * strzal + (2**w-1) <= n even = (n-(2**w-1))//2**(w-1) if even % ...
{ "input": [ "11 3\n", "1000000 100\n", "20 20\n", "11 6\n", "14 5\n", "503953551519879441 107364070317088317\n", "740983904691688399 3157918256124620\n", "745941355170254351 284128592904320663\n", "502333439 2047\n", "500648583102513041 1\n", "17276479 31\n", "76984596...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \e...
1294_F. Three Paths on a Tree_24622
You are given an unweighted tree with n vertices. Recall that a tree is a connected undirected graph without cycles. Your task is to choose three distinct vertices a, b, c on this tree such that the number of edges which belong to at least one of the simple paths between a and b, b and c, or a and c is the maximum pos...
import sys input = sys.stdin.readline n=int(input()) E=[[] for i in range(n+1)] for i in range(n-1): x,y=map(int,input().split()) E[x].append(y) E[y].append(x) def dfs(t): L=[-1]*(n+1) L[t]=0 Q=[t] while Q: x=Q.pop() for to in E[x]: if L[to]==-1: ...
{ "input": [ "8\n1 2\n2 3\n3 4\n4 5\n4 6\n3 7\n3 8\n", "7\n5 1\n6 1\n2 1\n1 3\n1 7\n1 4\n", "9\n1 8\n6 9\n6 7\n4 3\n3 5\n8 7\n2 6\n9 3\n", "5\n3 1\n2 1\n1 5\n1 4\n", "5\n2 5\n2 1\n3 2\n4 3\n", "6\n6 4\n4 1\n5 6\n3 2\n4 2\n", "9\n1 2\n9 8\n5 8\n7 5\n6 2\n5 3\n1 4\n1 5\n", "8\n2 1\n2 8\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an unweighted tree with n vertices. Recall that a tree is a connected undirected graph without cycles. Your task is to choose three distinct vertices a, b, c on this tr...
1337_B. Kana and Dragon Quest game_24627
Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon. <image> The dragon has a hit p...
import math t=int(input()) for i in range(t): x,n,m=map(int,input().split()) if(x-m*10<=0): print("YES") else: s=x for i in range(n): j=math.floor(s/2)+10 # print(x," ",j) if(j>s): break else: s=j if(s-(m*10)<=0): print("YES") else: print("NO")
{ "input": [ "7\n100 3 4\n189 3 4\n64 2 3\n63 2 3\n30 27 7\n10 9 1\n69117 21 2\n", "1\n1 0 0\n", "5\n1 30 0\n1 0 0\n100000 30 30\n10 1 1\n8 30 1\n", "1\n21 1 2\n", "1\n1 0 1\n", "5\n1 30 0\n1 0 0\n100000 30 20\n10 1 1\n8 30 1\n", "7\n100 3 2\n189 3 4\n64 2 3\n63 2 3\n30 27 7\n10 9 1\n69117...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana g...
137_A. Postcards and photos_24633
Polycarpus has postcards and photos hung in a row on the wall. He decided to put them away to the closet and hang on the wall a famous painter's picture. Polycarpus does it like that: he goes from the left to the right and removes the objects consecutively. As Polycarpus doesn't want any mix-ups to happen, he will not ...
import math l = list(map(str, input())) t = 0 while len(l) != 0: c = 0 b = l[0] x = 1 while True: if x == len(l): t += math.ceil(x / 5) l = [] break elif b == l[x]: x += 1 else: t += math.ceil(x/5) l = l[x:] ...
{ "input": [ "CPCPCPC\n", "CCCCCCCCCC\n", "CCCCCCPPCPPPPPPPPPP\n", "CCCCCCPPPPPP\n", "P\n", "PPPPPPPPPPPPPPPPPPPPPPPCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCPPPPPCCCPPPPCCCCCPCC\n", "CPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPCPC...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarpus has postcards and photos hung in a row on the wall. He decided to put them away to the closet and hang on the wall a famous painter's picture. Polycarpus does it like that:...
13_A. Numbers_24637
Little Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 18. Now he wonders what is an average value of sum of digits of the number A written in all bases from 2 to A - 1. Note that all computa...
import math def sumInBase(num, base): ans = 0 while num != 0: ans += num % base num //= base return ans A = int(input()) sumofAll = 0 for i in range(2, A): sumofAll += sumInBase(A, i) temp = math.gcd(sumofAll, A - 2) print('{}/{}'.format(sumofAll // temp, (A - 2) // temp))
{ "input": [ "3\n", "5\n", "31\n", "251\n", "277\n", "454\n", "733\n", "948\n", "131\n", "668\n", "197\n", "260\n", "305\n", "28\n", "406\n", "687\n", "417\n", "189\n", "141\n", "837\n", "659\n", "959\n", "514\n", "766\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 1...
1423_J. Bubble Cup hypothesis_24641
The Bubble Cup hypothesis stood unsolved for 130 years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem: Given a number m, how many polynomials P with coefficients in set {\{0,1,2,3,4,5,...
T = input() mod = int(1e9 + 7) a = map(int, input().split()) c = [] for n in a: b = (n // 2 + 2) b = b * b b //= 4 c.append(str(b % mod)) print(' '.join(c))
{ "input": [ "2\n2 4\n", "5\n3 7 3 4 7\n", "100\n751 557 779 329 39 213 844 289 137 50 951 284 671 474 829 906 736 395 366 22 133 418 552 649 636 109 974 775 852 971 384 945 335 961 472 651 335 543 560 135 85 952 558 838 147 644 688 727 940 991 309 705 409 27 774 951 92 277 835 804 589 103 529 11 304 171 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Bubble Cup hypothesis stood unsolved for 130 years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry...
1443_C. The Delivery Dilemma_24645
Petya is preparing for his birthday. He decided that there would be n different dishes on the dinner table, numbered from 1 to n. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his ord...
from itertools import accumulate for _ in range(int(input())): n, delivery, pickup = int(input()), [*map(int, input().split())], [*map(int, input().split())] dp = sorted(zip(delivery, pickup)) new_delivery = [i[0] for i in dp] new_pickup = [i[1] for i in dp] new_pickup_acc = [*accumulate(new_pickup)...
{ "input": [ "4\n4\n3 7 4 5\n2 1 2 4\n4\n1 2 3 4\n3 3 3 3\n2\n1 2\n10 10\n2\n10 10\n1 2\n", "4\n4\n3 7 4 5\n2 1 2 4\n4\n1 2 3 4\n3 3 3 5\n2\n1 2\n10 10\n2\n10 10\n1 2\n", "4\n4\n3 7 4 2\n2 1 2 4\n4\n1 2 3 4\n3 3 3 3\n2\n1 2\n10 10\n2\n10 10\n1 2\n", "4\n4\n3 7 4 5\n3 1 2 4\n4\n1 2 3 4\n3 3 3 5\n2\n0 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Petya is preparing for his birthday. He decided that there would be n different dishes on the dinner table, numbered from 1 to n. Since Petya doesn't like to cook, he wants to order t...
1468_L. Prime Divisors Selection_24648
Suppose you have a sequence of k integers A = [a_1, a_2, ... , a_k] where each a_i ≥ 2. A sequence of prime integers P = [p_1, p_2, ..., p_k] is called suitable for the sequence A if a_1 is divisible by p_1, a_2 is divisible by p_2 and so on. A sequence of prime integers P is called friendly if there are no unique in...
from math import gcd def t_prime(n): if n == 1: return False for p in [2,3,5,7,11,13,17,19,23]: if p < n and pow(p,n-1,n) != 1: return False return True primes = [i for i in range(2,10**5) if t_prime(i)];pset = set(primes);n, k = map(int, input().split());l = list(map(int, input()....
{ "input": [ "3 3\n2 4 6\n", "4 3\n2 4 6 16\n", "3 3\n2 4 16\n", "100 9\n6970 1369 8094 169 4774 125 6438 3306 8170 9222 6402 9230 2209 9842 3094 59 4096 8554 9790 8142 7810 7917 7310 9345 2010 2809 9804 4913 8930 361 1849 6222 17 7566 6726 4270 3481 6486 7665 9646 5329 2197 7462 6889 5934 6561 2226 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Suppose you have a sequence of k integers A = [a_1, a_2, ... , a_k] where each a_i ≥ 2. A sequence of prime integers P = [p_1, p_2, ..., p_k] is called suitable for the sequence A if ...
1493_F. Enchanted Matrix_24651
This is an interactive problem. There exists a matrix a of size n × m (n rows and m columns), you know only numbers n and m. The rows of the matrix are numbered from 1 to n from top to bottom, and columns of the matrix are numbered from 1 to m from left to right. The cell on the intersection of the x-th row and the y-...
import sys input = sys.stdin.readline flush = sys.stdout.flush from collections import Counter from math import sqrt def query(h, w, i1, j1, i2, j2): print("? {} {} {} {} {} {}".format(h, w, i1, j1, i2, j2)) flush() return int(input()) def fac(x): cnt = Counter() if not x % 2: while not x ...
{ "input": [ "3 4\n1\n1\n1\n0", "6 2\n1 1\n1 2\n1 1\n1 1\n1 2\n1 1\n", "4 5\n2 2 1 1 2\n1 1 1 1 1\n2 1 2 1 2\n2 1 2 1 1\n", "6 6\n1 2 1 2 1 2\n2 3 2 3 2 3\n3 4 3 4 3 4\n1 2 1 2 1 2\n2 3 2 3 2 3\n3 4 3 4 3 4\n", "36 6\n1 1 1 1 1 1\n1 1 1 1 1 1\n1 1 1 1 1 1\n1 1 1 1 1 1\n1 1 1 1 2 1\n1 1 1 1 1 1\n1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This is an interactive problem. There exists a matrix a of size n × m (n rows and m columns), you know only numbers n and m. The rows of the matrix are numbered from 1 to n from top ...
1516_C. Baby Ehab Partitions Again_24655
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so tha...
import collections import itertools from functools import reduce mod = (10 ** 9) + 7 def permutationbysum(): for _ in range(int(input())): num, l, r, achieve = map(int, input().split()) k = r - l + 1 if (k * (k + 1)) // 2 <= achieve <= (k * (num * 2 + 1 - k)) // 2: outpos = (r...
{ "input": [ "4\n6 3 9 12\n", "2\n1 2\n", "100\n32 64 96 128 160 192 224 256 288 320 352 384 416 448 480 512 544 576 608 640 672 704 736 768 800 832 864 896 928 960 992 1024 1056 1088 1120 1152 1184 1216 1248 1280 1312 1344 1376 1408 1440 1472 1504 1536 1568 1600 16 48 80 112 144 176 208 240 272 304 336 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the e...
1543_D1. RPD and Rap Sheet (Easy Version)_24659
This is the easy version of the problem. The only difference is that here k=2. You can make hacks only if both the versions of the problem are solved. This is an interactive problem. Every decimal number has a base k equivalent. The individual digits of a base k number are called k-its. Let's define the k-itwise XOR ...
try: import sys def solve(): n,k = map(int,input().split()) print(0) sys.stdout.flush() glad = int(input()) if(glad == 1): return for i in range(1 , n): print(i^(i-1)) sys.stdout.flush() glad = int(input()) ...
{ "input": [ "1\n5 2\n\n0\n\n0\n\n1\n", "1\n5 2\n", "18\n1 2\n2 2\n4 2\n8 2\n16 2\n32 2\n64 2\n128 2\n256 2\n512 2\n1024 2\n2048 2\n4096 2\n8192 2\n16384 2\n32768 2\n65536 2\n68929 2\n", "8\n17692 2\n10864 2\n23137 2\n40234 2\n13161 2\n7801 2\n45246 2\n36612 2\n", "18\n19135 2\n18239 2\n12273 2\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This is the easy version of the problem. The only difference is that here k=2. You can make hacks only if both the versions of the problem are solved. This is an interactive problem....
171_E. MYSTERIOUS LANGUAGE_24663
You are given a mysterious language (codenamed "Secret") available in "Custom Test" tab. Find out what this language is and write a program which outputs its name. Note that the program must be written in this language. Input This program has only one test, and it's empty (it doesn't give your program anything to rea...
n=input() print("INTERCAL")
{ "input": [ "1\n" ], "output": [ "INTERCAL" ] }
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a mysterious language (codenamed "Secret") available in "Custom Test" tab. Find out what this language is and write a program which outputs its name. Note that the progr...
191_C. Fools and Roads_24666
They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities there is a path (or else the fools would get upset). Also, between each ...
import os import sys from io import BytesIO, IOBase from types import GeneratorType from collections import defaultdict BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" ...
{ "input": [ "5\n1 2\n1 3\n2 4\n2 5\n2\n1 4\n3 5\n", "5\n3 4\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n3 5\n", "5\n3 4\n4 5\n1 2\n2 4\n3\n2 3\n1 3\n3 5\n", "5\n3 1\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n3 5\n", "5\n3 1\n4 5\n1 4\n2 1\n3\n2 3\n1 3\n3 5\n", "5\n3 4\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n5 5\n", "5\n3 1\n4 5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, populated by the fools and connected by the roads. All Berland roads are bidirectional....
215_C. Crosses_24670
There is a board with a grid consisting of n rows and m columns, the rows are numbered from 1 from top to bottom and the columns are numbered from 1 from left to right. In this grid we will denote the cell that lies on row number i and column number j as (i, j). A group of six numbers (a, b, c, d, x0, y0), where 0 ≤ a...
from sys import stdin def read(): return map(int, stdin.readline().split()) def ways(h,w,area): if area == h*w: return 2 * ( (h+1)//2 * (w+1)//2 ) - 1 if area > h*w: return 0 if area < h+w-1: return 0 area = h*w - area if area % 4 != 0: return 0 area //= 4 ans = 0 h //= 2 w //= 2 for a...
{ "input": [ "2 2 1\n", "3 4 5\n", "2 2 3\n", "2 20 37\n", "500 500 250000\n", "500 500 249995\n", "1 1 1\n", "2 1 1\n", "32 71 297\n", "20 20 8\n", "6 14 23\n", "2 78 117\n", "500 500 9997\n", "500 500 6913\n", "2 1 2\n", "3 3 5\n", "5 1 3\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a board with a grid consisting of n rows and m columns, the rows are numbered from 1 from top to bottom and the columns are numbered from 1 from left to right. In this grid w...
239_C. Not Wool Sequences_24674
A sequence of non-negative integers a1, a2, ..., an of length n is called a wool sequence if and only if there exists two integers l and r (1 ≤ l ≤ r ≤ n) such that <image>. In other words each wool sequence contains a subsequence of consecutive elements with xor equal to 0. The expression <image> means applying the o...
n, m = map(int, input().split()) MOD = 10 ** 9 + 9 ans = pow(2, m, MOD) - 1 step = pow(2, m, MOD) - 2 for i in range(n - 1): ans = (ans * step) % MOD step -= 1 while ans < 0: ans += MOD while ans >= MOD: ans -= MOD print(ans)
{ "input": [ "3 2\n", "86232 41348\n", "2 2\n", "100000 17\n", "1 1\n", "4 2\n", "1 99999\n", "4 3\n", "21857 94145\n", "1 29\n", "5444 31525\n", "50664 46559\n", "2 1\n", "7683 65667\n", "4 4\n", "4 1\n", "50000 100000\n", "12345 31\n", "250...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A sequence of non-negative integers a1, a2, ..., an of length n is called a wool sequence if and only if there exists two integers l and r (1 ≤ l ≤ r ≤ n) such that <image>. In other ...
264_A. Escape from Stones_24678
Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order. The stones always fall to the center of Liss's inter...
z,zz=input,lambda:list(map(int,z().split())) fast=lambda:stdin.readline().strip() zzz=lambda:[int(i) for i in fast().split()] szz,graph,mod,szzz=lambda:sorted(zz()),{},10**9+7,lambda:sorted(zzz()) from string import * from re import * from collections import * from queue import * from sys import * from collections impo...
{ "input": [ "llrlr\n", "lrlrr\n", "rrlll\n", "l\n", "rrlllrrrlrrlrrrlllrlrlrrrlllrllrrllrllrrlrlrrllllrlrrrrlrlllrlrrrlrlrllrlrlrrlrrllrrrlrlrlllrrllllrl\n", "rrrllrrrlllrlllrlrrr\n", "r\n", "lllrrlrlrllrrrrrllrl\n", "lllrlrllrl\n", "llrlrlrlrlrlrrlllllllrllllrllrrrlllrrllrllr...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall ...
288_A. Polo the Penguin and Strings_24682
Little penguin Polo adores strings. But most of all he adores strings of length n. One day he wanted to find a string that meets the following conditions: 1. The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct. 2. No two neighbouring...
a,b=[int(i) for i in input().split()] if b>a: print(-1) else: if b==1: if a>1: print(-1) else:print('a') else: b-=2 for i in range(0,a-b): if i%2==0:print('a',end='') else:print('b',end='') for i in range(b): print(chr(o...
{ "input": [ "4 7\n", "7 4\n", "68754 25\n", "7642 3\n", "999 7\n", "2 2\n", "100000 7\n", "6875 2\n", "123 21\n", "3 3\n", "1000000 1\n", "1 26\n", "26 2\n", "68754 25\n", "6791 21\n", "100000 20\n", "128 26\n", "26 26\n", "6791 21\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little penguin Polo adores strings. But most of all he adores strings of length n. One day he wanted to find a string that meets the following conditions: 1. The string consists o...
313_A. Ilya and Bank Account_24686
Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obligations. Moreover, they even have their own bank accounts. The state of a bank account is an integer. The state of a bank account can be a negative number. This means that the owner of the account ow...
n=int(input()) if n>=0: print(n) else: r=n+(2*n) n=str(n) k=len(n) ones=int(n[k-1]) huns=int(n[k-2]) if ones>=huns: print(int(n[:k-1])) else: f=n[:k-2]+n[k-1] print(int(f))
{ "input": [ "2230\n", "-100003\n", "-10\n", "-479977172\n", "-55\n", "-392784794\n", "-337396162\n", "46\n", "-31\n", "-23\n", "10\n", "-120\n", "-20\n", "423654797\n", "-623563697\n", "-648171877\n", "-384381709\n", "-272656295\n", "-24\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obligations. Moreover, they even have their own bank accounts. The...
358_D. Dima and Hares_24692
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more. Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with c...
from collections import defaultdict from itertools import accumulate import sys input = sys.stdin.readline ''' for CASES in range(int(input())): n, m = map(int, input().split()) n = int(input()) A = list(map(int, input().split())) S = input().strip() sys.stdout.write(" ".join(map(str,ANS))+"\n") ''' inf = 100000000000...
{ "input": [ "7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3\n", "4\n1 2 3 4\n4 3 2 1\n0 1 1 0\n", "3\n1 1 1\n1 2 1\n1 1 1\n", "1\n100000\n100000\n100000\n", "1\n0\n1\n4\n", "2\n3 5\n9 8\n4 0\n", "6\n1 2 0 1 6 4\n0 6 1 8 9 8\n4 1 4 3 9 8\n", "8\n7 3 3 5 9 9 8 1\n8 2 6 6 0 3 8 0\n1 2 5 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more. Dima felt so grateful to Inna about the present that he decided to buy her n ...
381_C. Sereja and Prefixes_24696
Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm. Sereja takes a blank piece of paper. Then he starts writing out the sequence in m stages. Each time he either adds a new number to the end of the sequence or takes l first elements of the current seq...
MAX_LEN = int(1e5) n = int(input()) a = [list(map(int, input().split())) for i in range(n)] m = int(input()) b = list(map(lambda x:int(x)-1, input().split())) curr, k, c, res = 0, 0, [], [] for i in range(n): t = a[i] last = curr if t[0] == 1: curr += 1 if len(c) < MAX_LEN: c.ap...
{ "input": [ "6\n1 1\n1 2\n2 2 1\n1 3\n2 5 2\n1 4\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n", "10\n1 57757\n1 45234\n1 80807\n1 38496\n1 27469\n1 42645\n1 72643\n1 33235\n1 10843\n1 80598\n10\n1 2 3 4 5 6 7 8 9 10\n", "3\n1 97601\n1 32580\n1 70519\n3\n1 2 3\n", "1\n1 1\n1\n1\n", "2\n1 33085\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm. Sereja takes a blank piece of paper. Then he starts writing ou...
402_B. Trees in a Row_24700
The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≤ i ≤ n) tree from the left has height ai meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all i (1 ≤ i < n), ai + 1 - ai = k, where k is the number the Queen...
n, k = map(int, input().split()) a = list(map(int, input().split())) tmp = {} for i in range(n): x = a[i] - i * k if x > 0: tmp[x] = tmp.get(x, 0) + 1 x = max(tmp, key = lambda x: tmp[x]) print(n - tmp[x]) if tmp[x] == n: exit(0) for i in range(n): if x + i * k > a[i]: print('+', i + 1, ...
{ "input": [ "4 1\n1 2 1 5\n", "4 1\n1 2 3 4\n", "4 1\n1 1 2 3\n", "10 99\n1 100 199 298 397 496 364 694 793 676\n", "50 5\n232 6 11 16 21 26 31 36 41 46 665 56 61 66 71 76 602 86 91 712 101 106 111 116 121 126 131 136 141 146 151 156 161 166 755 176 181 186 191 196 201 206 211 216 221 226 231 236...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≤ i ≤ n) tree from the left has height ai meters. Today the Queen decided to update the scenery o...
42_C. Safe cracking_24704
Right now you are to solve a very, very simple problem — to crack the safe. Four positive integers stand one by one on a circle protecting the safe. You know that to unlock this striking safe you have to make all four numbers equal to one. Operations are as follows: you may choose two adjacent numbers and increase both...
ring = list(map(int, input().split())) n = len(ring) record = [] def halve(pos): a, b = pos % n, (pos + 1) % n ring[a] //= 2 ring[b] //= 2 record.append('/%d' % (a + 1)) def increment(pos): a, b = pos % n, (pos + 1) % n ring[a] += 1 ring[b] += 1 record.append('+%d' % (a + 1)) while True: modified ...
{ "input": [ "3 3 1 1\n", "2 1 2 4\n", "1 2 4 2\n", "1 1 1 1\n", "698797440 708706737 226921438 376988902\n", "787217985 597629316 333185503 222888215\n", "2 2 2 2\n", "844565526 132558538 559319000 336388544\n", "698797440 708706737 226921438 376988902\n", "502963600 673961459...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Right now you are to solve a very, very simple problem — to crack the safe. Four positive integers stand one by one on a circle protecting the safe. You know that to unlock this strik...
452_A. Eevee_24708
You are solving the crossword problem K from IPSC 2014. You solved all the clues except for one: who does Eevee evolve into? You are not very into pokemons, but quick googling helped you find out, that Eevee can evolve into eight different pokemons: Vaporeon, Jolteon, Flareon, Espeon, Umbreon, Leafeon, Glaceon, and Syl...
n = int(input()) s = input() evol = ['vaporeon', 'jolteon', 'flareon', 'espeon', 'umbreon', 'leafeon', 'glaceon', 'sylveon'] for e in evol : if len(e) != n : continue for i in range(n): if s[i] != e[i] and s[i] != '.': break if i == n-1 : print(e) break
{ "input": [ "7\nj......\n", "7\n...feon\n", "7\n.l.r.o.\n", "7\n.l.ceon\n", "7\n..lveon\n", "8\n..p.....\n", "7\n.l.c...\n", "7\n..lv...\n", "8\n.a.o.e.n\n", "6\n......\n", "7\nglaceon\n", "7\n..b....\n", "6\n.s..o.\n", "7\n.y.....\n", "7\n..areon\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are solving the crossword problem K from IPSC 2014. You solved all the clues except for one: who does Eevee evolve into? You are not very into pokemons, but quick googling helped ...
521_E. Cycling City_24715
You are organizing a cycling race on the streets of the city. The city contains n junctions, some pairs of them are connected by roads; on each road you can move in any direction. No two roads connect the same pair of intersections, and no road connects the intersection with itself. You want the race to be open to bot...
from typing import List import sys n, m = [int(a) for a in input().split(' ')] adj: List[List[int]] = [[]] + [[] for _ in range(n)] visited: List[bool] = [False] * (n + 1) parent: List[int] = [0] * (n + 1) depth: List[int] = [0] * (n + 1) low: List[int] = [0] * (n + 1) cx: List[int] = [0] * (n + 1) cy: List[int] ...
{ "input": [ "5 6\n1 2\n1 3\n1 4\n2 5\n3 5\n4 5\n", "4 4\n1 2\n2 3\n3 4\n4 1\n", "70 1\n61 39\n", "10 5\n6 2\n2 1\n2 7\n10 7\n4 6\n", "10 12\n3 10\n2 9\n4 5\n5 10\n7 3\n8 4\n2 4\n8 7\n6 9\n9 1\n8 3\n1 3\n", "4 5\n4 3\n1 4\n3 1\n2 1\n2 4\n", "20 19\n19 4\n15 3\n14 16\n6 13\n19 9\n10 15\n13 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are organizing a cycling race on the streets of the city. The city contains n junctions, some pairs of them are connected by roads; on each road you can move in any direction. No ...
549_B. Looksery Party_24719
The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time ...
n=int(input().strip()) nums=['']+[' '+input().strip() for _ in range(n)] a=[0]+list(map(int,input().split())) def send(x): for i in range(1,n+1): if nums[x][i]=='1': a[i]-=1 vis=[0]*(n+1) while True: for i in range(1,n+1): if not vis[i] and not a[i]: vis[i]=1 ...
{ "input": [ "3\n101\n010\n001\n0 1 2\n", "4\n1111\n0101\n1110\n0001\n1 0 1 0\n", "1\n1\n1\n", "10\n1000100000\n0100000000\n0010001000\n0011000000\n0100100000\n0000010010\n1000001000\n0000000101\n0000000110\n0001000001\n1 2 1 1 1 0 1 1 1 1\n", "6\n100000\n010000\n001000\n000100\n000010\n000001\n1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyon...
575_D. Tablecity_24723
There was a big bank robbery in Tablecity. In order to catch the thief, the President called none other than Albert – Tablecity’s Chief of Police. Albert does not know where the thief is located, but he does know how he moves. Tablecity can be represented as 1000 × 2 grid, where every cell represents one district. Eac...
print(2001) for i in range(1 , 1001): print(f"{i} 1 {i} 2") print(f"1 1 1 2") for i in range(1 , 1001): print(f"{i} 1 {i} 2")
{ "input": [ "В этой задаче нет примеров ввода-вывода.\nThis problem doesn't have sample input and output.", "dummy\n", "udmmy\n", "В этой задаче нет примеров ввода-вывода.\nThis problem doesn't have sample inout and output.", "ymmdu\n", "В этой задаче нет примеров ввода-вывода.\nThis problem ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There was a big bank robbery in Tablecity. In order to catch the thief, the President called none other than Albert – Tablecity’s Chief of Police. Albert does not know where the thief...
597_B. Restaurant_24727
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the ...
n=int(input()) a=[] for i in range(n): a.append(tuple(map(int,input().split()))) a.sort(key=lambda x:x[1]) cnt=0;cur=-10 i=0 while i<n: if cur<a[i][0]: cur=a[i][1] cnt+=1 i+=1 print(cnt)
{ "input": [ "6\n4 8\n1 5\n4 7\n2 5\n1 3\n6 8\n", "5\n1 2\n2 3\n3 4\n4 5\n5 6\n", "2\n7 11\n4 7\n", "16\n4 80\n16 46\n15 16\n60 63\n8 54\n18 49\n67 99\n72 80\n1 8\n19 64\n1 54\n46 94\n2 89\n67 78\n21 47\n5 29\n", "15\n1 58\n15 21\n53 55\n59 90\n68 71\n29 51\n52 81\n32 52\n38 44\n57 59\n47 60\n27 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start...
618_D. Hamiltonian Spanning Tree_24731
A group of n cities is connected by a network of roads. There is an undirected road between every pair of cities, so there are <image> roads in total. It takes exactly y seconds to traverse any single road. A spanning tree is a set of roads containing exactly n - 1 roads such that it's possible to travel between any t...
from collections import defaultdict from collections import deque from functools import reduce n, x, y = [int(x) for x in input().split()] E = defaultdict(set) for i in range(n-1): u, v = [int(x) for x in input().split()] E[u].add(v) E[v].add(u) if x > y: for v in E: if len(E[v]) == n-1: ...
{ "input": [ "5 3 2\n1 2\n1 3\n3 4\n5 3\n", "5 2 3\n1 2\n1 3\n3 4\n5 3\n", "5 2 2\n1 2\n1 3\n1 4\n1 5\n", "2 3 4\n1 2\n", "4 100 1\n1 2\n2 3\n3 4\n", "50 23129 410924\n18 28\n17 23\n21 15\n18 50\n50 11\n32 3\n44 41\n50 31\n50 34\n5 14\n36 13\n22 40\n20 9\n9 43\n19 47\n48 40\n20 22\n33 45\n35 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A group of n cities is connected by a network of roads. There is an undirected road between every pair of cities, so there are <image> roads in total. It takes exactly y seconds to tr...
639_B. Bear and Forgotten Tree 3_24735
A tree is a connected undirected graph consisting of n vertices and n - 1 edges. Vertices are numbered 1 through n. Limak is a little polar bear and Radewoosh is his evil enemy. Limak once had a tree but Radewoosh stolen it. Bear is very sad now because he doesn't remember much about the tree — he can tell you only th...
n, d, h = list(map(int, input().split())) if h < (d + 1) // 2 or (h == d == 1 and n > 2): print(-1) exit() i = 1 while i <= h: print(i, i + 1) i += 1 if i <= d: print(1, h + 2) i = h + 2 while i <= d: print(i, i + 1) i += 1 i += 1 while i <= n: if h == d: print(2, i) els...
{ "input": [ "8 4 2\n", "8 5 2\n", "5 3 2\n", "15 6 4\n", "10 2 1\n", "15 2 1\n", "100000 99999 1\n", "5 3 2\n", "3 1 1\n", "11 1 1\n", "6 1 1\n", "4 1 1\n", "99999 1 1\n", "20 19 19\n", "4 3 3\n", "3 2 1\n", "2 1 1\n", "47 1 1\n", "10 1 1\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A tree is a connected undirected graph consisting of n vertices and n - 1 edges. Vertices are numbered 1 through n. Limak is a little polar bear and Radewoosh is his evil enemy. Lima...
690_A3. Collective Mindsets (hard)_24741
Heidi got one brain, thumbs up! But the evening isn't over yet and one more challenge awaits our dauntless agent: after dinner, at precisely midnight, the N attendees love to play a very risky game... Every zombie gets a number ni (1 ≤ ni ≤ N) written on his forehead. Although no zombie can see his own number, he can ...
t = int(input()) for _ in range(t): n, r = map(int, input().split()) s = sum(map(int, input().split())) x = 1 while (s + x + r) % n != 0: x += 1 print(x)
{ "input": [ "4\n2 1\n1\n2 2\n1\n2 1\n2\n2 2\n2\n", "2\n5 2\n2 2 2 2\n6 4\n3 2 6 1 2\n", "27\n3 1\n1 1\n3 2\n1 1\n3 3\n1 1\n3 1\n2 1\n3 2\n2 1\n3 3\n2 1\n3 1\n3 1\n3 2\n3 1\n3 3\n3 1\n3 1\n1 2\n3 2\n1 2\n3 3\n1 2\n3 1\n2 2\n3 2\n2 2\n3 3\n2 2\n3 1\n3 2\n3 2\n3 2\n3 3\n3 2\n3 1\n1 3\n3 2\n1 3\n3 3\n1 3\n3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Heidi got one brain, thumbs up! But the evening isn't over yet and one more challenge awaits our dauntless agent: after dinner, at precisely midnight, the N attendees love to play a v...
713_A. Sonya and Queries_24745
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type: 1. + ai — add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occur...
#Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction from collections import defaultdict from itertools import permutations BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 ...
{ "input": [ "4\n+ 200\n+ 200\n- 200\n? 0\n", "12\n+ 1\n+ 241\n? 1\n+ 361\n- 241\n? 0101\n+ 101\n? 101\n- 101\n? 101\n+ 4000\n? 0\n", "20\n+ 61\n+ 99\n+ 51\n+ 70\n+ 7\n+ 34\n+ 71\n+ 86\n+ 68\n+ 39\n+ 78\n+ 81\n+ 89\n? 10\n? 00\n? 10\n? 01\n? 01\n? 00\n? 00\n", "10\n+ 2\n- 2\n+ 778\n+ 3\n+ 4\n- 4\n+ 1\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of t...
733_E. Sleep in Class_24749
The academic year has just begun, but lessons and olympiads have already occupied all the free time. It is not a surprise that today Olga fell asleep on the Literature. She had a dream in which she was on a stairs. The stairs consists of n steps. The steps are numbered from bottom to top, it means that the lowest ste...
n=int(input()) string = input() q1,q2,B,C=[],[],[],[] A=0 for i in range(n): if string[i] == "D": q1.append(i) else: q2.append(n-1-i) for i in range(len(q1)): A+=(q1[i]-i)*2+1 B.append(A) A=0 temp = [] for i in range(len(q2)): A+=(q2[len(q2)-1-i]-i)*2+1 C.append(A) C.reverse() ...
{ "input": [ "3\nUUD\n", "10\nUUDUDUUDDU\n", "1\nD\n", "10\nDUDDUUDUDD\n", "50\nUUUUUUUUUUUUUUUUUUUUUUUUUDDDDDDDDDDDDDDDDDDDDDDDDD\n", "2\nDU\n", "4\nDDDU\n", "50\nDDDDDDDDDDDDDDDDDDDDDDDDDUUUUUUUUUUUUUUUUUUUUUUUUU\n", "5\nDDDUD\n", "3\nDUU\n", "1\nU\n", "10\nDDUDUUDDUD...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The academic year has just begun, but lessons and olympiads have already occupied all the free time. It is not a surprise that today Olga fell asleep on the Literature. She had a drea...
779_C. Dishonest Sellers_24755
Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi. Not all of sellers are honest, so now some products could be more expensive than after a week of discou...
n,k=map(int,input().split()) l=list(map(int,input().split())) m=list(map(int,input().split())) c=0 s=0 for i in range(n): if(l[i]<=m[i]): s+=l[i] c+=1 if(c>=k): for i in range(n): if(l[i]>m[i]): s+=m[i] print(s) else: r=k-c p=[] for i in range(n): i...
{ "input": [ "5 3\n3 4 7 10 3\n4 5 5 12 5\n", "3 1\n5 4 6\n3 1 5\n", "1 0\n9\n8\n", "5 3\n28 17 20 45 45\n39 12 41 27 9\n", "5 3\n10 17 23 26 35\n7 18 18 29 32\n", "5 2\n1 1 1 1 1\n2 2 2 2 2\n", "2 0\n4 10\n1 2\n", "10 5\n87 96 19 81 10 88 7 49 36 21\n11 75 28 28 74 17 64 19 81 31\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of...
848_E. Days of Floral Colours_24762
The Floral Clock has been standing by the side of Mirror Lake for years. Though unable to keep time, it reminds people of the passage of time and the good old days. On the rim of the Floral Clock are 2n flowers, numbered from 1 to 2n clockwise, each of which has a colour among all n possible ones. For each colour, the...
#https://codeforces.com/problemset/problem/848/E f = [0, 4, 8, -1, 16, -10, 4, -12, -48, 26, -44, 15, -16, -4, -4, -1] jly = 998244353 t = 0 x = [0, 0, 0, 24, 4, 240, 204, 1316, 2988, 6720, 26200, 50248, 174280, 436904, 1140888, 3436404] n = int(input()) for i in range(16, n + 1): t = 0 for j in range(0, 16): ...
{ "input": [ "4\n", "15\n", "3\n", "7\n", "13\n", "396\n", "998\n", "23333\n", "8\n", "49997\n", "33\n", "5\n", "831\n", "3939\n", "10492\n", "12\n", "30000\n", "20\n", "1000\n", "17\n", "11\n", "10001\n", "49999\n", "1000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Floral Clock has been standing by the side of Mirror Lake for years. Though unable to keep time, it reminds people of the passage of time and the good old days. On the rim of the...
870_C. Maximum splitting_24765
You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. An integer greater than 1 is composite, if it is not prime, i.e. if...
solve = lambda n: -1 if n in {1, 2, 3, 5, 7, 11} else n // 4 - n % 2 print(*[solve(int(input())) for i in range(int(input()))])
{ "input": [ "3\n1\n2\n3\n", "1\n12\n", "2\n6\n8\n", "1\n10000001\n", "20\n8\n13\n20\n12\n9\n16\n4\n19\n7\n15\n10\n6\n14\n11\n3\n2\n5\n17\n18\n1\n", "3\n4\n6\n9\n", "100\n611\n513\n544\n463\n38\n778\n347\n317\n848\n664\n382\n108\n718\n33\n334\n876\n234\n22\n944\n305\n159\n245\n513\n691\n63...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and prin...
897_B. Chtholly's request_24769
— Thanks a lot for today. — I experienced so many great things. — You gave me memories like dreams... But I have to leave now... — One last request, can you... — Help me solve a Codeforces problem? — ...... — What? Chtholly has been thinking about a problem for days: If a number is palindrome and length of its ...
import itertools as it k, p = [int(elem) for elem in input().split()] def build_palindroms(digits): global k global val n = digits // 2 values = [10 ** (digits - 1 - i) + 10 ** i for i in range(n)] for comb in it.product(range(1, 10), *[range(0, 10) for i in range(n - 1)]): if not k: ...
{ "input": [ "2 100\n", "5 30\n", "77809 868097296\n", "90898 94010922\n", "1 1000000000\n", "100000 1\n", "22951 23793302\n", "58832 563860457\n", "100000 1000000000\n", "67298 349286579\n", "41163 472310076\n", "90234 156145441\n", "60276 428978808\n", "46304 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: — Thanks a lot for today. — I experienced so many great things. — You gave me memories like dreams... But I have to leave now... — One last request, can you... — Help me solve a C...
918_C. The Monster_24773
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. <image> Thus, he came up with a puz...
s = input() res, n = 0, len(s) for i in range(n-1): j, c, q = i , 0, 0 while j < n and c + q >= 0: if(s[j] == '('): c += 1 elif(s[j] == ')'): c -= 1 else: q += 1 if(c < q): c, q = q, c res += (c == q) j += 1 print(res)
{ "input": [ "((?))\n", "??()??\n", ")?(??((???????()?(?????????)??(????????((?)?????)????)??????(?????)?)?????)??????(??()??????)????????)?)()??????????????())????????(???)??)????????????????????(?????)??)???)??(???????????????)???)??)?\n", "??\n", "(?\n", "))((()(()((((()))())()())((())())((...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his...
940_C. Phone Numbers_24777
And where the are the phone numbers? You are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such that its set of letters is a subset of the set of letters of s and s is lexicographically smaller than t. It's guaranteed that the answ...
n,k=map(int,input().split()) s=input() l=sorted(list(set(list(s)))) if k<=n: for i in range(k-1,-1,-1): z=[] for j in l: if s[i]<j: z.append(j) if len(z)>0: z.sort() t=s[:i]+z[0]+l[0]*(k-i-1) break else: t=s+(k-n)*l[0] print(t)
{ "input": [ "3 3\nabc\n", "2 3\nba\n", "3 2\nabc\n", "3 3\nayy\n", "3 1\nazz\n", "10 9\nbcegikmyyy\n", "1 3\nf\n", "3 99948\nttt\n", "5 10\nkekff\n", "10 3\nggjnohlepu\n", "8 8\nlolololo\n", "10 2\nkmiejhcimj\n", "8 9\npppppppp\n", "10 5\nhqqqqcdddb\n", "7 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: And where the are the phone numbers? You are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such...
p02632 AtCoder Beginner Contest 171 - Strivore_24794
How many strings can be obtained by applying the following operation on a string S exactly K times: "choose one lowercase English letter and insert it somewhere"? The answer can be enormous, so print it modulo (10^9+7). Constraints * K is an integer between 1 and 10^6 (inclusive). * S is a string of length between 1...
import sys,queue,math,copy,itertools,bisect,collections,heapq def main(): MOD = 10**9 + 7 NI = lambda : int(sys.stdin.readline()) SI = lambda : sys.stdin.readline().rstrip() K = NI() S = SI() LS = len(S) f = [1] * (K+LS) r = [1] * (K+LS) c = 1 for i in range(1, K + LS): ...
{ "input": [ "5\noof", "37564\nwhydidyoudesertme", "5\nooe", "37564\nwhydidzoudesertme", "5\neoo", "37564\nwhxdidzoudesertme", "5\ndoo", "37564\nwhxdidzoudesertne", "5\ndop", "37564\nwhxdiczoudesertne", "5\nodp", "37564\nwhxdiczovdesertne", "5\npdo", "37564\nent...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: How many strings can be obtained by applying the following operation on a string S exactly K times: "choose one lowercase English letter and insert it somewhere"? The answer can be e...
p02763 AtCoder Beginner Contest 157 - Simple String Queries_24798
You are given a string S of length N consisting of lowercase English letters. Process Q queries of the following two types: * Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.) * Type 2: answer the number of different characters occurring in the substring of S betwee...
def main(): import sys b=sys.stdin.buffer input=b.readline n=int(input()) d=[0]*n+[1<<c-97for c in input()[:n]] for i in range(n-1,0,-1):d[i]=d[i+i]|d[i-~i] r=[] add=r.append input() for q,a,b in zip(*[iter(b.read().split())]*3): i,s=int(a)+n-1,0 if q<b'2': d[i]=1<<b[0]-97 while ...
{ "input": [ "7\nabcdbbd\n6\n2 3 6\n1 5 z\n2 1 1\n1 4 a\n1 7 d\n2 1 7", "7\nabcdbbd\n6\n2 3 6\n1 5 y\n2 1 1\n1 4 a\n1 7 d\n2 1 7", "7\nabccbbd\n6\n2 3 6\n1 5 y\n2 1 1\n1 4 a\n1 7 d\n2 0 7", "7\nabdcbbd\n6\n2 3 6\n1 5 z\n2 1 1\n1 4 a\n1 7 d\n2 0 7", "7\nabcdbbd\n6\n2 3 0\n1 5 z\n2 1 1\n1 4 a\n1 7 d...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a string S of length N consisting of lowercase English letters. Process Q queries of the following two types: * Type 1: change the i_q-th character of S to c_q. (Do no...
p02898 AtCoder Beginner Contest 142 - Roller Coaster_24802
N friends of Takahashi has come to a theme park. To ride the most popular roller coaster in the park, you must be at least K centimeters tall. The i-th friend is h_i centimeters tall. How many of the Takahashi's friends can ride the roller coaster? Constraints * 1 \le N \le 10^5 * 1 \le K \le 500 * 1 \le h_i \le 5...
n, k = map(int, input().split()) h = list(map(int, input().split())) print(sum(x >= k for x in h))
{ "input": [ "5 1\n100 200 300 400 500", "4 150\n150 140 100 200", "1 500\n499", "5 1\n100 200 300 467 500", "4 150\n151 140 100 200", "0 500\n499", "1 500\n654", "5 2\n000 200 300 467 500", "4 112\n150 140 100 200", "5 2\n100 200 300 467 500", "-1 500\n499", "5 2\n100 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: N friends of Takahashi has come to a theme park. To ride the most popular roller coaster in the park, you must be at least K centimeters tall. The i-th friend is h_i centimeters tal...
p03033 AtCoder Beginner Contest 128 - Roadwork_24806
There is an infinitely long street that runs west to east, which we consider as a number line. There are N roadworks scheduled on this street. The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5. Q people are standing at coordinate 0. The i-th person will start the coordinate 0 ...
from collections import deque, defaultdict from heapq import heappush, heappop import sys input = sys.stdin.readline N, Q = map(int, input().split()) events = [None] * (2*N) for i in range(N): S, T, X = map(int, input().split()) events[2*i] = (S-X, X) events[2*i+1] = (T-X, -X) events.sort(key=lambda x: x[0...
{ "input": [ "4 6\n1 3 2\n7 13 10\n18 20 13\n3 4 2\n0\n1\n2\n3\n5\n8", "4 6\n1 3 2\n7 13 1\n18 20 13\n3 4 2\n0\n1\n2\n3\n5\n8", "4 6\n1 4 2\n8 13 1\n18 20 12\n3 4 2\n0\n1\n2\n3\n5\n8", "4 5\n1 4 2\n8 13 1\n18 20 12\n3 4 2\n0\n1\n2\n3\n5\n8", "4 5\n1 4 0\n8 13 1\n18 20 12\n3 4 2\n0\n1\n2\n3\n5\n8",...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is an infinitely long street that runs west to east, which we consider as a number line. There are N roadworks scheduled on this street. The i-th roadwork blocks the point at c...
p03174 Educational DP Contest - Matching_24810
There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman...
def popcount(i): assert 0 <= i < 0x100000000 i = i - ((i >> 1) & 0x55555555) i = (i & 0x33333333) + ((i >> 2) & 0x33333333) return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24 N = int(input()) A = [list(map(int, input().split())) for _ in range(N)] mod = 10**9+7 dp = [0]*(2**N) dp[0] ...
{ "input": [ "1\n0", "4\n0 1 0 0\n0 0 0 1\n1 0 0 0\n0 0 1 0", "21\n0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1\n1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0\n0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1\n0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0\n1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0\n0 1 1 0 1 1 1 0 1 1 1 0 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = ...
p03322 AtCoder Regular Contest 099 - Eating Symbols Hard_24813
In Takahashi's mind, there is always an integer sequence of length 2 \times 10^9 + 1: A = (A_{-10^9}, A_{-10^9 + 1}, ..., A_{10^9 - 1}, A_{10^9}) and an integer P. Initially, all the elements in the sequence A in Takahashi's mind are 0, and the value of the integer P is 0. When Takahashi eats symbols `+`, `-`, `>` an...
import random from collections import defaultdict def solve(n, s, xs, m): ans = [10 ** 9] * (n + 1) for x in xs: p = 0 h = 0 y = 1 r = pow(x, m - 2, m) pos = [0] * (n + 1) hashes = [0] * (n + 1) for i, c in enumerate(s, start=1): if c == '>':...
{ "input": [ "5\n+>+<-", "5\n+>+-<", "48\n-+><<><><><>>>+-<<>->>><<><<-+<>><+<<>+><-+->><<", "5\n+-+<>", "5\n*>+-<", "48\n<<>>-+-<>+><<+<>><+-<<><<>>>-><<-+>>><><><><<>+-", "5\n><+-+", "5\n-<+>+", "48\n-+><<><><><>>>+-<<>->>><<><<-+<>-<+<<>+><>+->><<", "5\n>-+<+", "5\n>-+;+...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In Takahashi's mind, there is always an integer sequence of length 2 \times 10^9 + 1: A = (A_{-10^9}, A_{-10^9 + 1}, ..., A_{10^9 - 1}, A_{10^9}) and an integer P. Initially, all the...
p03476 AtCoder Beginner Contest 084 - 2017-like Number_24817
We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime. You are given Q queries. In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i. Constraints * 1≤Q≤10^5 * 1≤l_i≤r_i≤10^5 * l_i and r_i are odd. * All input values ...
from itertools import accumulate q=int(input()) lr=[list(map(int,input().split())) for i in range(q)] p=[2] pc=[0]*100001 pc[2]=1 ans=[0]*100001 for i in range(3,100001): for j in p: if(i%j==0): break if(j*j>i): p.append(i) pc[i]=1 if(pc[i//2+1]==1): ans[i]=1 break ans=li...
{ "input": [ "1\n3 7", "4\n13 13\n7 11\n7 11\n2017 2017", "6\n1 53\n13 91\n37 55\n19 51\n73 91\n13 49", "1\n3 9", "1\n5 9", "1\n9 9", "4\n13 13\n7 0\n7 11\n2017 2017", "6\n1 53\n7 91\n37 55\n19 51\n73 91\n13 49", "4\n11 13\n7 1\n7 13\n2017 2017", "6\n1 53\n7 91\n37 55\n19 51\n7...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime. You are given Q queries. In the i-th query, given two odd numbers l_i and r_i, find the number of od...
p03641 AtCoder Regular Contest 080 - Young Maids_24820
Let N be a positive even number. We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is constructing another permutation of (1, 2, ..., N), q, following the procedure below. First, let q be an empty sequence. Then, perform the following operation until p becomes empty: * Select two adjacent elem...
inf = 10**10 def sg_func(a,b): res = [0,0,0] if(a[2]==0): res[0] = min(a[0],b[0]) res[1] = min(a[1],b[1]) else: res[0] = min(a[0],b[1]) res[1] = min(a[1],b[0]) res[2] = (a[2]+b[2])%2 return res class Seg_cus(): def __init__(self,x): #####単位元###### ...
{ "input": [ "2\n1 2", "8\n4 6 3 2 8 5 7 1", "4\n3 2 4 1", "2\n0 2", "8\n4 6 5 2 8 5 7 1", "4\n3 3 4 1", "2\n-1 2", "8\n4 6 5 2 8 5 0 1", "2\n-1 0", "8\n2 6 5 2 8 5 0 1", "2\n-1 1", "8\n2 6 5 2 8 4 0 1", "2\n0 1", "8\n2 6 5 2 1 4 0 1", "2\n0 0", "2\n0 -1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let N be a positive even number. We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is constructing another permutation of (1, 2, ..., N), q, following the proc...
p03798 AtCoder Beginner Contest 055 - Menagerie_24824
Snuke, who loves animals, built a zoo. There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle. The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered...
N = int(input()) s = input() for t in ["SS","SW","WS","WW"]: for i in range(1,N-1): if (s[i] =="o" and t[i]=="S") or (s[i]=="x" and t[i]=="W"): t += t[i-1] else: if t[i-1] =="S": t += "W" else: t +="S" if (t[N-2]==t[0] and ((s[N-1]=...
{ "input": [ "10\noxooxoxoox", "6\nooxoox", "3\noox", "3\nxoo", "6\nxooxoo", "10\nxooxoxooxo", "10\nxxooxoxooo", "10\noxooooxxox", "10\nxoxxooooxo", "10\noooxoxooxx", "10\noxooooxxxo", "10\noxoooxxoox", "10\noxooooxoxx", "10\noxoooxooxx", "10\nxxoxxooooo", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke, who loves animals, built a zoo. There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle. The animal numbered i (2≤i≤N-1) is adjac...
p03965 AtCoder Beginner Contest 046 - AtCoDeer and Rock-Paper_24828
AtCoDeer the deer and his friend TopCoDeer is playing a game. The game consists of N turns. In each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-scissors, under the following condition: (※) After each turn, (the number of times the player has played Paper)≦(the number of times the ...
s = input() n = len(s) print(s[(n+1)//2:].count("g")-s[:(n+1)//2].count("p"))
{ "input": [ "gpg", "ggppgggpgg", "ggpgggppgg", "ggp", "ggggggpppg", "ggppgpgggg", "ggppggpggg", "gggpggppgg", "gggggpppgg", "pggpggpggg", "ggggpppggg", "gggpggpggp", "gggppgpggg", "gpgppggggg", "gpggpgpggg", "ggggpgppgg", "gggpppgggg", "ggggggpp...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: AtCoDeer the deer and his friend TopCoDeer is playing a game. The game consists of N turns. In each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-s...
p00056 Goldbach's Conjecture_24832
It is known that even numbers greater than or equal to 4 can be represented by the sum of two prime numbers. This is called the Goldbach's conjecture, and computer calculations have confirmed that it is correct up to a fairly large number. For example, 10 can be represented by the sum of two prime numbers, 7 + 3 and 5 ...
import bisect,sys from itertools import * n=range(50001);a=list(n);a[1]=0 for i in range(2,224):a[i*2::i]=[0]*len(a[i*2::i]) for x in map(int,sys.stdin): if x:print(sum(1 for d in compress(n[:x//2+1],a[:x//2+1])if a[x-d]))
{ "input": [ "10\n11\n0", "10\n22\n0", "10\n34\n0", "10\n28\n0", "10\n31\n0", "10\n3\n0", "10\n0\n0", "10\n48\n0", "10\n106\n0", "10\n140\n0", "10\n160\n0", "10\n114\n0", "10\n378\n0", "10\n220\n0", "10\n150\n0", "10\n336\n0", "10\n560\n0", "10\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: It is known that even numbers greater than or equal to 4 can be represented by the sum of two prime numbers. This is called the Goldbach's conjecture, and computer calculations have c...
p00187 Stoning Fortune_24836
With the motto "Creating your own path," a shrine created a fortune-telling fortune with its own hands. Ask the person who draws the lottery to throw six stones first, then the line segment connecting the first and second of the thrown stones, the line segment connecting the third and fourth, the fifth and six The fort...
# Aizu Problem 0187: Stoning Fortune # import sys, math, os, bisect # read input: PYDEV = os.environ.get('PYDEV') if PYDEV=="True": sys.stdin = open("sample-input.txt", "rt") def on_segment(p, q, r): # Given three colinear points p, q, r, the function checks if # point q lies on line segment 'pr' retu...
{ "input": [ "-3 -2 9 6\n3 -2 7 6\n-1 0 5 0\n2 2 -1 -1\n0 1 2 1\n-3 -1 3 1\n0 0 0 0", "-3 -2 9 6\n3 -2 7 6\n-1 0 5 0\n2 2 0 -1\n0 1 2 1\n-3 -1 3 1\n0 0 0 0", "-1 -2 8 12\n3 -2 7 6\n0 0 9 0\n2 2 0 -1\n1 1 2 1\n-3 -1 3 1\n0 0 0 0", "-1 -2 9 6\n3 -2 7 6\n-1 0 9 0\n2 2 0 -1\n1 2 2 0\n-3 -1 3 1\n0 0 0 0", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: With the motto "Creating your own path," a shrine created a fortune-telling fortune with its own hands. Ask the person who draws the lottery to throw six stones first, then the line s...
p00342 Maximization of Rational Expression_24839
N different natural numbers are given. If you select four different ones and set them as $ A $, $ B $, $ C $, $ D $, the following formula $ \ Frac {A + B} {C --D} $ I want to find the maximum value of. Given N different natural numbers, choose 4 different from them and create a program to find the maximum valu...
n = int(input()) a = sorted(map(int,input().split())) b = [(a[i + 1] - a[i], i) for i in range(n -1)] b.sort(key = lambda x:x[0]) if b[0][1] < n - 2:print((a[-1] + a[-2]) / b[0][0]) elif b[0][1] == n - 3: if b[1][1] == n - 2:print(max((a[-1] + a[-2]) / b[2][0], (a[-1] + a[-4]) / b[0][0], (a[-3] + a[-4]) / b[1][0]))...
{ "input": [ "5\n22 100 42 3 86", "10\n1 2 3 4 5 6 7 8 9 10", "4\n100000 99999 8 1", "6\n15 21 36 10 34 5", "4\n100000 26243 8 1", "6\n15 21 36 0 34 5", "4\n100000 40706 8 1", "6\n15 21 36 0 34 2", "4\n100000 71561 8 1", "6\n15 3 36 0 34 2", "4\n100000 71561 15 1", "6\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: N different natural numbers are given. If you select four different ones and set them as $ A $, $ B $, $ C $, $ D $, the following formula $ \ Frac {A + B} {C --D} $ I want to fin...
p00538 Cake 2_24843
Carving the cake 2 (Cake 2) JOI-kun and IOI-chan are twin brothers and sisters. JOI has been enthusiastic about making sweets lately, and JOI tried to bake a cake and eat it today, but when it was baked, IOI who smelled it came, so we decided to divide the cake. became. The cake is round. We made radial cuts from a p...
import sys sys.setrecursionlimit(100000) N, *A = map(int, open(0).read().split()) memo = [[-1]*N for i in range(N)] for i in range(N): memo[i][i] = A[i] if N % 2 else 0 def dfs(p, q, t): if memo[p][q] != -1: return memo[p][q] if t: memo[p][q] = r = max(A[p] + dfs((p+1)%N, q, 0), A[q] + dfs(p...
{ "input": [ "5\n2\n8\n1\n10\n9", "5\n2\n8\n1\n10\n12", "5\n2\n8\n1\n10\n13", "5\n2\n8\n1\n18\n24", "5\n3\n8\n0\n18\n24", "5\n3\n5\n0\n18\n24", "5\n3\n5\n0\n17\n4", "5\n5\n5\n0\n26\n4", "5\n2\n8\n1\n0\n9", "5\n2\n3\n1\n10\n12", "5\n4\n8\n1\n18\n13", "5\n2\n8\n1\n18\n19"...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Carving the cake 2 (Cake 2) JOI-kun and IOI-chan are twin brothers and sisters. JOI has been enthusiastic about making sweets lately, and JOI tried to bake a cake and eat it today, b...
p00700 Exploring Caves_24846
There are many caves deep in mountains found in the countryside. In legend, each cave has a treasure hidden within the farthest room from the cave's entrance. The Shogun has ordered his Samurais to explore these caves with Karakuri dolls (robots) and to find all treasures. These robots move in the caves and log relativ...
for _ in range(int(input())): m=dx=dy=0 while 1: x,y=map(int,input().split()) if x==y==0:break dx+=x dy+=y l=dx*dx+dy*dy if (m==l and mx<dx) or m<l:m,mx,my=l,dx,dy print(mx,my)
{ "input": [ "3\n1 0\n0 1\n-1 0\n1 0\n0 5\n-1 0\n0 -1\n5 0\n0 1\n0 -1\n1 0\n0 -4\n-3 0\n0 3\n2 0\n0 -2\n-1 0\n0 1\n0 -1\n1 0\n0 2\n-2 0\n0 -3\n3 0\n0 4\n-4 0\n0 -5\n-2 0\n0 0\n1 0\n-1 0\n0 0\n2 0\n0 1\n-1 0\n0 1\n-1 0\n0 -2\n0 0", "3\n1 0\n0 1\n-1 0\n1 0\n0 5\n-1 0\n0 -1\n5 0\n0 1\n0 -1\n1 0\n0 -4\n-3 0\n0 3\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are many caves deep in mountains found in the countryside. In legend, each cave has a treasure hidden within the farthest room from the cave's entrance. The Shogun has ordered h...
p00841 Atomic Car Race_24849
In the year 2020, a race of atomically energized cars will be held. Unlike today’s car races, fueling is not a concern of racing teams. Cars can run throughout the course without any refueling. Instead, the critical factor is tire (tyre). Teams should carefully plan where to change tires of their cars. The race is a r...
def solve(): from bisect import bisect from itertools import accumulate from sys import stdin f_i = stdin while True: n = int(f_i.readline()) if n == 0: break a = list(map(int, f_i.readline().split())) b = float(f_i.readline()) r, v, ...
{ "input": [ "2\n2 3\n1.0\n1 1.0 0.1 0.3\n5\n5 10 15 20 25\n0.15\n1 1.0 0.04 0.5\n10\n1783 3640 3991 4623 5465 5481 6369 6533 6865 8425\n4.172\n72 59.4705 0.0052834 0.0611224\n0", "2\n2 3\n1.0\n1 1.0 0.1 0.3\n5\n5 10 15 20 25\n0.15\n1 1.0 0.04 0.5\n10\n1783 3640 3991 4623 5465 5481 6369 6533 6865 8425\n4.172\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the year 2020, a race of atomically energized cars will be held. Unlike today’s car races, fueling is not a concern of racing teams. Cars can run throughout the course without any ...
p00973 Fair Chocolate-Cutting_24852
Fair Chocolate-Cutting You are given a flat piece of chocolate of convex polygon shape. You are to cut it into two pieces of precisely the same amount with a straight knife. Write a program that computes, for a given convex polygon, the maximum and minimum lengths of the line segments that divide the polygon into two...
#!/usr/bin/python3 from decimal import Decimal from fractions import Fraction import math import os import sys def main(): N = read_int() A = [Vec(*read_ints()) for _ in range(N)] print(*solve(N, A), sep='\n') def solve(N, Points): if DEBUG: dprint('polygon({!r}, fill=False)'.format([(v.x, ...
{ "input": [ "4\n0 0\n10 0\n10 10\n0 10", "4\n0 0\n10 0\n10 4\n0 10", "4\n0 0\n10 0\n10 2\n0 10", "4\n0 0\n10 0\n10 2\n0 9", "4\n0 0\n10 1\n10 2\n0 9", "4\n0 0\n10 1\n10 2\n1 9", "4\n0 0\n9 1\n10 2\n1 9", "4\n0 0\n9 1\n10 4\n1 9", "4\n0 0\n10 0\n14 10\n0 10", "4\n0 0\n7 0\n10 4...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Fair Chocolate-Cutting You are given a flat piece of chocolate of convex polygon shape. You are to cut it into two pieces of precisely the same amount with a straight knife. Write a...
p01105 Boolean Expression Compressor_24854
Boolean Expression Compressor You are asked to build a compressor for Boolean expressions that transforms expressions to the shortest form keeping their meaning. The grammar of the Boolean expressions has terminals `0` `1` `a` `b` `c` `d` `-` `^` `*` `(` `)`, start symbol <E> and the following production rule: > <E>...
a=65280; b=61680; c=52428; d=43690; e=65535 from heapq import heappush, heappop base = [a, b, c, d, e, 0] Q = [(1, el) for el in base] L = {el: 1 for el in base} H = [] get = L.get push = H.append while Q: l, p = heappop(Q) if L[p] < l: continue if l+1 < get(p ^ e, 17): L[p ^ e] = l+1 if l+1...
{ "input": [ "0\n(a*(1*b))\n(1^a)\n(-(-a*-b)*a)\n(a^(b^(c^d)))\n.", "0\n(a*(1*b))\n(1^a)\n(-(-a*-b)*a)\nca^(b^((^d)))\n.", "0\n(a*(1*b))\n(1^a)\n(-(-b*-b)*a)\nca^(b^((^d)))\n.", "0\n(a*(1*b))\n(0^a)\n(-(-b*-b)*a)\nca^(b^((^e)))\n.", "0\n(a*(0*b))\n(1^a)\n(-(-a*-b)*a)\n(a^(b^(c^d)))\n.", "0\na(...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Boolean Expression Compressor You are asked to build a compressor for Boolean expressions that transforms expressions to the shortest form keeping their meaning. The grammar of the ...
p01558 Substring_24861
Given a string of length n s = s1, s2,…, sn and m queries. Each query qk (1 ≤ k ≤ m) is one of four types, "L ++", "L-", "R ++", "R-", and l [for the kth query qk. k] and r [k] are defined below. * L ++: l [k] = l [k-1] + 1, r [k] = r [k-1] * L-: l [k] = l [k-1] -1, r [k] = r [k-1] * R ++: l [k] = l [k-1], r [k] = r...
def main(): n, m = map(int, input().split()) s = input() mem = set() base = ord("a") - 1 mod1 = 1000000007 mod2 = 2147483647 h1 = [0] h2 = [0] for c in s: h1.append((h1[-1] * 27 + ord(c) - base) % mod1) h2.append((h2[-1] * 27 + ord(c) - base) % mod2) pow_mem1 = {0:1} pow_mem2 = {0:1} def...
{ "input": [ "5 4\nabcde\nR++\nR++\nL++\nL--", "10 13\naacacbabac\nR++\nR++\nL++\nR++\nR++\nL++\nL++\nR++\nR++\nL--\nL--\nR--\nR--", "4 6\nabab\nR++\nL++\nR++\nL++\nR++\nL++", "4 6\nbaba\nR++\nL++\nR++\nL++\nR++\nL++", "6 4\nabcde\nR++\nR++\nL++\nL--", "6 2\nabdde\nR++\nR++\nL++\nL--", "4 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given a string of length n s = s1, s2,…, sn and m queries. Each query qk (1 ≤ k ≤ m) is one of four types, "L ++", "L-", "R ++", "R-", and l [for the kth query qk. k] and r [k] are de...
p01720 Minus One_24864
Ikta has an extraordinary feeling for undirected graphs. Ikta likes the undirected graph G and its two-point s, t pair (G, s, t) that has the greatest "beauty". The "beauty" of a pair (G, s, t) is the edge e = \\ {u, v \\} (u and v are two different points of G), and the shortest path from s to t in G. Is the number of...
from collections import deque, defaultdict n, m, s, t = map(int, input().split()) s -= 1 t -= 1 edges = [[] for _ in range(n)] for _ in range(m): x, y = map(int, input().split()) x -= 1 y -= 1 edges[x].append(y) edges[y].append(x) def dist_from(s): INF = 10 ** 20 dist = [INF] * n dist[s] = 0 que = de...
{ "input": [ "4 3 1 4\n1 2\n3 4\n4 1", "9 7 8 9\n9 6\n6 5\n3 6\n3 7\n2 5\n8 5\n1 4", "9 8 7 8\n2 6\n4 9\n8 6\n9 2\n3 8\n1 8\n8 5\n7 9", "3 2 1 3\n1 2\n2 3", "4 3 1 1\n1 2\n3 4\n4 1", "9 8 7 8\n2 6\n4 9\n8 6\n9 2\n3 8\n2 8\n8 5\n7 9", "3 3 1 3\n1 2\n2 3", "9 8 7 8\n2 6\n4 9\n8 6\n9 2\n3...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ikta has an extraordinary feeling for undirected graphs. Ikta likes the undirected graph G and its two-point s, t pair (G, s, t) that has the greatest "beauty". The "beauty" of a pair...
p01858 Sendame_24868
A: Isono, let's do that! --Sendame - story Nakajima "Isono ~, let's do that!" Isono "What is that, Nakajima" Nakajima "Look, that's that. It's hard to explain because I have to express it in letters for some reason." Isono "No, it seems that you can put in figures and photos, right?" <image> Nakajima "It's true!...
K = int(input()) Is = [input() for i in range(K)] Ns = [input() for i in range(K)] atk_I = 0 atk_N = 0 #"mamoru", "tameru", "kougekida" #"Isono-kun", "Nakajima-kun", "Hikiwake-kun" for i, n in zip(Is, Ns): i_n = [i, n] if (i_n.count("mamoru") == 2): pass elif (i_n.count("mamoru") == 1): if (i_n.count("tameru") =...
{ "input": [ "3\ntameru\ntameru\ntameru\ntameru\nkougekida\ntameru", "3\ntameru\ntameru\nuremat\ntameru\nkougekida\ntameru", "3\ntameru\ntameru\nuremat\ntameru\nadikeguok\ntameru", "3\ntameru\ntameru\nuremat\ntameru\nadikeguok\ntamdru", "3\ntameru\nuremat\nuremat\ntameru\nadikeguok\ntamdru", "...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A: Isono, let's do that! --Sendame - story Nakajima "Isono ~, let's do that!" Isono "What is that, Nakajima" Nakajima "Look, that's that. It's hard to explain because I have to ex...
p02280 Binary Trees_24875
A rooted binary tree is a tree with a root node in which every node has at most two children. Your task is to write a program which reads a rooted binary tree T and prints the following information for each node u of T: * node ID of u * parent of u * sibling of u * the number of children of u * depth of u * height of...
class Node: def __init__(self, parent, left, right): self.parent = parent self.left = left self.right = right d = {} def get_all_depth(t, u, p): d[u] = p if t[u].right is not None: get_all_depth(t, t[u].right, p+1) if t[u].left is not None: get_all_depth(t, t[u]....
{ "input": [ "9\n0 1 4\n1 2 3\n2 -1 -1\n3 -1 -1\n4 5 8\n5 6 7\n6 -1 -1\n7 -1 -1\n8 -1 -1" ], "output": [ "node 0: parent = -1, sibling = -1, degree = 2, depth = 0, height = 3, root\nnode 1: parent = 0, sibling = 4, degree = 2, depth = 1, height = 1, internal node\nnode 2: parent = 1, sibling = 3, degree =...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A rooted binary tree is a tree with a root node in which every node has at most two children. Your task is to write a program which reads a rooted binary tree T and prints the follow...
p02427 Enumeration of Subsets I_24878
Print all subsets of a set $S$, which contains $0, 1, ... n-1$ as elements. Note that we represent $0, 1, ... n-1$ as 00...0001, 00...0010, 00...0100, ..., 10...0000 in binary respectively and the integer representation of a subset is calculated by bitwise OR of existing elements. Constraints * $1 \leq n \leq 18$ In...
n = int(input()) print("0:") for b in range(1,2**n): a = bin(b)[2:] a = a[::-1] ans = [] for i in range(len(a)): if a[i] == "1": ans.append(i) ans.sort() print(str(b)+":",*ans)
{ "input": [ "4", "2", "1", "0", "6", "8", "3", "7", "5", "-1", "6", "8", "7", "2", "3", "5", "0", "-1", "1" ], "output": [ "0:\n1: 0\n2: 1\n3: 0 1\n4: 2\n5: 0 2\n6: 1 2\n7: 0 1 2\n8: 3\n9: 0 3\n10: 1 3\n11: 0 1 3\n12: 2 3\n13: 0 2 3\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Print all subsets of a set $S$, which contains $0, 1, ... n-1$ as elements. Note that we represent $0, 1, ... n-1$ as 00...0001, 00...0010, 00...0100, ..., 10...0000 in binary respect...
1055_C. Lucky Days_24890
Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1...
def gcd(a, b): return b if a == 0 else gcd(b % a, a) l1, r1, t1 = map(int, input().split()) l2, r2, t2 = map(int, input().split()) g = gcd(t1, t2) d = l2 - l1 s = d % g #print(g, d, s) ans1 = min(r1 - l1 - s, r2 - l2) + 1 s = (g - s) % g #print(s) ans2 = min(r1 - l1, r2 - l2 - s) + 1 print(max(ans1, ans2, 0)) #p...
{ "input": [ "0 2 5\n1 3 5\n", "0 1 3\n2 3 6\n", "111000111 111000119 999999994\n111000103 111000110 999999973\n", "0 0 3\n1 7 8\n", "4 18 999999960\n24 36 999999920\n", "3 3 1000000000\n1 2 999999997\n", "1 2 4\n0 0 6\n", "11 13 18\n4 6 12\n", "1 666666661 998776554\n5 666666666 9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and ...
1077_D. Cutting Out_24894
You are given an array s consisting of n integers. You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s. Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find s...
def check(d,h): temp=0 for i in d.values(): temp+=i//mid if temp>=k: return True return False n,k=map(int,input().split()) arr=list(map(int,input().split())) d={} for i in arr: if i in d:d[i]+=1 else:d[i]=1 l=1 r=n ans=0 while l<=r: mid=(l+r)//2 if check(d,mid): ans=max(ans,mid) ...
{ "input": [ "7 3\n1 2 3 2 4 3 1\n", "15 2\n1 2 1 1 1 2 1 1 2 1 2 1 1 1 1\n", "10 4\n1 3 1 3 10 3 7 7 12 3\n", "1 1\n1\n", "36 17\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3\n", "6 2\n1 2 2 3 4 4\n", "5 2\n2 2 2 3 1\n", "2 2\n1 1\n", "40 3\n1 1 1 1 1 1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an array s consisting of n integers. You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s. Cutting out t...
1098_C. Construct a tree_24897
Misha walked through the snowy forest and he was so fascinated by the trees to decide to draw his own tree! Misha would like to construct a rooted tree with n vertices, indexed from 1 to n, where the root has index 1. Every other vertex has a parent p_i, and i is called a child of vertex p_i. Vertex u belongs to the s...
def clc(mid): sm , i , cp = 0 , 1 , n cur = 1 while(cp + 1 > cur): sm = sm + i * cur i+= 1 cp -= cur cur = cur * mid return sm + i * cp n , s = map(int,input().split()) sm = n * (n + 1) // 2 dp = [0] * 100005 x = [0] * 100005 y = [0] * 100005 x[0] = 1 if s + 1 < 2 * n: ...
{ "input": [ "6 15\n", "3 5\n", "4 42\n", "675 1424\n", "100 334\n", "100 298\n", "100 362\n", "99 293\n", "630 2359\n", "3 3\n", "8265 17316\n", "652 1643\n", "386 925\n", "98 527\n", "896 2367\n", "2 1\n", "98 418\n", "98 280\n", "99 312\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Misha walked through the snowy forest and he was so fascinated by the trees to decide to draw his own tree! Misha would like to construct a rooted tree with n vertices, indexed from ...
1119_D. Frets On Fire_24901
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day. In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …...
import sys import math def lower(num,arr): ans=-1 low,high=0,len(arr)-1 while (low<=high): mid=(low+high)//2 if arr[mid]<=num: ans=max(ans,mid) low=mid+1 else: high=mid-1 return ans n=int(sys.stdin.readline()) arr=list(map(int,sys.stdin.readlin...
{ "input": [ "6\n3 1 4 1 5 9\n3\n7 7\n0 2\n8 17\n", "2\n1 500000000000000000\n2\n1000000000000000000 1000000000000000000\n0 1000000000000000000\n", "10\n2 5 3 3 4 2 3 5 3 0\n20\n4 5\n2 4\n0 2\n1 2\n0 1\n2 3\n0 4\n1 4\n2 4\n0 5\n1 5\n1 5\n2 5\n1 4\n0 3\n0 4\n5 5\n1 3\n0 3\n3 5\n", "10\n13 18 10 3 9 19 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day. In return, the fleas made a bigger uk...
1146_A. Love "A"_24905
Alice has a string s. She really likes the letter "a". She calls a string good if strictly more than half of the characters in that string are "a"s. For example "aaabb", "axaa" are good strings, and "baca", "awwwa", "" (empty string) are not. Alice can erase some characters from her string s. She would like to know wh...
lita=input() a=lita.count('a') b=len(lita) if a>(b/2): print(b) else: print(2*a-1)
{ "input": [ "aaabaa\n", "xaxxxxa\n", "a\n", "qfggoxltgxqqirqazusxblbhhuajrjrsuojnwvdnzwymhjx\n", "abababababababababababababababababababababababavv\n", "ap\n", "kvujzkexnkqygxalmlpupghpqqsvqsabpbgrhmwixfroghpm\n", "ass\n", "dya\n", "abababababababababababababababababababababab...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice has a string s. She really likes the letter "a". She calls a string good if strictly more than half of the characters in that string are "a"s. For example "aaabb", "axaa" are go...
1167_A. Telephone Number_24909
A telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388 and 80000011223388 are not. You are given a string s of length n, consisting of digits. In one operation you can delete any character from strin...
def check(n ,s): if n < 11: print("NO") return k = n-11 t = "8" if t in s[:k+1]: print("YES") return else: print("NO") return for i in range(int(input())): n = int(input()) s = input() check(n,s)
{ "input": [ "2\n13\n7818005553535\n11\n31415926535\n", "1\n11\n83583640644\n", "1\n11\n80000000000\n", "1\n11\n81840233933\n", "2\n13\n14222439333841\n11\n31415926535\n", "1\n11\n124350148638\n", "2\n13\n3872943379171\n11\n45489461254\n", "1\n18\n80000000000\n", "2\n13\n1422243933...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388 and 80000...
1185_D. Extra Element_24913
A sequence a_1, a_2, ..., a_k is called an arithmetic progression if for each i from 1 to k elements satisfy the condition a_i = a_1 + c ⋅ (i - 1) for some fixed c. For example, these five sequences are arithmetic progressions: [5, 7, 9, 11], [101], [101, 100, 99], [13, 97] and [5, 5, 5, 5, 5]. And these four sequence...
n=int(input()) a=list(map(int,input().split())) res=-1 a=list(map(lambda x: (x[1],x[0]),enumerate(a))) a.sort() cnt=0 if n==2: res=1 else: #keep 1 and 2 diff=a[1][0]-a[0][0] cnt=0 prev=a[0][0] ind=0 for i in range(1,len(a)): if a[i][0]-prev==diff: prev=a[i][0] else: cnt+=1 ind=a[...
{ "input": [ "8\n1 2 3 4 5 6 7 8\n", "5\n2 6 8 7 4\n", "4\n1 2 4 8\n", "3\n1 2 7\n", "5\n1 10 20 50 100\n", "5\n2 3 4 6 8\n", "5\n10 20 30 40 11\n", "2\n3 4\n", "4\n3 5 5 7\n", "5\n-2 -1 0 1 2\n", "5\n10 20 30 40 10\n", "5\n10 20 30 40 20\n", "10\n5 5 5 5 5 5 5 5 5 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A sequence a_1, a_2, ..., a_k is called an arithmetic progression if for each i from 1 to k elements satisfy the condition a_i = a_1 + c ⋅ (i - 1) for some fixed c. For example, thes...
1204_B. Mislove Has Lost an Array_24917
Mislove had an array a_1, a_2, ⋅⋅⋅, a_n of n positive integers, but he has lost it. He only remembers the following facts about it: * The number of different numbers in the array is not less than l and is not greater than r; * For each array's element a_i either a_i = 1 or a_i is even and there is a number (a_i)/(...
from sys import stdin def count(N, l, r): resultmin, resultmax = 0, 0 fmax, smax = 1, 1 for n in range(N): if l: resultmin += fmax fmax *= 2 l -= 1 else: resultmin += 1 if r: resultmax += smax smax *= 2 ...
{ "input": [ "4 2 2\n", "5 1 5\n", "114 18 19\n", "655 3 18\n", "1000 1 20\n", "106 16 18\n", "588 13 15\n", "1000 20 20\n", "1000 1 1\n", "1 1 1\n", "408 13 13\n", "879 17 17\n", "17 2 14\n", "624 7 17\n", "114 11 19\n", "539 3 18\n", "2 1 1\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mislove had an array a_1, a_2, ⋅⋅⋅, a_n of n positive integers, but he has lost it. He only remembers the following facts about it: * The number of different numbers in the array i...
1220_G. Geolocation_24921
You are working for the Gryzzl company, headquartered in Pawnee, Indiana. The new national park has been opened near Pawnee recently and you are to implement a geolocation system, so people won't get lost. The concept you developed is innovative and minimalistic. There will be n antennas located somewhere in the park....
import sys import math n = int(input()) x = [0]*n y = [0]*n for i in range(n): x[i], y[i] = map(int, input().split()) sx = sum(x) sy = sum(y) for i in range(n): x[i] = n * x[i] - sx y[i] = n * y[i] - sy m = int(input()) d = [0]*n e = [0]*n HD = 0 def check(a, b): global HD HE = 0 fo...
{ "input": [ "3\n0 0\n0 1\n1 0\n1\n1 1 2\n", "4\n0 0\n0 1\n1 0\n1 1\n2\n0 1 1 2\n2 5 5 8\n", "3\n0 0\n1 1\n1 0\n1\n1 1 2\n", "3\n0 0\n2 1\n1 0\n1\n1 1 2\n", "3\n0 0\n2 1\n1 0\n1\n1 2 2\n", "3\n0 -1\n2 1\n0 0\n1\n1 2 2\n", "4\n0 0\n0 1\n1 0\n1 1\n2\n0 1 1 2\n2 3 5 8\n", "4\n-1 0\n0 1\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are working for the Gryzzl company, headquartered in Pawnee, Indiana. The new national park has been opened near Pawnee recently and you are to implement a geolocation system, so...
1266_A. Competitive Programmer_24926
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...
T = int(input()) for t in range(T): a = [int(x) for x in list(input())] if sum(a)%3==0 and a.count(0)>0 and (a.count(0)+a.count(2)+a.count(4)+a.count(6)+a.count(8)>1): print("red") else: print("cyan")
{ "input": [ "6\n603\n006\n205\n228\n1053\n0000000000000000000000000000000000000000000000\n", "1\n900\n", "1\n300\n", "2\n10\n200\n", "15\n5318008\n379009\n06\n79153975193751375917591379919397337913333535330\n791539751937513759175913799193973379133301353533\n123456789023487138475693874561834576138...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: 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 eff...
1287_C. Garland_24930
Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland....
import sys n = int(input()) if n == 1: print(0) sys.exit() arr = list(map(int, input().split())) arr = [i % 2 if i != 0 else -1 for i in arr] skipped = sum([i == -1 for i in arr]) if skipped == n: print(1) sys.exit() ans = 0 for i in range(n - 1): if (arr[i] == 0) and (arr[i + 1] == 1) or (ar...
{ "input": [ "7\n1 0 0 5 0 0 2\n", "5\n0 5 0 2 3\n", "1\n0\n", "100\n0 92 0 75 16 87 0 0 0 0 5 0 0 0 0 60 0 0 52 35 0 17 59 0 0 0 57 0 0 0 0 0 0 0 0 58 41 0 0 0 44 38 0 0 0 0 0 0 68 0 0 67 25 89 0 0 0 0 0 0 0 47 39 0 0 0 0 0 40 0 74 3 0 0 0 0 0 0 0 0 0 81 45 0 0 0 0 0 0 0 0 64 0 0 0 0 0 56 2 11\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary o...
1307_C. Cow and Message_24934
Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside. The text is a string s of lowercase Latin letters. She considers a string t as hidden in string s if t exists as a subsequence of s whose indices form an arithmetic pr...
import os import sys def main(): s=input() dp=[[0]*(27) for i in range(27)] d=[0]*27 for i in range(len(s)): k=ord(s[i])-97 for j in range(27): dp[k][j]+=d[j] d[k]+=1 ans=0 for i in range(27): for j in range(27): ans=max(ans, dp[i][j]) for i in d: ans=max(ans , i) print(ans) if __name__ == "__ma...
{ "input": [ "aaabb\n", "lol\n", "usaco\n", "llllloooolllll\n", "bdhzijklmaopxqrstnfugvcwyeb\n", "z\n", "zyxwvutsrqponmlkjihgfedcba\n", "aaaa\n", "cc\n", "qqq\n", "doodddoo\n", "bdhzijklmaopxqrstnfugvcwye\n", "abcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxabcdefgh...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside. The text is a string s of lowe...
1330_C. Dreamoon Likes Coloring_24938
Dreamoon likes coloring cells very much. There is a row of n cells. Initially, all cells are empty (don't contain any color). Cells are numbered from 1 to n. You are given an integer m and m integers l_1, l_2, …, l_m (1 ≤ l_i ≤ n) Dreamoon will perform m operations. In i-th operation, Dreamoon will choose a number ...
import sys input = sys.stdin.readline n, m = map(int, input().split()) l = list(map(int, input().split())) sum_l = sum(l) if sum_l < n: print(-1) exit() diff = sum_l - n prev = 1 ans = [0] for i in range(m): ans.append(max(prev + ans[-1] - diff, ans[-1] + 1)) diff -= prev - (ans[-1] - ans[-2]) ...
{ "input": [ "10 1\n1\n", "5 3\n3 2 2\n", "6 3\n2 2 6\n", "6 3\n1 2 2\n", "5 3\n3 2 2\n", "2 2\n1 2\n", "100 3\n45 10 45\n", "10000 3\n3376 5122 6812\n", "100000 10\n31191 100000 99999 99999 99997 100000 99996 99994 99995 99993\n", "2000 100\n5 128 1368 1679 1265 313 1854 1512 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dreamoon likes coloring cells very much. There is a row of n cells. Initially, all cells are empty (don't contain any color). Cells are numbered from 1 to n. You are given an intege...
1350_C. Orac and LCM_24942
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow: * \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x. * lcm(s) is the minimum positive integer x, that divisible on all integ...
#x,y = map(int,input().split()) #arr = [int(i) for i in input().split()] #x,y = map(int,input().split()) from collections import Counter import math mod = 10**9+7 n = int(input()) arr = [int(i) for i in input().split()] suffix = [arr[-1]] for i in range(n-1): x = suffix[0] y = math.gcd(x,arr[n-i-2]) suffix.insert(...
{ "input": [ "10\n540 648 810 648 720 540 594 864 972 648\n", "4\n10 24 40 80\n", "2\n1 1\n", "2\n199999 200000\n", "2\n10007 20014\n", "2\n4 6\n", "2\n256 256\n", "2\n198761 199999\n", "5\n25 25 5 5 5\n", "10\n972 972 324 972 324 648 1944 243 324 474\n", "2\n3 3\n", "3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow: * \gcd(s) is the maximum posi...
1393_B. Applejack and Storages_24948
This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well). A...
import sys input = sys.stdin.readline n = int(input()) a = list(map(int,input().split())) u = [0]*int(1e5+1) for i in a: u[i] += 1 can1 = 0 can2 = 0 for i in u: can1 += i//4 can2 += i%4//2 q = int(input()) for i in range(q): p, x = map(str,input().split()) x = int(x) if p == "+": u[x] +...
{ "input": [ "6\n1 1 1 2 1 1\n6\n+ 2\n+ 1\n- 1\n+ 2\n- 1\n+ 2\n", "12\n1 1 1 1 2 2 2 2 3 3 3 3\n1\n+ 10\n", "5\n1 1 1 1 1\n1\n+ 1\n", "10\n1 1 1 1 1 1 1 1 1 1\n1\n+ 1\n", "1\n1\n34\n- 1\n+ 1\n+ 1\n+ 1\n+ 1\n+ 1\n+ 1\n+ 1\n+ 1\n+ 1\n- 1\n- 1\n- 1\n- 1\n- 1\n+ 2\n+ 2\n+ 2\n+ 2\n- 1\n+ 2\n+ 2\n- 1\n+...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages ...
1418_A. Buying Torches_24952
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft k torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: * exchange 1 stick for x sticks (you lose 1 stick and gain x sticks...
""" - math - from math import factorial from math import gcd from math import pi large_p = 10**9 + 7 from itertools import accumulate # 累積和 from operator import mul - other - from collections import Counter from itertools import combinations from itertools import combinations_with_replacement from itertools import perm...
{ "input": [ "5\n2 1 5\n42 13 24\n12 11 12\n1000000000 1000000000 1000000000\n2 1000000000 1000000000\n", "5\n2 2 5\n42 13 24\n12 11 12\n1000000000 1000000000 1000000000\n2 1000000000 1000000000\n", "5\n2 2 5\n42 13 24\n12 11 12\n1000000000 1000000000 1000000000\n2 1000000000 1000001000\n", "5\n2 2 5\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft k torches. One torch can be crafted using one stick and one coal. Hopefully, you...
145_C. Lucky Subsequence_24957
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has sequence a consisting of n integers. The subsequence of the sequence a is such subseque...
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) a = [4,7] d = dict() idx = 0 for p in range(1,10): for m in range(1<<p): v = 0 for i in range(p): v = v*10+a[(m >> i)&1] d[v] = idx idx += 1 #print(d) c = [0]*idx b = 0 for v in map(int, input().split()): ...
{ "input": [ "3 2\n10 10 10\n", "4 2\n4 4 7 7\n", "7 4\n1 2 3 4 5 6 7\n", "10 1\n1 2 3 4 5 6 7 8 9 10\n", "2 2\n44 44\n", "10 7\n1 2 3 4 5 6 7 8 9 10\n", "13 5\n44 44 44 44 44 44 44 44 77 55 66 99 55\n", "7 4\n7 7 7 7 7 7 7\n", "2 2\n47 47\n", "3 2\n1 47 47\n", "5 2\n47 47 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744...
1485_E. Move and Swap_24961
You are given n - 1 integers a_2, ..., a_n and a tree with n vertices rooted at vertex 1. The leaves are all at the same distance d from the root. Recall that a tree is a connected undirected graph without cycles. The distance between two vertices is the number of edges on the simple path between them. All non-root v...
from collections import deque T = int(input()) def solve(E, W): nodes = {idx: (w, []) for idx, w in weights.items()} for u, v in E: nodes[u][1].append(v) nodes[v][1].append(u) layers = [] visited = set([1]) parents = {idx: None for idx in weights.keys()} Q = deque([1]) whi...
{ "input": [ "4\n14\n1 1 1 2 3 4 4 5 5 6 7 8 8\n2 3 7 7 6 9 5 9 7 3 6 6 5\n6\n1 2 2 3 4\n32 78 69 5 41\n15\n1 15 1 10 4 9 11 2 4 1 8 6 10 11\n62 13 12 43 39 65 42 86 25 38 19 19 43 62\n15\n11 2 7 6 9 8 10 1 1 1 5 3 15 2\n50 19 30 35 9 45 13 24 8 44 16 26 10 40\n", "4\n14\n1 1 1 2 3 4 4 5 5 6 7 8 8\n2 3 7 7 6 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given n - 1 integers a_2, ..., a_n and a tree with n vertices rooted at vertex 1. The leaves are all at the same distance d from the root. Recall that a tree is a connected ...
1536_A. Omkar and Bad Story_24967
Omkar has received a message from Anton saying "Your story for problem A is confusing. Just make a formal statement." Because of this, Omkar gives you an array a = [a_1, a_2, …, a_n] of n distinct integers. An array b = [b_1, b_2, …, b_k] is called nice if for any two distinct elements b_i, b_j of b, |b_i-b_j| appears ...
from sys import stdin input = stdin.readline def solve(): n = int(input()) a = [int(x) for x in input().split()] for i in a: if i < 0: print("NO") return ans = list(range(0, 101)) print("YES") print(len(ans)) print(*ans) t = int(input()) for _ in range(t): ...
{ "input": [ "4\n3\n3 0 9\n2\n3 4\n5\n-7 3 13 -2 8\n4\n4 8 12 6\n", "6\n7\n-28 -70 -14 -56 98 70 56\n3\n28 56 98\n10\n56 -56 98 14 28 0 70 -42 -84 42\n4\n-42 -84 -70 -14\n7\n14 -56 42 28 -84 -42 -14\n2\n-70 84\n", "5\n4\n100 -100 0 50\n3\n50 0 -50\n5\n-50 100 -100 0 50\n5\n100 -100 0 50 -50\n4\n100 -100 0...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Omkar has received a message from Anton saying "Your story for problem A is confusing. Just make a formal statement." Because of this, Omkar gives you an array a = [a_1, a_2, …, a_n] ...
183_A. Headquarters_24971
Sensation, sensation in the two-dimensional kingdom! The police have caught a highly dangerous outlaw, member of the notorious "Pihters" gang. The law department states that the outlaw was driving from the gang's headquarters in his car when he crashed into an ice cream stall. The stall, the car, and the headquarters e...
n = int(input()) r = 1; c = 1 for i in range(n): s = input() if (len(s) == 4): r += 1; c += 1 elif(s == "UL" or s == "DR"): r += 1 else: c += 1 print(r * c)
{ "input": [ "2\nDR\nDL\n", "3\nUR\nUL\nULDR\n", "10\nUR\nDR\nUL\nDR\nUL\nULDR\nUR\nUL\nULDR\nUL\n", "6\nUL\nDL\nDL\nUL\nUL\nDL\n", "1\nDR\n", "1\nUR\n", "1\nULDR\n", "1\nUL\n", "4\nULDR\nUR\nULDR\nUR\n", "10\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL\n", "10\nUL\nUR\nUR\nDR\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sensation, sensation in the two-dimensional kingdom! The police have caught a highly dangerous outlaw, member of the notorious "Pihters" gang. The law department states that the outla...
230_B. T-primes_24977
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors. You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. Input The f...
N=1000000 a=[1]*N a[0]=a[1]=0 s=set() for i,x in enumerate(a): if x: s.add(i*i) for j in range(i+i,N,i): a[j] = 0 n= int(input()) lis = list(map(int,input().split())) for i in lis: print("YES" if i in s else "NO")
{ "input": [ "3\n4 5 6\n", "1\n10\n", "1\n225\n", "1\n1\n", "1\n16\n", "22\n1 2 3 4 5 6 7 8 9 10 12752041 64 121 144 27550356289 124 24657 23756 135153365 25235235235 42351351 81\n", "1\n999993399999\n", "1\n81\n", "6\n549755813888 847288609443 762939453125 678223072849 28531167061...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct ...
256_A. Almost Arithmetical Progression_24981
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1·q (i > 1), where q is som...
R = lambda: map(int, input().split()) n = int(input()) arr = list(R()) dp = [[0] * (n + 1) for i in range(n + 1)] for i in range(n): p = -1 for j in range(i): dp[i][j] = max(dp[i][j], dp[j][p] + 1) p = j if arr[j] == arr[i] else p print(max(max(dp[i]) for i in range(n)) + 1)
{ "input": [ "2\n3 5\n", "4\n10 20 10 30\n", "8\n2 2 5 3 4 3 3 2\n", "5\n4 4 3 5 1\n", "2\n468 335\n", "6\n2 3 2 2 1 3\n", "5\n479 359 963 465 706\n", "49\n516 161 416 850 361 861 833 233 281 798 225 771 841 111 481 617 463 305 743 945 833 141 70 617 511 522 840 505 753 544 931 213 626...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progressi...
27_A. Next Test_24985
«Polygon» is a system which allows to create programming tasks in a simple and professional way. When you add a test to the problem, the corresponding form asks you for the test index. As in most cases it is clear which index the next test will have, the system suggests the default value of the index. It is calculated ...
def main(): MAX = 3005 n = int(input()) nums = list(map(int, input().split(' '))) a = [False] * MAX for i in nums: a[i] = True for i in range(1, MAX): if not a[i]: return i return -1 print(main())
{ "input": [ "3\n1 7 2\n", "1\n3\n", "4\n4 2 3 1\n", "1\n3000\n", "5\n3 1 4 2 5\n", "7\n1 5 4 7 2 3 6\n", "4\n6 4 3 5\n", "8\n2 8 3 7 6 9 1 5\n", "3\n3 4 1\n", "10\n1 7 13 6 5 10 3 8 2 4\n", "1\n1\n", "7\n3 2 1 6 5 7 4\n", "1\n2\n", "9\n10 5 9 3 8 7 1 2 4\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: «Polygon» is a system which allows to create programming tasks in a simple and professional way. When you add a test to the problem, the corresponding form asks you for the test index...
329_A. Purification_24991
You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming an n × n grid. The rows are numbered 1 through n from top to bottom, and the columns are numbered 1 through n from left to right. At the far side of the ro...
a = [] n = int(input()) for i in range(0, n): a.append(input()) l = [0 for i in range(0, n)] c = [0 for i in range(0, n)] for i in range(0, n): for j in range(0, n): if a[i][j] == 'E': l[i] += 1 c[j] += 1 if n in l and n in c: print('-1') exit() if n in l: for j...
{ "input": [ "3\nEEE\nE..\nE.E\n", "3\n.E.\nE.E\n.E.\n", "5\nEE.EE\nE.EE.\nE...E\n.EE.E\nEE.EE\n", "4\n....\nE..E\nEEE.\n.EE.\n", "3\n.E.\nE.E\n.E.\n", "2\nEE\nEE\n", "3\nE..\nEEE\nE..\n", "3\n..E\n.EE\n.EE\n", "17\n.EEEEE...EEEE..EE\nEEE..E...EEEEE..E\n.E..E..EEE.EE...E\n.EEE.EE.....
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming an n × n grid. The...
34_D. Road Map_24995
There are n cities in Berland. Each city has its index — an integer number from 1 to n. The capital has index r1. All the roads in Berland are two-way. The road system is such that there is exactly one path from the capital to each city, i.e. the road map looks like a tree. In Berland's chronicles the road map is kept ...
import sys sys.setrecursionlimit(100000) n, r1, r2 = map(int, input().split()) a = list(map(int, input().split())) g = {} num = 0 for k, v in enumerate(a): num += 1 if num == r1: num += 1 # print(k, num, v) if num not in g: g[num] = [] g[num].append(v) if v not in g: g...
{ "input": [ "3 2 3\n2 2\n", "6 2 4\n6 1 2 4 2\n", "2 2 1\n2\n", "100 62 42\n45 66 13 18 93 47 83 19 41 66 5 70 16 29 54 28 65 91 92 24 90 54 7 40 77 53 68 58 70 65 68 30 94 80 16 18 69 21 94 1 99 61 97 2 69 15 79 38 61 46 88 32 57 84 35 18 17 77 38 91 84 2 53 54 68 19 13 78 62 16 63 17 76 49 28 45 94...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n cities in Berland. Each city has its index — an integer number from 1 to n. The capital has index r1. All the roads in Berland are two-way. The road system is such that th...
396_B. On Sum of Fractions_25001
Let's assume that * v(n) is the largest prime number, that does not exceed n; * u(n) is the smallest prime number strictly greater than n. Find <image>. Input The first line contains integer t (1 ≤ t ≤ 500) — the number of testscases. Each of the following t lines of the input contains integer n (2 ≤ n ≤ ...
T = int( input() ) MAX = 33000 bePrime = [0] * MAX; primNum = [] for j in range(2, MAX): if bePrime[j] == 0: primNum.append( j ) i = j while i < MAX: bePrime[i] = 1 i = i + j def isPrime( a ): for j in primNum: if j >= a: return True ...
{ "input": [ "2\n2\n3\n", "1\n1000000000\n", "5\n3\n6\n9\n10\n5\n", "6\n885\n419\n821\n635\n63\n480\n", "1\n649580447\n", "5\n7\n40\n37\n25\n4\n", "5\n5\n8\n18\n17\n17\n", "5\n79\n149\n136\n194\n124\n", "5\n72\n72\n30\n75\n11\n", "1\n1000001000\n", "5\n3\n6\n9\n17\n5\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's assume that * v(n) is the largest prime number, that does not exceed n; * u(n) is the smallest prime number strictly greater than n. Find <image>. Input The first li...
418_C. Square Table_25005
While resting on the ship after the "Russian Code Cup" a boy named Misha invented an interesting game. He promised to give his quadrocopter to whoever will be the first one to make a rectangular table of size n × m, consisting of positive integers such that the sum of the squares of numbers for each row and each column...
""" Codeforces Round 241 Div 1 Problem E 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...
{ "input": [ "1 1\n", "1 2\n", "4 23\n", "2 1\n", "33 1\n", "3 75\n", "77 93\n", "1 4\n", "7 14\n", "4 1\n", "2 11\n", "100 100\n", "77 20\n", "58 2\n", "30 31\n", "48 2\n", "48 24\n", "2 4\n", "3 1\n", "33 2\n", "3 57\n", "77 57\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: While resting on the ship after the "Russian Code Cup" a boy named Misha invented an interesting game. He promised to give his quadrocopter to whoever will be the first one to make a ...
445_A. DZY Loves Chessboard_25009
DZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the sa...
n,m = map(int,input().split()) for i in range(n): s = list(input()) for j in range(m): if s[j]==".": if (i+j)%2==0: s[j]="W" else:s[j]="B" k="".join(s) print(k)
{ "input": [ "3 3\n.-.\n---\n--.", "1 1\n.\n", "2 2\n..\n..\n", "8 35\n......................-............\n...-..........-........-...........\n...........-.......................\n-..................................\n...................................\n...........-............-.........-\n............
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: DZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY want...
467_B. Fedor and New Game_25013
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3». The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each ...
def dif(a, b): cont = 0 for i in range(0, len(a)): if a[i]!=b[i]: cont+=1 return cont n, m, k = [int(x) for x in input().split()] lista = [] string = '' for i in range (0, m+1): string = bin(int(input()))[2:] string = ('0'*(n-len(string)))+string if i<m: lista.appe...
{ "input": [ "7 3 1\n8\n5\n111\n17\n", "3 3 3\n1\n2\n3\n4\n", "13 27 7\n1893\n370\n7320\n5753\n4418\n2146\n2702\n5013\n5959\n1929\n7611\n7013\n540\n3454\n4271\n2887\n4724\n3520\n2111\n5845\n1868\n7659\n3040\n3775\n6507\n4917\n7506\n2515\n", "5 6 3\n16\n9\n24\n5\n17\n27\n20\n", "9 20 2\n197\n45\n21...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3». The game has (m + 1) players and n types...
53_C. Little Frog_25021
Once upon a time a little frog whose name was Vasya decided to travel around his home swamp. Overall there are n mounds on the swamp, located on one line. The distance between the neighboring mounds is one meter. Vasya wants to visit all the mounds in one day; besides, he wants to visit each one exactly once. For that ...
from itertools import zip_longest n = int(input()) z = (n+1)//2 for p,q in zip_longest(range(1,z+1),range(n,z,-1)): if q: print(p, q, end=' ') else: print(p)
{ "input": [ "3\n", "2\n", "7079\n", "9999\n", "7000\n", "9997\n", "4653\n", "9149\n", "2877\n", "5\n", "3114\n", "10000\n", "10000\n", "9998\n", "3193\n", "9997\n", "2956\n", "6\n", "3114\n", "9995\n", "9995\n", "9998\n", "4\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once upon a time a little frog whose name was Vasya decided to travel around his home swamp. Overall there are n mounds on the swamp, located on one line. The distance between the nei...
567_E. President and Roads_25025
Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer. Once a year the President visited his historic home town t, for which his motorcade passes alo...
from heapq import * import sys MOD = 1000000181 def addM(a,b): return (a+b)%MOD def mulM(a,b): return (a*b)%MOD def dijk(adj,n,s): dist = [10**18]*n ways = [0]*n frontier = [] dist[s] = 0 ways[s] = 1 heappush(frontier,(0,s)) while (len(frontier)>0): x = heappop(frontier) ...
{ "input": [ "3 3 1 3\n1 2 10\n2 3 10\n1 3 100\n", "2 2 1 2\n1 2 1\n1 2 2\n", "6 7 1 6\n1 2 2\n1 3 10\n2 3 7\n2 4 8\n3 5 3\n4 5 2\n5 6 1\n", "3 3 1 3\n2 3 7\n2 3 7\n1 2 6\n", "5 7 3 2\n5 4 8\n3 1 2\n1 2 20\n1 5 8\n4 2 4\n1 5 8\n5 4 9\n", "3 3 1 3\n1 2 1\n1 3 2\n2 3 1\n", "10 10 1 10\n1 4 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are connected by one-way roads, the travel time fo...
610_D. Vika and Segments_25030
Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbo...
from sys import stdin from itertools import repeat from collections import defaultdict def main(): n = int(stdin.readline()) h = defaultdict(list) w = defaultdict(list) for i in range(n): a, b, c, d = map(int, input().split()) if a > c: a, c = c, a if b > d: ...
{ "input": [ "3\n0 1 2 1\n1 4 1 2\n0 3 2 3\n", "4\n-2 -1 2 -1\n2 1 -2 1\n-1 -2 -1 2\n1 2 1 -2\n", "1\n1 1 1 1\n", "4\n553245544 -999999997 553245544 -918743333\n999999988 -668043590 264717840 -668043590\n-999999961 121002405 999999920 121002405\n999999985 121822043 -796706494 121822043\n", "10\n-3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical...
630_N. Forecast_25034
The Department of economic development of IT City created a model of city development till year 2100. To prepare report about growth perspectives it is required to get growth estimates from the model. To get the growth estimates it is required to solve a quadratic equation. Since the Department of economic developmen...
a,b,c=map(float,input().split()) q=((b*b-4*a*c)**0.5) x=((-b+q)/(2*a)) y=((-b-q)/(2*a)) print(max(x,y)) print(min(x,y))
{ "input": [ "1 30 200\n", "1 1 0\n", "-779 -814 321\n", "-1 1 1\n", "1000 1000 -999\n", "1 1 -1\n", "633 304 -186\n", "-1 1000 1\n", "1 0 -9\n", "1000 1 -1\n", "-181 -227 368\n", "-779 -814 481\n", "-1 2 1\n", "1000 1000 -1322\n", "2 1 -1\n", "633 498 -...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Department of economic development of IT City created a model of city development till year 2100. To prepare report about growth perspectives it is required to get growth estimat...