## [SOLUTION] Two Characters, Two Colors Solution Codeforces

G. Two Characters, Two Colors time limit per test 4 seconds memory limit per test 512 megabytes input standard input output standard output You are given a string consisting of characters 0 and/or 1. You have to paint every character of this string into one of two colors, red or blue. If you paint the i�-th character red, you … Read more

## [SOLUTION] Fancy Arrays SOLUTION CODEFORCES

F. Fancy Arrays time limit per test 4 seconds memory limit per test 512 megabytes input standard input output standard output Let’s call an array a� of n� non-negative integers fancy if the following conditions hold: at least one from the numbers x�, x+1�+1, …, x+k−1�+�−1 appears in the array; consecutive elements of the array differ by at most k� (i.e. |ai−ai−1|≤k|��−��−1|≤� for each i∈[2,n]�∈[2,�]). You are given n�, x� and k�. Your task … Read more

## [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