[solution] Infinite Card Game Solution Codeforces

E. Infinite Card Game time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output Monocarp and Bicarp are playing a card game. Each card has two parameters: an attack value and a defence value. A card s� beats another card t� if the attack of s� is strictly greater than the defence of t�. … Read more

[SOLUTION] XOR Construction Solution Codeforces

# Function to construct array b based on a def construct_array(n, a): b = [0] * n b[0] = 0 for i in range(1, n – 1): b[i] = a[i – 1] ^ b[i – 1] all_elements = set(range(n)) used_elements = set(b) missing_element = list(all_elements – used_elements) b[n – 1] = a[n – 2] ^ … Read more

[SOLUTION] Torn Lucky Ticket Solution Codeforces

A ticket is a non-empty string of digits from 11 to 99. A lucky ticket is such a ticket that: it has an even length; the sum of digits in the first half is equal to the sum of digits in the second half. You are given n� ticket pieces s1,s2,…,sn�1,�2,…,��. How many pairs (i,j)(�,�) (for 1≤i,j≤n1≤�,�≤�) are there such that si+sj��+�� is a lucky ticket? Note that it’s possible … Read more

[SOLUTION] Points and Minimum Distance Solution Codeforces

B. Points and Minimum Distance time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output You are given a sequence of integers a� of length 2n2�. You have to split these 2n2� integers into n� pairs; each pair will represent the coordinates of a point on a plane. Each number from the sequence a� should become … Read more

[SOLUTION] A. Treasure Chest SOLUTION CODEFORCES

A. Treasure Chest SOLUTION CODEFORCES time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Monocarp has found a treasure map. The map represents the treasure location as an OX axis. Monocarp is at 00, the treasure chest is at x�, the key to the chest is at y�. Obviously, … Read more

[SOLUTION] Summing the values SOLUTION CODECHEF

Problem SOLUTION CODECHEF For an array �B of length �M, we define its value �(�)f(B) as follows: �(�)=∑�=1�−1((∑�=1���)−(∑�=�+1���))f(B)=i=1∑M−1​((j=1∑i​Bj​)−(j=i+1∑M​Bj​)) In particular, if �=1M=1 then �(�)=0f(B)=0. You’re given an array �A of length �N. Find the sum of values of all of its subarrays. That is, compute ∑�=1�∑�=���(�[�…�])L=1∑N​R=L∑N​f(A[L…R]) where �[�…�]A[L…R] denotes the subarray [��,��+1,��+2,…,��][AL​,AL+1​,AL+2​,…,AR​]. The answer can be large, so print it modulo 998244353998244353. Input Format The first line of input will contain a single … Read more

[SOLUTION] Smallest Excluded LCM SOLUTION CODECHEF

Problem SOLUTION CODECHEF You are given an array �A containing �N integers. Find the smallest integer �x that doesn’t occur as the LCM of some subsequence of �A. The LCM (lowest common multiple) of a set of integers is the smallest positive integer that is a multiple of them all. For example, LCM(2,3)=6,LCM(4,5,8,1)=40,LCM(13)=13LCM(2,3)=6,LCM(4,5,8,1)=40,LCM(13)=13. Input Format The first line of input will contain a single integer �T, denoting the … Read more

[SOLUTION] Count Possibilities SOLUTION CODECHEF

Problem SOLUTION CODECHEF You are given a directed tree with �N vertices. The edges are directed away from the root. For each �=1,2,3,…,�i=1,2,3,…,N, count the number of vertices that can possibly appear at the �i-th position of a topological order of this tree. That is, for each �i, find the number of vertices �u such that there exists a topological order �T of the graph satisfying ��=�Ti​=u. Input Format … Read more

[SOLUTION] Reach Anywhere SOLUTION CODECHEF

Problem SOLUTION CODECHEF You are given a simple undirected graph with �N vertices and �M edges. Find the smallest non-negative integer �K such that for every vertex �u (1≤�≤�1≤u≤N), there exists a walk of length exactly �K from 11 to �u. If no such integer exists, print −1−1 instead. Notes: It is allowed to repeat both vertices and edges in a walk. The length of a walk equals the number of edges in it. Input … Read more

[SOLUTION] Save People SOLUTION CODECHEF

Problem SOLUTION CODECHEF Chef has a 22-D grid with �N rows and �M columns. The cell at the intersection of the �i-th row and �j-th column is denoted cell (�,�)(i,j). Initially, only cell (�,�)(x,y) of the grid is infected. Chef can select exactly one non-infected cell and vaccinate it. Then, the infection and the vaccine spread across the grid, as follows: First, any cell that is neither vaccinated nor infected, but … Read more