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
1009_D. Relatively Prime Graph_6601
Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|. Construct a rela...
from math import gcd from itertools import islice def get_all(n): for i in range(1, n+1): for j in range(i+1, n+1): if gcd(i, j) == 1: yield i, j def solve(n, m): x = list(islice(get_all(n), m)) if len(x) == m >= n - 1: return x res = solve(*map(int, input().split())) if res is...
{ "input": [ "5 6\n", "6 12\n", "2 2\n", "1 3\n", "3 5\n", "5 2\n", "5 6\n", "3 4\n", "4 2\n", "3 1\n", "10 31\n", "4 4\n", "1 1\n", "571 99404\n", "100000 99998\n", "530 100000\n", "4 1\n", "1 4\n", "100000 1\n", "10 32\n", "3 3\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge bet...
1032_A. Kitchen Utensils_6605
The king's birthday dinner was attended by k guests. The dinner was quite a success: every person has eaten several dishes (though the number of dishes was the same for every person) and every dish was served alongside with a new set of kitchen utensils. All types of utensils in the kingdom are numbered from 1 to 100....
import math n, k = map(int, input().split()) a = [int(t) for t in input().split()] d = {} for i in a: if d.get(i) is None: d[i] = 0 d[i] += 1 print(math.ceil(max(d.values()) / k) * len(d.keys()) * k - n)
{ "input": [ "10 3\n1 3 3 1 3 5 5 5 5 100\n", "5 2\n1 2 2 1 3\n", "100 1\n17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The king's birthday dinner was attended by k guests. The dinner was quite a success: every person has eaten several dishes (though the number of dishes was the same for every person) ...
1145_D. Pigeon d'Or_6617
From "ftying rats" to urban saniwation workers - can synthetic biology tronsform how we think of pigeons? The upiquitous pigeon has long been viewed as vermin - spleading disease, scavenging through trush, and defecating in populous urban spases. Yet they are product of selextive breeding for purposes as diverse as r...
n = int(input()) a = list(map(int, input().split())) print(2+(min(a)^a[2]))
{ "input": [ "5\n1 2 3 4 5\n", "7\n24 10 8 26 25 5 16\n", "7\n18 29 23 23 1 14 5\n", "9\n23 1 2 26 9 11 23 10 26\n", "7\n4 13 8 20 31 17 3\n", "6\n12 12 29 10 30 32\n", "8\n9 24 29 4 20 14 8 31\n", "7\n2 6 7 1 3 4 5\n", "5\n14 27 8 7 28\n", "6\n32 9 29 17 24 20\n", "8\n22 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: From "ftying rats" to urban saniwation workers - can synthetic biology tronsform how we think of pigeons? The upiquitous pigeon has long been viewed as vermin - spleading disease, s...
1166_C. A Tale of Two Lands_6621
The legend of the foundation of Vectorland talks of two integers x and y. Centuries ago, the array king placed two markers at points |x| and |y| on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at poin...
num = int(input()) data = [abs(int(i)) for i in input().split()] data.sort() def bins(a, b, n): if a == b: if data[a] <= n: return a+1 else: return a else: m = (a+b)//2 if data[m] <= n: return bins(m+1,b,n) else: return bi...
{ "input": [ "2\n3 6\n", "3\n2 5 -3\n", "3\n0 1000000000 -1000000000\n", "10\n9 1 2 3 5 7 4 10 6 8\n", "20\n55 -14 -28 13 -67 -23 58 2 -87 92 -80 62 -44 86 18 97 -47 63 32 94\n", "50\n-482 -431 -457 -473 -428 -427 -406 -422 -426 -472 -407 -441 -408 -475 -463 -443 -447 -450 -412 -436 -481 -454 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The legend of the foundation of Vectorland talks of two integers x and y. Centuries ago, the array king placed two markers at points |x| and |y| on the number line and conquered all t...
1185_A. Ropewalkers_6625
Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad. The rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions a, b and c respectively. At the end of the performa...
a, b, c, d = map(int, input().split()) a, b, c = sorted([a, b, c]) def solve1(a, b, c): r1 = max(0, d-(b-a)) b += r1 r2 = max(0, d-(c-b)) return r1+r2 def solve2(a, b, c): r1 = max(0, d-(c-b)) r2 = max(0, d-(b-a)) return r1+r2 def solve3(a, b, c): r1 = max(0, d-(c-b)) b -= r1 r2...
{ "input": [ "5 2 6 3\n", "2 3 10 4\n", "3 1 5 6\n", "8 3 3 2\n", "1 1 1 1\n", "1 2 1 1\n", "716270982 22102638 553198855 1000000000\n", "3 2 1 4\n", "3 5 8 1\n", "3 2 2 3\n", "89 983 751 1000\n", "1 4 4 5\n", "1 5 4 8\n", "9 6 2 5\n", "1 1 1 1000000000\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad. The rope is straight and infinite in both directions. At t...
1203_E. Boxers_6629
There are n boxers, the weight of the i-th boxer is a_i. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number. It is necessary to choose the largest boxing team in terms of the number o...
N = int(input()) A = sorted([int(a) for a in input().split()]) k = 0 ans = 0 for i in range(N): if k > A[i]: pass elif k >= A[i] - 1: ans += 1 k += 1 else: ans += 1 k = A[i] - 1 print(ans)
{ "input": [ "6\n1 1 1 4 4 4\n", "4\n3 2 4 1\n", "10\n8 9 4 9 6 10 8 2 7 1\n", "125\n8 85 125 177 140 158 2 152 67 102 9 61 29 188 102 193 112 194 15 7 133 134 145 7 89 193 130 149 134 51 13 194 191 149 73 106 151 85 42 53 163 134 127 7 51 61 178 105 117 197 35 21 122 164 133 165 135 12 192 60 54 187 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n boxers, the weight of the i-th boxer is a_i. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, ...
1220_C. Substring Game in the Lesson_6633
Mike and Ann are sitting in the classroom. The lesson is boring, so they decided to play an interesting game. Fortunately, all they need to play this game is a string s and a number k (0 ≤ k < |s|). At the beginning of the game, players are given a substring of s with left border l and right border r, both equal to k ...
s = input().rstrip() minimum = "z" for ch in s: if ch > minimum: print("Ann") else: print("Mike") minimum = ch
{ "input": [ "abba\n", "cba\n", "dyqyq\n", "pefrpnmnlfxihesbjxybgvxdvvbliljvqoxxzvhlpfxcucqurvzeygbnpghxkgaethtkgjvlctneuuqzfwypkajbfcmxpadcqdjwcrxjghjagep\n", "aabdbddcaabbcccabcbadadbbcddcbbdbbaadcbcdabdccbadbacdbbdcdbbccacca\n", "qrwrere\n", "vzcaonudppbkckblxwhscsobbkzdhvpdhcdytsrnsfkz...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mike and Ann are sitting in the classroom. The lesson is boring, so they decided to play an interesting game. Fortunately, all they need to play this game is a string s and a number k...
1245_E. Hyakugoku and Ladders_6637
Hyakugoku has just retired from being the resident deity of the South Black Snail Temple in order to pursue her dream of becoming a cartoonist. She spent six months in that temple just playing "Cat's Cradle" so now she wants to try a different game — "Snakes and Ladders". Unfortunately, she already killed all the snake...
X = [[int(a) for a in input().split()] for _ in range(10)] Y = [(i//10, 9-i%10 if (i//10)&1 else i%10) for i in range(100)] Z = [[i * 10 + 9 - j if i & 1 else i * 10 + j for j in range(10)] for i in range(10)] E = [0] * 100 F = [0] * 100 for i in range(1, 6): F[i] = E[i] = (sum(E[:i]) + 6) / i for i in range(6, 100...
{ "input": [ "0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 6 6 6 6 6 6 0 0 0\n1 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n", "0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Hyakugoku has just retired from being the resident deity of the South Black Snail Temple in order to pursue her dream of becoming a cartoonist. She spent six months in that temple jus...
1265_C. Beautiful Regional Contest_6641
So the Beautiful Regional Contest (BeRC) has come to an end! n students took part in the contest. The final standings are already known: the participant in the i-th place solved p_i problems. Since the participants are primarily sorted by the number of solved problems, then p_1 ≥ p_2 ≥ ... ≥ p_n. Help the jury distrib...
def func(a, total): d = {} temp = total for no in a: if total - 1 >= 0: total -= 1 if no in d: d[no] += 1 else: d[no] = 1 else: if no in d: del d[no] break if len(d) < 3: r...
{ "input": [ "5\n12\n5 4 4 3 2 2 1 1 1 1 1 1\n4\n4 3 2 1\n1\n1000000\n20\n20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\n32\n64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11\n", "5\n12\n5 4 4 3 2 2 1 1 1 1 1 1\n4\n4 3 2 1\n1\n1000000\n20\n20 19 18 17 16 15 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: So the Beautiful Regional Contest (BeRC) has come to an end! n students took part in the contest. The final standings are already known: the participant in the i-th place solved p_i p...
134_B. Pairs of Numbers_6651
Let's assume that we have a pair of numbers (a, b). We can get a new pair (a + b, b) or (a, a + b) from the given pair in a single step. Let the initial pair of numbers be (1,1). Your task is to find number k, that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals n...
import sys def solve(): n = int(input()) if n == 1: return 0 res = 1000000 for other in range(n - 1, 0, -1): pair = [n, other] temp = 0 while (pair[0] > 1 or pair[1] > 1) and (pair[0] > 0 and pair[1] > 0): pair.sort() multiples = (pair[1] - 1) // pair[0] ...
{ "input": [ "5\n", "1\n", "999998\n", "4\n", "123455\n", "10000\n", "998756\n", "1000000\n", "999879\n", "345612\n", "6\n", "999997\n", "990001\n", "8\n", "623423\n", "2\n", "345634\n", "777777\n", "9009\n", "100000\n", "2009\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 we have a pair of numbers (a, b). We can get a new pair (a + b, b) or (a, a + b) from the given pair in a single step. Let the initial pair of numbers be (1,1). You...
1370_B. GCD Compression_6655
Ashish has an array a of consisting of 2n positive integers. He wants to compress a into an array b of size n-1. To do this, he first discards exactly 2 (any two) elements from a. He then performs the following operation until there are no elements left in a: * Remove any two elements from a and append their sum to...
t = int(input()) def swap(a,b): if len(a)>len(b): return b,a else: return a,b def printcomb(a,b): a, b = swap(a,b) for i in range(0,len(a),2): print(a[i]+1, a[i+1]+1) for i in range(0,len(b),2): print(b[i]+1, b[i+1]+1) for i in range(t): n = int(...
{ "input": [ "3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3\n", "10\n9\n7 5 4 3 1 4 8 8 2 10 5 9 6 9 4 9 8 7\n2\n4 6 4 2\n2\n9 4 7 7\n4\n9 2 3 7 6 7 6 4\n10\n1 3 7 4 6 5 8 2 7 7 1 4 2 10 3 10 9 7 3 7\n10\n6 3 6 10 10 7 6 4 4 2 8 3 9 6 4 3 2 10 3 7\n3\n7 8 7 8 3 9\n5\n7 4 4 4 7 5 9 7 7 3\n3\n7 6 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ashish has an array a of consisting of 2n positive integers. He wants to compress a into an array b of size n-1. To do this, he first discards exactly 2 (any two) elements from a. He ...
1417_C. k-Amazing Numbers_6661
You are given an array a consisting of n integers numbered from 1 to n. Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there i...
import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.wri...
{ "input": [ "3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1\n", "3\n5\n4 4 4 4 2\n5\n1 2 3 4 5\n6\n1 3 1 5 3 1\n", "3\n5\n4 4 4 4 2\n5\n1 2 3 4 5\n6\n1 3 1 5 3 2\n", "3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 6 1 5 3 1\n", "3\n5\n4 4 3 4 2\n5\n1 2 3 4 5\n6\n1 3 1 5 3 2\n", "3\n5\n1 4 3 4 5\n5\n4 4 4...
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 a consisting of n integers numbered from 1 to n. Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of ...
1434_A. Perform Easily_6664
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has 6 strings and an infinite number of frets numbered from 1. Fretting the fret number j on the i-th string produces the note a_{i} + j. Tayuya wants to play a melody of n notes. Each note can be play...
import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.wri...
{ "input": [ "1 4 100 10 30 5\n6\n101 104 105 110 130 200\n", "1 1 2 2 3 3\n7\n13 4 11 12 11 13 12\n", "5 4 7 6 4 1\n10\n19 16 18 12 16 15 16 20 16 14\n", "158260522 877914575 602436426 24979445 861648772 623690081\n1\n896194147\n", "58260522 77914575 2436426 24979445 61648772 23690081\n10\n582107...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has 6 strings and an infinite number of frets numbered from 1. Fr...
1459_A. Red-Blue Shuffle_6668
There are n cards numbered 1, …, n. The card i has a red digit r_i and a blue digit b_i written on it. We arrange all n cards in random order from left to right, with all permutations of 1, …, n having the same probability. We then read all red digits on the cards from left to right, and obtain an integer R. In the sa...
for _ in range(int(input())): n=int(input()) s=input() s1=input() a=0 b=0 for i in range(n): if s[i]>s1[i]: a=a+1 elif s[i]<s1[i]: b=b+1 if a>b: print("RED") elif b>a: print("BLUE") else: print("EQUAL") ...
{ "input": [ "3\n3\n777\n111\n3\n314\n159\n5\n09281\n09281\n", "3\n3\n777\n111\n3\n314\n159\n5\n14195\n09281\n", "3\n3\n777\n111\n3\n314\n159\n5\n14195\n18546\n", "3\n3\n777\n111\n3\n196\n159\n5\n14195\n11156\n", "3\n3\n777\n111\n3\n314\n159\n5\n14195\n18703\n", "3\n3\n150\n111\n3\n314\n159\n5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n cards numbered 1, …, n. The card i has a red digit r_i and a blue digit b_i written on it. We arrange all n cards in random order from left to right, with all permutation...
1485_A. Add and Divide_6672
You have two positive integers a and b. You can perform two kinds of operations: * a = ⌊ a/b ⌋ (replace a with the integer part of the division between a and b) * b=b+1 (increase b by 1) Find the minimum number of operations required to make a=0. Input The first line contains a single integer t (1 ≤ t ≤ 10...
def incre(a,b): flag=0 if a//b==0: return 1 if b==1: b+=1 flag=1 tb=b ans=float("inf") if a//b==0: return 1+flag while b<=a: ta=a cnt=b-tb while ta: ta//=b cnt+=1 b+=1 if ans<cnt: return ans+flag ans=min(ans,cnt) return ans+flag for i in range(int(input())): a,b=map(int,input().st...
{ "input": [ "6\n9 2\n1337 1\n1 1\n50000000 4\n991026972 997\n1234 5678\n", "9\n871488411 1\n361430393 1\n555768197 15\n191 44\n635551913 23\n282073944 35\n128 69\n842026539 6\n40 39\n", "19\n45713448 35\n788943748 304424261\n345831907 1\n887647097 205879610\n467441822 2\n716573439 455654889\n385540845 22...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have two positive integers a and b. You can perform two kinds of operations: * a = ⌊ a/b ⌋ (replace a with the integer part of the division between a and b) * b=b+1 (increa...
1509_B. TMT Document_6676
The student council has a shared document file. Every day, some members of the student council write the sequence TMT (short for Towa Maji Tenshi) in it. However, one day, the members somehow entered the sequence into the document at the same time, creating a jumbled mess. Therefore, it is Suguru Doujima's task to fig...
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": [ "5\n3\nTMT\n3\nMTT\n6\nTMTMTT\n6\nTMTTTT\n6\nTTMMTT\n", "5\n3\nTMT\n3\nMTT\n6\nTMTMTT\n6\nMTTTTT\n6\nTTMMTT\n", "5\n3\nTMT\n3\nMTT\n6\nTMTMTT\n6\nMTTTTT\n6\nTTMTTM\n", "5\n3\nTMT\n3\nMTT\n6\nTTTMMT\n6\nTMTTTT\n6\nTTMMTT\n", "5\n3\nTTM\n3\nTTM\n6\nTMTMTT\n6\nMTTTTT\n6\nTTMMTT\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The student council has a shared document file. Every day, some members of the student council write the sequence TMT (short for Towa Maji Tenshi) in it. However, one day, the member...
1535_C. Unstable String_6680
You are given a string s consisting of the characters 0, 1, and ?. Let's call a string unstable if it consists of the characters 0 and 1 and any two adjacent characters are different (i. e. it has the form 010101... or 101010...). Let's call a string beautiful if it consists of the characters 0, 1, and ?, and you can...
for s in[*open(0)][1:]: r=p=i=j=k=0 for x in s[:-1]: i+=1;x=ord(x)^i&1 if x<50:k=(k,j)[p!=x];p=x;j=i r+=i-k print(r)
{ "input": [ "3\n0?10\n???\n?10??1100\n", "1\n00000000\n", "1\n00000100\n", "3\n1?10\n???\n?10??1100\n", "1\n00000101\n", "1\n01010000\n", "1\n01110000\n", "1\n11000000\n", "1\n01000110\n", "1\n01000101\n", "1\n01000111\n", "1\n10001011\n", "1\n10100101\n", "1\n...
2CODEFORCES
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 consisting of the characters 0, 1, and ?. Let's call a string unstable if it consists of the characters 0 and 1 and any two adjacent characters are different...
182_B. Vasya's Calendar_6684
Vasya lives in a strange world. The year has n months and the i-th month has ai days. Vasya got a New Year present — the clock that shows not only the time, but also the date. The clock's face can display any number from 1 to d. It is guaranteed that ai ≤ d for all i from 1 to n. The clock does not keep information ab...
d = int(input()) n = int(input()) months = list(map(int, input().split())) changes = 0 for month in months[:-1]: changes += d - month print(changes)
{ "input": [ "31\n12\n31 28 31 30 31 30 31 31 30 31 30 31\n", "5\n3\n3 4 3\n", "4\n2\n2 2\n", "102\n100\n31 22 59 16 11 56 81 4 19 31 8 72 4 92 18 7 13 12 62 40 34 67 40 23 96 4 90 28 3 18 54 49 10 71 73 79 69 7 41 75 59 13 2 78 72 6 95 33 52 97 7 86 57 94 12 93 19 94 59 28 5 96 46 102 2 101 57 85 53 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya lives in a strange world. The year has n months and the i-th month has ai days. Vasya got a New Year present — the clock that shows not only the time, but also the date. The cl...
205_D. Little Elephant and Cards_6688
The Little Elephant loves to play with color cards. He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks...
def solve(): n = int(input()) cards = [] cnt = {} for i in range(n): card = tuple(map(int, input().split(' '))) cards.append(card) cnt[card[0]] = [0, 0] cnt[card[1]] = [0, 0] for card in cards: if card[0] != card[1]: cnt[card[0]][0] += 1 ...
{ "input": [ "3\n4 7\n4 7\n7 4\n", "5\n4 7\n7 4\n2 11\n9 7\n1 1\n", "1\n1 2\n", "2\n1 1\n1 1\n", "5\n1 2\n1 3\n4 1\n5 1\n6 7\n", "4\n1 5\n2 5\n3 5\n4 4\n", "5\n1 5\n2 5\n3 5\n4 7\n2 5\n", "5\n1 6\n2 6\n3 6\n4 4\n5 5\n", "12\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Little Elephant loves to play with color cards. He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards...
22_C. System Administrator_6692
Bob got a job as a system administrator in X corporation. His first task was to connect n servers with the help of m two-way direct connection so that it becomes possible to transmit data from one server to any other server via these connections. Each direct connection has to link two different servers, each pair of se...
if __name__ == '__main__': n, m, v = (int(_) for _ in input().split()) if m < n - 1: print(-1) elif m > (n - 1) * (n - 2) / 2 + 1: print(-1) else: v = v % n print((v - 1) % n + 1, v + 1) for i in range(1, n - 1): print((v + i) % n + 1, (v + i + 1) %...
{ "input": [ "5 6 3\n", "6 100 1\n", "10 39377 1\n", "300 44553 1\n", "20 155 1\n", "300 44553 300\n", "100000 99998 1\n", "100000 0 100000\n", "200 34305 75\n", "30 393 29\n", "100 4283 65\n", "100000 99998 100000\n", "20 155 1\n", "5 6 3\n", "30 393 29\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bob got a job as a system administrator in X corporation. His first task was to connect n servers with the help of m two-way direct connection so that it becomes possible to transmit ...
255_B. Code Parsing_6696
Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with string s, consisting of characters "x" and "y", and uses two following operations at runtime: 1. Find two consecutive characters in the string, such that the first of them equals "y", and the s...
from sys import stdin,stdout import bisect as bs nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int,stdin.readline().split())) for _ in range(1):#nmbr()): s=input() n=len(s) x=s.count('x') y=n-x if y>=x: for i in range(y-x): stdout.write('y') else: for i ...
{ "input": [ "x\n", "yxyxy\n", "xxxxxy\n", "xxxyx\n", "xxxxxxxxxxxyxyyxxxxyxxxxxyxxxxxyxxxxxxxxyx\n", "yxyyxyyx\n", "yxxyxyx\n", "xxx\n", "xyyxyyyyyxxxxxxxyxyxyyxyyxyyxxyxyxyxxxyxxy\n", "yxxxx\n", "xxyxx\n", "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n", "xxxxxxxxx...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with string s, consisting of characters "x" and "y", and uses tw...
279_B. Books_6700
When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the ...
import bisect n,t=list(map(int,input().split())) a=list(map(int,input().rstrip().split())) b=[0] sol=0 for i in range(n): b.append(b[-1]+a[i]) b.pop(0) for i in range(n): sol=max(sol,bisect.bisect_right(b,t)-i) t+=a[i] print(sol)
{ "input": [ "4 5\n3 1 2 1\n", "3 3\n2 2 3\n", "20 30\n8 1 2 6 9 4 1 9 9 10 4 7 8 9 5 7 1 8 7 4\n", "2 10\n6 4\n", "100 100\n75 92 18 6 81 67 7 92 100 65 82 32 50 67 85 31 80 91 84 63 39 52 92 81 1 98 24 12 43 48 17 86 51 72 48 95 45 50 12 66 19 79 49 89 34 1 97 75 20 33 96 27 42 23 73 71 93 1 85 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book h...
301_A. Yaroslav and Sequence_6704
Yaroslav has an array, consisting of (2·n - 1) integers. In a single operation Yaroslav can change the sign of exactly n elements in the array. In other words, in one operation Yaroslav can select exactly n array elements, and multiply each of them by -1. Yaroslav is now wondering: what maximum sum of array elements c...
#!/usr/bin/python3 n = int(input()) data = list(map(int, input().split())) negative, zero, positive = 0, 0, 0 for element in data: if element < 0: negative += 1 elif element == 0: zero += 1 else: positive += 1 seen = {} min_negative = negative def go(negative, positive): glob...
{ "input": [ "2\n-1 -100 -1\n", "2\n50 50 50\n", "5\n270 -181 957 -509 -6 937 -175 434 -625\n", "6\n-403 901 -847 -708 -624 413 -293 709 886 445 716\n", "4\n717 473 344 -51 -548 703 -869\n", "5\n-1 -2 -3 -4 -5 -6 -7 8 9\n", "22\n549 635 969 -519 -858 185 -788 126 -840 533 -560 168 -539 -96...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Yaroslav has an array, consisting of (2·n - 1) integers. In a single operation Yaroslav can change the sign of exactly n elements in the array. In other words, in one operation Yarosl...
327_D. Block Tower_6708
After too much playing on paper, Iahub has switched to computer games. The game he plays is called "Block Towers". It is played in a rectangular grid with n rows and m columns (it contains n × m cells). The goal of the game is to build your own city. Some cells in the grid are big holes, where Iahub can't build any bui...
#Connected component import sys from collections import deque sys.setrecursionlimit(501 * 501) n,m = [int(i) for i in input().split()] a=[[0 for i in range(m)] for i in range(n)] d=[(0,1),(0,-1),(1,0),(-1,0)] q=deque() def main(): global a ans=[] first=[] q=deque() for i in range(n): l...
{ "input": [ "1 3\n...\n", "2 3\n..#\n.#.\n", "1 1\n#\n", "1 500\n...............................................................................................................................................................................................................................................
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After too much playing on paper, Iahub has switched to computer games. The game he plays is called "Block Towers". It is played in a rectangular grid with n rows and m columns (it con...
373_A. Collecting Beats is Fun_6714
Cucumber boy is fan of Kyubeat, a famous music game. Kyubeat has 16 panels for playing arranged in 4 × 4 table. When a panel lights up, he has to press that panel. Each panel has a timing to press (the preffered time when a player should press it), and Cucumber boy is able to press at most k panels in a time with his...
n = int(input()) memo = {} for _ in range(4): s = input() for i in s: if i != '.': if i not in memo: memo[i] = 1 else: memo[i] += 1 res = True for k, v in memo.items(): if v > n*2: res = False if res: print("YES") else: pri...
{ "input": [ "5\n..1.\n1111\n..1.\n..1.\n", "1\n.135\n1247\n3468\n5789\n", "1\n....\n12.1\n.2..\n.2..\n", "4\n7777\n..7.\n.7..\n7...\n", "2\n8888\n8888\n8888\n8888\n", "4\n1111\n1221\n1221\n1111\n", "4\n...1\n..2.\n.3..\n4...\n", "1\n....\n....\n....\n....\n", "5\n4.4.\n4.4.\n4444\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Cucumber boy is fan of Kyubeat, a famous music game. Kyubeat has 16 panels for playing arranged in 4 × 4 table. When a panel lights up, he has to press that panel. Each panel has a ...
394_C. Dominoes_6718
During the break, we decided to relax and play dominoes. Our box with Domino was empty, so we decided to borrow the teacher's dominoes. The teacher responded instantly at our request. He put nm dominoes on the table as an n × 2m rectangle so that each of the n rows contained m dominoes arranged horizontally. Each half...
n, m = map(int, input().split()) doubles, singles = 0, 0 for r in range(n): for s in input().split(): if s == '11': doubles += 1 elif s != '00': singles += 1 lines = { 'zero': ' '.join(m * [ '00' ]), 'double': ' '.join(m * [ '11' ]), 'single_0': ' '.join(m * [ '01...
{ "input": [ "4 1\n11\n10\n01\n00\n", "2 3\n01 11 00\n00 01 11\n", "1 1\n11\n", "2 3\n00 10 01\n01 01 00\n", "9 9\n10 10 10 01 10 11 11 01 10\n11 00 10 10 11 10 01 00 00\n10 00 11 01 00 01 01 11 10\n10 11 10 00 01 11 11 10 11\n01 11 11 01 11 00 10 00 01\n01 00 00 10 01 01 10 00 01\n11 10 11 10 01 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: During the break, we decided to relax and play dominoes. Our box with Domino was empty, so we decided to borrow the teacher's dominoes. The teacher responded instantly at our request...
466_C. Number of Ways_6726
You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same. More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that <image>...
def NoW(xs): if sum(xs) % 3 != 0: return 0 else: part = sum(xs) // 3 ci = ret = 0 acum = xs[0] for i, x in enumerate(xs[1:]): if acum == 2*part: # print("2. x=",x) ret += ci if acum == part: # print("...
{ "input": [ "2\n4 1\n", "4\n0 1 -1 0\n", "5\n1 2 3 0 3\n", "5\n1 1 1 1 1\n", "4\n0 2 -1 2\n", "9\n0 0 0 0 0 0 0 0 0\n", "100\n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elemen...
48_A. Rock-paper-scissors_6730
Uncle Fyodor, Matroskin the Cat and Sharic the Dog live their simple but happy lives in Prostokvashino. Sometimes they receive parcels from Uncle Fyodor’s parents and sometimes from anonymous benefactors, in which case it is hard to determine to which one of them the package has been sent. A photographic rifle is obvio...
I = lambda: int(input()) IL = lambda: list(map(int, input().split())) L = [input()[0] for i in '123'] gt = {'r': 'rp', 'p': 'ps', 's': 'sr'} print('F' if L[1] not in gt[L[0]] and L[2] not in gt[L[0]] else 'M' if L[0] not in gt[L[1]] and L[2] not in gt[L[1]] else 'S' if L[0] not in gt[L[2]] and L[1] not in...
{ "input": [ "scissors\nrock\nrock\n", "scissors\npaper\nrock\n", "rock\nrock\nrock\n", "paper\nrock\nrock\n", "rock\npaper\nrock\n", "scissors\nscissors\nrock\n", "rock\nscissors\npaper\n", "rock\npaper\nscissors\n", "scissors\nscissors\nscissors\n", "scissors\nscissors\npaper...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Uncle Fyodor, Matroskin the Cat and Sharic the Dog live their simple but happy lives in Prostokvashino. Sometimes they receive parcels from Uncle Fyodor’s parents and sometimes from a...
567_A. Lineland Mail_6738
All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position xi — a coordinate on the Ox axis. No two cities are located at a single point. Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (bec...
n = int(input()) a = [int(i) for i in input().split()] for i in range(n): print(min(abs(a[i]-a[(i+1)%n]), abs(a[i]-a[i-1])), max(abs(a[i]-a[0]), abs(a[i]-a[-1])))
{ "input": [ "2\n-1 1\n", "4\n-5 -2 2 7\n", "3\n-1000000000 0 1000000000\n", "5\n-2 -1 0 1 2\n", "10\n-876779400 -829849659 -781819137 -570920213 18428128 25280705 121178189 219147240 528386329 923854124\n", "3\n-1 0 1\n", "4\n-1 0 1 3\n", "10\n-857422304 -529223472 82412729 145077145 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position xi — a coordinate on the Ox axis. No two cities are located at a single p...
588_C. Duff and Weight Lifting_6742
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimi...
from collections import * import sys import math from functools import reduce def factors(n): return set(reduce(list.__add__,([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) def li():return [int(i) for i in input().rstrip('\n').split(' ')] def st():return input().rstrip('\n') def val():return i...
{ "input": [ "5\n1 1 2 3 3\n", "4\n0 1 2 3\n", "13\n92 194 580495 0 10855 41704 13 96429 33 213 0 92 140599\n", "1\n120287\n", "35\n130212 3176 77075 8071 18 1369 7539 1683 80757 1847 0 1374 122 8524 4 2 21333 270264 4 9254 151921 0 1 33596 73002 54382 0 1 29233 75952 15 38892 1877 6167 4\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Du...
60_E. Mushroom Gnomes_6746
Once upon a time in the thicket of the mushroom forest lived mushroom gnomes. They were famous among their neighbors for their magic mushrooms. Their magic nature made it possible that between every two neighboring mushrooms every minute grew another mushroom with the weight equal to the sum of weights of two neighbori...
#!/usr/bin/pypy3 from sys import stdin, stdout input, print = stdin.readline, stdout.write p = 0 def readints(): return list(map(int, input().split())) def writeln(x): print(str(x) + '\n') def mod(x): return (x % p + p) % p def matmul(a, b): n = len(a) c = [[0 for x in range(n)] for y in range(n...
{ "input": [ "2 1 0 657276545\n1 2\n", "4 5 0 10000\n1 2 3 4\n", "2 1 1 888450282\n1 2\n", "8 4 9 371687114\n1 7 22 31 35 38 62 84\n", "2 1000000000 1000000000000 10000\n1 2\n", "1 1 1 2\n0\n", "4 0 8 414790855\n1 88 97 99\n", "1 1 1 2\n1000000000\n", "1 1 1 2\n2\n", "1 0 1 100...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once upon a time in the thicket of the mushroom forest lived mushroom gnomes. They were famous among their neighbors for their magic mushrooms. Their magic nature made it possible tha...
630_J. Divisibility_6750
IT City company developing computer games invented a new way to reward its employees. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is divisible by all numbers from 2 to 10 every developer ...
n = int(input()) a = n // 2 + n // 3 + n // 5 + n // 7 b = n // 6 + n // 10 + n // 14 + n // 15 + n // 21 + n // 35 c = n // 30 + n // 42 + n // 70 + n // 105 d = n // 210 print(n - (a - b + c - d))
{ "input": [ "3000\n", "2519\n", "12\n", "2520\n", "1\n", "3628800\n", "718281828459045235\n", "2521\n", "987654321234567890\n", "1000000000000000000\n", "314159265\n", "504000000000000000\n", "2130\n", "4873\n", "5350110\n", "1209365011581446835\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: IT City company developing computer games invented a new way to reward its employees. After a new game release users start buying it actively, and the company tracks the number of sal...
659_B. Qualifying Contest_6754
Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m ...
#!/usr/bin/env python3 read_ints = lambda : list(map(int, input().split())) if __name__ == '__main__': n, m = read_ints() R = [[] for _ in range(m)] for i in range(n): n,r,s = input().split() r = int(r) s = int(s) R[r-1].append((s,n)) for r in R: r.sort(reverse...
{ "input": [ "5 2\nIvanov 1 763\nAndreev 2 800\nPetrov 1 595\nSidorov 1 790\nSemenov 2 503\n", "5 2\nIvanov 1 800\nAndreev 2 763\nPetrov 1 800\nSidorov 1 800\nSemenov 2 503\n", "2 1\na 1 10\nb 1 10\n", "2 1\nOAELh 1 733\nbFGs 1 270\n", "2 1\nA 1 5\nB 1 5\n", "3 1\nA 1 5\nB 1 4\nC 1 4\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying conte...
681_D. Gifts by the List_6758
Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n. Each man has at most one father but may have arbitrary number of sons. Man number A is considered to b...
# [https://codeforces.com/contest/681/submission/37694242 <- https://codeforces.com/contest/681/status/D <- https://codeforces.com/contest/681 <- https://codeforces.com/blog/entry/45425 <- https://codeforces.com/problemset/problem/681/D <- https://algoprog.ru/material/pc681pD] (n, m) = map(int, input().split()) adj = ...
{ "input": [ "3 2\n1 2\n2 3\n1 2 1\n", "4 2\n1 2\n3 4\n1 2 3 3\n", "1 0\n1\n", "4 3\n4 3\n3 2\n2 1\n3 4 4 4\n", "4 3\n1 2\n2 3\n3 4\n1 1 1 2\n", "4 3\n1 2\n2 3\n3 4\n1 1 3 2\n", "2 1\n2 1\n1 2\n", "2 1\n2 1\n2 2\n", "4 3\n1 2\n2 3\n3 4\n1 1 1 4\n", "4 3\n1 2\n2 3\n3 4\n2 1 1 2\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number...
727_A. Transformation: from A to B_6764
Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2·x); * append the digit 1 to the right of current number (that is, replace the number x by 10·x + 1). You need to help Vasil...
n, target = map(int, input().split()) def keep(target, ans): #print(target) while target % 2 == 0: target = target // 2 ans = [target]+ans if str(target).endswith("1") and target != 1: return keep((target-1) // 10, [(target-1) // 10]+ans) else: return target, ans next,...
{ "input": [ "4 42\n", "2 162\n", "100 40021\n", "999999999 1000000000\n", "13494 1079528\n", "92387 184774\n", "25539 510782222\n", "91939 9193911\n", "9411 188222222\n", "250000000 705032705\n", "1 111111111\n", "30518 610361\n", "96022 1920442\n", "30070 3007...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number ...
748_B. Santa Claus and Keyboard Check_6768
Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. ...
#k=int(input()) #n,m=map(int,input().split()) #a=list(map(int,input().split())) #b=list(map(int,input().split())) a=input() b=input() l=len(a) d=set() d2=set() deg=dict() for i in range(l): if a[i]==b[i]: d2.add(a[i]) deg[a[i]]=0 deg[b[i]]=0 for i in range(l): if a[i]!=b[i]: if ...
{ "input": [ "helloworld\nehoolwlroz\n", "hastalavistababy\nhastalavistababy\n", "merrychristmas\nchristmasmerry\n", "acbacacbbb\nacbacacbbb\n", "dacbdbbbdd\nadbdadddaa\n", "ijpanyhovzwjjxsvaiyhchfaulcsdgfszjnwtoqbtaqygfmxuwvynvlhqhvmkjbooklxfhmqlqvfoxlnoclfxtbhvnkmhjcmrsdc\nijpanyhovzwjjxsvai...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa ...
771_A. Bear and Friendship Condition_6772
Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that membe...
n,m = map(int,input().split()) dic = {} edges = [] from collections import Counter def find(node): tmp = node while node!=dic[node]: node = dic[node] while tmp!=dic[tmp]: dic[tmp],tmp=node,dic[tmp] return node for _ in range(m): p1,p2 = map(int,input().split()) dic.setdefault(p1,...
{ "input": [ "10 4\n4 3\n5 10\n8 9\n1 2\n", "4 4\n3 1\n2 3\n3 4\n1 2\n", "3 2\n1 2\n2 3\n", "4 3\n1 3\n3 4\n1 4\n", "4 5\n1 2\n1 3\n1 4\n2 3\n2 4\n", "6 6\n1 2\n2 3\n3 4\n4 5\n5 6\n1 6\n", "4 3\n1 2\n1 3\n1 4\n", "4 4\n1 2\n2 3\n3 4\n4 1\n", "5 5\n1 2\n2 3\n3 4\n4 5\n5 1\n", "1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members...
796_A. Buying A House_6776
Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: hou...
def main(): n, m, k = map(int, input().split()) m -= 1 a = list(map(int, input().split())) min_dist = n for i, ai in enumerate(a): if ai != 0 and ai <= k: min_dist = min(min_dist, abs(i - m)) print(min_dist * 10) if __name__ == '__main__': # import sys # sys.stdin...
{ "input": [ "10 5 100\n1 0 1 0 0 0 0 0 1 1\n", "5 1 20\n0 27 32 21 19\n", "7 3 50\n62 0 0 0 99 33 22\n", "10 2 14\n2 0 1 26 77 39 41 100 13 32\n", "30 29 10\n59 79 34 12 100 6 1 58 18 73 54 11 37 46 89 90 80 85 73 45 64 5 31 0 89 19 0 74 0 82\n", "100 100 34\n5 73 0 0 44 0 0 0 79 55 0 0 0 0 0...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n house...
816_B. Karen and Coffee_6780
To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n ...
ranges, maps, strs = range, map, str fout = "" count = 0 n, k, q = maps(int, input().split()) rangeArr = [] for i in ranges(200002): rangeArr.append(0) for recipe in ranges(n): left, right = maps(int, input().split()) rangeArr[left] += 1 rangeArr[right + 1] -= 1 for recipe in ranges(1, len(rangeArr)): rangeA...
{ "input": [ "3 2 4\n91 94\n92 97\n97 99\n92 94\n93 97\n95 96\n90 100\n", "2 1 1\n1 1\n200000 200000\n90 100\n", "1 1 1\n1 1\n1 1\n", "1 1 1\n200000 200000\n200000 200000\n", "1 1 1\n186195 200000\n200000 200000\n", "3 2 4\n91 94\n92 97\n97 186\n92 94\n93 97\n95 96\n90 100\n", "2 1 1\n1 1\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. ...
841_B. Godsend_6784
Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts a...
input() for t in input().split(): if int(t) % 2: print("First") break else: print("Second")
{ "input": [ "4\n1 3 2 3\n", "2\n2 2\n", "6\n2 2 1 1 4 2\n", "3\n1 2 2\n", "5\n1 1 1 1 1\n", "3\n2 1 1\n", "4\n2 1 1 2\n", "4\n1000000000 1000000000 1000000000 1\n", "11\n2 2 2 2 2 1 2 2 2 2 2\n", "2\n1 1\n", "1\n1\n", "1\n0\n", "4\n2 4 6 8\n", "4\n3 3 4 4\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choo...
862_C. Mahmoud and Ehab and the xor_6788
Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets. Dr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to f...
n, x = map(int, input().split()) if (n == 2 and x == 0): print("NO") quit() a = 2**17 b = 2**18 ans = 0 print("YES") if(n == 1): print(x) quit() if (n == 2): print(x, 0) quit() for i in range(n-3): print(i+1, end = " ") ans = (ans ^ (i+1)) if (ans == x): print(b, a, a+b) else: pr...
{ "input": [ "3 6\n", "5 5\n", "3 3\n", "3 10203\n", "4 2\n", "5 9\n", "1 0\n", "3 0\n", "3 1\n", "3 4\n", "5 5\n", "2 3\n", "10 2\n", "1 1\n", "3 2\n", "4 0\n", "5 2\n", "5 6\n", "4 3\n", "3 6\n", "5 3\n", "32 32\n", "5 1\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to...
888_A. Local Extrema_6792
You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai - 1 and ai > ai + 1). Since a1 and an have ...
n = int(input()) arr = [int(x) for x in input().split()] cnt = 0 for i in range(1,n-1): if arr[i]< arr[i-1] and arr[i] < arr[i+1]:cnt += 1 else: if arr[i]> arr[i-1] and arr[i] > arr[i+ 1]: cnt += 1 print(cnt)
{ "input": [ "3\n1 2 3\n", "4\n1 5 2 5\n", "3\n3 2 3\n", "1\n548\n", "3\n1 2 1\n", "1\n1\n", "2\n1 1\n", "2\n1 1000\n", "3\n3 2 4\n", "1\n1061\n", "3\n1 0 1\n", "1\n2\n", "2\n1 2\n", "2\n1 1100\n", "3\n1 3 3\n", "4\n1 5 2 1\n", "3\n3 1 4\n", "1\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 array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element...
910_B. Door Frames_6796
Petya has equal wooden bars of length n. He wants to make a frame for two equal doors. Each frame has two vertical (left and right) sides of length a and one top side of length b. A solid (i.e. continuous without breaks) piece of bar is needed for each side. Determine a minimal number of wooden bars which are needed t...
n = int(input()) a = int(input()) b = int(input()) ans = 6 cnt = 0 cur = 2 cnt += 2 * ((n - b) // a) while cnt < 4: cur += 1 cnt += (n // a) ans = min(ans, cur) if b * 2 <= n: cur, cnt = 0, 0 cur = 1 cnt += ((n - 2 * b) // a) while cnt < 4: cur += 1 cnt += (n // a) ans = min(...
{ "input": [ "6\n4\n2\n", "5\n3\n4\n", "8\n1\n2\n", "20\n5\n6\n", "8\n3\n5\n", "586\n22\n272\n", "266\n38\n164\n", "648\n624\n384\n", "11\n3\n5\n", "100\n40\n60\n", "5\n2\n4\n", "828\n363\n56\n", "593\n442\n112\n", "7\n4\n1\n", "1000\n1\n498\n", "5\n1\n3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Petya has equal wooden bars of length n. He wants to make a frame for two equal doors. Each frame has two vertical (left and right) sides of length a and one top side of length b. A s...
959_E. Mahmoud and Ehab and the xor-MST_6802
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight <image> (where <image> is t...
import math n = int(input()) ans = 0 cur = 1 while cur < n: cnt = math.ceil((n-cur)/(cur << 1)) ans += cnt*cur cur <<= 1 print(ans)
{ "input": [ "4\n", "2\n", "7\n", "536870913\n", "1048576\n", "1024\n", "999999999999\n", "123456789\n", "3\n", "10\n", "12000\n", "1000\n", "549755813889\n", "549755813888\n", "100000\n", "536870911\n", "1000000000000\n", "65536\n", "5368709...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0...
986_C. AND Graph_6806
You are given a set of size m with integer elements between 0 and 2^{n}-1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and only if x \& y = 0. Here \& is the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). C...
n, m = map(int, input().split()) a = set(map(int, input().split())) y = 2 ** n mk = [0] * (2 * y) cur = 0 for x in a: if mk[x]: continue mk[x] = 1 st = [x] def push(v): if not mk[v]: mk[v] = 1; st.append(v) while st: u = st.pop() if u < y: push(y + u) els...
{ "input": [ "2 3\n1 2 3\n", "5 5\n5 19 10 20 12\n", "6 19\n15 23 27 29 30 31 43 46 47 51 53 55 57 58 59 60 61 62 63\n", "3 5\n3 5 0 6 7\n", "0 1\n0\n", "1 1\n0\n", "1 1\n1\n", "6 22\n21 23 26 28 31 35 38 39 41 42 44 45 47 50 51 52 54 55 56 59 62 63\n", "6 35\n5 7 10 11 13 14 17 18...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a set of size m with integer elements between 0 and 2^{n}-1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and...
p02583 AtCoder Beginner Contest 175 - Making Triangle_6820
We have sticks numbered 1, \cdots, N. The length of Stick i (1 \leq i \leq N) is L_i. In how many ways can we choose three of the sticks with different lengths that can form a triangle? That is, find the number of triples of integers (i, j, k) (1 \leq i < j < k \leq N) that satisfy both of the following conditions: ...
n=int(input()) s=input().split() s=[int(i) for i in s] s.sort() a=0 for i in range(len(s)-2): for j in range(i+1,len(s)-1): for k in range(j+1,len(s)): if s[i]!=s[j] and s[j]!=s[k] and s[i]+s[j]>s[k]: a+=1 print(a)
{ "input": [ "2\n1 1", "10\n9 4 6 1 9 6 10 6 6 8", "6\n4 5 4 3 3 5", "5\n4 4 9 7 5", "2\n1 0", "10\n9 4 8 1 9 6 10 6 6 8", "6\n4 5 8 3 3 5", "5\n4 4 3 7 5", "10\n9 4 16 1 9 6 10 6 6 8", "6\n4 5 6 3 3 5", "5\n4 0 3 7 5", "10\n9 7 16 1 9 6 10 6 6 8", "5\n4 -1 0 7 5", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have sticks numbered 1, \cdots, N. The length of Stick i (1 \leq i \leq N) is L_i. In how many ways can we choose three of the sticks with different lengths that can form a triang...
p02714 AtCoder Beginner Contest 162 - RGB Triplets_6824
We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k. * j - i \neq k - j. Constraints * 1 \leq N \leq 4000 * S is a string of length N consisting of...
from collections import * N=int(input()) S=input() c=Counter(S) ans=c['R']*c['G']*c['B'] for i in range(1,N//2+1): for j in range(i,N-i): if S[j-i]!=S[j] and S[j]!=S[j+i] and S[j+i]!=S[j-i]: ans-=1 print(ans)
{ "input": [ "4\nRRGB", "39\nRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB", "4\nBGRR", "39\nBBBGBBBRBGBRBGBRBGGBRRRBBGRRBBGGBRGBRBR", "39\nRBRBRRBGGBBRRGBBGRRBGGBRBGBRBGBRBBBGBBB", "4\nGRRB", "39\nRRRBGRBGGBBRRGBBRBRBGGBRBGBRBGBRBBBGBBB", "39\nBBBGBBBRBGBRBGBRBGGBRRBBBGRRBBGGBRGRRBR", "...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have a string S of length N consisting of `R`, `G`, and `B`. Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: * S_i \...
p02843 Sumitomo Mitsui Trust Bank Programming Contest 2019 - 100 to 105_6828
AtCoder Mart sells 1000000 of each of the six items below: * Riceballs, priced at 100 yen (the currency of Japan) each * Sandwiches, priced at 101 yen each * Cookies, priced at 102 yen each * Cakes, priced at 103 yen each * Candies, priced at 104 yen each * Computers, priced at 105 yen each Takahashi wants to buy s...
X = int(input()) q,mod = divmod(X,100) if 5*q<mod: print(0) else: print(1)
{ "input": [ "615", "217", "386", "0", "59", "115", "10", "53", "3", "84", "4", "97", "8", "173", "7", "89", "5", "145", "2", "192", "1", "160", "73", "11", "12", "18", "9", "13", "14", "16", "6...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: AtCoder Mart sells 1000000 of each of the six items below: * Riceballs, priced at 100 yen (the currency of Japan) each * Sandwiches, priced at 101 yen each * Cookies, priced at 102 y...
p02980 AtCoder Grand Contest 035 - Two Histograms_6831
We have a square grid with N rows and M columns. Takahashi will write an integer in each of the squares, as follows: * First, write 0 in every square. * For each i=1,2,...,N, choose an integer k_i (0\leq k_i\leq M), and add 1 to each of the leftmost k_i squares in the i-th row. * For each j=1,2,...,M, choose an intege...
N, M = map(int, input().split()) mod = 998244353 if N > M: N, M = M, N fact = [0] * (M + 1) powm = [0] * (M + 1) pown = [0] * (M + 1) fact[0] = 1 powm[0] = 1 pown[0] = 1 for i in range(1, M + 1): fact[i] = fact[i - 1] * i % mod powm[i] = powm[i - 1] * (M + 1) % mod pown[i] = pown[i - 1] * (N + 1) % mod def p...
{ "input": [ "10 7", "2 3", "1 2", "314159 265358", "10 11", "4 3", "2 2", "314159 217986", "10 10", "1 3", "2 4", "180944 217986", "1 11", "0 3", "2 8", "180944 374453", "1 19", "1 8", "180944 99984", "1 35", "180944 102049", "1 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have a square grid with N rows and M columns. Takahashi will write an integer in each of the squares, as follows: * First, write 0 in every square. * For each i=1,2,...,N, choose ...
p03263 AtCoder Beginner Contest 109 - Make Them Even_6837
There is a grid of square cells with H horizontal rows and W vertical columns. The cell at the i-th row and the j-th column will be denoted as Cell (i, j). In Cell (i, j), a_{ij} coins are placed. You can perform the following operation any number of times: Operation: Choose a cell that was not chosen before and con...
(h,w),*a=[map(int,o.split())for o in open(0)] B=[];l=[] for y in range(h): *A,=a[y];x=0 for x in range(1,w):c=A[x-1]%2;l+=[(y+1,x,y+1,x+1)]*c;A[x]+=c B+=A[x], if y and B[-2]%2:l+=(y,w,y+1,w),;B[y]+=1 print(len(l)) for a in l:print(*a)
{ "input": [ "1 5\n9 9 9 9 9", "3 2\n1 0\n2 1\n1 0", "2 3\n1 2 3\n0 1 1", "1 5\n9 9 9 9 11", "3 2\n1 0\n2 1\n1 -1", "2 3\n1 2 3\n0 2 1", "2 3\n1 2 3\n-1 2 1", "2 3\n1 2 6\n-1 2 1", "0 5\n9 3 15 9 11", "2 3\n0 2 7\n-1 2 0", "2 3\n0 2 7\n-1 1 0", "2 3\n0 1 7\n-1 0 0", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a grid of square cells with H horizontal rows and W vertical columns. The cell at the i-th row and the j-th column will be denoted as Cell (i, j). In Cell (i, j), a_{ij} coi...
p03421 AtCoder Regular Contest 091 - LISDL_6841
Determine if there exists a sequence obtained by permuting 1,2,...,N that satisfies the following conditions: * The length of its longest increasing subsequence is A. * The length of its longest decreasing subsequence is B. If it exists, construct one such sequence. Constraints * 1 \leq N,A,B \leq 3\times 10^5 * ...
n, a, b = [int(item) for item in input().split()] if n < a + (b-1) or n > a + (b-1) * a: print(-1) exit() for i in range(a): print(n - a + i + 1, end=" ") n -= a if n == 0: print("") exit() loop = n // (b-1) option = n % (b-1) for j in range(b-1): if option > 0: for i in range(loop+1): ...
{ "input": [ "7 7 1", "300000 300000 300000", "5 3 2", "7 0 1", "1 1 1", "6 1 6", "6 3 3", "8 2 4", "2 1 2", "4 2 2", "3 1 3", "3 2 2", "566480 300000 300000", "5 0 2", "7 0 2", "566480 313898 300000", "5 -1 2", "7 -1 2", "566480 327138 30000...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Determine if there exists a sequence obtained by permuting 1,2,...,N that satisfies the following conditions: * The length of its longest increasing subsequence is A. * The length of...
p03579 CODE FESTIVAL 2017 qual B - 3 Steps_6845
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i. Rng will add new edges to the graph by repeating the following operation: * Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly thr...
import sys,queue,math,copy,itertools,bisect,collections,heapq def main(): sys.setrecursionlimit(10**7) INF = 10**18 MOD = 10**9 + 7 LI = lambda : [int(x) for x in sys.stdin.readline().split()] _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()] NI = lambda : int(sys.stdin.readline()...
{ "input": [ "6 5\n1 2\n2 3\n3 4\n4 5\n5 6", "5 5\n1 2\n2 3\n3 1\n5 4\n5 1", "6 5\n1 2\n2 3\n3 4\n4 5\n4 6", "5 5\n1 2\n2 3\n3 1\n5 3\n5 1", "6 5\n1 2\n2 2\n3 4\n4 5\n5 6", "5 5\n1 2\n2 3\n3 2\n5 4\n5 1", "6 5\n1 2\n2 3\n3 4\n4 5\n3 6", "12 5\n1 2\n2 3\n3 3\n0 5\n4 6", "9 5\n1 2\n2...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i. Rng will add new edges to the graph ...
p03736 AtCoder Regular Contest 073 - Many Moves_6848
There are N squares in a row. The squares are numbered 1, 2, ..., N from left to right. You have two pieces, initially placed on square A and B, respectively. You will be asked to process Q queries of the following kind, in the order received: * Given an integer x_i, move one of the two pieces of your choice to squar...
import sys read = sys.stdin.buffer.read INF = 10**12 class Rmin(): def __init__(self, size): #the number of nodes is 2n-1 self.n = 1 << (size.bit_length()) self.node = [INF] * (2*self.n-1) def Access(self, x): return self.node[x+self.n-1] def Update(self, x, val): x += self.n-1 self.node[x] = val wh...
{ "input": [ "8 3 1 8\n3 5 1", "9 2 1 9\n5 9", "9 2 1 9\n5 1", "11 16 8 1\n1 1 5 1 11 4 5 2 5 3 3 3 5 5 6 7", "8 3 1 8\n1 5 1", "9 1 1 9\n5 9", "11 16 8 1\n1 2 5 1 11 4 5 2 5 3 3 3 5 5 6 7", "9 2 1 6\n5 9", "9 2 1 9\n1 1", "8 3 1 8\n1 7 1", "9 2 1 9\n2 1", "11 16 8 1\n1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N squares in a row. The squares are numbered 1, 2, ..., N from left to right. You have two pieces, initially placed on square A and B, respectively. You will be asked to pr...
p03898 CODE FESTIVAL 2016 Relay (Parallel) - Problem on Tree_6851
There is a tree with N vertices, numbered 1 through N. The i-th of the N-1 edges connects the vertices p_i and q_i. Among the sequences of distinct vertices v_1, v_2, ..., v_M that satisfy the following condition, find the maximum value of M. * For every 1 \leq i < M, the path connecting the vertices v_i and v_{i+1}...
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 7) """ ・次数3以上の点は使わないとしてよい ・次数1の点は全て回収できる。次数2の点を、パスとなる形で回収する。 """ N = int(readline()) PQ = [tuple(int(x)-1 for x in line.split()) for line in readlines()] graph = [[] for _ in range(N)] for p,q in PQ: graph[p].app...
{ "input": [ "10\n7 9\n1 2\n6 4\n8 1\n3 7\n6 5\n2 10\n9 6\n2 6", "4\n1 2\n2 3\n2 4", "10\n7 9\n1 2\n6 5\n8 1\n3 7\n6 5\n2 10\n9 6\n2 6", "10\n7 9\n1 2\n6 4\n8 1\n3 7\n2 5\n2 10\n9 6\n2 6", "10\n7 9\n1 2\n6 4\n8 1\n3 1\n6 5\n2 10\n9 6\n2 6", "4\n1 2\n2 3\n4 4", "4\n1 2\n3 3\n4 4", "4\n2...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a tree with N vertices, numbered 1 through N. The i-th of the N-1 edges connects the vertices p_i and q_i. Among the sequences of distinct vertices v_1, v_2, ..., v_M that ...
p00004 Simultaneous Equation_6855
Write a program which solve a simultaneous equation: ax + by = c dx + ey = f The program should print x and y for given a, b, c, d, e and f (-1,000 ≤ a, b, c, d, e, f ≤ 1,000). You can suppose that given equation has a unique solution. Input The input consists of several data sets, 1 line for each data set. In a ...
import sys lines = sys.stdin.readlines() for l in lines: nums = list(map(int, l.split())) A = [nums[0], nums[1], nums[3], nums[4]] B = [nums[2], nums[5]] detA = A[0]*A[3] - A[1]*A[2] inA = [A[3], -A[1], -A[2], A[0]] solutions = [n/detA+0.0 for n in [inA[0]*B[0] + inA[1]*B[1], inA[2]*B[0] + inA[...
{ "input": [ "1 2 3 4 5 6\n2 -1 -2 -1 -1 -5", "2 -1 -3 1 -1 -3\n2 -1 -3 -9 9 27", "1 2 3 4 5 6\n2 -1 -4 -1 -1 -5", "2 -1 -3 1 0 -3\n2 -1 -3 -9 9 27", "1 2 3 4 5 5\n2 -1 -4 -1 -1 -5", "3 -1 -3 1 0 -3\n2 -1 -3 -9 9 27", "1 2 3 4 5 4\n2 -1 -4 -1 -1 -5", "3 -1 -3 1 0 -3\n2 -1 -3 -9 18 27",...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Write a program which solve a simultaneous equation: ax + by = c dx + ey = f The program should print x and y for given a, b, c, d, e and f (-1,000 ≤ a, b, c, d, e, f ≤ 1,000). You ...
p00136 Frequency Distribution of Height_6859
The height of the student was measured at the medical examination. Create a program that takes height data as input, creates a frequency distribution, and outputs it. The frequency distribution is divided into 6 classes in 5 cm increments, and the number of people is indicated by * (half-width asterisk). However, if th...
# -*- coding: utf-8 -*- """ """ import sys from sys import stdin input = stdin.readline def main(args): ranks = [0] * 6 n = int(input()) for _ in range(n): h = float(input()) if h < 160.0: h = 160.0 if h >= 190.0: h = 189.9 pos = int((h - 160.0) / ...
{ "input": [ "4\n180.3\n168.2\n165.5\n175.3", "21\n179.4\n171.5\n156.6\n173.0\n169.4\n181.2\n172.4\n170.0\n163.6\n165.9\n173.5\n168.2\n162.5\n172.0\n175.1\n172.3\n167.5\n175.9\n186.2\n168.0\n178.6", "4\n180.3\n168.42932428490565\n165.5\n175.3", "21\n180.11768905370886\n171.5\n156.6\n173.0\n169.4\n181....
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The height of the student was measured at the medical examination. Create a program that takes height data as input, creates a frequency distribution, and outputs it. The frequency di...
p00457 Chain_6863
problem There are the following games. N characters are lined up in a vertical row. The color of these characters is red, blue, or yellow, and in the initial state, four or more characters of the same color are not lined up in a row. The player can select a character at a certain position and change it to another col...
# p周辺の4つは消えることが前提 def erase(array, i, N): if array[i - 1] == array[i]: up = i - 1 down = i elif array[i] == array[i + 1]: up = i down = i + 1 else: return N - 2 while True: if array[up] == -1 or array[down] == -1 or not array[up] == array[down]: ...
{ "input": [ "12\n3\n2\n1\n1\n2\n3\n2\n2\n2\n1\n1\n3\n12\n3\n2\n1\n1\n2\n3\n2\n1\n3\n2\n1\n3\n0", "12\n3\n2\n1\n1\n2\n3\n2\n2\n2\n1\n1\n3\n12\n3\n2\n1\n1\n2\n5\n2\n1\n3\n2\n1\n3\n0", "12\n3\n2\n1\n1\n2\n3\n2\n2\n2\n1\n1\n3\n12\n3\n2\n2\n1\n2\n5\n2\n1\n3\n2\n1\n3\n0", "12\n3\n2\n1\n1\n2\n3\n2\n0\n2\n1\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: problem There are the following games. N characters are lined up in a vertical row. The color of these characters is red, blue, or yellow, and in the initial state, four or more cha...
p00647 It's our delight!!_6867
You are a student of University of Aizu. And you work part-time at a restaurant. Staffs of the restaurant are well trained to be delighted to provide more delicious products faster. The speed providing products particularly depends on skill of the staff. So, the manager of the restaurant want to know how long it take...
while 1: N = int(input()) if N == 0: break a = b = c = 0 a0 = b0 = c0 = 0 for i in range(N): s0, s1 = input().split() h, m = map(int, s0.split(":")) tm = 100*h + m m1 = int(s1) if m1 < m: m1 += 60 if 1100 <= tm < 1500: i...
{ "input": [ "5\n12:57 59\n20:12 15\n12:19 21\n18:52 03\n16:09 14\n0", "5\n12:57 59\n20:22 15\n12:19 21\n18:52 03\n16:09 14\n0", "5\n12:57 59\n22:02 15\n12:19 21\n18:52 03\n16:09 14\n0", "5\n12:57 105\n20:12 15\n12:19 21\n18:52 03\n16:09 14\n0", "5\n12:57 59\n20:22 15\n12:19 38\n18:52 03\n16:09 14...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are a student of University of Aizu. And you work part-time at a restaurant. Staffs of the restaurant are well trained to be delighted to provide more delicious products faster. ...
p01493 DisconnectedGame_6877
Example Input 3 NNN NNN NNN Output Taro
from collections import deque import sys sys.setrecursionlimit(10**6) readline = sys.stdin.readline write = sys.stdout.write def solve(): N = int(readline()) G = [[] for i in range(N)] for i in range(N): *vs, = readline() for j in range(N): if vs[j] == "Y": G[i].a...
{ "input": [ "3\nNNN\nNNN\nNNN", "3\nNNN\nNNO\nNNN", "3\nNMN\nNNO\nNNN", "3\nNMN\nNNP\nNNN", "3\nMMN\nNNO\nNNN", "3\nMMN\nONN\nNNN", "3\nMMN\nONN\nMNN", "3\nMMN\nNNO\nMNN", "3\nMMN\nNNO\nMON", "3\nNMN\nNNO\nMON", "3\nNMN\nNNO\nMNN", "3\nNMN\nMNO\nMNN", "3\nNLN\nMNO\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Example Input 3 NNN NNN NNN Output Taro ### Input: 3 NNN NNN NNN ### Output: Taro ### Input: 3 NNN NNO NNN ### Output: Taro ### Code: from collections import deque import sys...
p01806 Dice Stamp_6882
Dice Stamp Dice stamp At a local fair, you found a game store you've never seen before. This is a game in which N 6-sided dice are dropped and rolled on the board. More precisely, N buttons are tied to N dice on a one-to-one basis, and pressing the button causes the corresponding dice to fall onto the board. It is a ...
def main(): while True: n = int(input()) if n == 0:break def spin(dice, direct): l, r, f, b, d, u = dice if direct == "R": return [d, u, f, b, r, l] if direct == "L": return [u, d, f, b, l, r] if direct == "B": return [l, r, d, u, b, f] if direct == "F...
{ "input": [ "1\n0 0\n1 2 3 4 5 6\nRRRRBBBBLLLLFFFF\n2\n0 0\n1 1 1 1 1 1\nRRR\n2 2\n100 100 100 100 100 100\nFFF\n1\n1000 -1000\n1 2 3 4 5 6\nLFRB\n4\n-3 -4\n1 2 3 4 5 6\nBBBBBBBB\n4 -3\n11 12 13 14 15 16\nLLLLLLLL\n3 4\n21 22 23 24 25 26\nFFFFFFFF\n-4 3\n31 32 33 34 35 36\nRRRRRRRR\n3\n-2 -2\n9 3 1 1 1 1\nRRRRBL...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dice Stamp Dice stamp At a local fair, you found a game store you've never seen before. This is a game in which N 6-sided dice are dropped and rolled on the board. More precisely, N...
p01941 Indecision_6884
D: Indecision-Indecision- problem Ebi-chan has been addicted to gal games lately, and her goal now is to capture two heroines at the same time. Ebi-chan can use several events to increase her liking from the heroine, but only one heroine can be selected for each event. However, Ebi-chan does not forget to follow the...
N, C = map(int, input().split()) events = [] for i in range(N): events.append(list(map(int, input().split()))) def solve(): L = 250 dp = [[-10**18]*(L+1) for i in range(C+1)] for j in range(C+1): dp[j][0] = 0 for a, b, c in events: for j in range(C-c, -1, -1): for k in r...
{ "input": [ "3 4\n3 2 2\n2 1 1\n2 1 1", "3 4\n4 2 2\n2 1 1\n2 1 1", "3 4\n4 2 2\n2 1 1\n3 2 1", "3 4\n4 2 2\n2 1 2\n3 2 1", "1 4\n4 2 2\n3 1 4\n3 2 1", "1 4\n5 2 5\n3 1 1\n3 2 2", "1 4\n4 1 4\n2 1 -1\n0 3 0", "3 4\n3 2 2\n2 1 1\n2 1 2", "1 4\n5 3 2\n3 1 1\n3 2 2", "3 4\n4 2 2\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: D: Indecision-Indecision- problem Ebi-chan has been addicted to gal games lately, and her goal now is to capture two heroines at the same time. Ebi-chan can use several events to i...
p02090 Shuttle Run_6886
C: Shuttle Run Story University H has a unique physical fitness test program, aiming to grow the mindset of students. Among the items in the program, shuttle run is well-known as especially eccentric one. Surprisingly, there are yokans (sweet beans jellies) on a route of the shuttle run. Greedy Homura-chan would like...
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()]...
{ "input": [ "1 3\n1 2", "1 3\n1 4", "1 3\n1 5", "1 3\n1 10", "1 0\n0 2", "1 3\n1 1", "1 3\n1 9", "1 0\n0 0", "1 3\n0 15", "1 3\n2 12", "1 3\n2 24", "1 3\n2 13", "1 3\n1 8", "1 3\n1 18", "1 2\n0 3", "1 3\n1 20", "1 5\n2 14", "1 4\n1 6", "1 3\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: C: Shuttle Run Story University H has a unique physical fitness test program, aiming to grow the mindset of students. Among the items in the program, shuttle run is well-known as es...
p02222 Painting Tree_6889
Problem statement Modulo is very good at drawing trees. Over the years, Modulo has drawn a picture of a tree with $ N $ vertices. The vertices of this tree are numbered from $ 1 $ to $ N $, and the vertices $ a_i $ and $ b_i $$ (1 \ leq i \ leq N-1) $ are directly connected by edges. All vertices are not yet painted....
import sys from collections import deque readline = sys.stdin.readline def parorder(Edge, p): N = len(Edge) par = [0]*N par[p] = -1 stack = [p] order = [] visited = set([p]) ast = stack.append apo = order.append while stack: vn = stack.pop() apo(vn) for vf in...
{ "input": [ "7\n1 3\n2 3\n3 4\n4 5\n4 6\n6 7", "7\n1 3\n2 3\n3 4\n4 5\n4 6\n3 7", "7\n1 3\n2 3\n3 4\n7 5\n4 6\n6 7", "7\n1 2\n2 3\n3 4\n4 5\n4 6\n6 7", "7\n1 5\n2 3\n3 4\n1 5\n4 6\n6 4", "7\n1 3\n2 3\n3 4\n4 5\n4 6\n6 4", "7\n1 3\n2 3\n3 4\n7 5\n4 6\n3 7", "7\n1 5\n2 3\n3 4\n4 5\n4 6\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem statement Modulo is very good at drawing trees. Over the years, Modulo has drawn a picture of a tree with $ N $ vertices. The vertices of this tree are numbered from $ 1 $ t...
p02374 Range Query on a Tree_6892
Write a program which manipulates a weighted rooted tree $T$ with the following operations: * $add(v,w)$: add $w$ to the edge which connects node $v$ and its parent * $getSum(u)$: report the sum of weights of all edges from the root to node $u$ The given tree $T$ consists of $n$ nodes and every node has a unique I...
import sys sys.setrecursionlimit(int(1e6)) class B_I_T: def __init__(self, n): self.n = n self.data = [0] * (n + 1) def get_sum(self, i): ret = 0 while i > 0: ret += self.data[i] i -= (i & -i) return ret def add(self, i, w): i...
{ "input": [ "6\n2 1 2\n2 3 5\n0\n0\n0\n1 4\n7\n1 1\n0 3 10\n1 2\n0 4 20\n1 3\n0 5 40\n1 4", "2\n1 1\n0\n4\n0 1 1\n1 1\n0 1 1\n1 1", "4\n1 1\n1 2\n1 3\n0\n6\n0 3 1000\n0 2 1000\n0 1 1000\n1 1\n1 2\n1 3", "2\n1 1\n0\n5\n0 1 1\n1 1\n0 1 1\n1 1", "2\n1 1\n0\n5\n0 1 2\n1 1\n0 1 1\n1 1", "6\n2 1 2\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Write a program which manipulates a weighted rooted tree $T$ with the following operations: * $add(v,w)$: add $w$ to the edge which connects node $v$ and its parent * $getSum(u)$: r...
1006_F. Xor-Paths_6901
There is a rectangular grid of size n × m. Each cell has a number written on it; the number on the cell (i, j) is a_{i, j}. Your task is to calculate the number of paths from the upper-left cell (1, 1) to the bottom-right cell (n, m) meeting the following constraints: * You can move to the right or to the bottom onl...
from collections import* n, m, k = map(int, input().split()) b = [[int(v) for v in input().split()] for _ in range(n)] if m < n: a = [[b[j][i] for j in range(n)] for i in range(m)] b = a m, n = n, m cntrs = [Counter() for _ in range(n)] d = (n + m-1) // 2 for i in range(1<<d): ones = bin(i).count('1') ...
{ "input": [ "3 4 1000000000000000000\n1 3 3 3\n0 3 3 2\n3 0 1 1\n", "3 3 11\n2 1 5\n7 10 0\n12 6 4\n", "3 4 2\n1 3 3 3\n0 3 3 2\n3 0 1 1\n", "1 1 982347923479\n1\n", "2 10 2047\n1 2 4 8 16 32 64 128 256 512\n2 4 8 16 32 64 128 256 512 1024\n", "1 1 1\n1\n", "20 1 1048575\n1\n2\n4\n8\n16\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a rectangular grid of size n × m. Each cell has a number written on it; the number on the cell (i, j) is a_{i, j}. Your task is to calculate the number of paths from the uppe...
1051_E. Vasya and Big Integers_6906
Vasya owns three big integers — a, l, r. Let's define a partition of x such a sequence of strings s_1, s_2, ..., s_k that s_1 + s_2 + ... + s_k = x, where + is a concatanation of strings. s_i is the i-th element of the partition. For example, number 12345 has the following partitions: ["1", "2", "3", "4", "5"], ["123",...
def kmp(pat,text,t): s=pat+"?"+text; #z[i] es el tamaño del prefijo mas largo de, formado por una subcadena s[i:...] z=[0 for i in range(len(s))] L=0;R=0;n=len(s); for i in range(1,len(s)): if i>R: L=R=i while R<n and s[R-L]==s[R]: R+=1 z[i...
{ "input": [ "135\n1\n15\n", "10000\n0\n9\n", "8698272232067125804692796793285996652907461532074057999849092982935510140661249431760679611642931478\n6\n68270\n", "1254298816872758107814873768638894851215334772406487549640220733524354123084977151799244616404710412\n35\n9634401298\n", "123\n0\n0\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya owns three big integers — a, l, r. Let's define a partition of x such a sequence of strings s_1, s_2, ..., s_k that s_1 + s_2 + ... + s_k = x, where + is a concatanation of stri...
1073_F. Choosing Two Paths_6909
You are given an undirected unweighted tree consisting of n vertices. An undirected tree is a connected undirected graph with n - 1 edges. Your task is to choose two pairs of vertices of this tree (all the chosen vertices should be distinct) (x_1, y_1) and (x_2, y_2) in such a way that neither x_1 nor y_1 belong to t...
import sys n = int(sys.stdin.readline()) edges = [[] for _ in range(n)] for _ in range(n - 1): i, j = tuple(int(k) for k in sys.stdin.readline().split()) i -= 1 j -= 1 edges[i].append(j) edges[j].append(i) # Prunes the graph starting from the vertices with # only 1 edge until we reach a vertex wit...
{ "input": [ "9\n9 3\n3 5\n1 2\n4 3\n4 7\n1 7\n4 6\n3 8\n", "10\n6 8\n10 3\n3 7\n5 8\n1 7\n7 2\n2 9\n2 8\n1 4\n", "11\n1 2\n2 3\n3 4\n1 5\n1 6\n6 7\n5 8\n5 9\n4 10\n4 11\n", "7\n1 4\n1 5\n1 6\n2 3\n2 4\n4 7\n", "6\n1 2\n1 3\n1 4\n4 5\n4 6\n", "14\n3 2\n4 10\n10 6\n7 12\n8 7\n14 1\n8 4\n5 10\n3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an undirected unweighted tree consisting of n vertices. An undirected tree is a connected undirected graph with n - 1 edges. Your task is to choose two pairs of vertic...
1095_F. Make It Connected_6913
You are given an undirected graph consisting of n vertices. A number is written on each vertex; the number on vertex i is a_i. Initially there are no edges in the graph. You may add some edges to this graph, but you have to pay for them. The cost of adding an edge between vertices x and y is a_x + a_y coins. There are...
n, m = map(int, input().split()) a = list(map(int, input().split())) e = [] for _ in range(m) : u, v, w = map(int, input().split()) e.append((u-1, v-1, w)) a = sorted(zip(a, range(n)), key = lambda x : x[0]) for i in range(1, n) : e.append((a[0][1], a[i][1], a[0][0] + a[i][0])) fa = list(range(n)) rk = [...
{ "input": [ "5 4\n1 2 3 4 5\n1 2 8\n1 3 10\n1 4 7\n1 5 15\n", "3 2\n1 3 3\n2 3 5\n2 1 1\n", "4 0\n1 3 3 7\n", "1 0\n4\n", "10 0\n9 9 5 2 2 5 8 5 1 7\n", "10 1\n2 2 9 7 2 3 7 7 1 1\n5 8 4\n", "2 1\n100000000000 100000000000\n1 2 199999999999\n", "10 10\n3 4 5 10 10 9 5 4 7 8\n1 5 5\n10...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an undirected graph consisting of n vertices. A number is written on each vertex; the number on vertex i is a_i. Initially there are no edges in the graph. You may add ...
1117_A. Best Subsegment_6917
You are given array a_1, a_2, ..., a_n. Find the subsegment a_l, a_{l+1}, ..., a_r (1 ≤ l ≤ r ≤ n) with maximum arithmetic mean (1)/(r - l + 1)∑_{i=l}^{r}{a_i} (in floating-point numbers, i.e. without any rounding). If there are many such subsegments find the longest one. Input The first line contains single integer...
n=int(input()) s=[int(x) for x in input().split()] el=max(s) pos1=-1 pos2=-1 c=0 ans=0 for i in range(0,len(s)): if(s[i]==el): c=c+1 else: ans=max(ans,c) c=0 ans=max(ans,c) print(ans)
{ "input": [ "5\n6 1 6 6 0\n", "2\n1 5\n", "5\n1 0 0 0 0\n", "15\n2 2 1 2 2 2 2 1 2 2 2 1 2 2 3\n", "2\n1 2\n", "4\n2 2 1 3\n", "3\n0 0 0\n", "5\n1 3 3 1 3\n", "10\n0 0 0 0 0 0 0 0 0 0\n", "2\n1000 16777252\n", "1\n0\n", "4\n0 0 0 0\n", "7\n1 1 1 0 1 2 2\n", "5\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given array a_1, a_2, ..., a_n. Find the subsegment a_l, a_{l+1}, ..., a_r (1 ≤ l ≤ r ≤ n) with maximum arithmetic mean (1)/(r - l + 1)∑_{i=l}^{r}{a_i} (in floating-point numb...
1143_C. Queen_6921
You are given a rooted tree with vertices numerated from 1 to n. A tree is a connected graph without cycles. A rooted tree has a special vertex named root. Ancestors of the vertex i are all vertices on the path from the root to the vertex i, except the vertex i itself. The parent of the vertex i is the nearest to the ...
""" https://codeforces.com/problemset/problem/1143/C """ n = int(input()) respect = [0 for _ in range(n)] child_respect = [0 for _ in range(n)] root = -1 for i in range(n): p, c = map(int,input().split()) if p == -1: root = i continue respect[i] = c if p != -1 and not c: child_respec...
{ "input": [ "5\n3 1\n1 1\n-1 0\n2 1\n3 0\n", "8\n2 1\n-1 0\n1 0\n1 1\n1 1\n4 0\n5 1\n7 0\n", "5\n-1 0\n1 1\n1 1\n2 0\n3 0\n", "1\n-1 0\n", "5\n-1 0\n1 0\n1 1\n2 0\n3 0\n", "8\n2 1\n-1 0\n1 1\n1 1\n1 1\n4 0\n5 1\n7 0\n", "8\n2 1\n-1 0\n1 0\n1 1\n2 1\n4 0\n5 1\n7 0\n", "5\n3 1\n1 1\n-1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a rooted tree with vertices numerated from 1 to n. A tree is a connected graph without cycles. A rooted tree has a special vertex named root. Ancestors of the vertex i ...
1163_C1. Power Transmission (Easy Edition)_6925
This problem is same as the next one, but has smaller constraints. It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system cons...
import sys import collections import math import heapq from operator import itemgetter def getint(): return int(input()) def getints(): return [int(x) for x in input().split(' ')] n = getint() points = [tuple(getints()) for _ in range(n)] result = 0 slopes = collections.defaultdict(set) for i in range(n - 1...
{ "input": [ "4\n0 0\n1 1\n0 3\n1 2\n", "4\n0 0\n0 2\n0 4\n2 0\n", "3\n-1 -1\n1 0\n3 1\n", "2\n10000 10000\n-10000 -10000\n", "5\n-8893 8986\n-3629 9045\n-7719 -6470\n-258 4491\n-6902 -6866\n", "25\n1964 -4517\n5939 -4080\n9503 -7541\n-5037 -6950\n-9914 5015\n-435 7555\n-9321 -2202\n-5036 4224...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This problem is same as the next one, but has smaller constraints. It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power s...
1184_A1. Heidi Learns Hashing (Easy)_6929
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor. Heidi figured out that Madame Kovarian uses a...
from math import sqrt def get_int(): from sys import stdin return int(stdin.readline().replace('\n', '')) def is_even(n): return n%2 == 0 def heidi_hash(r): k = r-1 rt = int(sqrt(k)) for x in range(1, rt+2): if k % x == 0: temp = k/x -x -1 if temp > 0 and is_e...
{ "input": [ "19\n", "16\n", "768407398177\n", "7\n", "156231653273\n", "426262703497\n", "688373050717\n", "149890929717\n", "5\n", "185589070745\n", "943447972637\n", "15\n", "21\n", "540024979445\n", "963761198401\n", "215283472163\n", "17\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's n...
1201_D. Treasure Hunting_6933
You are on the island which can be represented as a n × m table. The rows are numbered from 1 to n and the columns are numbered from 1 to m. There are k treasures on the island, the i-th of them is located at the position (r_i, c_i). Initially you stand at the lower left corner of the island, at the position (1, 1). I...
from sys import stdin from bisect import bisect_left input=stdin.readline n,m,k,q=map(int,input().split(' ')) x=sorted(list(map(int,input().split(' ')))for i in range(k)) y=sorted(list(map(int,input().split(' ')))) def rr(c0,c1,c2): return abs(c2-c0)+abs(c1-c2) def tm(c0,c1): t=bisect_left(y,c0) tt=[] ...
{ "input": [ "3 6 3 2\n1 6\n2 2\n3 4\n1 6\n", "3 3 3 2\n1 1\n2 1\n3 1\n2 3\n", "3 5 3 2\n1 2\n2 3\n3 1\n1 5\n", "2 11 7 6\n1 1\n1 5\n1 2\n1 4\n1 11\n1 8\n1 3\n3 6 5 10 2 7\n", "2 2 2 2\n1 1\n1 2\n1 2\n", "200000 200000 2 2\n104889 58569\n855 142967\n125218 47859\n", "5 5 10 3\n3 1\n3 2\n3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are on the island which can be represented as a n × m table. The rows are numbered from 1 to n and the columns are numbered from 1 to m. There are k treasures on the island, the i...
1219_C. Periodic integer number_6937
Alice became interested in periods of integer numbers. We say positive X integer number is periodic with length L if there exists positive integer number P with L digits such that X can be written as PPPP…P. For example: X = 123123123 is periodic number with length L = 3 and L = 9 X = 42424242 is periodic number with...
import math as m def main(): n = int(input()) s = input() l = len(s) p = int(m.ceil(l/n)) if l%n!=0 : t = '1' for i in range(1,n): t+='0' for i in range(0,p): print(t ,end = '') else : z = s[0:n] t='' for i in range(0,p): t+=z if t > s: print(t) return z = str(int(s[0:n])+1) if ...
{ "input": [ "3\n12345\n", "3\n123456\n", "3\n145023023\n", "4\n50265026\n", "23423\n194891689139076096992551711930850812246226117899439276062220542806230168006813560185626908668051204795339243671896011343435079871950197856859138515672529836297513259753021023368790532408991815180814378959240214902...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice became interested in periods of integer numbers. We say positive X integer number is periodic with length L if there exists positive integer number P with L digits such that X c...
1243_C. Tile Painting_6941
Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate. The path consists of n consecutive tiles, numbered from 1 to n. Ujan will paint each tile in some color. He will consider the path aesthetic if for any two different tiles wit...
def prime_factor(n): ass = [] for i in range(2,int(n**0.5)+1): while n % i==0: ass.append(i) n = n//i if n != 1: ass.append(n) return ass n = int(input()) p = list(set(prime_factor(n))) if len(p) == 1: print(p[0]) else: print(1)
{ "input": [ "5\n", "4\n", "2\n", "54241012609\n", "4294967318\n", "370758709373\n", "546739553053\n", "922350872881\n", "442654694329\n", "832972004929\n", "931667836027\n", "6469693230\n", "9\n", "243\n", "549755813888\n", "902076349729\n", "200000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate. The path consists of n consecutive til...
1263_B. PIN Codes_6945
A PIN code is a string that consists of exactly 4 digits. Examples of possible PIN codes: 7013, 0000 and 0990. Please note that the PIN code can begin with any digit, even with 0. Polycarp has n (2 ≤ n ≤ 10) bank cards, the PIN code of the i-th card is p_i. Polycarp has recently read a recommendation that it is bette...
#from statistics import median #import collections #aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0] # from math import gcd # from itertools import combinations,permutations,accumulate, product # (string,3) 3回 # #from collections import deque # from collections import deque,defaultdict,...
{ "input": [ "3\n2\n1234\n0600\n2\n1337\n1337\n4\n3139\n3139\n3139\n3139\n", "1\n3\n1111\n1111\n1112\n", "1\n3\n0000\n0000\n0009\n", "1\n4\n0000\n0000\n0000\n0001\n", "1\n3\n1234\n1234\n1230\n", "3\n10\n0000\n0000\n0000\n0000\n0000\n0000\n0000\n0000\n0000\n0000\n1\n1234\n7\n1234\n4567\n8901\n2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A PIN code is a string that consists of exactly 4 digits. Examples of possible PIN codes: 7013, 0000 and 0990. Please note that the PIN code can begin with any digit, even with 0. Po...
1328_A. Divisibility Problem_6953
You are given two positive integers a and b. In one move you can increase a by 1 (replace a with a+1). Your task is to find the minimum number of moves you need to do in order to make a divisible by b. It is possible, that you have to make 0 moves, as a is already divisible by b. You have to answer t independent test c...
t=int(input()) for i in range(0,t): a,b=input().split() a=int(a) b=int(b) print((b-a%b)%b)
{ "input": [ "5\n10 4\n13 9\n100 13\n123 456\n92 46\n", "1\n515151 2\n", "21\n1 3218\n28 10924\n908802 141084\n82149 9274\n893257 10248\n2750048 802705\n2857 142\n980 209385\n1 3218\n28 10924\n908802 141084\n82149 9274\n893257 10248\n2750048 802705\n2857 142\n980 209385\n1 3218\n28 10924\n908802 141084\n8...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two positive integers a and b. In one move you can increase a by 1 (replace a with a+1). Your task is to find the minimum number of moves you need to do in order to make...
1348_A. Phoenix and Balance_6957
Phoenix has n coins with weights 2^1, 2^2, ..., 2^n. He knows that n is even. He wants to split the coins into two piles such that each pile has exactly n/2 coins and the difference of weights between the two piles is minimized. Formally, let a denote the sum of weights in the first pile, and b denote the sum of weigh...
t=eval(input()) for j in range(t): n=eval(input()) if n==2 : print(2) else: sum=2 for i in range(1,n//2): sum=sum+2**(i+1) print(sum)
{ "input": [ "2\n2\n4\n", "100\n2\n4\n6\n8\n10\n12\n14\n16\n18\n20\n22\n24\n26\n28\n30\n30\n30\n30\n20\n30\n30\n18\n14\n16\n30\n24\n30\n30\n10\n20\n30\n10\n2\n18\n30\n30\n12\n24\n30\n24\n18\n28\n30\n16\n30\n30\n30\n30\n22\n26\n30\n30\n30\n10\n14\n6\n18\n30\n16\n30\n30\n4\n30\n20\n30\n30\n30\n30\n30\n24\n30\n3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Phoenix has n coins with weights 2^1, 2^2, ..., 2^n. He knows that n is even. He wants to split the coins into two piles such that each pile has exactly n/2 coins and the difference ...
1413_A. Finding Sasuke_6964
Naruto has sneaked into the Orochimaru's lair and is now looking for Sasuke. There are T rooms there. Every room has a door into it, each door can be described by the number n of seals on it and their integer energies a_1, a_2, ..., a_n. All energies a_i are nonzero and do not exceed 100 by absolute value. Also, n is e...
T = int(input()) a = [] b=[] i = 0 j = 0 n = 0 for i in range(T): n = int(input()) a = list(map(int, input().split())) b.append([]) for j in range(int(n/2)): if (a[j*2] > a[j*2 + 1]): b[i].append(a[j*2 + 1]) b[i].append(-a[j*2]) elif(a[j*2 + 1] >= a[j*2]): ...
{ "input": [ "2\n2\n1 100\n4\n1 2 3 6\n", "2\n2\n1 100\n4\n1 2 3 6\n", "6\n6\n-1 1 1 1 1 1\n6\n1 -1 1 1 1 1\n6\n1 1 -1 1 1 1\n6\n1 1 1 -1 1 1\n6\n1 1 1 1 -1 1\n6\n1 1 1 1 1 -1\n", "2\n2\n1 110\n4\n1 2 3 6\n", "6\n6\n-1 1 1 1 1 1\n6\n1 -1 1 1 1 1\n6\n1 1 -1 1 2 1\n6\n1 1 1 -1 1 1\n6\n1 1 1 1 -1 1\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Naruto has sneaked into the Orochimaru's lair and is now looking for Sasuke. There are T rooms there. Every room has a door into it, each door can be described by the number n of seal...
1455_A. Strange Functions_6968
Let's define a function f(x) (x is a positive integer) as follows: write all digits of the decimal representation of x backwards, then get rid of the leading zeroes. For example, f(321) = 123, f(120) = 21, f(1000000) = 1, f(111) = 111. Let's define another function g(x) = (x)/(f(f(x))) (x is a positive integer as well...
from sys import stdout,stdin,maxsize from collections import defaultdict,deque import math t=int(stdin.readline()) for _ in range(t): #n=int(stdin.readline()) #d=map(int,stdin.readline().split()) #l=list(map(int,stdin.readline().split())) s=input() print(len(s)) ...
{ "input": [ "5\n4\n37\n998244353\n1000000007\n12345678901337426966631415\n", "1\n10000000\n", "1\n1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "1\n9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's define a function f(x) (x is a positive integer) as follows: write all digits of the decimal representation of x backwards, then get rid of the leading zeroes. For example, f(32...
1479_B2. Painting the Array II_6972
The only difference between the two versions is that this version asks the minimal possible answer. Homer likes arrays a lot. Today he is painting an array a_1, a_2, ..., a_n with two kinds of colors, white and black. A painting assignment for a_1, a_2, ..., a_n is described by an array b_1, b_2, ..., b_n that b_i ind...
import sys input = lambda: sys.stdin.readline().rstrip() N = int(input()) A = [int(a) for a in input().split()] S = set() ans = 0 la = -1 for a in A: if a == la: continue elif a in S: S = {a, la} else: S.add(a) la = a ans += 1 print(ans)
{ "input": [ "7\n1 2 1 2 1 2 1\n", "6\n1 2 3 1 2 2\n", "1\n1\n", "11\n2 3 10 3 3 8 9 2 11 10 3\n", "11\n10 5 8 8 2 7 9 3 5 1 10\n", "15\n4 4 8 8 10 10 15 7 2 15 2 12 12 3 15\n", "11\n2 6 10 3 3 8 9 2 11 10 3\n", "11\n10 5 8 8 2 9 9 3 5 1 10\n", "6\n1 2 3 1 3 2\n", "6\n1 1 3 1 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The only difference between the two versions is that this version asks the minimal possible answer. Homer likes arrays a lot. Today he is painting an array a_1, a_2, ..., a_n with tw...
1506_A. Strange Table_6976
Polycarp found a rectangular table consisting of n rows and m columns. He noticed that each cell of the table has its number, obtained by the following algorithm "by columns": * cells are numbered starting from one; * cells are numbered from left to right by columns, and inside each column from top to bottom; ...
def somefunc(n, m, x): if n == m and n == 1: return x ans = 0 row = 0 if x % n == 0: # 2 row = x / n else: row = x // n + 1 col = x % n if col == 0: col = n ans = int(m * (col - 1)) ans += row return int(ans) for t in range(int(input()...
{ "input": [ "5\n1 1 1\n2 2 3\n3 5 11\n100 100 7312\n1000000 1000000 1000000000000\n", "5\n1 1 1\n2 2 3\n3 5 11\n100 100 7312\n1000000 1000010 1000000000000\n", "5\n1 1 1\n2 2 3\n3 7 11\n100 100 7312\n1000000 1000000 1000000000000\n", "5\n1 1 1\n2 2 3\n3 5 11\n100 100 7312\n1001000 1000010 10000000000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp found a rectangular table consisting of n rows and m columns. He noticed that each cell of the table has its number, obtained by the following algorithm "by columns": * c...
1529_B. Sifid and Strange Subsequences_6980
A sequence (b_1, b_2, …, b_k) is called strange, if the absolute difference between any pair of its elements is greater than or equal to the maximum element in the sequence. Formally speaking, it's strange if for every pair (i, j) with 1 ≤ i<j ≤ k, we have |a_i-a_j|≥ MAX, where MAX is the largest element of the sequenc...
""" Author : Ashish Sasmal Python3 / PyPy3 """ from sys import stdin as sin def aint():return int(input()) def amap():return map(int,sin.readline().split()) def alist():return list(map(int,sin.readline().split())) def astr():return input() for _ in range(aint()): n = aint() l = alist() l.sort() ...
{ "input": [ "6\n4\n-1 -2 0 0\n7\n-3 4 -2 0 -4 6 1\n5\n0 5 -3 2 -5\n3\n2 3 1\n4\n-3 0 2 0\n6\n-3 -2 -1 1 1 1\n", "1\n1\n0\n", "1\n5\n-1 -1 -1 -1 -1\n", "1\n6\n1 1 0 0 0 0\n", "1\n3\n1000000000 1000000000 1000000000\n", "1\n2\n-1000000000 1000000000\n", "1\n4\n-4 -1 0 3\n", "1\n2\n0 1\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A sequence (b_1, b_2, …, b_k) is called strange, if the absolute difference between any pair of its elements is greater than or equal to the maximum element in the sequence. Formally ...
160_B. Unlucky Ticket_6984
Each of you probably has your personal experience of riding public transportation and buying tickets. After a person buys a ticket (which traditionally has an even number of digits), he usually checks whether the ticket is lucky. Let us remind you that a ticket is lucky if the sum of digits in its first half matches th...
number_of_testcases = 1 #int(input()) for _ in range(number_of_testcases): number_of_digits = int(input()) ticket_number = input() ticket_number = list(ticket_number) first_half = ticket_number[:number_of_digits] second_half = ticket_number[number_of_digits:] #print(first_half) first_half....
{ "input": [ "2\n3754\n", "2\n0135\n", "2\n2421\n", "20\n7405800505032736115894335199688161431589\n", "100\n0200210221001101010012012020022010201112010122111221222212002021012010010021002011012101220020212200334566516320533011102060451405316422565521225350011310353421331302016234554002665321162364...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Each of you probably has your personal experience of riding public transportation and buying tickets. After a person buys a ticket (which traditionally has an even number of digits), ...
227_D. Naughty Stone Piles_6993
There are n piles of stones of sizes a1, a2, ..., an lying on the table in front of you. During one move you can take one pile and add it to the other. As you add pile i to pile j, the size of pile j increases by the current size of pile i, and pile i stops existing. The cost of the adding operation equals the size of...
n = int(input()) stones = list(map(lambda t : int(t), input().split())) q = int(input()) queries = list(map(lambda t : int(t), input().split())) stones.sort() added_stones = [] added_stones.append(stones[0]) for i in range(1, n, 1): added_stones.append(stones[i] + added_stones[i - 1]) computed_queries = {} for ...
{ "input": [ "5\n2 3 4 1 1\n2\n2 3\n", "3\n1 6 8\n1\n6\n", "1\n10\n5\n5 3 7 7 1\n", "4\n7 4 1 7\n3\n6 8 3\n", "1\n1\n3\n2 1 10\n", "1\n1\n3\n2 1 10\n", "2\n2 9\n5\n4 10 7 3 4\n", "1\n10\n5\n5 3 7 7 1\n", "1\n2\n5\n7 3 9 8 1\n", "2\n9 3\n1\n6\n", "1\n7\n4\n6 2 3 3\n", "2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n piles of stones of sizes a1, a2, ..., an lying on the table in front of you. During one move you can take one pile and add it to the other. As you add pile i to pile j, t...
251_B. Playing with Permutations_6997
Little Petya likes permutations a lot. Recently his mom has presented him permutation q1, q2, ..., qn of length n. A permutation a of length n is a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n), all integers there are distinct. There is only one thing Petya likes more than permutations: playing with little Masha...
n,k=map(int,input().strip().split()) a=list(map(int,input().strip().split())) b=list(map(int,input().strip().split())) ups = [[i+1 for i in range(n)]] downs = [[i+1 for i in range(n)]] def apply(arr): out = [0]*n for i in range(n): out[i] = arr[a[i]-1] return out def unapply(arr): out = [0]*n ...
{ "input": [ "4 1\n4 3 1 2\n3 4 2 1\n", "4 1\n4 3 1 2\n2 1 4 3\n", "4 3\n4 3 1 2\n3 4 2 1\n", "4 1\n2 3 4 1\n1 2 3 4\n", "4 2\n4 3 1 2\n2 1 4 3\n", "4 2\n2 1 4 3\n2 1 4 3\n", "13 2\n2 3 4 5 6 7 8 9 10 11 12 13 1\n3 4 5 6 7 8 9 10 11 12 13 1 2\n", "1 1\n1\n1\n", "8 10\n2 3 1 5 6 7 8...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little Petya likes permutations a lot. Recently his mom has presented him permutation q1, q2, ..., qn of length n. A permutation a of length n is a sequence of integers a1, a2, ..., ...
276_C. Little Girl and Maximum Sum_7001
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_i, r_i (1 ≤ l_i ≤ r_i ≤ n). You need to f...
n,m = map(int,input().split()) a = list(map(int,input().split())) l = [0]*(n+2) for i in range(m): x,y=map(int,input().split()) l[x] += 1 l[y+1] -= 1 for i in range(2, n+2): l[i] += l[i-1] l.sort(reverse=True) a.sort(reverse=True) # print(l, a) ans=0 for i in range(n): ans += l[i]*a[i] print(ans)
{ "input": [ "5 3\n5 2 4 1 3\n1 5\n2 3\n2 3\n", "3 3\n5 3 2\n1 2\n2 3\n1 3\n", "31 48\n45 19 16 42 38 18 50 7 28 40 39 25 45 14 36 18 27 30 16 4 22 6 1 23 16 47 14 35 27 47 2\n6 16\n11 28\n4 30\n25 26\n11 30\n5 9\n4 17\n15 17\n10 25\n15 26\n1 3\n9 26\n8 29\n16 30\n5 24\n27 30\n9 10\n22 29\n2 6\n15 24\n6 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are i...
322_C. Ciel and Robot_7007
Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string s. Each character of s is one move operation. There are four move operations at all: * 'U': go up, (x, y) → (x, y+1); * 'D': go down, (x, y) → (x, y-1); * 'L': go left...
target = tuple(map(int, input().split())) s = input() #print(target) #print(s) ok = False pos = (0, 0) for c in s: if c == 'L': pos = (pos[0]-1, pos[1]) if c == 'R': pos = (pos[0]+1, pos[1]) if c == 'U': pos = (pos[0], pos[1]+1) if c == 'D': pos = (pos[0], pos[1]-1) if pos == target: ok = True if...
{ "input": [ "0 0\nD\n", "-1 1000000000\nLRRLU\n", "2 2\nRU\n", "1 2\nRU\n", "999999999 -999999999\nRRRRRRRRRRRRRRRRRRRRRRRRRDDDDDDDDDDDDDDDDDDDDDDDDDLLLLLLLLLLLLLLLLLLLLLLLUUUUUUUUUUUUUUUUUUUUUUU\n", "0 -3\nRDDL\n", "0 0\nUURRDL\n", "100 100\nRUL\n", "1000000000 1000000000\nRURURU...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string s. Each character of s is one move operatio...
347_A. Difference Row_7011
You want to arrange n integers a1, a2, ..., an in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers. More formally, let's denote some arrangement as a sequence of integers x1, x2, ..., xn, where sequence x is a permutation of sequence a. The ...
# t=int(input()) # import math # for _ in range(t): # n,k=list(map(int,input().split())) # s=input() # a=[] # summer=0 # for i in range(len(s)): # if(s[i]=='1'): # a.append(i) # i=0 # while(i<len(a)-1): # dist=a[i+1]-k-1-(a[i]+k+1)+1 # # print(a,dist) # ...
{ "input": [ "5\n100 -100 50 0 -50\n", "50\n-262 -377 -261 903 547 759 -800 -53 670 92 758 109 547 877 152 -901 -318 -527 -388 24 139 -227 413 -135 811 -886 -22 -526 -643 -431 284 609 -745 -62 323 -441 743 -800 86 862 587 -513 -468 -651 -760 197 141 -414 -909 438\n", "2\n1000 -1000\n", "3\n-1 -1 -1\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You want to arrange n integers a1, a2, ..., an in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers. More...
370_B. Berland Bingo_7015
Lately, a national version of a bingo game has become very popular in Berland. There are n players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the i-th player contains mi numbers. During the game the host takes num...
n = int(input()) cards = [None] + [[] for i in range(n)] for i in range(1, n+1): cards[i] = list(map(int, input().split()))[1:] ans = [None] + [True for i in range(n)] #print(cards, ans) for i in range(1, n + 1): for j in range( 1, n + 1): if i == j : continue; if set(cards[i]) & se...
{ "input": [ "2\n1 1\n1 1\n", "3\n1 1\n3 2 4 1\n2 10 11\n", "40\n2 12 19\n4 10 7 1 3\n2 15 17\n1 6\n3 17 8 20\n4 8 16 11 18\n2 2 7\n4 12 13 8 7\n3 6 1 15\n3 19 11 13\n1 2\n2 16 14\n5 1 17 8 9 5\n1 2\n3 15 17 12\n4 20 4 19 18\n1 10\n4 12 1 17 16\n4 5 10 8 11\n1 10\n1 13\n1 17\n2 19 18\n1 3\n2 6 20\n1 8\n2 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Lately, a national version of a bingo game has become very popular in Berland. There are n players playing the game, each player has a card with numbers. The numbers on each card are ...
441_C. Valera and Tubes _7023
Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y). Valera wants to place...
import sys import math as mt #input=sys.stdin.buffer.readline #import bisect mod=1000000007 #t=int(input()) #tot=0 t=1 for __ in range(t): #n=int(input()) n,m,k=map(int,input().split()) x,y=1,0 d=0 r=1 ch=1 cnt1=1 for i in range(k-1): print(2,end=" ") for j in range(...
{ "input": [ "3 3 3\n", "2 3 1\n", "2 4 3\n", "5 5 12\n", "5 5 3\n", "3 3 4\n", "60 136 8\n", "2 3 3\n", "32 27 153\n", "95 4 177\n", "2 298 1\n", "15 68 212\n", "7 5 17\n", "298 2 1\n", "2 2 2\n", "2 15 3\n", "89 82 2330\n", "135 91 4352\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from lef...
463_D. Gargari and Permutations_7027
Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can...
n, k = map(int, input().split()) ra = [[0] * k for _ in range(n)] for p in range(k): for i, v in enumerate(map(int, input().split())): v -= 1 ra[v][p] = i g = [[] for _ in range(n)] for u in range(n): for v in range(n): if all(x < y for x, y in zip(ra[u], ra[v])): g[u].append...
{ "input": [ "4 3\n1 4 2 3\n4 1 2 3\n1 2 4 3\n", "6 3\n2 5 1 4 6 3\n5 1 4 6 2 3\n5 4 2 6 3 1\n", "6 3\n2 5 1 4 6 3\n5 1 4 3 2 6\n5 4 2 6 3 1\n", "41 4\n24 15 17 35 13 41 4 14 23 5 8 16 21 18 30 36 6 22 11 29 26 1 40 31 7 3 32 10 28 38 12 20 39 37 34 19 33 27 2 25 9\n22 13 25 24 38 35 29 12 15 8 11 37 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them c...
487_C. Prefix Product Sequence_7031
Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence <image>. Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1]. Input The only input line contains an integer n (1 ≤ n ≤ 105). Output In the first output line, print ...
n = int(input()) if n == 1: print('YES\n1') exit(0) if n == 4: print('YES\n1 3 2 4') exit(0) for p in range(2, int(n ** 0.5) + 1): if n % p == 0: print('NO') exit(0) print('YES') print(1) for j in range(2, n): print(j * pow(j - 1, n - 2, n) % n) print(n)
{ "input": [ "6\n", "7\n", "2\n", "1\n", "16553\n", "6577\n", "55004\n", "18047\n", "5\n", "4\n", "1941\n", "7137\n", "7\n", "8677\n", "2647\n", "3\n", "19333\n", "31513\n", "10529\n", "21341\n", "10\n", "8647\n", "1499\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence <image>. Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a perm...
510_B. Fox And Two Dots_7035
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this: <image> Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors. The key of this game is to find a cycle that contain d...
''' Zijian He 1429876 ''' # Import from sys import setrecursionlimit setrecursionlimit(10**6) # Function from class class Graph: def __init__ (self): self._alist = {} def add_vertex (self, vertex): if vertex not in self._alist: self._alist[vertex] = set() def add_edge (self, ...
{ "input": [ "2 13\nABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n", "7 6\nAAAAAB\nABBBAB\nABAAAB\nABABBB\nABAAAB\nABBBAB\nAAAAAB\n", "3 4\nAAAA\nABCA\nAAAA\n", "3 4\nAAAA\nABCA\nAADA\n", "4 4\nYYYR\nBYBY\nBBBY\nBBBY\n", "10 10\nHIICQRHPUJ\nBCDUKHMBFK\nPFTUIDOBOE\nQQPITLRKUP\nERMUJMOSMF\nMRSICEILQB\nODIGFNCHF...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this: <image> Each cell contains a dot that has some col...
535_C. Tavas and Karafs_7039
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. <image> Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is ...
a,b,n=map(int,input().split()) for _ in range(n): l,t,m=map(int,input().split()) lo=l hi=100000000 while lo<hi: mid=(lo+hi)//2 count=(mid-l)+1 first=a+(l-1)*b last=a+(mid-1)*b if last<=t and (count*(first+last))//2<=m*t: lo=mid+1 else: ...
{ "input": [ "1 5 2\n1 5 10\n2 7 4\n", "2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8\n", "1 1 1\n1 1000000 1000000\n", "447 74474 4\n47 777474 747\n74 744744 74477\n477 477447 777\n7 477777 444444\n", "1000000 1000000 1\n1000000 1000000 1000000\n", "1 1 4\n1 1000000 1000000\n1 1 1000000\n1 1000000 1\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. ...
586_A. Alena's Schedule_7045
Alena has successfully passed the entrance exams to the university and is now looking forward to start studying. One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes). The University works in such a way that every day it...
n = int(input()) a = list(map(int, input().split())) + [0] home = True ans = 0 for i in range(n): if a[i]: ans += 1 home = False elif not a[i + 1] and not home: home = True elif not home: ans += 1 print(ans)
{ "input": [ "5\n0 1 0 1 1\n", "7\n1 0 1 0 0 1 0\n", "1\n0\n", "95\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "44\n0 1 1 1 1 0 1 0 0 1 0 1 0 0 1 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alena has successfully passed the entrance exams to the university and is now looking forward to start studying. One two-hour lesson at the Russian university is traditionally called...
608_B. Hamming Distance Sum_7049
Genos needs your help. He was asked to solve the following programming problem by Saitama: The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as <image>, where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming ...
from sys import stdin input=stdin.readline ''' if i+k ''' def getcnt(a): cnt=[[0,0] for i in range(len(a)+1)] for i in range(len(a)): # if i==0: # cnt[i][a[i]]+=1 # else: cnt[i+1][0]=cnt[i][0] cnt[i+1][1]=cnt[i][1] cnt[i+1][a[i]]+=1 return cnt def f(a,b):...
{ "input": [ "01\n00111\n", "0011\n0110\n", "1001101001101110101101000\n01111000010011111111110010001101000100011110101111\n", "1\n0\n", "0\n1\n", "0\n0\n", "1\n1\n", "1110010001000101001011111\n00011011000000100001010000010100110011010001111010\n", "1001101001101110101101000\n0111...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Genos needs your help. He was asked to solve the following programming problem by Saitama: The length of some string s is denoted |s|. The Hamming distance between two strings s and ...
62_A. A Student's Dream_7053
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them. A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable profess...
g_l,g_r=input().split() b_l,b_r=input().split() g_l=int(g_l) g_r=int(g_r) b_l=int(b_l) b_r=int(b_r) flag=0 if g_l==b_r: flag=1 elif g_l>b_r and g_l-1==b_r: flag=1 elif b_r>g_l and 2*g_l+2>=b_r: flag=1 if flag==0: if g_r == b_l: flag = 1 elif g_r > b_l and g_r - 1 == b_l: flag = 1 ...
{ "input": [ "4 5\n3 3\n", "5 1\n10 5\n", "1 2\n11 6\n", "100 100\n50 50\n", "13 23\n19 63\n", "5 23\n12 17\n", "65 8\n73 25\n", "100 3\n4 1\n", "2 25\n8 24\n", "19 17\n5 8\n", "24 43\n96 39\n", "11 17\n6 4\n", "25 9\n16 12\n", "58 86\n40 46\n", "100 1\n4 1\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still up...
724_A. Checking the Calendar_7063
You are given names of two days of the week. Please, determine whether it is possible that during some non-leap year the first day of some month was equal to the first day of the week you are given, while the first day of the next month was equal to the second day of the week you are given. Both months should belong t...
a = str(input()) b = str(input()) k = {'monday':1,'tuesday':2,'wednesday':3,'thursday':4,'friday':5,'saturday':6,'sunday':7} a=k[a]-1 b=k[b]-1 res = False res = res or ((a+2)%7+1)%7==b res = res or ((a+1)%7+1)%7==b res = res or ((a+6)%7+1)%7==b if res: print("YES") else: print("NO")
{ "input": [ "monday\ntuesday\n", "saturday\ntuesday\n", "sunday\nsunday\n", "friday\ntuesday\n", "thursday\nmonday\n", "tuesday\nsunday\n", "thursday\nsaturday\n", "saturday\nfriday\n", "thursday\nsunday\n", "monday\nsaturday\n", "saturday\nsunday\n", "saturday\nsaturd...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given names of two days of the week. Please, determine whether it is possible that during some non-leap year the first day of some month was equal to the first day of the wee...
746_A. Compote_7067
Nikolay has a lemons, b apples and c pears. He decided to cook a compote. According to the recipe the fruits should be in the ratio 1: 2: 4. It means that for each lemon in the compote should be exactly 2 apples and exactly 4 pears. You can't crumble up, break up or cut these fruits into pieces. These fruits — lemons, ...
l = int(input()) a = int(input()) p = int(input()) ll = l aa = a // 2 pp = p // 4 print(min(ll,aa,pp) * 1 + min(ll,aa,pp) * 2 + min(ll,aa,pp) * 4)
{ "input": [ "2\n5\n7\n", "4\n7\n13\n", "2\n3\n2\n", "1000\n500\n1000\n", "1\n1000\n1000\n", "1000\n2\n1000\n", "15\n313\n525\n", "204\n412\n824\n", "1000\n1000\n3\n", "1000\n1000\n4\n", "5\n4\n16\n", "127\n256\n512\n", "1\n2\n999\n", "22\n481\n633\n", "1\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Nikolay has a lemons, b apples and c pears. He decided to cook a compote. According to the recipe the fruits should be in the ratio 1: 2: 4. It means that for each lemon in the compot...