Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- code_contests-0104/instruction.md +44 -0
- code_contests-0113/instruction.md +87 -0
- code_contests-0114/instruction.md +81 -0
- code_contests-0122/instruction.md +71 -0
- code_contests-0123/instruction.md +84 -0
- code_contests-0124/instruction.md +184 -0
- code_contests-0125/instruction.md +91 -0
- code_contests-0140/instruction.md +38 -0
- code_contests-0149/instruction.md +62 -0
- code_contests-0157/instruction.md +50 -0
- code_contests-0171/instruction.md +43 -0
- code_contests-0176/instruction.md +56 -0
- code_contests-0178/instruction.md +54 -0
- code_contests-0179/instruction.md +38 -0
- code_contests-0185/instruction.md +76 -0
- code_contests-0311/instruction.md +77 -0
- code_contests-0316/instruction.md +31 -0
- code_contests-0317/instruction.md +81 -0
- code_contests-0318/instruction.md +74 -0
- code_contests-0319/instruction.md +59 -0
- code_contests-0321/instruction.md +173 -0
- code_contests-0328/instruction.md +64 -0
- code_contests-0329/instruction.md +78 -0
- code_contests-0336/instruction.md +75 -0
- code_contests-0343/instruction.md +69 -0
- code_contests-0344/instruction.md +86 -0
- code_contests-0362/instruction.md +69 -0
- code_contests-0374/instruction.md +68 -0
- code_contests-0380/instruction.md +43 -0
- code_contests-0386/instruction.md +54 -0
- code_contests-0387/instruction.md +67 -0
- code_contests-0388/instruction.md +42 -0
- code_contests-0391/instruction.md +116 -0
- code_contests-0512/instruction.md +66 -0
- code_contests-0515/instruction.md +153 -0
- code_contests-0522/instruction.md +51 -0
- code_contests-0524/instruction.md +65 -0
- code_contests-0533/instruction.md +93 -0
- code_contests-0534/instruction.md +57 -0
- code_contests-0540/instruction.md +75 -0
- code_contests-0546/instruction.md +62 -0
- code_contests-0547/instruction.md +73 -0
- code_contests-0548/instruction.md +62 -0
- code_contests-0558/instruction.md +56 -0
- code_contests-0570/instruction.md +72 -0
- code_contests-0577/instruction.md +116 -0
- code_contests-0578/instruction.md +81 -0
- code_contests-0579/instruction.md +77 -0
- code_contests-0583/instruction.md +88 -0
- code_contests-0701/instruction.md +45 -0
code_contests-0104/instruction.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# cdva1502
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Your task is very simple. Given K numbers A1, A2, ..., AK. You need to find f(N) mod max(A1, A2, ..., AK) .
|
| 5 |
+
f(N)=N!
|
| 6 |
+
|
| 7 |
+
Input
|
| 8 |
+
|
| 9 |
+
First line contains single integer T denoting the number of test cases..
|
| 10 |
+
First line of each test case contains two integers N and K.
|
| 11 |
+
Next line contains K integers Ak
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
Output
|
| 15 |
+
For each test case, print a single line containing desired output
|
| 16 |
+
|
| 17 |
+
Constraints
|
| 18 |
+
|
| 19 |
+
1 <= T <= 100
|
| 20 |
+
1 <= N <=100
|
| 21 |
+
1 <= K <=100000
|
| 22 |
+
1 <= Ak <=10^9
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
Example
|
| 26 |
+
Input:
|
| 27 |
+
1
|
| 28 |
+
5 3
|
| 29 |
+
200 6 9
|
| 30 |
+
|
| 31 |
+
Output:
|
| 32 |
+
120
|
| 33 |
+
|
| 34 |
+
## Contest Information
|
| 35 |
+
- **Contest ID**: 0
|
| 36 |
+
- **Problem Index**:
|
| 37 |
+
- **Points**: 0.0
|
| 38 |
+
- **Rating**: 0
|
| 39 |
+
- **Tags**: None
|
| 40 |
+
- **Time Limit**: None seconds
|
| 41 |
+
- **Memory Limit**: 0 bytes
|
| 42 |
+
|
| 43 |
+
## Task
|
| 44 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0113/instruction.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1088_D. Ehab and another another xor problem
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
This is an interactive problem!
|
| 5 |
+
|
| 6 |
+
Ehab plays a game with Laggy. Ehab has 2 hidden integers (a,b). Laggy can ask a pair of integers (c,d) and Ehab will reply with:
|
| 7 |
+
|
| 8 |
+
* 1 if a ⊕ c>b ⊕ d.
|
| 9 |
+
* 0 if a ⊕ c=b ⊕ d.
|
| 10 |
+
* -1 if a ⊕ c<b ⊕ d.
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
Operation a ⊕ b is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of two numbers a and b.
|
| 15 |
+
|
| 16 |
+
Laggy should guess (a,b) with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.
|
| 17 |
+
|
| 18 |
+
It's guaranteed that 0 ≤ a,b<2^{30}.
|
| 19 |
+
|
| 20 |
+
Input
|
| 21 |
+
|
| 22 |
+
See the interaction section.
|
| 23 |
+
|
| 24 |
+
Output
|
| 25 |
+
|
| 26 |
+
To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer.
|
| 27 |
+
|
| 28 |
+
Interaction
|
| 29 |
+
|
| 30 |
+
To ask a question, print "? c d" (without quotes). Both c and d must be non-negative integers less than 2^{30}. Don't forget to flush the output after printing any question.
|
| 31 |
+
|
| 32 |
+
After each question, you should read the answer as mentioned in the legend. If the interactor replies with -2, that means you asked more than 62 queries and your program should terminate.
|
| 33 |
+
|
| 34 |
+
To flush the output, you can use:-
|
| 35 |
+
|
| 36 |
+
* fflush(stdout) in C++.
|
| 37 |
+
* System.out.flush() in Java.
|
| 38 |
+
* stdout.flush() in Python.
|
| 39 |
+
* flush(output) in Pascal.
|
| 40 |
+
* See the documentation for other languages.
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
Hacking:
|
| 45 |
+
|
| 46 |
+
To hack someone, print the 2 space-separated integers a and b (0 ≤ a,b<2^{30}).
|
| 47 |
+
|
| 48 |
+
Example
|
| 49 |
+
|
| 50 |
+
Input
|
| 51 |
+
|
| 52 |
+
1
|
| 53 |
+
-1
|
| 54 |
+
0
|
| 55 |
+
|
| 56 |
+
Output
|
| 57 |
+
|
| 58 |
+
? 2 1
|
| 59 |
+
? 1 2
|
| 60 |
+
? 2 0
|
| 61 |
+
! 3 1
|
| 62 |
+
|
| 63 |
+
Note
|
| 64 |
+
|
| 65 |
+
In the sample:
|
| 66 |
+
|
| 67 |
+
The hidden numbers are a=3 and b=1.
|
| 68 |
+
|
| 69 |
+
In the first query: 3 ⊕ 2 = 1 and 1 ⊕ 1 = 0, so the answer is 1.
|
| 70 |
+
|
| 71 |
+
In the second query: 3 ⊕ 1 = 2 and 1 ⊕ 2 = 3, so the answer is -1.
|
| 72 |
+
|
| 73 |
+
In the third query: 3 ⊕ 2 = 1 and 1 ⊕ 0 = 1, so the answer is 0.
|
| 74 |
+
|
| 75 |
+
Then, we printed the answer.
|
| 76 |
+
|
| 77 |
+
## Contest Information
|
| 78 |
+
- **Contest ID**: 1088
|
| 79 |
+
- **Problem Index**: D
|
| 80 |
+
- **Points**: 2000.0
|
| 81 |
+
- **Rating**: 2000
|
| 82 |
+
- **Tags**: bitmasks, constructive algorithms, implementation, interactive
|
| 83 |
+
- **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
|
| 84 |
+
- **Memory Limit**: 256000000 bytes
|
| 85 |
+
|
| 86 |
+
## Task
|
| 87 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0114/instruction.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1107_D. Compression
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
You are given a binary matrix A of size n × n. Let's denote an x-compression of the given matrix as a matrix B of size n/x × n/x such that for every i ∈ [1, n], j ∈ [1, n] the condition A[i][j] = B[⌈ i/x ⌉][⌈ j/x ⌉] is met.
|
| 5 |
+
|
| 6 |
+
Obviously, x-compression is possible only if x divides n, but this condition is not enough. For example, the following matrix of size 2 × 2 does not have any 2-compression:
|
| 7 |
+
|
| 8 |
+
01 10
|
| 9 |
+
|
| 10 |
+
For the given matrix A, find maximum x such that an x-compression of this matrix is possible.
|
| 11 |
+
|
| 12 |
+
Note that the input is given in compressed form. But even though it is compressed, you'd better use fast input.
|
| 13 |
+
|
| 14 |
+
Input
|
| 15 |
+
|
| 16 |
+
The first line contains one number n (4 ≤ n ≤ 5200) — the number of rows and columns in the matrix A. It is guaranteed that n is divisible by 4.
|
| 17 |
+
|
| 18 |
+
Then the representation of matrix follows. Each of n next lines contains n/4 one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from 0 to 9 or as uppercase Latin letters from A to F). Binary representation of each of these numbers denotes next 4 elements of the matrix in the corresponding row. For example, if the number B is given, then the corresponding elements are 1011, and if the number is 5, then the corresponding elements are 0101.
|
| 19 |
+
|
| 20 |
+
Elements are not separated by whitespaces.
|
| 21 |
+
|
| 22 |
+
Output
|
| 23 |
+
|
| 24 |
+
Print one number: maximum x such that an x-compression of the given matrix is possible.
|
| 25 |
+
|
| 26 |
+
Examples
|
| 27 |
+
|
| 28 |
+
Input
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
8
|
| 32 |
+
E7
|
| 33 |
+
E7
|
| 34 |
+
E7
|
| 35 |
+
00
|
| 36 |
+
00
|
| 37 |
+
E7
|
| 38 |
+
E7
|
| 39 |
+
E7
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
Output
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
1
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
Input
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
4
|
| 52 |
+
7
|
| 53 |
+
F
|
| 54 |
+
F
|
| 55 |
+
F
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
Output
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
1
|
| 62 |
+
|
| 63 |
+
Note
|
| 64 |
+
|
| 65 |
+
The first example corresponds to the matrix:
|
| 66 |
+
|
| 67 |
+
11100111 11100111 11100111 00000000 00000000 11100111 11100111 11100111
|
| 68 |
+
|
| 69 |
+
It is easy to see that the answer on this example is 1.
|
| 70 |
+
|
| 71 |
+
## Contest Information
|
| 72 |
+
- **Contest ID**: 1107
|
| 73 |
+
- **Problem Index**: D
|
| 74 |
+
- **Points**: 0.0
|
| 75 |
+
- **Rating**: 1800
|
| 76 |
+
- **Tags**: dp, implementation, math, number theory
|
| 77 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 500000000} seconds
|
| 78 |
+
- **Memory Limit**: 256000000 bytes
|
| 79 |
+
|
| 80 |
+
## Task
|
| 81 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0122/instruction.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1277_E. Two Fairs
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
There are n cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from 1 to n.
|
| 5 |
+
|
| 6 |
+
Two fairs are currently taking place in Berland — they are held in two different cities a and b (1 ≤ a, b ≤ n; a ≠ b).
|
| 7 |
+
|
| 8 |
+
Find the number of pairs of cities x and y (x ≠ a, x ≠ b, y ≠ a, y ≠ b) such that if you go from x to y you will have to go through both fairs (the order of visits doesn't matter). Formally, you need to find the number of pairs of cities x,y such that any path from x to y goes through a and b (in any order).
|
| 9 |
+
|
| 10 |
+
Print the required number of pairs. The order of two cities in a pair does not matter, that is, the pairs (x,y) and (y,x) must be taken into account only once.
|
| 11 |
+
|
| 12 |
+
Input
|
| 13 |
+
|
| 14 |
+
The first line of the input contains an integer t (1 ≤ t ≤ 4⋅10^4) — the number of test cases in the input. Next, t test cases are specified.
|
| 15 |
+
|
| 16 |
+
The first line of each test case contains four integers n, m, a and b (4 ≤ n ≤ 2⋅10^5, n - 1 ≤ m ≤ 5⋅10^5, 1 ≤ a,b ≤ n, a ≠ b) — numbers of cities and roads in Berland and numbers of two cities where fairs are held, respectively.
|
| 17 |
+
|
| 18 |
+
The following m lines contain descriptions of roads between cities. Each of road description contains a pair of integers u_i, v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — numbers of cities connected by the road.
|
| 19 |
+
|
| 20 |
+
Each road is bi-directional and connects two different cities. It is guaranteed that from any city you can pass to any other by roads. There can be more than one road between a pair of cities.
|
| 21 |
+
|
| 22 |
+
The sum of the values of n for all sets of input data in the test does not exceed 2⋅10^5. The sum of the values of m for all sets of input data in the test does not exceed 5⋅10^5.
|
| 23 |
+
|
| 24 |
+
Output
|
| 25 |
+
|
| 26 |
+
Print t integers — the answers to the given test cases in the order they are written in the input.
|
| 27 |
+
|
| 28 |
+
Example
|
| 29 |
+
|
| 30 |
+
Input
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
3
|
| 34 |
+
7 7 3 5
|
| 35 |
+
1 2
|
| 36 |
+
2 3
|
| 37 |
+
3 4
|
| 38 |
+
4 5
|
| 39 |
+
5 6
|
| 40 |
+
6 7
|
| 41 |
+
7 5
|
| 42 |
+
4 5 2 3
|
| 43 |
+
1 2
|
| 44 |
+
2 3
|
| 45 |
+
3 4
|
| 46 |
+
4 1
|
| 47 |
+
4 2
|
| 48 |
+
4 3 2 1
|
| 49 |
+
1 2
|
| 50 |
+
2 3
|
| 51 |
+
4 1
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
Output
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
4
|
| 58 |
+
0
|
| 59 |
+
1
|
| 60 |
+
|
| 61 |
+
## Contest Information
|
| 62 |
+
- **Contest ID**: 1277
|
| 63 |
+
- **Problem Index**: E
|
| 64 |
+
- **Points**: 1000.0
|
| 65 |
+
- **Rating**: 1900
|
| 66 |
+
- **Tags**: combinatorics, dfs and similar, dsu, graphs
|
| 67 |
+
- **Time Limit**: {'seconds': 3, 'nanos': 0} seconds
|
| 68 |
+
- **Memory Limit**: 256000000 bytes
|
| 69 |
+
|
| 70 |
+
## Task
|
| 71 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0123/instruction.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1297_E. Modernization of Treeland
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Treeland consists of n cities and n-1 two-way roads connecting pairs of cities. From every city, you can reach every other city moving only by the roads. You are right, the system of cities and roads in this country forms an undirected tree.
|
| 5 |
+
|
| 6 |
+
The government has announced a program for the modernization of urban infrastructure of some cities. You have been assigned to select an arbitrary subset of cities S to upgrade (potentially all the cities) that satisfies the following requirements:
|
| 7 |
+
|
| 8 |
+
* the subset of cities must be "connected", that is, from any city of the subset S you can get to any other city of the subset S by roads, moving only through cities from S,
|
| 9 |
+
* the number of "dead-ends" in S must be equal to the given number k. A city is a "dead-end" if it is the only city in S or connected to exactly one another city from S.
|
| 10 |
+
|
| 11 |
+
<image> This shows one of the possible ways to select S (blue vertices) for a given configuration and k=4. Dead-ends are vertices with numbers 1, 4, 6 and 7.
|
| 12 |
+
|
| 13 |
+
Help Treeland upgrade its cities. Find any of the possible subsets S or determine that such a subset does not exist.
|
| 14 |
+
|
| 15 |
+
Input
|
| 16 |
+
|
| 17 |
+
The first line contains an integer t (1 ≤ t ≤ 10^4) — the number of test cases in the input. This is followed by the test cases themselves.
|
| 18 |
+
|
| 19 |
+
Each test case begins with a line that contains two integers n and k (2 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ k ≤ n) — the number of cities in Treeland and the number of "dead-end" cities required in the subset S.
|
| 20 |
+
|
| 21 |
+
This is followed by n-1 lines with road descriptions. Each road is given by two integers x and y (1 ≤ x, y ≤ n; x ≠ y) — the numbers of the cities that are connected by this road. It is guaranteed that from every city you can reach any other, moving only by the roads.
|
| 22 |
+
|
| 23 |
+
The sum of the values of n for all test cases in the input does not exceed 3 ⋅ 10^5.
|
| 24 |
+
|
| 25 |
+
Output
|
| 26 |
+
|
| 27 |
+
For each test case print Yes or No (in any case, upper or lower), depending on whether the answer exists or not. If the answer exists, then print an integer m (1 ≤ m ≤ n) — the number of cities in the found subset. Then print m different numbers from 1 to n — the numbers of the cities in the found subset. City numbers can be printed in any order. If there are several answers, print any of them.
|
| 28 |
+
|
| 29 |
+
Example
|
| 30 |
+
|
| 31 |
+
Input
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
4
|
| 35 |
+
10 4
|
| 36 |
+
4 5
|
| 37 |
+
5 2
|
| 38 |
+
2 1
|
| 39 |
+
1 3
|
| 40 |
+
1 9
|
| 41 |
+
9 10
|
| 42 |
+
2 7
|
| 43 |
+
7 8
|
| 44 |
+
5 6
|
| 45 |
+
4 3
|
| 46 |
+
1 2
|
| 47 |
+
2 3
|
| 48 |
+
3 4
|
| 49 |
+
5 3
|
| 50 |
+
1 2
|
| 51 |
+
1 3
|
| 52 |
+
1 4
|
| 53 |
+
1 5
|
| 54 |
+
4 1
|
| 55 |
+
1 2
|
| 56 |
+
2 4
|
| 57 |
+
2 3
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
Output
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
Yes
|
| 64 |
+
9
|
| 65 |
+
1 2 4 5 6 7 8 9 10
|
| 66 |
+
No
|
| 67 |
+
Yes
|
| 68 |
+
4
|
| 69 |
+
1 3 4 5
|
| 70 |
+
Yes
|
| 71 |
+
1
|
| 72 |
+
4
|
| 73 |
+
|
| 74 |
+
## Contest Information
|
| 75 |
+
- **Contest ID**: 1297
|
| 76 |
+
- **Problem Index**: E
|
| 77 |
+
- **Points**: 0.0
|
| 78 |
+
- **Rating**: 0
|
| 79 |
+
- **Tags**: *special, dfs and similar, trees
|
| 80 |
+
- **Time Limit**: {'seconds': 5, 'nanos': 0} seconds
|
| 81 |
+
- **Memory Limit**: 256000000 bytes
|
| 82 |
+
|
| 83 |
+
## Task
|
| 84 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0124/instruction.md
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1320_F. Blocks and Sensors
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Polycarp plays a well-known computer game (we won't mention its name). Every object in this game consists of three-dimensional blocks — axis-aligned cubes of size 1 × 1 × 1. These blocks are unaffected by gravity, so they can float in the air without support. The blocks are placed in cells of size 1 × 1 × 1; each cell either contains exactly one block or is empty. Each cell is represented by its coordinates (x, y, z) (the cell with these coordinates is a cube with opposite corners in (x, y, z) and (x + 1, y + 1, z + 1)) and its contents a_{x, y, z}; if the cell is empty, then a_{x, y, z} = 0, otherwise a_{x, y, z} is equal to the type of the block placed in it (the types are integers from 1 to 2 ⋅ 10^5).
|
| 5 |
+
|
| 6 |
+
Polycarp has built a large structure consisting of blocks. This structure can be enclosed in an axis-aligned rectangular parallelepiped of size n × m × k, containing all cells (x, y, z) such that x ∈ [1, n], y ∈ [1, m], and z ∈ [1, k]. After that, Polycarp has installed 2nm + 2nk + 2mk sensors around this parallelepiped. A sensor is a special block that sends a ray in some direction and shows the type of the first block that was hit by this ray (except for other sensors). The sensors installed by Polycarp are adjacent to the borders of the parallelepiped, and the rays sent by them are parallel to one of the coordinate axes and directed inside the parallelepiped. More formally, the sensors can be divided into 6 types:
|
| 7 |
+
|
| 8 |
+
* there are mk sensors of the first type; each such sensor is installed in (0, y, z), where y ∈ [1, m] and z ∈ [1, k], and it sends a ray that is parallel to the Ox axis and has the same direction;
|
| 9 |
+
* there are mk sensors of the second type; each such sensor is installed in (n + 1, y, z), where y ∈ [1, m] and z ∈ [1, k], and it sends a ray that is parallel to the Ox axis and has the opposite direction;
|
| 10 |
+
* there are nk sensors of the third type; each such sensor is installed in (x, 0, z), where x ∈ [1, n] and z ∈ [1, k], and it sends a ray that is parallel to the Oy axis and has the same direction;
|
| 11 |
+
* there are nk sensors of the fourth type; each such sensor is installed in (x, m + 1, z), where x ∈ [1, n] and z ∈ [1, k], and it sends a ray that is parallel to the Oy axis and has the opposite direction;
|
| 12 |
+
* there are nm sensors of the fifth type; each such sensor is installed in (x, y, 0), where x ∈ [1, n] and y ∈ [1, m], and it sends a ray that is parallel to the Oz axis and has the same direction;
|
| 13 |
+
* finally, there are nm sensors of the sixth type; each such sensor is installed in (x, y, k + 1), where x ∈ [1, n] and y ∈ [1, m], and it sends a ray that is parallel to the Oz axis and has the opposite direction.
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
Polycarp has invited his friend Monocarp to play with him. Of course, as soon as Monocarp saw a large parallelepiped bounded by sensor blocks, he began to wonder what was inside of it. Polycarp didn't want to tell Monocarp the exact shape of the figure, so he provided Monocarp with the data from all sensors and told him to try guessing the contents of the parallelepiped by himself.
|
| 18 |
+
|
| 19 |
+
After some hours of thinking, Monocarp has no clue about what's inside the sensor-bounded space. But he does not want to give up, so he decided to ask for help. Can you write a program that will analyze the sensor data and construct any figure that is consistent with it?
|
| 20 |
+
|
| 21 |
+
Input
|
| 22 |
+
|
| 23 |
+
The first line contains three integers n, m and k (1 ≤ n, m, k ≤ 2 ⋅ 10^5, nmk ≤ 2 ⋅ 10^5) — the dimensions of the parallelepiped.
|
| 24 |
+
|
| 25 |
+
Then the sensor data follows. For each sensor, its data is either 0, if the ray emitted from it reaches the opposite sensor (there are no blocks in between), or an integer from 1 to 2 ⋅ 10^5 denoting the type of the first block hit by the ray. The data is divided into 6 sections (one for each type of sensors), each consecutive pair of sections is separated by a blank line, and the first section is separated by a blank line from the first line of the input.
|
| 26 |
+
|
| 27 |
+
The first section consists of m lines containing k integers each. The j-th integer in the i-th line is the data from the sensor installed in (0, i, j).
|
| 28 |
+
|
| 29 |
+
The second section consists of m lines containing k integers each. The j-th integer in the i-th line is the data from the sensor installed in (n + 1, i, j).
|
| 30 |
+
|
| 31 |
+
The third section consists of n lines containing k integers each. The j-th integer in the i-th line is the data from the sensor installed in (i, 0, j).
|
| 32 |
+
|
| 33 |
+
The fourth section consists of n lines containing k integers each. The j-th integer in the i-th line is the data from the sensor installed in (i, m + 1, j).
|
| 34 |
+
|
| 35 |
+
The fifth section consists of n lines containing m integers each. The j-th integer in the i-th line is the data from the sensor installed in (i, j, 0).
|
| 36 |
+
|
| 37 |
+
Finally, the sixth section consists of n lines containing m integers each. The j-th integer in the i-th line is the data from the sensor installed in (i, j, k + 1).
|
| 38 |
+
|
| 39 |
+
Output
|
| 40 |
+
|
| 41 |
+
If the information from the input is inconsistent, print one integer -1.
|
| 42 |
+
|
| 43 |
+
Otherwise, print the figure inside the parallelepiped as follows. The output should consist of nmk integers: a_{1, 1, 1}, a_{1, 1, 2}, ..., a_{1, 1, k}, a_{1, 2, 1}, ..., a_{1, 2, k}, ..., a_{1, m, k}, a_{2, 1, 1}, ..., a_{n, m, k}, where a_{i, j, k} is the type of the block in (i, j, k), or 0 if there is no block there. If there are multiple figures consistent with sensor data, describe any of them.
|
| 44 |
+
|
| 45 |
+
For your convenience, the sample output is formatted as follows: there are n separate sections for blocks having x = 1, x = 2, ..., x = n; each section consists of m lines containing k integers each. Note that this type of output is acceptable, but you may print the integers with any other formatting instead (even all integers on the same line), only their order matters.
|
| 46 |
+
|
| 47 |
+
Examples
|
| 48 |
+
|
| 49 |
+
Input
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
4 3 2
|
| 53 |
+
|
| 54 |
+
1 4
|
| 55 |
+
3 2
|
| 56 |
+
6 5
|
| 57 |
+
|
| 58 |
+
1 4
|
| 59 |
+
3 2
|
| 60 |
+
6 7
|
| 61 |
+
|
| 62 |
+
1 4
|
| 63 |
+
1 4
|
| 64 |
+
0 0
|
| 65 |
+
0 7
|
| 66 |
+
|
| 67 |
+
6 5
|
| 68 |
+
6 5
|
| 69 |
+
0 0
|
| 70 |
+
0 7
|
| 71 |
+
|
| 72 |
+
1 3 6
|
| 73 |
+
1 3 6
|
| 74 |
+
0 0 0
|
| 75 |
+
0 0 7
|
| 76 |
+
|
| 77 |
+
4 3 5
|
| 78 |
+
4 2 5
|
| 79 |
+
0 0 0
|
| 80 |
+
0 0 7
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
Output
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
1 4
|
| 87 |
+
3 0
|
| 88 |
+
6 5
|
| 89 |
+
|
| 90 |
+
1 4
|
| 91 |
+
3 2
|
| 92 |
+
6 5
|
| 93 |
+
|
| 94 |
+
0 0
|
| 95 |
+
0 0
|
| 96 |
+
0 0
|
| 97 |
+
|
| 98 |
+
0 0
|
| 99 |
+
0 0
|
| 100 |
+
0 7
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
Input
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
1 1 1
|
| 107 |
+
|
| 108 |
+
0
|
| 109 |
+
|
| 110 |
+
0
|
| 111 |
+
|
| 112 |
+
0
|
| 113 |
+
|
| 114 |
+
0
|
| 115 |
+
|
| 116 |
+
0
|
| 117 |
+
|
| 118 |
+
0
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
Output
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
0
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
Input
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
1 1 1
|
| 131 |
+
|
| 132 |
+
0
|
| 133 |
+
|
| 134 |
+
0
|
| 135 |
+
|
| 136 |
+
1337
|
| 137 |
+
|
| 138 |
+
0
|
| 139 |
+
|
| 140 |
+
0
|
| 141 |
+
|
| 142 |
+
0
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
Output
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
-1
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
Input
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
1 1 1
|
| 155 |
+
|
| 156 |
+
1337
|
| 157 |
+
|
| 158 |
+
1337
|
| 159 |
+
|
| 160 |
+
1337
|
| 161 |
+
|
| 162 |
+
1337
|
| 163 |
+
|
| 164 |
+
1337
|
| 165 |
+
|
| 166 |
+
1337
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
Output
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
1337
|
| 173 |
+
|
| 174 |
+
## Contest Information
|
| 175 |
+
- **Contest ID**: 1320
|
| 176 |
+
- **Problem Index**: F
|
| 177 |
+
- **Points**: 2500.0
|
| 178 |
+
- **Rating**: 3500
|
| 179 |
+
- **Tags**: brute force
|
| 180 |
+
- **Time Limit**: {'seconds': 3, 'nanos': 0} seconds
|
| 181 |
+
- **Memory Limit**: 512000000 bytes
|
| 182 |
+
|
| 183 |
+
## Task
|
| 184 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0125/instruction.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 133_D. Piet
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Piet is one of the most known visual esoteric programming languages. The programs in Piet are constructed from colorful blocks of pixels and interpreted using pretty complicated rules. In this problem we will use a subset of Piet language with simplified rules.
|
| 5 |
+
|
| 6 |
+
The program will be a rectangular image consisting of colored and black pixels. The color of each pixel will be given by an integer number between 0 and 9, inclusive, with 0 denoting black. A block of pixels is defined as a rectangle of pixels of the same color (not black). It is guaranteed that all connected groups of colored pixels of the same color will form rectangular blocks. Groups of black pixels can form arbitrary shapes.
|
| 7 |
+
|
| 8 |
+
The program is interpreted using movement of instruction pointer (IP) which consists of three parts:
|
| 9 |
+
|
| 10 |
+
* current block pointer (BP); note that there is no concept of current pixel within the block;
|
| 11 |
+
* direction pointer (DP) which can point left, right, up or down;
|
| 12 |
+
* block chooser (CP) which can point to the left or to the right from the direction given by DP; in absolute values CP can differ from DP by 90 degrees counterclockwise or clockwise, respectively.
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
Initially BP points to the block which contains the top-left corner of the program, DP points to the right, and CP points to the left (see the orange square on the image below).
|
| 17 |
+
|
| 18 |
+
One step of program interpretation changes the state of IP in a following way. The interpreter finds the furthest edge of the current color block in the direction of the DP. From all pixels that form this edge, the interpreter selects the furthest one in the direction of CP. After this, BP attempts to move from this pixel into the next one in the direction of DP. If the next pixel belongs to a colored block, this block becomes the current one, and two other parts of IP stay the same. It the next pixel is black or outside of the program, BP stays the same but two other parts of IP change. If CP was pointing to the left, now it points to the right, and DP stays the same. If CP was pointing to the right, now it points to the left, and DP is rotated 90 degrees clockwise.
|
| 19 |
+
|
| 20 |
+
This way BP will never point to a black block (it is guaranteed that top-left pixel of the program will not be black).
|
| 21 |
+
|
| 22 |
+
You are given a Piet program. You have to figure out which block of the program will be current after n steps.
|
| 23 |
+
|
| 24 |
+
Input
|
| 25 |
+
|
| 26 |
+
The first line of the input contains two integer numbers m (1 ≤ m ≤ 50) and n (1 ≤ n ≤ 5·107). Next m lines contain the rows of the program. All the lines have the same length between 1 and 50 pixels, and consist of characters 0-9. The first character of the first line will not be equal to 0.
|
| 27 |
+
|
| 28 |
+
Output
|
| 29 |
+
|
| 30 |
+
Output the color of the block which will be current after n steps of program interpretation.
|
| 31 |
+
|
| 32 |
+
Examples
|
| 33 |
+
|
| 34 |
+
Input
|
| 35 |
+
|
| 36 |
+
2 10
|
| 37 |
+
12
|
| 38 |
+
43
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
Output
|
| 42 |
+
|
| 43 |
+
1
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
Input
|
| 47 |
+
|
| 48 |
+
3 12
|
| 49 |
+
1423
|
| 50 |
+
6624
|
| 51 |
+
6625
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
Output
|
| 55 |
+
|
| 56 |
+
6
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
Input
|
| 60 |
+
|
| 61 |
+
5 9
|
| 62 |
+
10345
|
| 63 |
+
23456
|
| 64 |
+
34567
|
| 65 |
+
45678
|
| 66 |
+
56789
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
Output
|
| 70 |
+
|
| 71 |
+
5
|
| 72 |
+
|
| 73 |
+
Note
|
| 74 |
+
|
| 75 |
+
In the first example IP changes in the following way. After step 1 block 2 becomes current one and stays it after two more steps. After step 4 BP moves to block 3, after step 7 — to block 4, and finally after step 10 BP returns to block 1.
|
| 76 |
+
|
| 77 |
+
<image>
|
| 78 |
+
|
| 79 |
+
The sequence of states of IP is shown on the image: the arrows are traversed clockwise, the main arrow shows direction of DP, the side one — the direction of CP.
|
| 80 |
+
|
| 81 |
+
## Contest Information
|
| 82 |
+
- **Contest ID**: 133
|
| 83 |
+
- **Problem Index**: D
|
| 84 |
+
- **Points**: 1500.0
|
| 85 |
+
- **Rating**: 2100
|
| 86 |
+
- **Tags**: implementation
|
| 87 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 88 |
+
- **Memory Limit**: 256000000 bytes
|
| 89 |
+
|
| 90 |
+
## Task
|
| 91 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0140/instruction.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 290_D. Orange
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
<image>
|
| 5 |
+
|
| 6 |
+
Input
|
| 7 |
+
|
| 8 |
+
The first line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be a letter of English alphabet, lowercase or uppercase.
|
| 9 |
+
|
| 10 |
+
The second line of the input is an integer between 0 and 26, inclusive.
|
| 11 |
+
|
| 12 |
+
Output
|
| 13 |
+
|
| 14 |
+
Output the required string.
|
| 15 |
+
|
| 16 |
+
Examples
|
| 17 |
+
|
| 18 |
+
Input
|
| 19 |
+
|
| 20 |
+
AprilFool
|
| 21 |
+
14
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
Output
|
| 25 |
+
|
| 26 |
+
AprILFooL
|
| 27 |
+
|
| 28 |
+
## Contest Information
|
| 29 |
+
- **Contest ID**: 290
|
| 30 |
+
- **Problem Index**: D
|
| 31 |
+
- **Points**: 0.0
|
| 32 |
+
- **Rating**: 1400
|
| 33 |
+
- **Tags**: *special, implementation
|
| 34 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 35 |
+
- **Memory Limit**: 256000000 bytes
|
| 36 |
+
|
| 37 |
+
## Task
|
| 38 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0149/instruction.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 500_C. New Year Book Reading
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi.
|
| 5 |
+
|
| 6 |
+
As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.
|
| 7 |
+
|
| 8 |
+
1. He lifts all the books above book x.
|
| 9 |
+
2. He pushes book x out of the stack.
|
| 10 |
+
3. He puts down the lifted books without changing their order.
|
| 11 |
+
4. After reading book x, he puts book x on the top of the stack.
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
<image>
|
| 16 |
+
|
| 17 |
+
He decided to read books for m days. In the j-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integer bj (1 ≤ bj ≤ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.
|
| 18 |
+
|
| 19 |
+
After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
|
| 20 |
+
|
| 21 |
+
Input
|
| 22 |
+
|
| 23 |
+
The first line contains two space-separated integers n (2 ≤ n ≤ 500) and m (1 ≤ m ≤ 1000) — the number of books, and the number of days for which Jaehyun would read books.
|
| 24 |
+
|
| 25 |
+
The second line contains n space-separated integers w1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book.
|
| 26 |
+
|
| 27 |
+
The third line contains m space separated integers b1, b2, ..., bm (1 ≤ bj ≤ n) — the order of books that he would read. Note that he can read the same book more than once.
|
| 28 |
+
|
| 29 |
+
Output
|
| 30 |
+
|
| 31 |
+
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
|
| 32 |
+
|
| 33 |
+
Examples
|
| 34 |
+
|
| 35 |
+
Input
|
| 36 |
+
|
| 37 |
+
3 5
|
| 38 |
+
1 2 3
|
| 39 |
+
1 3 2 3 1
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
Output
|
| 43 |
+
|
| 44 |
+
12
|
| 45 |
+
|
| 46 |
+
Note
|
| 47 |
+
|
| 48 |
+
Here's a picture depicting the example. Each vertical column presents the stacked books.
|
| 49 |
+
|
| 50 |
+
<image>
|
| 51 |
+
|
| 52 |
+
## Contest Information
|
| 53 |
+
- **Contest ID**: 500
|
| 54 |
+
- **Problem Index**: C
|
| 55 |
+
- **Points**: 1000.0
|
| 56 |
+
- **Rating**: 1600
|
| 57 |
+
- **Tags**: constructive algorithms, greedy, implementation, math
|
| 58 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 59 |
+
- **Memory Limit**: 256000000 bytes
|
| 60 |
+
|
| 61 |
+
## Task
|
| 62 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0157/instruction.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 691_D. Swaps in Permutation
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (aj, bj).
|
| 5 |
+
|
| 6 |
+
At each step you can choose a pair from the given positions and swap the numbers in that positions. What is the lexicographically maximal permutation one can get?
|
| 7 |
+
|
| 8 |
+
Let p and q be two permutations of the numbers 1, 2, ..., n. p is lexicographically smaller than the q if a number 1 ≤ i ≤ n exists, so pk = qk for 1 ≤ k < i and pi < qi.
|
| 9 |
+
|
| 10 |
+
Input
|
| 11 |
+
|
| 12 |
+
The first line contains two integers n and m (1 ≤ n, m ≤ 106) — the length of the permutation p and the number of pairs of positions.
|
| 13 |
+
|
| 14 |
+
The second line contains n distinct integers pi (1 ≤ pi ≤ n) — the elements of the permutation p.
|
| 15 |
+
|
| 16 |
+
Each of the last m lines contains two integers (aj, bj) (1 ≤ aj, bj ≤ n) — the pairs of positions to swap. Note that you are given a positions, not the values to swap.
|
| 17 |
+
|
| 18 |
+
Output
|
| 19 |
+
|
| 20 |
+
Print the only line with n distinct integers p'i (1 ≤ p'i ≤ n) — the lexicographically maximal permutation one can get.
|
| 21 |
+
|
| 22 |
+
Example
|
| 23 |
+
|
| 24 |
+
Input
|
| 25 |
+
|
| 26 |
+
9 6
|
| 27 |
+
1 2 3 4 5 6 7 8 9
|
| 28 |
+
1 4
|
| 29 |
+
4 7
|
| 30 |
+
2 5
|
| 31 |
+
5 8
|
| 32 |
+
3 6
|
| 33 |
+
6 9
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
Output
|
| 37 |
+
|
| 38 |
+
7 8 9 4 5 6 1 2 3
|
| 39 |
+
|
| 40 |
+
## Contest Information
|
| 41 |
+
- **Contest ID**: 691
|
| 42 |
+
- **Problem Index**: D
|
| 43 |
+
- **Points**: 0.0
|
| 44 |
+
- **Rating**: 1700
|
| 45 |
+
- **Tags**: dfs and similar, dsu, math
|
| 46 |
+
- **Time Limit**: {'seconds': 5, 'nanos': 0} seconds
|
| 47 |
+
- **Memory Limit**: 256000000 bytes
|
| 48 |
+
|
| 49 |
+
## Task
|
| 50 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0171/instruction.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# battle-of-stalingrad-1
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
The bloodiest battle of World War II has started. Germany and its allies have attacked the Soviet union army to gain control of the city of Stalingrad (now Volgograd) in the south-western Soviet Union.The war has become intensive and the soviet union's 64th army is fighting against the germans. General "Vasily Chuikov" is commanding the soviet union troops on a large triangular soviet army post located on the west bank of the Don River.General "Vasily Chuikov" very well knows the location of three corners of soviet army post.He is trying to detect any intruder inside the post.He is able to locate the position of enemy soldiers through his radar. Now if the location of any enemy soldier is known,it is required to tell whether the enemy is inside or outside the soviet army post.
|
| 5 |
+
|
| 6 |
+
note: Any location is given by its coordinates(X,Y).
|
| 7 |
+
|
| 8 |
+
INPUT:
|
| 9 |
+
|
| 10 |
+
The first line contain number of soviet army post "T"."T" lines follows then.Each of the "T" lines contains three coordinates of soviet army post (X1,Y1),(X2,Y2),(X3,Y3) and the coordinates of enemy soldier (X4,Y4).
|
| 11 |
+
|
| 12 |
+
OUTPUT:
|
| 13 |
+
|
| 14 |
+
For each testcase print "OUTSIDE" If the enemy soldier is outside the triangular shaped soviet army post,if the enemy is inside the post, print "INSIDE".
|
| 15 |
+
Also if the three known coordinates of soviet army post does not form a triangle,print "INSIDE".
|
| 16 |
+
|
| 17 |
+
Constraints:
|
| 18 |
+
1 ≤ T ≤ 1000
|
| 19 |
+
-100 ≤ coordinates ≤ 100
|
| 20 |
+
|
| 21 |
+
SAMPLE INPUT
|
| 22 |
+
2
|
| 23 |
+
-1 0 1 0 0 1 3 3
|
| 24 |
+
-5 0 5 0 0 5 1 0SAMPLE OUTPUT
|
| 25 |
+
OUTSIDE
|
| 26 |
+
INSIDE
|
| 27 |
+
|
| 28 |
+
Explanation
|
| 29 |
+
|
| 30 |
+
In the first case,the enemy is outside the post.
|
| 31 |
+
In the second case,the enemy is inside the post.
|
| 32 |
+
|
| 33 |
+
## Contest Information
|
| 34 |
+
- **Contest ID**: 0
|
| 35 |
+
- **Problem Index**:
|
| 36 |
+
- **Points**: 0.0
|
| 37 |
+
- **Rating**: 0
|
| 38 |
+
- **Tags**: None
|
| 39 |
+
- **Time Limit**: None seconds
|
| 40 |
+
- **Memory Limit**: 0 bytes
|
| 41 |
+
|
| 42 |
+
## Task
|
| 43 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0176/instruction.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# new-world-11
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
The time has arrived when the world is going to end. But don't worry, because the new world yuga will start soon. Manu (carrier of mankind) has been assigned the job to carry all the necessary elements of current yuga to the upcoming yuga.
|
| 5 |
+
|
| 6 |
+
There are N stones arranged in a straight line. In order to fulfill the task, Manu has to travel from the first stone to the last one in a very specific way. First of all, he has to do it using exactly K jumps. In a single jump, he can jump from a stone with coordinate xi to a stone with coordinate xj if and only if xi < xj. We denote the value xj - xi as the length of such a jump.
|
| 7 |
+
|
| 8 |
+
Your task is to help Manu minimize the maximum length of a jump he makes in his journey, in order to reach the last stone in exactly K jumps and save the mankind. This is the answer you have to provide.
|
| 9 |
+
|
| 10 |
+
Input:
|
| 11 |
+
|
| 12 |
+
In the first line, there is a single integer T denoting the number of test cases to handle. Then, description of T tests follow. In the first line of each such description, two integers N and K are given. This is followed by N space separated integers in the second line, each one denoting a single stone location.
|
| 13 |
+
|
| 14 |
+
Output:
|
| 15 |
+
|
| 16 |
+
Output exactly T lines, and in the i^th of them, print a single integer denoting the answer to the i^th test case.
|
| 17 |
+
|
| 18 |
+
Constraints:
|
| 19 |
+
|
| 20 |
+
1 ≤ T ≤ 10
|
| 21 |
+
2 ≤ N ≤ 10^5
|
| 22 |
+
1 ≤ K ≤ (N-1)
|
| 23 |
+
1 ≤ Coordinates of stones ≤ 10^9
|
| 24 |
+
|
| 25 |
+
Note:
|
| 26 |
+
|
| 27 |
+
It is not necessary that Manu steps on each stone.
|
| 28 |
+
|
| 29 |
+
Location of all stones are given in ascending order.SAMPLE INPUT
|
| 30 |
+
2
|
| 31 |
+
4 1
|
| 32 |
+
2 15 36 43
|
| 33 |
+
3 2
|
| 34 |
+
27 30 35
|
| 35 |
+
|
| 36 |
+
SAMPLE OUTPUT
|
| 37 |
+
41
|
| 38 |
+
5
|
| 39 |
+
|
| 40 |
+
Explanation
|
| 41 |
+
|
| 42 |
+
Case #1: Since we have to make exactly 1 jump, we need to jump directly from the first stone to the last one, so the result equals 43 - 2 = 41.
|
| 43 |
+
|
| 44 |
+
Case#2: Since we have to make exactly 2 jumps, we need to jump to all of the stones. The minimal longest jump to do this is 5, because 35 - 30 = 5.
|
| 45 |
+
|
| 46 |
+
## Contest Information
|
| 47 |
+
- **Contest ID**: 0
|
| 48 |
+
- **Problem Index**:
|
| 49 |
+
- **Points**: 0.0
|
| 50 |
+
- **Rating**: 0
|
| 51 |
+
- **Tags**: None
|
| 52 |
+
- **Time Limit**: None seconds
|
| 53 |
+
- **Memory Limit**: 0 bytes
|
| 54 |
+
|
| 55 |
+
## Task
|
| 56 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0178/instruction.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# sherlock-and-kgb
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
The russian intelligence agency KGB make an encryption technique to send their passwords. Originally the password is of 3 characters. After encryption the password is converted into 3 numbers A-B-C.
|
| 5 |
+
|
| 6 |
+
Now, Sherlock wants to decrypt the password encryption technique of KGB. Sherlock knows that every number has only 2 possible values which are following:
|
| 7 |
+
|
| 8 |
+
POSSIBLE VALUES OF "A": K, X
|
| 9 |
+
|
| 10 |
+
POSSIBLE VALUES OF "B": G, Y
|
| 11 |
+
|
| 12 |
+
POSSIBLE VALUES OF "C": B, Z
|
| 13 |
+
|
| 14 |
+
Help Sherlock to detect the encryption technique of the KGB and tell him the original password.
|
| 15 |
+
[See problem explanation for further clarifiaction]
|
| 16 |
+
|
| 17 |
+
INPUT:
|
| 18 |
+
|
| 19 |
+
The first line consist number of test cases T followed by T lines, each line consist of 3 space separated integers A, B, C.
|
| 20 |
+
|
| 21 |
+
OUTPUT
|
| 22 |
+
|
| 23 |
+
Print the correct message in format of C-C-C
|
| 24 |
+
|
| 25 |
+
Constraints:
|
| 26 |
+
|
| 27 |
+
1 ≤ T ≤ 50000
|
| 28 |
+
|
| 29 |
+
1 ≤ A, B, C ≤ 1000000000
|
| 30 |
+
|
| 31 |
+
SAMPLE INPUT
|
| 32 |
+
2
|
| 33 |
+
27 25 4
|
| 34 |
+
25 26 16
|
| 35 |
+
|
| 36 |
+
SAMPLE OUTPUT
|
| 37 |
+
K-G-B
|
| 38 |
+
X-Y-B
|
| 39 |
+
|
| 40 |
+
Explanation
|
| 41 |
+
|
| 42 |
+
For test case 1, the password sent is 27-25-4, then 27 can be either 'K' or 'X', similar case for 25 & 4. After applying the encryption 27 is converted into K, 25 is converted into G and 4 is converted into B.
|
| 43 |
+
|
| 44 |
+
## Contest Information
|
| 45 |
+
- **Contest ID**: 0
|
| 46 |
+
- **Problem Index**:
|
| 47 |
+
- **Points**: 0.0
|
| 48 |
+
- **Rating**: 0
|
| 49 |
+
- **Tags**: None
|
| 50 |
+
- **Time Limit**: None seconds
|
| 51 |
+
- **Memory Limit**: 0 bytes
|
| 52 |
+
|
| 53 |
+
## Task
|
| 54 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0179/instruction.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# the-competitive-class-3
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Jiro is a fun loving lad. His classmates are very competitive and are striving for getting a good rank, while he loves seeing them fight for it. The results are going to be out soon. He is anxious to know what is the rank of each of his classmates.
|
| 5 |
+
|
| 6 |
+
Rank of i'th student = 1+ (Number of students having strictly greater marks than him. Students with equal marks are ranked same.)
|
| 7 |
+
|
| 8 |
+
Can you help Jiro figure out rank of all his classmates?
|
| 9 |
+
|
| 10 |
+
Input:
|
| 11 |
+
The first line contains an integer N, denoting the number of students in the class.
|
| 12 |
+
The second line contains N space separated integers, Mi are the marks of the i'th student.
|
| 13 |
+
|
| 14 |
+
Ouput:
|
| 15 |
+
Print N space separated integers i.e., ranks of the students in a new line.
|
| 16 |
+
|
| 17 |
+
Constraints:
|
| 18 |
+
1 ≤ N ≤ 1000
|
| 19 |
+
1 ≤ Mi ≤ 1000
|
| 20 |
+
|
| 21 |
+
SAMPLE INPUT
|
| 22 |
+
4
|
| 23 |
+
62 96 82 55
|
| 24 |
+
|
| 25 |
+
SAMPLE OUTPUT
|
| 26 |
+
3 1 2 4
|
| 27 |
+
|
| 28 |
+
## Contest Information
|
| 29 |
+
- **Contest ID**: 0
|
| 30 |
+
- **Problem Index**:
|
| 31 |
+
- **Points**: 0.0
|
| 32 |
+
- **Rating**: 0
|
| 33 |
+
- **Tags**: None
|
| 34 |
+
- **Time Limit**: None seconds
|
| 35 |
+
- **Memory Limit**: 0 bytes
|
| 36 |
+
|
| 37 |
+
## Task
|
| 38 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0185/instruction.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# p03194 CADDi 2018 for Beginners - Product and GCD
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
There are N integers a_1, a_2, ..., a_N not less than 1. The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.
|
| 5 |
+
|
| 6 |
+
Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.
|
| 7 |
+
|
| 8 |
+
Constraints
|
| 9 |
+
|
| 10 |
+
* 1 \leq N \leq 10^{12}
|
| 11 |
+
* 1 \leq P \leq 10^{12}
|
| 12 |
+
|
| 13 |
+
Input
|
| 14 |
+
|
| 15 |
+
Input is given from Standard Input in the following format:
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
N P
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
Output
|
| 22 |
+
|
| 23 |
+
Print the answer.
|
| 24 |
+
|
| 25 |
+
Examples
|
| 26 |
+
|
| 27 |
+
Input
|
| 28 |
+
|
| 29 |
+
3 24
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
Output
|
| 33 |
+
|
| 34 |
+
2
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
Input
|
| 38 |
+
|
| 39 |
+
5 1
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
Output
|
| 43 |
+
|
| 44 |
+
1
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
Input
|
| 48 |
+
|
| 49 |
+
1 111
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
Output
|
| 53 |
+
|
| 54 |
+
111
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
Input
|
| 58 |
+
|
| 59 |
+
4 972439611840
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
Output
|
| 63 |
+
|
| 64 |
+
206
|
| 65 |
+
|
| 66 |
+
## Contest Information
|
| 67 |
+
- **Contest ID**: 0
|
| 68 |
+
- **Problem Index**:
|
| 69 |
+
- **Points**: 0.0
|
| 70 |
+
- **Rating**: 0
|
| 71 |
+
- **Tags**:
|
| 72 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 73 |
+
- **Memory Limit**: 1073741824 bytes
|
| 74 |
+
|
| 75 |
+
## Task
|
| 76 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0311/instruction.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# p02405 Print a Chessboard
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Draw a chessboard which has a height of H cm and a width of W cm. For example, the following figure shows a chessboard which has a height of 6 cm and a width of 10 cm.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
.#.#.#.#.
|
| 8 |
+
.#.#.#.#.#
|
| 9 |
+
.#.#.#.#.
|
| 10 |
+
.#.#.#.#.#
|
| 11 |
+
.#.#.#.#.
|
| 12 |
+
.#.#.#.#.#
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
Note that the top left corner should be drawn by '#'.
|
| 16 |
+
|
| 17 |
+
Constraints
|
| 18 |
+
|
| 19 |
+
* 1 ≤ H ≤ 300
|
| 20 |
+
* 1 ≤ W ≤ 300
|
| 21 |
+
|
| 22 |
+
Input
|
| 23 |
+
|
| 24 |
+
The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.
|
| 25 |
+
|
| 26 |
+
The input ends with two 0 (when both H and W are zero).
|
| 27 |
+
|
| 28 |
+
Output
|
| 29 |
+
|
| 30 |
+
For each dataset, print the chessboard made of '#' and '.'.
|
| 31 |
+
|
| 32 |
+
Print a blank line after each dataset.
|
| 33 |
+
|
| 34 |
+
Example
|
| 35 |
+
|
| 36 |
+
Input
|
| 37 |
+
|
| 38 |
+
3 4
|
| 39 |
+
5 6
|
| 40 |
+
3 3
|
| 41 |
+
2 2
|
| 42 |
+
1 1
|
| 43 |
+
0 0
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
Output
|
| 47 |
+
|
| 48 |
+
#.#.
|
| 49 |
+
.#.#
|
| 50 |
+
#.#.
|
| 51 |
+
|
| 52 |
+
#.#.#.
|
| 53 |
+
.#.#.#
|
| 54 |
+
#.#.#.
|
| 55 |
+
.#.#.#
|
| 56 |
+
#.#.#.
|
| 57 |
+
|
| 58 |
+
#.#
|
| 59 |
+
.#.
|
| 60 |
+
#.#
|
| 61 |
+
|
| 62 |
+
#.
|
| 63 |
+
.#
|
| 64 |
+
|
| 65 |
+
#
|
| 66 |
+
|
| 67 |
+
## Contest Information
|
| 68 |
+
- **Contest ID**: 0
|
| 69 |
+
- **Problem Index**:
|
| 70 |
+
- **Points**: 0.0
|
| 71 |
+
- **Rating**: 0
|
| 72 |
+
- **Tags**:
|
| 73 |
+
- **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
|
| 74 |
+
- **Memory Limit**: 134217728 bytes
|
| 75 |
+
|
| 76 |
+
## Task
|
| 77 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0316/instruction.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pcsc1
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
George is getting tired of the decimal number system. He intends to switch and use the septenary (base 7) number system for his future needs. Write a program to help George start his transformation into the septenary number system by taking in a list of decimal numbers and print out the corresponding septenary number.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
Input
|
| 8 |
+
A list of numbers in decimal format ending with -1.
|
| 9 |
+
|
| 10 |
+
Output
|
| 11 |
+
A list of numbers in septenary.
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
Example
|
| 15 |
+
Input:
|
| 16 |
+
1 2 88 42 99 -1
|
| 17 |
+
|
| 18 |
+
Output:
|
| 19 |
+
1 2 154 60 201
|
| 20 |
+
|
| 21 |
+
## Contest Information
|
| 22 |
+
- **Contest ID**: 0
|
| 23 |
+
- **Problem Index**:
|
| 24 |
+
- **Points**: 0.0
|
| 25 |
+
- **Rating**: 0
|
| 26 |
+
- **Tags**: None
|
| 27 |
+
- **Time Limit**: None seconds
|
| 28 |
+
- **Memory Limit**: 0 bytes
|
| 29 |
+
|
| 30 |
+
## Task
|
| 31 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0317/instruction.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# stadium
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
The bustling town of Siruseri has just one sports stadium. There
|
| 5 |
+
are a number of schools, colleges, sports associations, etc. that
|
| 6 |
+
use this stadium as the venue for their sports events.
|
| 7 |
+
Anyone interested in using the stadium has to apply to the Manager
|
| 8 |
+
of the stadium indicating both the starting date (a positive integer
|
| 9 |
+
S) and the length of the sporting event in days (a positive integer D)
|
| 10 |
+
they plan to organise. Since these requests could overlap it may not
|
| 11 |
+
be possible to satisfy everyone.
|
| 12 |
+
|
| 13 |
+
It is the job of the Manager to decide who gets to use the
|
| 14 |
+
stadium and who does not. The Manager, being a genial man, would like
|
| 15 |
+
to keep as many organisations happy as possible and hence would
|
| 16 |
+
like to allocate the stadium so that maximum number of events are held.
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
Suppose, for example, the Manager receives the following 4 requests:
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
Event No.
|
| 24 |
+
Start Date
|
| 25 |
+
Length
|
| 26 |
+
|
| 27 |
+
125
|
| 28 |
+
297
|
| 29 |
+
3156
|
| 30 |
+
493
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
He would allot the stadium to events 1, 4 and 3. Event 1 begins on day 2
|
| 34 |
+
and ends on day 6, event 4 begins on day 9 and ends on day 11 and event
|
| 35 |
+
3 begins on day 15 and ends on day 20. You can verify that it is not possible
|
| 36 |
+
to schedule all the 4 events (since events 2 and 3 overlap and only one of
|
| 37 |
+
them can get to use the stadium).
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
Your task is to help the manager find the best possible allotment (i.e.,
|
| 41 |
+
the maximum number of events that can use the stadium).
|
| 42 |
+
|
| 43 |
+
Input format
|
| 44 |
+
|
| 45 |
+
The first line of the input will contain a single integer N (N ≤ 100000)
|
| 46 |
+
indicating the number of events for which the Manager has received a request.
|
| 47 |
+
Lines 2,3,...,N+1 describe the requirements of the N events.
|
| 48 |
+
Line i+1 contains two integer Si and Di indicating the starting date
|
| 49 |
+
and the duration of event i. You may assume that 1 ≤ Si ≤ 1000000 and
|
| 50 |
+
1 ≤ Di ≤ 1000.
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
Output format
|
| 54 |
+
|
| 55 |
+
Your output must consist of a single line containing a single integer M,
|
| 56 |
+
indicating the maximum possible number of events that can use the stadium.
|
| 57 |
+
|
| 58 |
+
Example:
|
| 59 |
+
Sample input:
|
| 60 |
+
|
| 61 |
+
4
|
| 62 |
+
2 5
|
| 63 |
+
9 7
|
| 64 |
+
15 6
|
| 65 |
+
9 3
|
| 66 |
+
|
| 67 |
+
Sample output:
|
| 68 |
+
|
| 69 |
+
3
|
| 70 |
+
|
| 71 |
+
## Contest Information
|
| 72 |
+
- **Contest ID**: 0
|
| 73 |
+
- **Problem Index**:
|
| 74 |
+
- **Points**: 0.0
|
| 75 |
+
- **Rating**: 0
|
| 76 |
+
- **Tags**: None
|
| 77 |
+
- **Time Limit**: None seconds
|
| 78 |
+
- **Memory Limit**: 0 bytes
|
| 79 |
+
|
| 80 |
+
## Task
|
| 81 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0318/instruction.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1007_E. Mini Metro
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
In a simplified version of a "Mini Metro" game, there is only one subway line, and all the trains go in the same direction. There are n stations on the line, a_i people are waiting for the train at the i-th station at the beginning of the game. The game starts at the beginning of the 0-th hour. At the end of each hour (couple minutes before the end of the hour), b_i people instantly arrive to the i-th station. If at some moment, the number of people at the i-th station is larger than c_i, you lose.
|
| 5 |
+
|
| 6 |
+
A player has several trains which he can appoint to some hours. The capacity of each train is k passengers. In the middle of the appointed hour, the train goes from the 1-st to the n-th station, taking as many people at each station as it can accommodate. A train can not take people from the i-th station if there are people at the i-1-th station.
|
| 7 |
+
|
| 8 |
+
If multiple trains are appointed to the same hour, their capacities are being added up and they are moving together.
|
| 9 |
+
|
| 10 |
+
The player wants to stay in the game for t hours. Determine the minimum number of trains he will need for it.
|
| 11 |
+
|
| 12 |
+
Input
|
| 13 |
+
|
| 14 |
+
The first line contains three integers n, t, and k (1 ≤ n, t ≤ 200, 1 ≤ k ≤ 10^9) — the number of stations on the line, hours we want to survive, and capacity of each train respectively.
|
| 15 |
+
|
| 16 |
+
Each of the next n lines contains three integers a_i, b_i, and c_i (0 ≤ a_i, b_i ≤ c_i ≤ 10^9) — number of people at the i-th station in the beginning of the game, number of people arriving to i-th station in the end of each hour and maximum number of people at the i-th station allowed respectively.
|
| 17 |
+
|
| 18 |
+
Output
|
| 19 |
+
|
| 20 |
+
Output a single integer number — the answer to the problem.
|
| 21 |
+
|
| 22 |
+
Examples
|
| 23 |
+
|
| 24 |
+
Input
|
| 25 |
+
|
| 26 |
+
3 3 10
|
| 27 |
+
2 4 10
|
| 28 |
+
3 3 9
|
| 29 |
+
4 2 8
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
Output
|
| 33 |
+
|
| 34 |
+
2
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
Input
|
| 38 |
+
|
| 39 |
+
4 10 5
|
| 40 |
+
1 1 1
|
| 41 |
+
1 0 1
|
| 42 |
+
0 5 8
|
| 43 |
+
2 7 100
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
Output
|
| 47 |
+
|
| 48 |
+
12
|
| 49 |
+
|
| 50 |
+
Note
|
| 51 |
+
|
| 52 |
+
<image>
|
| 53 |
+
|
| 54 |
+
Let's look at the sample. There are three stations, on the first, there are initially 2 people, 3 people on the second, and 4 people on the third. Maximal capacities of the stations are 10, 9, and 8 respectively.
|
| 55 |
+
|
| 56 |
+
One of the winning strategies is to appoint two trains to the first and the third hours. Then on the first hour, the train takes all of the people from the stations, and at the end of the hour, 4 people arrive at the first station, 3 on the second, and 2 on the third.
|
| 57 |
+
|
| 58 |
+
In the second hour there are no trains appointed, and at the end of it, the same amount of people are arriving again.
|
| 59 |
+
|
| 60 |
+
In the third hour, the train first takes 8 people from the first station, and when it arrives at the second station, it takes only 2 people because it can accommodate no more than 10 people. Then it passes by the third station because it is already full. After it, people arrive at the stations once more, and the game ends.
|
| 61 |
+
|
| 62 |
+
As there was no such moment when the number of people at a station exceeded maximal capacity, we won using two trains.
|
| 63 |
+
|
| 64 |
+
## Contest Information
|
| 65 |
+
- **Contest ID**: 1007
|
| 66 |
+
- **Problem Index**: E
|
| 67 |
+
- **Points**: 2500.0
|
| 68 |
+
- **Rating**: 3400
|
| 69 |
+
- **Tags**: dp
|
| 70 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 71 |
+
- **Memory Limit**: 256000000 bytes
|
| 72 |
+
|
| 73 |
+
## Task
|
| 74 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0319/instruction.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1030_E. Vasya and Good Sequences
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Vasya has a sequence a consisting of n integers a_1, a_2, ..., a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (... 00000000110_2) into 3 (... 00000000011_2), 12 (... 000000001100_2), 1026 (... 10000000010_2) and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence.
|
| 5 |
+
|
| 6 |
+
Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with [bitwise exclusive or](https://en.wikipedia.org/wiki/Exclusive_or) of all elements equal to 0.
|
| 7 |
+
|
| 8 |
+
For the given sequence a_1, a_2, …, a_n Vasya'd like to calculate number of integer pairs (l, r) such that 1 ≤ l ≤ r ≤ n and sequence a_l, a_{l + 1}, ..., a_r is good.
|
| 9 |
+
|
| 10 |
+
Input
|
| 11 |
+
|
| 12 |
+
The first line contains a single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — length of the sequence.
|
| 13 |
+
|
| 14 |
+
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{18}) — the sequence a.
|
| 15 |
+
|
| 16 |
+
Output
|
| 17 |
+
|
| 18 |
+
Print one integer — the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and the sequence a_l, a_{l + 1}, ..., a_r is good.
|
| 19 |
+
|
| 20 |
+
Examples
|
| 21 |
+
|
| 22 |
+
Input
|
| 23 |
+
|
| 24 |
+
3
|
| 25 |
+
6 7 14
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
Output
|
| 29 |
+
|
| 30 |
+
2
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
Input
|
| 34 |
+
|
| 35 |
+
4
|
| 36 |
+
1 2 1 16
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
Output
|
| 40 |
+
|
| 41 |
+
4
|
| 42 |
+
|
| 43 |
+
Note
|
| 44 |
+
|
| 45 |
+
In the first example pairs (2, 3) and (1, 3) are valid. Pair (2, 3) is valid since a_2 = 7 → 11, a_3 = 14 → 11 and 11 ⊕ 11 = 0, where ⊕ — bitwise exclusive or. Pair (1, 3) is valid since a_1 = 6 → 3, a_2 = 7 → 13, a_3 = 14 → 14 and 3 ⊕ 13 ⊕ 14 = 0.
|
| 46 |
+
|
| 47 |
+
In the second example pairs (1, 2), (2, 3), (3, 4) and (1, 4) are valid.
|
| 48 |
+
|
| 49 |
+
## Contest Information
|
| 50 |
+
- **Contest ID**: 1030
|
| 51 |
+
- **Problem Index**: E
|
| 52 |
+
- **Points**: 2000.0
|
| 53 |
+
- **Rating**: 2000
|
| 54 |
+
- **Tags**: bitmasks, dp
|
| 55 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 56 |
+
- **Memory Limit**: 256000000 bytes
|
| 57 |
+
|
| 58 |
+
## Task
|
| 59 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0321/instruction.md
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1075_D. Intersecting Subtrees
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
You are playing a strange game with Li Chen. You have a tree with n nodes drawn on a piece of paper. All nodes are unlabeled and distinguishable. Each of you independently labeled the vertices from 1 to n. Neither of you know the other's labelling of the tree.
|
| 5 |
+
|
| 6 |
+
You and Li Chen each chose a subtree (i.e., a connected subgraph) in that tree. Your subtree consists of the vertices labeled x_1, x_2, …, x_{k_1} in your labeling, Li Chen's subtree consists of the vertices labeled y_1, y_2, …, y_{k_2} in his labeling. The values of x_1, x_2, …, x_{k_1} and y_1, y_2, …, y_{k_2} are known to both of you.
|
| 7 |
+
|
| 8 |
+
<image> The picture shows two labelings of a possible tree: yours on the left and Li Chen's on the right. The selected trees are highlighted. There are two common nodes.
|
| 9 |
+
|
| 10 |
+
You want to determine whether your subtrees have at least one common vertex. Luckily, your friend Andrew knows both labelings of the tree. You can ask Andrew at most 5 questions, each of which is in one of the following two forms:
|
| 11 |
+
|
| 12 |
+
* A x: Andrew will look at vertex x in your labeling and tell you the number of this vertex in Li Chen's labeling.
|
| 13 |
+
* B y: Andrew will look at vertex y in Li Chen's labeling and tell you the number of this vertex in your labeling.
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
Determine whether the two subtrees have at least one common vertex after asking some questions. If there is at least one common vertex, determine one of your labels for any of the common vertices.
|
| 18 |
+
|
| 19 |
+
Interaction
|
| 20 |
+
|
| 21 |
+
Each test consists of several test cases.
|
| 22 |
+
|
| 23 |
+
The first line of input contains a single integer t (1 ≤ t ≤ 100) — the number of test cases.
|
| 24 |
+
|
| 25 |
+
For each testcase, your program should interact in the following format.
|
| 26 |
+
|
| 27 |
+
The first line contains a single integer n (1 ≤ n ≤ 1 000) — the number of nodes in the tree.
|
| 28 |
+
|
| 29 |
+
Each of the next n-1 lines contains two integers a_i and b_i (1≤ a_i, b_i≤ n) — the edges of the tree, indicating an edge between node a_i and b_i according to your labeling of the nodes.
|
| 30 |
+
|
| 31 |
+
The next line contains a single integer k_1 (1 ≤ k_1 ≤ n) — the number of nodes in your subtree.
|
| 32 |
+
|
| 33 |
+
The next line contains k_1 distinct integers x_1,x_2,…,x_{k_1} (1 ≤ x_i ≤ n) — the indices of the nodes in your subtree, according to your labeling. It is guaranteed that these vertices form a subtree.
|
| 34 |
+
|
| 35 |
+
The next line contains a single integer k_2 (1 ≤ k_2 ≤ n) — the number of nodes in Li Chen's subtree.
|
| 36 |
+
|
| 37 |
+
The next line contains k_2 distinct integers y_1, y_2, …, y_{k_2} (1 ≤ y_i ≤ n) — the indices (according to Li Chen's labeling) of the nodes in Li Chen's subtree. It is guaranteed that these vertices form a subtree according to Li Chen's labelling of the tree's nodes.
|
| 38 |
+
|
| 39 |
+
Test cases will be provided one by one, so you must complete interacting with the previous test (i.e. by printing out a common node or -1 if there is not such node) to start receiving the next one.
|
| 40 |
+
|
| 41 |
+
You can ask the Andrew two different types of questions.
|
| 42 |
+
|
| 43 |
+
* You can print "A x" (1 ≤ x ≤ n). Andrew will look at vertex x in your labeling and respond to you with the number of this vertex in Li Chen's labeling.
|
| 44 |
+
* You can print "B y" (1 ≤ y ≤ n). Andrew will look at vertex y in Li Chen's labeling and respond to you with the number of this vertex in your labeling.
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
You may only ask at most 5 questions per tree.
|
| 49 |
+
|
| 50 |
+
When you are ready to answer, print "C s", where s is your label of a vertex that is common to both subtrees, or -1, if no such vertex exists. Printing the answer does not count as a question. Remember to flush your answer to start receiving the next test case.
|
| 51 |
+
|
| 52 |
+
After printing a question do not forget to print end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
|
| 53 |
+
|
| 54 |
+
* fflush(stdout) or cout.flush() in C++;
|
| 55 |
+
* System.out.flush() in Java;
|
| 56 |
+
* flush(output) in Pascal;
|
| 57 |
+
* stdout.flush() in Python;
|
| 58 |
+
* see documentation for other languages.
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
If the judge responds with -1, it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive Wrong Answer; it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
|
| 63 |
+
|
| 64 |
+
Hack Format
|
| 65 |
+
|
| 66 |
+
To hack, use the following format. Note that you can only hack with one test case.
|
| 67 |
+
|
| 68 |
+
The first line should contain a single integer t (t=1).
|
| 69 |
+
|
| 70 |
+
The second line should contain a single integer n (1 ≤ n ≤ 1 000).
|
| 71 |
+
|
| 72 |
+
The third line should contain n integers p_1, p_2, …, p_n (1≤ p_i≤ n) — a permutation of 1 to n. This encodes the labels that Li Chen chose for his tree. In particular, Li Chen chose label p_i for the node you labeled i.
|
| 73 |
+
|
| 74 |
+
Each of the next n-1 lines should contain two integers a_i and b_i (1≤ a_i, b_i≤ n). These edges should form a tree.
|
| 75 |
+
|
| 76 |
+
The next line should contain a single integer k_1 (1 ≤ k_1 ≤ n).
|
| 77 |
+
|
| 78 |
+
The next line should contain k_1 distinct integers x_1,x_2,…,x_{k_1} (1 ≤ x_i ≤ n). These vertices should form a subtree.
|
| 79 |
+
|
| 80 |
+
The next line should contain a single integer k_2 (1 ≤ k_2 ≤ n).
|
| 81 |
+
|
| 82 |
+
The next line should contain k_2 distinct integers y_1, y_2, …, y_{k_2} (1 ≤ y_i ≤ n). These vertices should form a subtree in Li Chen's tree according to the permutation above.
|
| 83 |
+
|
| 84 |
+
Examples
|
| 85 |
+
|
| 86 |
+
Input
|
| 87 |
+
|
| 88 |
+
1
|
| 89 |
+
3
|
| 90 |
+
1 2
|
| 91 |
+
2 3
|
| 92 |
+
1
|
| 93 |
+
1
|
| 94 |
+
1
|
| 95 |
+
2
|
| 96 |
+
2
|
| 97 |
+
1
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
Output
|
| 101 |
+
|
| 102 |
+
A 1
|
| 103 |
+
B 2
|
| 104 |
+
C 1
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
Input
|
| 108 |
+
|
| 109 |
+
2
|
| 110 |
+
6
|
| 111 |
+
1 2
|
| 112 |
+
1 3
|
| 113 |
+
1 4
|
| 114 |
+
4 5
|
| 115 |
+
4 6
|
| 116 |
+
4
|
| 117 |
+
1 3 4 5
|
| 118 |
+
3
|
| 119 |
+
3 5 2
|
| 120 |
+
3
|
| 121 |
+
6
|
| 122 |
+
1 2
|
| 123 |
+
1 3
|
| 124 |
+
1 4
|
| 125 |
+
4 5
|
| 126 |
+
4 6
|
| 127 |
+
3
|
| 128 |
+
1 2 3
|
| 129 |
+
3
|
| 130 |
+
4 1 6
|
| 131 |
+
5
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
Output
|
| 135 |
+
|
| 136 |
+
B 2
|
| 137 |
+
C 1
|
| 138 |
+
A 1
|
| 139 |
+
C -1
|
| 140 |
+
|
| 141 |
+
Note
|
| 142 |
+
|
| 143 |
+
For the first sample, Li Chen's hidden permutation is [2, 3, 1], and for the second, his hidden permutation is [5, 3, 2, 4, 1, 6] for both cases.
|
| 144 |
+
|
| 145 |
+
In the first sample, there is a tree with three nodes in a line. On the top, is how you labeled the tree and the subtree you chose, and the bottom is how Li Chen labeled the tree and the subtree he chose:
|
| 146 |
+
|
| 147 |
+
<image>
|
| 148 |
+
|
| 149 |
+
In the first question, you ask Andrew to look at node 1 in your labelling and tell you the label of it in Li Chen's labelling. Andrew responds with 2. At this point, you know that both of your subtrees contain the same node (i.e. node 1 according to your labeling), so you can output "C 1" and finish. However, you can also ask Andrew to look at node 2 in Li Chen's labelling and tell you the label of it in your labelling. Andrew responds with 1 (this step was given with the only reason — to show you how to ask questions).
|
| 150 |
+
|
| 151 |
+
For the second sample, there are two test cases. The first looks is the one from the statement:
|
| 152 |
+
|
| 153 |
+
<image>
|
| 154 |
+
|
| 155 |
+
We first ask "B 2", and Andrew will tell us 3. In this case, we know 3 is a common vertex, and moreover, any subtree with size 3 that contains node 3 must contain node 1 as well, so we can output either "C 1" or "C 3" as our answer.
|
| 156 |
+
|
| 157 |
+
In the second case in the second sample, the situation looks as follows:
|
| 158 |
+
|
| 159 |
+
<image>
|
| 160 |
+
|
| 161 |
+
In this case, you know that the only subtree of size 3 that doesn't contain node 1 is subtree 4,5,6. You ask Andrew for the label of node 1 in Li Chen's labelling and Andrew says 5. In this case, you know that Li Chen's subtree doesn't contain node 1, so his subtree must be consist of the nodes 4,5,6 (in your labelling), thus the two subtrees have no common nodes.
|
| 162 |
+
|
| 163 |
+
## Contest Information
|
| 164 |
+
- **Contest ID**: 1075
|
| 165 |
+
- **Problem Index**: D
|
| 166 |
+
- **Points**: 1250.0
|
| 167 |
+
- **Rating**: 1900
|
| 168 |
+
- **Tags**: dfs and similar, interactive, trees
|
| 169 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 170 |
+
- **Memory Limit**: 256000000 bytes
|
| 171 |
+
|
| 172 |
+
## Task
|
| 173 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0328/instruction.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1219_H. Function Composition
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
We are definitely not going to bother you with another generic story when Alice finds about an array or when Alice and Bob play some stupid game. This time you'll get a simple, plain text.
|
| 5 |
+
|
| 6 |
+
First, let us define several things. We define function F on the array A such that F(i, 1) = A[i] and F(i, m) = A[F(i, m - 1)] for m > 1. In other words, value F(i, m) represents composition A[...A[i]] applied m times.
|
| 7 |
+
|
| 8 |
+
You are given an array of length N with non-negative integers. You are expected to give an answer on Q queries. Each query consists of two numbers – m and y. For each query determine how many x exist such that F(x,m) = y.
|
| 9 |
+
|
| 10 |
+
Input
|
| 11 |
+
|
| 12 |
+
The first line contains one integer N (1 ≤ N ≤ 2 ⋅ 10^5) – the size of the array A. The next line contains N non-negative integers – the array A itself (1 ≤ A_i ≤ N). The next line contains one integer Q (1 ≤ Q ≤ 10^5) – the number of queries. Each of the next Q lines contain two integers m and y (1 ≤ m ≤ 10^{18}, 1≤ y ≤ N).
|
| 13 |
+
|
| 14 |
+
Output
|
| 15 |
+
|
| 16 |
+
Output exactly Q lines with a single integer in each that represent the solution. Output the solutions in the order the queries were asked in.
|
| 17 |
+
|
| 18 |
+
Example
|
| 19 |
+
|
| 20 |
+
Input
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
10
|
| 24 |
+
2 3 1 5 6 4 2 10 7 7
|
| 25 |
+
5
|
| 26 |
+
10 1
|
| 27 |
+
5 7
|
| 28 |
+
10 6
|
| 29 |
+
1 1
|
| 30 |
+
10 8
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
Output
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
3
|
| 37 |
+
0
|
| 38 |
+
1
|
| 39 |
+
1
|
| 40 |
+
0
|
| 41 |
+
|
| 42 |
+
Note
|
| 43 |
+
|
| 44 |
+
For the first query we can notice that F(3, 10) = 1,\ F(9, 10) = 1 and F(10, 10) = 1.
|
| 45 |
+
|
| 46 |
+
For the second query no x satisfies condition F(x, 5) = 7.
|
| 47 |
+
|
| 48 |
+
For the third query F(5, 10) = 6 holds.
|
| 49 |
+
|
| 50 |
+
For the fourth query F(3, 1) = 1.
|
| 51 |
+
|
| 52 |
+
For the fifth query no x satisfies condition F(x, 10) = 8.
|
| 53 |
+
|
| 54 |
+
## Contest Information
|
| 55 |
+
- **Contest ID**: 1219
|
| 56 |
+
- **Problem Index**: H
|
| 57 |
+
- **Points**: 0.0
|
| 58 |
+
- **Rating**: 2900
|
| 59 |
+
- **Tags**: dfs and similar
|
| 60 |
+
- **Time Limit**: {'seconds': 0, 'nanos': 500000000} seconds
|
| 61 |
+
- **Memory Limit**: 256000000 bytes
|
| 62 |
+
|
| 63 |
+
## Task
|
| 64 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0329/instruction.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1244_C. The Football Season
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
The football season has just ended in Berland. According to the rules of Berland football, each match is played between two teams. The result of each match is either a draw, or a victory of one of the playing teams. If a team wins the match, it gets w points, and the opposing team gets 0 points. If the game results in a draw, both teams get d points.
|
| 5 |
+
|
| 6 |
+
The manager of the Berland capital team wants to summarize the results of the season, but, unfortunately, all information about the results of each match is lost. The manager only knows that the team has played n games and got p points for them.
|
| 7 |
+
|
| 8 |
+
You have to determine three integers x, y and z — the number of wins, draws and loses of the team. If there are multiple answers, print any of them. If there is no suitable triple (x, y, z), report about it.
|
| 9 |
+
|
| 10 |
+
Input
|
| 11 |
+
|
| 12 |
+
The first line contains four integers n, p, w and d (1 ≤ n ≤ 10^{12}, 0 ≤ p ≤ 10^{17}, 1 ≤ d < w ≤ 10^{5}) — the number of games, the number of points the team got, the number of points awarded for winning a match, and the number of points awarded for a draw, respectively. Note that w > d, so the number of points awarded for winning is strictly greater than the number of points awarded for draw.
|
| 13 |
+
|
| 14 |
+
Output
|
| 15 |
+
|
| 16 |
+
If there is no answer, print -1.
|
| 17 |
+
|
| 18 |
+
Otherwise print three non-negative integers x, y and z — the number of wins, draws and losses of the team. If there are multiple possible triples (x, y, z), print any of them. The numbers should meet the following conditions:
|
| 19 |
+
|
| 20 |
+
* x ⋅ w + y ⋅ d = p,
|
| 21 |
+
* x + y + z = n.
|
| 22 |
+
|
| 23 |
+
Examples
|
| 24 |
+
|
| 25 |
+
Input
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
30 60 3 1
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
Output
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
17 9 4
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
Input
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
10 51 5 4
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
Output
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
-1
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
Input
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
20 0 15 5
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
Output
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
0 0 20
|
| 59 |
+
|
| 60 |
+
Note
|
| 61 |
+
|
| 62 |
+
One of the possible answers in the first example — 17 wins, 9 draws and 4 losses. Then the team got 17 ⋅ 3 + 9 ⋅ 1 = 60 points in 17 + 9 + 4 = 30 games.
|
| 63 |
+
|
| 64 |
+
In the second example the maximum possible score is 10 ⋅ 5 = 50. Since p = 51, there is no answer.
|
| 65 |
+
|
| 66 |
+
In the third example the team got 0 points, so all 20 games were lost.
|
| 67 |
+
|
| 68 |
+
## Contest Information
|
| 69 |
+
- **Contest ID**: 1244
|
| 70 |
+
- **Problem Index**: C
|
| 71 |
+
- **Points**: 1500.0
|
| 72 |
+
- **Rating**: 2000
|
| 73 |
+
- **Tags**: brute force, math, number theory
|
| 74 |
+
- **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
|
| 75 |
+
- **Memory Limit**: 256000000 bytes
|
| 76 |
+
|
| 77 |
+
## Task
|
| 78 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0336/instruction.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1391_C. Cyclic Permutations
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
|
| 5 |
+
|
| 6 |
+
Consider a permutation p of length n, we build a graph of size n using it as follows:
|
| 7 |
+
|
| 8 |
+
* For every 1 ≤ i ≤ n, find the largest j such that 1 ≤ j < i and p_j > p_i, and add an undirected edge between node i and node j
|
| 9 |
+
* For every 1 ≤ i ≤ n, find the smallest j such that i < j ≤ n and p_j > p_i, and add an undirected edge between node i and node j
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
|
| 14 |
+
|
| 15 |
+
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
|
| 16 |
+
|
| 17 |
+
A permutation p is cyclic if the graph built using p has at least one simple cycle.
|
| 18 |
+
|
| 19 |
+
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
|
| 20 |
+
|
| 21 |
+
Please refer to the Notes section for the formal definition of a simple cycle
|
| 22 |
+
|
| 23 |
+
Input
|
| 24 |
+
|
| 25 |
+
The first and only line contains a single integer n (3 ≤ n ≤ 10^6).
|
| 26 |
+
|
| 27 |
+
Output
|
| 28 |
+
|
| 29 |
+
Output a single integer 0 ≤ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
|
| 30 |
+
|
| 31 |
+
Examples
|
| 32 |
+
|
| 33 |
+
Input
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
4
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
Output
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
16
|
| 43 |
+
|
| 44 |
+
Input
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
583291
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
Output
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
135712853
|
| 54 |
+
|
| 55 |
+
Note
|
| 56 |
+
|
| 57 |
+
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 → 3 → 2 → 1 → 4.
|
| 58 |
+
|
| 59 |
+
Nodes v_1, v_2, …, v_k form a simple cycle if the following conditions hold:
|
| 60 |
+
|
| 61 |
+
* k ≥ 3.
|
| 62 |
+
* v_i ≠ v_j for any pair of indices i and j. (1 ≤ i < j ≤ k)
|
| 63 |
+
* v_i and v_{i+1} share an edge for all i (1 ≤ i < k), and v_1 and v_k share an edge.
|
| 64 |
+
|
| 65 |
+
## Contest Information
|
| 66 |
+
- **Contest ID**: 1391
|
| 67 |
+
- **Problem Index**: C
|
| 68 |
+
- **Points**: 1250.0
|
| 69 |
+
- **Rating**: 1500
|
| 70 |
+
- **Tags**: combinatorics, dp, graphs, math
|
| 71 |
+
- **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
|
| 72 |
+
- **Memory Limit**: 256000000 bytes
|
| 73 |
+
|
| 74 |
+
## Task
|
| 75 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0343/instruction.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 161_B. Discounts
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheaper). If there are several items with the same minimum price, the discount is available for only one of them!
|
| 5 |
+
|
| 6 |
+
Polycarpus has k carts, and he wants to buy up all stools and pencils from the supermarket. Help him distribute the stools and the pencils among the shopping carts, so that the items' total price (including the discounts) is the least possible.
|
| 7 |
+
|
| 8 |
+
Polycarpus must use all k carts to purchase the items, no shopping cart can remain empty. Each shopping cart can contain an arbitrary number of stools and/or pencils.
|
| 9 |
+
|
| 10 |
+
Input
|
| 11 |
+
|
| 12 |
+
The first input line contains two integers n and k (1 ≤ k ≤ n ≤ 103) — the number of items in the supermarket and the number of carts, correspondingly. Next n lines describe the items as "ci ti" (without the quotes), where ci (1 ≤ ci ≤ 109) is an integer denoting the price of the i-th item, ti (1 ≤ ti ≤ 2) is an integer representing the type of item i (1 for a stool and 2 for a pencil). The numbers in the lines are separated by single spaces.
|
| 13 |
+
|
| 14 |
+
Output
|
| 15 |
+
|
| 16 |
+
In the first line print a single real number with exactly one decimal place — the minimum total price of the items, including the discounts.
|
| 17 |
+
|
| 18 |
+
In the following k lines print the descriptions of the items in the carts. In the i-th line print the description of the i-th cart as "t b1 b2 ... bt" (without the quotes), where t is the number of items in the i-th cart, and the sequence b1, b2, ..., bt (1 ≤ bj ≤ n) gives the indices of items to put in this cart in the optimal distribution. All indices of items in all carts should be pairwise different, each item must belong to exactly one cart. You can print the items in carts and the carts themselves in any order. The items are numbered from 1 to n in the order in which they are specified in the input.
|
| 19 |
+
|
| 20 |
+
If there are multiple optimal distributions, you are allowed to print any of them.
|
| 21 |
+
|
| 22 |
+
Examples
|
| 23 |
+
|
| 24 |
+
Input
|
| 25 |
+
|
| 26 |
+
3 2
|
| 27 |
+
2 1
|
| 28 |
+
3 2
|
| 29 |
+
3 1
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
Output
|
| 33 |
+
|
| 34 |
+
5.5
|
| 35 |
+
2 1 2
|
| 36 |
+
1 3
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
Input
|
| 40 |
+
|
| 41 |
+
4 3
|
| 42 |
+
4 1
|
| 43 |
+
1 2
|
| 44 |
+
2 2
|
| 45 |
+
3 2
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
Output
|
| 49 |
+
|
| 50 |
+
8.0
|
| 51 |
+
1 1
|
| 52 |
+
2 4 2
|
| 53 |
+
1 3
|
| 54 |
+
|
| 55 |
+
Note
|
| 56 |
+
|
| 57 |
+
In the first sample case the first cart should contain the 1st and 2nd items, and the second cart should contain the 3rd item. This way each cart has a stool and each cart has a 50% discount for the cheapest item. The total price of all items will be: 2·0.5 + (3 + 3·0.5) = 1 + 4.5 = 5.5.
|
| 58 |
+
|
| 59 |
+
## Contest Information
|
| 60 |
+
- **Contest ID**: 161
|
| 61 |
+
- **Problem Index**: B
|
| 62 |
+
- **Points**: 1000.0
|
| 63 |
+
- **Rating**: 1700
|
| 64 |
+
- **Tags**: constructive algorithms, greedy, sortings
|
| 65 |
+
- **Time Limit**: {'seconds': 3, 'nanos': 0} seconds
|
| 66 |
+
- **Memory Limit**: 256000000 bytes
|
| 67 |
+
|
| 68 |
+
## Task
|
| 69 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0344/instruction.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 180_D. Name
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Everything got unclear to us in a far away constellation Tau Ceti. Specifically, the Taucetians choose names to their children in a very peculiar manner.
|
| 5 |
+
|
| 6 |
+
Two young parents abac and bbad think what name to give to their first-born child. They decided that the name will be the permutation of letters of string s. To keep up with the neighbours, they decided to call the baby so that the name was lexicographically strictly larger than the neighbour's son's name t.
|
| 7 |
+
|
| 8 |
+
On the other hand, they suspect that a name tax will be introduced shortly. According to it, the Taucetians with lexicographically larger names will pay larger taxes. That's the reason abac and bbad want to call the newborn so that the name was lexicographically strictly larger than name t and lexicographically minimum at that.
|
| 9 |
+
|
| 10 |
+
The lexicographical order of strings is the order we are all used to, the "dictionary" order. Such comparison is used in all modern programming languages to compare strings. Formally, a string p of length n is lexicographically less than string q of length m, if one of the two statements is correct:
|
| 11 |
+
|
| 12 |
+
* n < m, and p is the beginning (prefix) of string q (for example, "aba" is less than string "abaa"),
|
| 13 |
+
* p1 = q1, p2 = q2, ..., pk - 1 = qk - 1, pk < qk for some k (1 ≤ k ≤ min(n, m)), here characters in strings are numbered starting from 1.
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
Write a program that, given string s and the heighbours' child's name t determines the string that is the result of permutation of letters in s. The string should be lexicographically strictly more than t and also, lexicographically minimum.
|
| 18 |
+
|
| 19 |
+
Input
|
| 20 |
+
|
| 21 |
+
The first line contains a non-empty string s (1 ≤ |s| ≤ 5000), where |s| is its length. The second line contains a non-empty string t (1 ≤ |t| ≤ 5000), where |t| is its length. Both strings consist of lowercase Latin letters.
|
| 22 |
+
|
| 23 |
+
Output
|
| 24 |
+
|
| 25 |
+
Print the sought name or -1 if it doesn't exist.
|
| 26 |
+
|
| 27 |
+
Examples
|
| 28 |
+
|
| 29 |
+
Input
|
| 30 |
+
|
| 31 |
+
aad
|
| 32 |
+
aac
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
Output
|
| 36 |
+
|
| 37 |
+
aad
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
Input
|
| 41 |
+
|
| 42 |
+
abad
|
| 43 |
+
bob
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
Output
|
| 47 |
+
|
| 48 |
+
daab
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
Input
|
| 52 |
+
|
| 53 |
+
abc
|
| 54 |
+
defg
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
Output
|
| 58 |
+
|
| 59 |
+
-1
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
Input
|
| 63 |
+
|
| 64 |
+
czaaab
|
| 65 |
+
abcdef
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
Output
|
| 69 |
+
|
| 70 |
+
abczaa
|
| 71 |
+
|
| 72 |
+
Note
|
| 73 |
+
|
| 74 |
+
In the first sample the given string s is the sought one, consequently, we do not need to change the letter order there.
|
| 75 |
+
|
| 76 |
+
## Contest Information
|
| 77 |
+
- **Contest ID**: 180
|
| 78 |
+
- **Problem Index**: D
|
| 79 |
+
- **Points**: 0.0
|
| 80 |
+
- **Rating**: 1900
|
| 81 |
+
- **Tags**: greedy, strings
|
| 82 |
+
- **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
|
| 83 |
+
- **Memory Limit**: 256000000 bytes
|
| 84 |
+
|
| 85 |
+
## Task
|
| 86 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0362/instruction.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 609_B. The Best Gift
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres.
|
| 5 |
+
|
| 6 |
+
In the bookshop, Jack decides to buy two books of different genres.
|
| 7 |
+
|
| 8 |
+
Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book.
|
| 9 |
+
|
| 10 |
+
The books are given by indices of their genres. The genres are numbered from 1 to m.
|
| 11 |
+
|
| 12 |
+
Input
|
| 13 |
+
|
| 14 |
+
The first line contains two positive integers n and m (2 ≤ n ≤ 2·105, 2 ≤ m ≤ 10) — the number of books in the bookstore and the number of genres.
|
| 15 |
+
|
| 16 |
+
The second line contains a sequence a1, a2, ..., an, where ai (1 ≤ ai ≤ m) equals the genre of the i-th book.
|
| 17 |
+
|
| 18 |
+
It is guaranteed that for each genre there is at least one book of that genre.
|
| 19 |
+
|
| 20 |
+
Output
|
| 21 |
+
|
| 22 |
+
Print the only integer — the number of ways in which Jack can choose books.
|
| 23 |
+
|
| 24 |
+
It is guaranteed that the answer doesn't exceed the value 2·109.
|
| 25 |
+
|
| 26 |
+
Examples
|
| 27 |
+
|
| 28 |
+
Input
|
| 29 |
+
|
| 30 |
+
4 3
|
| 31 |
+
2 1 3 1
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
Output
|
| 35 |
+
|
| 36 |
+
5
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
Input
|
| 40 |
+
|
| 41 |
+
7 4
|
| 42 |
+
4 2 3 1 2 4 3
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
Output
|
| 46 |
+
|
| 47 |
+
18
|
| 48 |
+
|
| 49 |
+
Note
|
| 50 |
+
|
| 51 |
+
The answer to the first test sample equals 5 as Sasha can choose:
|
| 52 |
+
|
| 53 |
+
1. the first and second books,
|
| 54 |
+
2. the first and third books,
|
| 55 |
+
3. the first and fourth books,
|
| 56 |
+
4. the second and third books,
|
| 57 |
+
5. the third and fourth books.
|
| 58 |
+
|
| 59 |
+
## Contest Information
|
| 60 |
+
- **Contest ID**: 609
|
| 61 |
+
- **Problem Index**: B
|
| 62 |
+
- **Points**: 0.0
|
| 63 |
+
- **Rating**: 1100
|
| 64 |
+
- **Tags**: constructive algorithms, implementation
|
| 65 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 66 |
+
- **Memory Limit**: 256000000 bytes
|
| 67 |
+
|
| 68 |
+
## Task
|
| 69 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0374/instruction.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 886_D. Restoration of string
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring.
|
| 5 |
+
|
| 6 |
+
You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes).
|
| 7 |
+
|
| 8 |
+
A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string.
|
| 9 |
+
|
| 10 |
+
The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap.
|
| 11 |
+
|
| 12 |
+
String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ.
|
| 13 |
+
|
| 14 |
+
Input
|
| 15 |
+
|
| 16 |
+
The first line contains integer n (1 ≤ n ≤ 105) — the number of strings in the set.
|
| 17 |
+
|
| 18 |
+
Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct.
|
| 19 |
+
|
| 20 |
+
The total length of the strings doesn't exceed 105.
|
| 21 |
+
|
| 22 |
+
Output
|
| 23 |
+
|
| 24 |
+
Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings.
|
| 25 |
+
|
| 26 |
+
Examples
|
| 27 |
+
|
| 28 |
+
Input
|
| 29 |
+
|
| 30 |
+
4
|
| 31 |
+
mail
|
| 32 |
+
ai
|
| 33 |
+
lru
|
| 34 |
+
cf
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
Output
|
| 38 |
+
|
| 39 |
+
cfmailru
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
Input
|
| 43 |
+
|
| 44 |
+
3
|
| 45 |
+
kek
|
| 46 |
+
preceq
|
| 47 |
+
cheburek
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
Output
|
| 51 |
+
|
| 52 |
+
NO
|
| 53 |
+
|
| 54 |
+
Note
|
| 55 |
+
|
| 56 |
+
One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum.
|
| 57 |
+
|
| 58 |
+
## Contest Information
|
| 59 |
+
- **Contest ID**: 886
|
| 60 |
+
- **Problem Index**: D
|
| 61 |
+
- **Points**: 2000.0
|
| 62 |
+
- **Rating**: 2000
|
| 63 |
+
- **Tags**: constructive algorithms, graphs, implementation
|
| 64 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 65 |
+
- **Memory Limit**: 256000000 bytes
|
| 66 |
+
|
| 67 |
+
## Task
|
| 68 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0380/instruction.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# bobs-journey-5
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Bob is travelling from one city to another. In his way, he sees many other cities pass by. What he does instead of learning the full names of the cities, he learns just the first character of the cities. For example, if he passes by "bhopal", he will just remember the 'b'.
|
| 5 |
+
|
| 6 |
+
Given the list of N cities that come in his way, print "YES" or "NO" depending on if he is able to remember all the cities distinctly or not.
|
| 7 |
+
|
| 8 |
+
Note: City name consists of small English alphabets only.
|
| 9 |
+
|
| 10 |
+
Input and Output:
|
| 11 |
+
First line contains T, the number of testcases. Each testcase consists of N, the number of cities. Next N lines contain the names of the cities.
|
| 12 |
+
For each testcase, print "YES" or "NO" (quotes for clarity).
|
| 13 |
+
|
| 14 |
+
Constraints:
|
| 15 |
+
1 ≤ T ≤ 100
|
| 16 |
+
1 ≤ N ≤ 1000
|
| 17 |
+
1 ≤ Length of each city name ≤ 10
|
| 18 |
+
|
| 19 |
+
SAMPLE INPUT
|
| 20 |
+
2
|
| 21 |
+
2
|
| 22 |
+
bhopal
|
| 23 |
+
delhi
|
| 24 |
+
3
|
| 25 |
+
bhopal
|
| 26 |
+
delhi
|
| 27 |
+
dehradun
|
| 28 |
+
|
| 29 |
+
SAMPLE OUTPUT
|
| 30 |
+
YES
|
| 31 |
+
NO
|
| 32 |
+
|
| 33 |
+
## Contest Information
|
| 34 |
+
- **Contest ID**: 0
|
| 35 |
+
- **Problem Index**:
|
| 36 |
+
- **Points**: 0.0
|
| 37 |
+
- **Rating**: 0
|
| 38 |
+
- **Tags**: None
|
| 39 |
+
- **Time Limit**: None seconds
|
| 40 |
+
- **Memory Limit**: 0 bytes
|
| 41 |
+
|
| 42 |
+
## Task
|
| 43 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0386/instruction.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# reversemerge-shuffle-reverse
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Given a string, S, we define some operations on the string as follows:
|
| 5 |
+
|
| 6 |
+
a. reverse(S) denotes the string obtained by reversing string S. E.g.: reverse("abc") = "cba"
|
| 7 |
+
|
| 8 |
+
b. shuffle(S) denotes any string that's a permutation of string S. E.g.: shuffle("god") ∈ ['god', 'gdo', 'ogd', 'odg', 'dgo', 'dog']
|
| 9 |
+
|
| 10 |
+
c. merge(S1,S2) denotes any string that's obtained by interspersing the two strings S1 & S2, maintaining the order of characters in both.
|
| 11 |
+
E.g.: S1 = "abc" & S2 = "def", one possible result of merge(S1,S2) could be "abcdef", another could be "abdecf", another could be "adbecf" and so on.
|
| 12 |
+
|
| 13 |
+
Given a string S such that S∈ merge(reverse(A), shuffle(A)), for some string A, can you find the lexicographically smallest A?
|
| 14 |
+
|
| 15 |
+
Input Format
|
| 16 |
+
|
| 17 |
+
A single line containing the string S.
|
| 18 |
+
|
| 19 |
+
Constraints:
|
| 20 |
+
|
| 21 |
+
S contains only lower-case English letters.
|
| 22 |
+
The length of string S is less than or equal to 10000.
|
| 23 |
+
|
| 24 |
+
Output Format
|
| 25 |
+
|
| 26 |
+
A string which is the lexicographically smallest valid A.
|
| 27 |
+
|
| 28 |
+
SAMPLE INPUT
|
| 29 |
+
eggegg
|
| 30 |
+
|
| 31 |
+
SAMPLE OUTPUT
|
| 32 |
+
egg
|
| 33 |
+
|
| 34 |
+
Explanation
|
| 35 |
+
|
| 36 |
+
reverse("egg") = "gge"
|
| 37 |
+
shuffle("egg") can be "egg"
|
| 38 |
+
"eggegg" belongs to merge of ("gge", "egg")
|
| 39 |
+
|
| 40 |
+
The split is: e(gge)gg.
|
| 41 |
+
|
| 42 |
+
egg is the lexicographically smallest.
|
| 43 |
+
|
| 44 |
+
## Contest Information
|
| 45 |
+
- **Contest ID**: 0
|
| 46 |
+
- **Problem Index**:
|
| 47 |
+
- **Points**: 0.0
|
| 48 |
+
- **Rating**: 0
|
| 49 |
+
- **Tags**: None
|
| 50 |
+
- **Time Limit**: None seconds
|
| 51 |
+
- **Memory Limit**: 0 bytes
|
| 52 |
+
|
| 53 |
+
## Task
|
| 54 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0387/instruction.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# special-matrix-1
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
You are given a square matrix of size n (it will be an odd integer). Rows are
|
| 5 |
+
indexed 0 to n-1 from top to bottom and columns are indexed 0 to n-1
|
| 6 |
+
form left to right. Matrix consists of only '*' and '.'.
|
| 7 |
+
'*' appears only once in the matrix while all other positions are occupied by '.'
|
| 8 |
+
|
| 9 |
+
Your task is to convert this matrix to a special matrix by following any of two
|
| 10 |
+
operations any number of times.
|
| 11 |
+
|
| 12 |
+
you can swap any two adjecent rows, i and i+1 (0 ≤ i < n-1)
|
| 13 |
+
|
| 14 |
+
you can swap any two adjecent columns, j and j+1 (0 ≤ j < n-1)
|
| 15 |
+
|
| 16 |
+
Special Matrix is one which contain '*' at middle of matrix.
|
| 17 |
+
e.g following is size 7 special matrix
|
| 18 |
+
|
| 19 |
+
.......
|
| 20 |
+
.......
|
| 21 |
+
.......
|
| 22 |
+
...*...
|
| 23 |
+
.......
|
| 24 |
+
.......
|
| 25 |
+
.......
|
| 26 |
+
Output no of steps to convert given matrix to special matrix.
|
| 27 |
+
|
| 28 |
+
INPUT:
|
| 29 |
+
|
| 30 |
+
first line contains t, no of test cases
|
| 31 |
+
first line of each test case contains n (size of matrix) followed by n lines
|
| 32 |
+
containing n characters each.
|
| 33 |
+
|
| 34 |
+
OUTPUT:
|
| 35 |
+
|
| 36 |
+
print t lines, each containing an integer, the answer for the test case.
|
| 37 |
+
|
| 38 |
+
Constraints:
|
| 39 |
+
|
| 40 |
+
0 ≤ t ≤ 50
|
| 41 |
+
3 ≤ n ≤ 20
|
| 42 |
+
|
| 43 |
+
SAMPLE INPUT
|
| 44 |
+
1
|
| 45 |
+
7
|
| 46 |
+
.......
|
| 47 |
+
.*.....
|
| 48 |
+
.......
|
| 49 |
+
.......
|
| 50 |
+
.......
|
| 51 |
+
.......
|
| 52 |
+
.......
|
| 53 |
+
|
| 54 |
+
SAMPLE OUTPUT
|
| 55 |
+
4
|
| 56 |
+
|
| 57 |
+
## Contest Information
|
| 58 |
+
- **Contest ID**: 0
|
| 59 |
+
- **Problem Index**:
|
| 60 |
+
- **Points**: 0.0
|
| 61 |
+
- **Rating**: 0
|
| 62 |
+
- **Tags**: None
|
| 63 |
+
- **Time Limit**: None seconds
|
| 64 |
+
- **Memory Limit**: 0 bytes
|
| 65 |
+
|
| 66 |
+
## Task
|
| 67 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0388/instruction.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# trailing-zero-problem-1
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Given integer n, find length of n! (which is factorial of n) excluding trailing zeros.
|
| 5 |
+
|
| 6 |
+
Input
|
| 7 |
+
|
| 8 |
+
The first line of the standard input contains one integer t (t<10001) which is the number of test cases.
|
| 9 |
+
|
| 10 |
+
In each of the next t lines there is number n (0 ≤ n ≤ 5*10^9).
|
| 11 |
+
|
| 12 |
+
Output
|
| 13 |
+
|
| 14 |
+
For each test, print the length of n! (which is factorial of n).
|
| 15 |
+
|
| 16 |
+
Constraints
|
| 17 |
+
|
| 18 |
+
1 ≤ t ≤ 10
|
| 19 |
+
1 ≤ n ≤ 10^9
|
| 20 |
+
|
| 21 |
+
SAMPLE INPUT
|
| 22 |
+
3
|
| 23 |
+
5
|
| 24 |
+
7
|
| 25 |
+
10
|
| 26 |
+
|
| 27 |
+
SAMPLE OUTPUT
|
| 28 |
+
2
|
| 29 |
+
3
|
| 30 |
+
5
|
| 31 |
+
|
| 32 |
+
## Contest Information
|
| 33 |
+
- **Contest ID**: 0
|
| 34 |
+
- **Problem Index**:
|
| 35 |
+
- **Points**: 0.0
|
| 36 |
+
- **Rating**: 0
|
| 37 |
+
- **Tags**: None
|
| 38 |
+
- **Time Limit**: None seconds
|
| 39 |
+
- **Memory Limit**: 0 bytes
|
| 40 |
+
|
| 41 |
+
## Task
|
| 42 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0391/instruction.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# p02834 AtCoder Beginner Contest 148 - Playing Tag on Tree
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally.
|
| 5 |
+
|
| 6 |
+
Takahashi is standing at Vertex u, and Aoki is standing at Vertex v.
|
| 7 |
+
|
| 8 |
+
Now, they will play a game of tag as follows:
|
| 9 |
+
|
| 10 |
+
* 1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vertex of his choice that is adjacent to his current vertex.
|
| 11 |
+
|
| 12 |
+
* 2. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Aoki moves to a vertex of his choice that is adjacent to his current vertex.
|
| 13 |
+
|
| 14 |
+
* 3. Go back to step 1.
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
Takahashi performs his moves so that the game ends as late as possible, while Aoki performs his moves so that the game ends as early as possible.
|
| 20 |
+
|
| 21 |
+
Find the number of moves Aoki will perform before the end of the game if both Takahashi and Aoki know each other's position and strategy.
|
| 22 |
+
|
| 23 |
+
It can be proved that the game is bound to end.
|
| 24 |
+
|
| 25 |
+
Constraints
|
| 26 |
+
|
| 27 |
+
* 2 \leq N \leq 10^5
|
| 28 |
+
* 1 \leq u,v \leq N
|
| 29 |
+
* u \neq v
|
| 30 |
+
* 1 \leq A_i,B_i \leq N
|
| 31 |
+
* The given graph is a tree.
|
| 32 |
+
|
| 33 |
+
Input
|
| 34 |
+
|
| 35 |
+
Input is given from Standard Input in the following format:
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
N u v
|
| 39 |
+
A_1 B_1
|
| 40 |
+
:
|
| 41 |
+
A_{N-1} B_{N-1}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
Output
|
| 45 |
+
|
| 46 |
+
Print the number of moves Aoki will perform before the end of the game.
|
| 47 |
+
|
| 48 |
+
Examples
|
| 49 |
+
|
| 50 |
+
Input
|
| 51 |
+
|
| 52 |
+
5 4 1
|
| 53 |
+
1 2
|
| 54 |
+
2 3
|
| 55 |
+
3 4
|
| 56 |
+
3 5
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
Output
|
| 60 |
+
|
| 61 |
+
2
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
Input
|
| 65 |
+
|
| 66 |
+
5 4 5
|
| 67 |
+
1 2
|
| 68 |
+
1 3
|
| 69 |
+
1 4
|
| 70 |
+
1 5
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
Output
|
| 74 |
+
|
| 75 |
+
1
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
Input
|
| 79 |
+
|
| 80 |
+
2 1 2
|
| 81 |
+
1 2
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
Output
|
| 85 |
+
|
| 86 |
+
0
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
Input
|
| 90 |
+
|
| 91 |
+
9 6 1
|
| 92 |
+
1 2
|
| 93 |
+
2 3
|
| 94 |
+
3 4
|
| 95 |
+
4 5
|
| 96 |
+
5 6
|
| 97 |
+
4 7
|
| 98 |
+
7 8
|
| 99 |
+
8 9
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
Output
|
| 103 |
+
|
| 104 |
+
5
|
| 105 |
+
|
| 106 |
+
## Contest Information
|
| 107 |
+
- **Contest ID**: 0
|
| 108 |
+
- **Problem Index**:
|
| 109 |
+
- **Points**: 0.0
|
| 110 |
+
- **Rating**: 0
|
| 111 |
+
- **Tags**:
|
| 112 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 113 |
+
- **Memory Limit**: 1073741824 bytes
|
| 114 |
+
|
| 115 |
+
## Task
|
| 116 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0512/instruction.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# p01289 Strange Couple
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Alice and Bob are going to drive from their home to a theater for a date. They are very challenging - they have no maps with them even though they don’t know the route at all (since they have just moved to their new home). Yes, they will be going just by their feeling.
|
| 5 |
+
|
| 6 |
+
The town they drive can be considered as an undirected graph with a number of intersections (vertices) and roads (edges). Each intersection may or may not have a sign. On intersections with signs, Alice and Bob will enter the road for the shortest route. When there is more than one such roads, they will go into one of them at random.
|
| 7 |
+
|
| 8 |
+
On intersections without signs, they will just make a random choice. Each random selection is made with equal probabilities. They can even choose to go back to the road they have just come along, on a random selection.
|
| 9 |
+
|
| 10 |
+
Calculate the expected distance Alice and Bob will drive before reaching the theater.
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
Input
|
| 15 |
+
|
| 16 |
+
The input consists of multiple datasets. Each dataset has the following format:
|
| 17 |
+
|
| 18 |
+
n s t
|
| 19 |
+
q1 q2 ... qn
|
| 20 |
+
a11 a12 ... a1n
|
| 21 |
+
a21 a22 ... a2n
|
| 22 |
+
.
|
| 23 |
+
.
|
| 24 |
+
.
|
| 25 |
+
an1 an2 ... ann
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
n is the number of intersections (n ≤ 100). s and t are the intersections the home and the theater are located respectively (1 ≤ s, t ≤ n, s ≠ t); qi (for 1 ≤ i ≤ n) is either 1 or 0, where 1 denotes there is a sign at the i-th intersection and 0 denotes there is not; aij (for 1 ≤ i, j ≤ n) is a positive integer denoting the distance of the road connecting the i-th and j-th intersections, or 0 indicating there is no road directly connecting the intersections. The distance of each road does not exceed 10.
|
| 29 |
+
|
| 30 |
+
Since the graph is undirectional, it holds aij = aji for any 1 ≤ i, j ≤ n. There can be roads connecting the same intersection, that is, it does not always hold aii = 0. Also, note that the graph is not always planar.
|
| 31 |
+
|
| 32 |
+
The last dataset is followed by a line containing three zeros. This line is not a part of any dataset and should not be processed.
|
| 33 |
+
|
| 34 |
+
Output
|
| 35 |
+
|
| 36 |
+
For each dataset, print the expected distance accurate to 10-8 , or "impossible" (without quotes) if there is no route to get to the theater. The distance may be printed with any number of digits after the decimal point.
|
| 37 |
+
|
| 38 |
+
Example
|
| 39 |
+
|
| 40 |
+
Input
|
| 41 |
+
|
| 42 |
+
5 1 5
|
| 43 |
+
1 0 1 0 0
|
| 44 |
+
0 2 1 0 0
|
| 45 |
+
2 0 0 1 0
|
| 46 |
+
1 0 0 1 0
|
| 47 |
+
0 1 1 0 1
|
| 48 |
+
0 0 0 1 0
|
| 49 |
+
0 0 0
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
Output
|
| 53 |
+
|
| 54 |
+
8.50000000
|
| 55 |
+
|
| 56 |
+
## Contest Information
|
| 57 |
+
- **Contest ID**: 0
|
| 58 |
+
- **Problem Index**:
|
| 59 |
+
- **Points**: 0.0
|
| 60 |
+
- **Rating**: 0
|
| 61 |
+
- **Tags**:
|
| 62 |
+
- **Time Limit**: {'seconds': 8, 'nanos': 0} seconds
|
| 63 |
+
- **Memory Limit**: 134217728 bytes
|
| 64 |
+
|
| 65 |
+
## Task
|
| 66 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0515/instruction.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# p01770 Arojam's Mask
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Problem statement
|
| 5 |
+
|
| 6 |
+
You are a hero. The world in which the hero is traveling consists of N cities and M roads connecting different cities. Road i connects town a_i and town b_i and can move in both directions.
|
| 7 |
+
|
| 8 |
+
The purpose of the brave is to start from town S and move to town T. S and T are different cities. The hero can leave the city on day 0 and travel through a single road in just one day. There is a limited number of days R for traveling on the road, and the R day cannot be exceeded.
|
| 9 |
+
|
| 10 |
+
The brave can also "blow the ocarina" in any city. When you blow the ocarina, the position is returned to the city S on the 0th day. In other words, if you move more than R times, you must play the ocarina at least once.
|
| 11 |
+
|
| 12 |
+
In addition, "events" can occur when the brave is located in a city. There are E types of events, and the i-th event occurs in the city c_ {i}. The event will occur automatically when the hero is there, creating a new road connecting the roads a'_i and b'_i. This road does not disappear when you blow the ocarina, and it does not overlap with the road that exists from the beginning or is added by another event. There is at most one event in a town.
|
| 13 |
+
|
| 14 |
+
Now, the hero's job today is to write a program that finds the minimum sum of the number of moves required to reach the city T and the number of times the ocarina is blown.
|
| 15 |
+
|
| 16 |
+
input
|
| 17 |
+
|
| 18 |
+
The input is given in the following format.
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
N M E S T R
|
| 22 |
+
a_ {0} b_ {0}
|
| 23 |
+
...
|
| 24 |
+
a_ {M−1} b_ {M−1}
|
| 25 |
+
a’_ {0} b’_ {0} c_ {0}
|
| 26 |
+
...
|
| 27 |
+
a’_ {E−1} b’_ {E−1} c_ {E−1}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
Constraint
|
| 31 |
+
|
| 32 |
+
* All inputs are integers
|
| 33 |
+
* 2 \ ≤ N \ ≤ 100
|
| 34 |
+
* 1 \ ≤ M + E \ ≤ N (N−1) / 2
|
| 35 |
+
* 0 \ ≤ E \ ≤ 4
|
| 36 |
+
* 0 \ ≤ R \ ≤ 100
|
| 37 |
+
* 0 \ ≤ S, T, a_i, b_i, a'_i, b'_i, c_i \ ≤ N−1
|
| 38 |
+
* S ≠ T
|
| 39 |
+
* a_ {i} ≠ b_ {i}, a'_ {i} ≠ b'_ {i}
|
| 40 |
+
* All pairs of a_ {i} and b_ {i}, a’_ {i} and b’_ {i} do not overlap
|
| 41 |
+
* If i ≠ j, then c_ {i} ≠ c_ {j}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
output
|
| 46 |
+
|
| 47 |
+
Output the answer in one line. If you cannot reach T by all means, output -1.
|
| 48 |
+
|
| 49 |
+
sample
|
| 50 |
+
|
| 51 |
+
Sample input 1
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
8 5 2 0 5 5
|
| 55 |
+
0 1
|
| 56 |
+
0 3
|
| 57 |
+
0 6
|
| 58 |
+
1 2
|
| 59 |
+
6 7
|
| 60 |
+
3 4 7
|
| 61 |
+
4 5 2
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
Sample output 1
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
9
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
For example, it may be moved as follows. Note that the roads (4,5) and (3,4) do not exist until the event occurs.
|
| 71 |
+
|
| 72 |
+
* If you go to city 2, you will be able to move from 4 to 5.
|
| 73 |
+
* Use ocarina to return to 0
|
| 74 |
+
* If you go to city 7, you will be able to move from 3 to 4.
|
| 75 |
+
* Use ocarina to return to 0
|
| 76 |
+
* Go straight from 0 to 5
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
<image>
|
| 81 |
+
|
| 82 |
+
Sample input 2
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
7 5 1 0 6 8
|
| 86 |
+
0 1
|
| 87 |
+
1 2
|
| 88 |
+
twenty three
|
| 89 |
+
3 4
|
| 90 |
+
4 5
|
| 91 |
+
3 6 5
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
Sample output 2
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
8
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
The best route is to never blow the ocarina.
|
| 101 |
+
|
| 102 |
+
Sample input 3
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
4 1 4 1 2 3
|
| 106 |
+
3 1
|
| 107 |
+
3 2 0
|
| 108 |
+
0 1 3
|
| 109 |
+
0 3 1
|
| 110 |
+
0 2 2
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
Sample output 3
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
Five
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
Events may occur in town S.
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
Example
|
| 126 |
+
|
| 127 |
+
Input
|
| 128 |
+
|
| 129 |
+
8 5 2 0 5 5
|
| 130 |
+
0 1
|
| 131 |
+
0 3
|
| 132 |
+
0 6
|
| 133 |
+
1 2
|
| 134 |
+
6 7
|
| 135 |
+
3 4 7
|
| 136 |
+
4 5 2
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
Output
|
| 140 |
+
|
| 141 |
+
9
|
| 142 |
+
|
| 143 |
+
## Contest Information
|
| 144 |
+
- **Contest ID**: 0
|
| 145 |
+
- **Problem Index**:
|
| 146 |
+
- **Points**: 0.0
|
| 147 |
+
- **Rating**: 0
|
| 148 |
+
- **Tags**:
|
| 149 |
+
- **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
|
| 150 |
+
- **Memory Limit**: 134217728 bytes
|
| 151 |
+
|
| 152 |
+
## Task
|
| 153 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0522/instruction.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# cng
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
In the country of Numberia, there is a city called Primeland. Sherry is one of the rich inhabitants of the city, who likes to collect coins of various denominations. Coins in primeland are little strange , they are only available in prime denominations (i.e as coins of value 2,3,5 etc.).Sherry is taking a tour of the country but as he visits people ask him for change, unfortunately Sherry took coins of only two kinds of denominations(the coins are of Primeland), he has already made a lot of such changes but then he wonders what is the maximum amount which he can't give change for using the coins he has.Remember he has coins of only two different denominations and has infinite number of them..
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
Input
|
| 8 |
+
First line of input will contain number of testcases,T.Following T lines will contain two integers c and d separated by a space denoting the denominations which sherry has while he is on the tour, both c and d are guaranteed to be prime.
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
Output
|
| 12 |
+
For each of the T testcases output one number per line the maximum amount which sherry can't afford to give change for.
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
Constraints
|
| 16 |
+
|
| 17 |
+
1 ≤ T ≤ 100
|
| 18 |
+
2 ≤ c,d ≤ 10^6
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
Example
|
| 23 |
+
Input:
|
| 24 |
+
2
|
| 25 |
+
3 5
|
| 26 |
+
2 3
|
| 27 |
+
|
| 28 |
+
Output:
|
| 29 |
+
7
|
| 30 |
+
1
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
Explanation
|
| 34 |
+
For Test Case 1 :
|
| 35 |
+
We see that 2 cannot be paid using coins of value 3 and 5.
|
| 36 |
+
In fact we can pay the following series of numbers using coins of value 3 and 5.
|
| 37 |
+
3,5,6,8,9,10.....and so on.
|
| 38 |
+
Clearly we cannot make change for 1,2,4,7. Out of these numbers 7 is maximum therefore 7 is the correct answer.
|
| 39 |
+
Basically find the list of numbers for which change can't be made using the two coins given in the input and print the maximum of those numbers as your answer.
|
| 40 |
+
|
| 41 |
+
## Contest Information
|
| 42 |
+
- **Contest ID**: 0
|
| 43 |
+
- **Problem Index**:
|
| 44 |
+
- **Points**: 0.0
|
| 45 |
+
- **Rating**: 0
|
| 46 |
+
- **Tags**: None
|
| 47 |
+
- **Time Limit**: None seconds
|
| 48 |
+
- **Memory Limit**: 0 bytes
|
| 49 |
+
|
| 50 |
+
## Task
|
| 51 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0524/instruction.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# lowsum
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
The Head Chef is studying the motivation and satisfaction level of his chefs . The motivation and satisfaction of a Chef can be represented as an integer . The Head Chef wants to know the N th smallest sum of one satisfaction value and one motivation value for various values of N . The satisfaction and motivation values may correspond to the same chef or different chefs . Given two arrays, the first array denoting the motivation value and the second array denoting the satisfaction value of the chefs . We can get a set of sums(add one element from the first array and one from the second). For each query ( denoted by an integer qi ( i = 1 to Q ) , Q denotes number of queries ) , find the qi th element in the set of sums ( in non-decreasing order ) .
|
| 5 |
+
|
| 6 |
+
Input
|
| 7 |
+
|
| 8 |
+
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
|
| 9 |
+
The first line of each test case contains a two space seperated integers K and Q denoting the number of chefs and the number of queries .
|
| 10 |
+
The second line of each test case contains K space-separated integers A1, A2, ..., AK denoting the motivation of Chefs.
|
| 11 |
+
The third line of each test case contains K space-separated integers B1, B2, ..., BK denoting the satisfaction of Chefs.
|
| 12 |
+
The next Q lines contain a single integer qi ( for i = 1 to Q ) , find the qi th element in the set of sums .
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
Output
|
| 16 |
+
|
| 17 |
+
For each query of each test case, output a single line containing the answer to the query of the testcase
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
Constraints
|
| 21 |
+
Should contain all the constraints on the input data that you may have. Format it like:
|
| 22 |
+
|
| 23 |
+
1 ≤ T ≤ 5
|
| 24 |
+
1 ≤ K ≤ 20000
|
| 25 |
+
1 ≤ Q ≤ 500
|
| 26 |
+
1 ≤ qi ( for i = 1 to Q ) ≤ 10000
|
| 27 |
+
1 ≤ Ai ≤ 10^18 ( for i = 1 to K )
|
| 28 |
+
1 ≤ Bi ≤ 10^18 ( for i = 1 to K )
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
Example
|
| 32 |
+
Input:
|
| 33 |
+
1
|
| 34 |
+
3 1
|
| 35 |
+
1 2 3
|
| 36 |
+
4 5 6
|
| 37 |
+
4
|
| 38 |
+
|
| 39 |
+
Output:
|
| 40 |
+
7
|
| 41 |
+
|
| 42 |
+
Explanation
|
| 43 |
+
Example case 1. There are 9 elements in the set of sums :
|
| 44 |
+
1 + 4 = 5
|
| 45 |
+
2 + 4 = 6
|
| 46 |
+
1 + 5 = 6
|
| 47 |
+
1 + 6 = 7
|
| 48 |
+
2 + 5 = 7
|
| 49 |
+
3 + 4 = 7
|
| 50 |
+
2 + 6 = 8
|
| 51 |
+
3 + 5 = 8
|
| 52 |
+
3 + 6 = 9
|
| 53 |
+
The fourth smallest element is 7.
|
| 54 |
+
|
| 55 |
+
## Contest Information
|
| 56 |
+
- **Contest ID**: 0
|
| 57 |
+
- **Problem Index**:
|
| 58 |
+
- **Points**: 0.0
|
| 59 |
+
- **Rating**: 0
|
| 60 |
+
- **Tags**: None
|
| 61 |
+
- **Time Limit**: None seconds
|
| 62 |
+
- **Memory Limit**: 0 bytes
|
| 63 |
+
|
| 64 |
+
## Task
|
| 65 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0533/instruction.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1148_C. Crazy Diamond
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
You are given a permutation p of integers from 1 to n, where n is an even number.
|
| 5 |
+
|
| 6 |
+
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
|
| 7 |
+
|
| 8 |
+
* take two indices i and j such that 2 ⋅ |i - j| ≥ n and swap p_i and p_j.
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
There is no need to minimize the number of operations, however you should use no more than 5 ⋅ n operations. One can show that it is always possible to do that.
|
| 13 |
+
|
| 14 |
+
Input
|
| 15 |
+
|
| 16 |
+
The first line contains a single integer n (2 ≤ n ≤ 3 ⋅ 10^5, n is even) — the length of the permutation.
|
| 17 |
+
|
| 18 |
+
The second line contains n distinct integers p_1, p_2, …, p_n (1 ≤ p_i ≤ n) — the given permutation.
|
| 19 |
+
|
| 20 |
+
Output
|
| 21 |
+
|
| 22 |
+
On the first line print m (0 ≤ m ≤ 5 ⋅ n) — the number of swaps to perform.
|
| 23 |
+
|
| 24 |
+
Each of the following m lines should contain integers a_i, b_i (1 ≤ a_i, b_i ≤ n, |a_i - b_i| ≥ n/2) — the indices that should be swapped in the corresponding swap.
|
| 25 |
+
|
| 26 |
+
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
|
| 27 |
+
|
| 28 |
+
Examples
|
| 29 |
+
|
| 30 |
+
Input
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
2
|
| 34 |
+
2 1
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
Output
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
1
|
| 41 |
+
1 2
|
| 42 |
+
|
| 43 |
+
Input
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
4
|
| 47 |
+
3 4 1 2
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
Output
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
4
|
| 54 |
+
1 4
|
| 55 |
+
1 4
|
| 56 |
+
1 3
|
| 57 |
+
2 4
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
Input
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
6
|
| 64 |
+
2 5 3 1 4 6
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
Output
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
3
|
| 71 |
+
1 5
|
| 72 |
+
2 5
|
| 73 |
+
1 4
|
| 74 |
+
|
| 75 |
+
Note
|
| 76 |
+
|
| 77 |
+
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
|
| 78 |
+
|
| 79 |
+
In the second example, pay attention that there is no need to minimize number of swaps.
|
| 80 |
+
|
| 81 |
+
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6].
|
| 82 |
+
|
| 83 |
+
## Contest Information
|
| 84 |
+
- **Contest ID**: 1148
|
| 85 |
+
- **Problem Index**: C
|
| 86 |
+
- **Points**: 1500.0
|
| 87 |
+
- **Rating**: 1700
|
| 88 |
+
- **Tags**: constructive algorithms, sortings
|
| 89 |
+
- **Time Limit**: {'seconds': 3, 'nanos': 0} seconds
|
| 90 |
+
- **Memory Limit**: 256000000 bytes
|
| 91 |
+
|
| 92 |
+
## Task
|
| 93 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0534/instruction.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1169_E. And Reachability
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Toad Pimple has an array of integers a_1, a_2, …, a_n.
|
| 5 |
+
|
| 6 |
+
We say that y is reachable from x if x<y and there exists an integer array p such that x = p_1 < p_2 < … < p_k=y, and a_{p_i} \& a_{p_{i+1}} > 0 for all integers i such that 1 ≤ i < k.
|
| 7 |
+
|
| 8 |
+
Here \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
|
| 9 |
+
|
| 10 |
+
You are given q pairs of indices, check reachability for each of them.
|
| 11 |
+
|
| 12 |
+
Input
|
| 13 |
+
|
| 14 |
+
The first line contains two integers n and q (2 ≤ n ≤ 300 000, 1 ≤ q ≤ 300 000) — the number of integers in the array and the number of queries you need to answer.
|
| 15 |
+
|
| 16 |
+
The second line contains n space-separated integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 300 000) — the given array.
|
| 17 |
+
|
| 18 |
+
The next q lines contain two integers each. The i-th of them contains two space-separated integers x_i and y_i (1 ≤ x_i < y_i ≤ n). You need to check if y_i is reachable from x_i.
|
| 19 |
+
|
| 20 |
+
Output
|
| 21 |
+
|
| 22 |
+
Output q lines. In the i-th of them print "Shi" if y_i is reachable from x_i, otherwise, print "Fou".
|
| 23 |
+
|
| 24 |
+
Example
|
| 25 |
+
|
| 26 |
+
Input
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
5 3
|
| 30 |
+
1 3 0 2 1
|
| 31 |
+
1 3
|
| 32 |
+
2 4
|
| 33 |
+
1 4
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
Output
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
Fou
|
| 40 |
+
Shi
|
| 41 |
+
Shi
|
| 42 |
+
|
| 43 |
+
Note
|
| 44 |
+
|
| 45 |
+
In the first example, a_3 = 0. You can't reach it, because AND with it is always zero. a_2 \& a_4 > 0, so 4 is reachable from 2, and to go from 1 to 4 you can use p = [1, 2, 4].
|
| 46 |
+
|
| 47 |
+
## Contest Information
|
| 48 |
+
- **Contest ID**: 1169
|
| 49 |
+
- **Problem Index**: E
|
| 50 |
+
- **Points**: 1500.0
|
| 51 |
+
- **Rating**: 2200
|
| 52 |
+
- **Tags**: bitmasks, dp
|
| 53 |
+
- **Time Limit**: {'seconds': 3, 'nanos': 0} seconds
|
| 54 |
+
- **Memory Limit**: 256000000 bytes
|
| 55 |
+
|
| 56 |
+
## Task
|
| 57 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0540/instruction.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1290_A. Mind Control
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
|
| 5 |
+
|
| 6 |
+
You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
|
| 7 |
+
|
| 8 |
+
Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x?
|
| 9 |
+
|
| 10 |
+
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
|
| 11 |
+
|
| 12 |
+
Input
|
| 13 |
+
|
| 14 |
+
The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The description of the test cases follows.
|
| 15 |
+
|
| 16 |
+
The first line of each test case contains three space-separated integers n, m and k (1 ≤ m ≤ n ≤ 3500, 0 ≤ k ≤ n - 1) — the number of elements in the array, your position in line and the number of people whose choices you can fix.
|
| 17 |
+
|
| 18 |
+
The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — elements of the array.
|
| 19 |
+
|
| 20 |
+
It is guaranteed that the sum of n over all test cases does not exceed 3500.
|
| 21 |
+
|
| 22 |
+
Output
|
| 23 |
+
|
| 24 |
+
For each test case, print the largest integer x such that you can guarantee to obtain at least x.
|
| 25 |
+
|
| 26 |
+
Example
|
| 27 |
+
|
| 28 |
+
Input
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
4
|
| 32 |
+
6 4 2
|
| 33 |
+
2 9 2 3 8 5
|
| 34 |
+
4 4 1
|
| 35 |
+
2 13 60 4
|
| 36 |
+
4 1 3
|
| 37 |
+
1 2 2 1
|
| 38 |
+
2 2 0
|
| 39 |
+
1 2
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
Output
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
8
|
| 46 |
+
4
|
| 47 |
+
1
|
| 48 |
+
1
|
| 49 |
+
|
| 50 |
+
Note
|
| 51 |
+
|
| 52 |
+
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.
|
| 53 |
+
|
| 54 |
+
* the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8];
|
| 55 |
+
* the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8];
|
| 56 |
+
* if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element);
|
| 57 |
+
* if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element).
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8.
|
| 62 |
+
|
| 63 |
+
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4.
|
| 64 |
+
|
| 65 |
+
## Contest Information
|
| 66 |
+
- **Contest ID**: 1290
|
| 67 |
+
- **Problem Index**: A
|
| 68 |
+
- **Points**: 500.0
|
| 69 |
+
- **Rating**: 1600
|
| 70 |
+
- **Tags**: brute force, data structures, implementation
|
| 71 |
+
- **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
|
| 72 |
+
- **Memory Limit**: 256000000 bytes
|
| 73 |
+
|
| 74 |
+
## Task
|
| 75 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0546/instruction.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 141_C. Queue
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
|
| 5 |
+
|
| 6 |
+
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
|
| 7 |
+
|
| 8 |
+
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
|
| 9 |
+
|
| 10 |
+
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
|
| 11 |
+
|
| 12 |
+
Input
|
| 13 |
+
|
| 14 |
+
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
|
| 15 |
+
|
| 16 |
+
Output
|
| 17 |
+
|
| 18 |
+
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
|
| 19 |
+
|
| 20 |
+
Examples
|
| 21 |
+
|
| 22 |
+
Input
|
| 23 |
+
|
| 24 |
+
4
|
| 25 |
+
a 0
|
| 26 |
+
b 2
|
| 27 |
+
c 0
|
| 28 |
+
d 0
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
Output
|
| 32 |
+
|
| 33 |
+
a 150
|
| 34 |
+
c 170
|
| 35 |
+
d 180
|
| 36 |
+
b 160
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
Input
|
| 40 |
+
|
| 41 |
+
4
|
| 42 |
+
vasya 0
|
| 43 |
+
petya 1
|
| 44 |
+
manya 3
|
| 45 |
+
dunay 3
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
Output
|
| 49 |
+
|
| 50 |
+
-1
|
| 51 |
+
|
| 52 |
+
## Contest Information
|
| 53 |
+
- **Contest ID**: 141
|
| 54 |
+
- **Problem Index**: C
|
| 55 |
+
- **Points**: 2000.0
|
| 56 |
+
- **Rating**: 1800
|
| 57 |
+
- **Tags**: constructive algorithms, greedy, sortings
|
| 58 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 59 |
+
- **Memory Limit**: 256000000 bytes
|
| 60 |
+
|
| 61 |
+
## Task
|
| 62 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0547/instruction.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1438_C. Engineer Artem
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Artem is building a new robot. He has a matrix a consisting of n rows and m columns. The cell located on the i-th row from the top and the j-th column from the left has a value a_{i,j} written in it.
|
| 5 |
+
|
| 6 |
+
If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells contain the same value, where two cells are called adjacent if they share a side.
|
| 7 |
+
|
| 8 |
+
Artem wants to increment the values in some cells by one to make a good.
|
| 9 |
+
|
| 10 |
+
More formally, find a good matrix b that satisfies the following condition —
|
| 11 |
+
|
| 12 |
+
* For all valid (i,j), either b_{i,j} = a_{i,j} or b_{i,j} = a_{i,j}+1.
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
For the constraints of this problem, it can be shown that such a matrix b always exists. If there are several such tables, you can output any of them. Please note that you do not have to minimize the number of increments.
|
| 17 |
+
|
| 18 |
+
Input
|
| 19 |
+
|
| 20 |
+
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10). Description of the test cases follows.
|
| 21 |
+
|
| 22 |
+
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of rows and columns, respectively.
|
| 23 |
+
|
| 24 |
+
The following n lines each contain m integers. The j-th integer in the i-th line is a_{i,j} (1 ≤ a_{i,j} ≤ 10^9).
|
| 25 |
+
|
| 26 |
+
Output
|
| 27 |
+
|
| 28 |
+
For each case, output n lines each containing m integers. The j-th integer in the i-th line is b_{i,j}.
|
| 29 |
+
|
| 30 |
+
Example
|
| 31 |
+
|
| 32 |
+
Input
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
3
|
| 36 |
+
3 2
|
| 37 |
+
1 2
|
| 38 |
+
4 5
|
| 39 |
+
7 8
|
| 40 |
+
2 2
|
| 41 |
+
1 1
|
| 42 |
+
3 3
|
| 43 |
+
2 2
|
| 44 |
+
1 3
|
| 45 |
+
2 2
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
Output
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
1 2
|
| 52 |
+
5 6
|
| 53 |
+
7 8
|
| 54 |
+
2 1
|
| 55 |
+
4 3
|
| 56 |
+
2 4
|
| 57 |
+
3 2
|
| 58 |
+
|
| 59 |
+
Note
|
| 60 |
+
|
| 61 |
+
In all the cases, you can verify that no two adjacent cells have the same value and that b is the same as a with some values incremented by one.
|
| 62 |
+
|
| 63 |
+
## Contest Information
|
| 64 |
+
- **Contest ID**: 1438
|
| 65 |
+
- **Problem Index**: C
|
| 66 |
+
- **Points**: 1250.0
|
| 67 |
+
- **Rating**: 2000
|
| 68 |
+
- **Tags**: 2-sat, chinese remainder theorem, constructive algorithms, fft, flows
|
| 69 |
+
- **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
|
| 70 |
+
- **Memory Limit**: 256000000 bytes
|
| 71 |
+
|
| 72 |
+
## Task
|
| 73 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0548/instruction.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1463_B. Find The Array
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
You are given an array [a_1, a_2, ..., a_n] such that 1 ≤ a_i ≤ 10^9. Let S be the sum of all elements of the array a.
|
| 5 |
+
|
| 6 |
+
Let's call an array b of n integers beautiful if:
|
| 7 |
+
|
| 8 |
+
* 1 ≤ b_i ≤ 10^9 for each i from 1 to n;
|
| 9 |
+
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
|
| 10 |
+
* 2 ∑ _{i = 1}^{n} |a_i - b_i| ≤ S.
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
|
| 15 |
+
|
| 16 |
+
Input
|
| 17 |
+
|
| 18 |
+
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
|
| 19 |
+
|
| 20 |
+
Each test case consists of two lines. The first line contains one integer n (2 ≤ n ≤ 50).
|
| 21 |
+
|
| 22 |
+
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
|
| 23 |
+
|
| 24 |
+
Output
|
| 25 |
+
|
| 26 |
+
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
|
| 27 |
+
|
| 28 |
+
Example
|
| 29 |
+
|
| 30 |
+
Input
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
4
|
| 34 |
+
5
|
| 35 |
+
1 2 3 4 5
|
| 36 |
+
2
|
| 37 |
+
4 6
|
| 38 |
+
2
|
| 39 |
+
1 1000000000
|
| 40 |
+
6
|
| 41 |
+
3 4 8 1 2 3
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
Output
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
3 3 3 3 3
|
| 48 |
+
3 6
|
| 49 |
+
1 1000000000
|
| 50 |
+
4 4 8 1 3 3
|
| 51 |
+
|
| 52 |
+
## Contest Information
|
| 53 |
+
- **Contest ID**: 1463
|
| 54 |
+
- **Problem Index**: B
|
| 55 |
+
- **Points**: 0.0
|
| 56 |
+
- **Rating**: 1400
|
| 57 |
+
- **Tags**: bitmasks, constructive algorithms, greedy
|
| 58 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 59 |
+
- **Memory Limit**: 256000000 bytes
|
| 60 |
+
|
| 61 |
+
## Task
|
| 62 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0558/instruction.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 305_A. Strange Addition
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Unfortunately, Vasya can only sum pairs of integers (a, b), such that for any decimal place at least one number has digit 0 in this place. For example, Vasya can sum numbers 505 and 50, but he cannot sum 1 and 4.
|
| 5 |
+
|
| 6 |
+
Vasya has a set of k distinct non-negative integers d1, d2, ..., dk.
|
| 7 |
+
|
| 8 |
+
Vasya wants to choose some integers from this set so that he could sum any two chosen numbers. What maximal number of integers can he choose in the required manner?
|
| 9 |
+
|
| 10 |
+
Input
|
| 11 |
+
|
| 12 |
+
The first input line contains integer k (1 ≤ k ≤ 100) — the number of integers.
|
| 13 |
+
|
| 14 |
+
The second line contains k distinct space-separated integers d1, d2, ..., dk (0 ≤ di ≤ 100).
|
| 15 |
+
|
| 16 |
+
Output
|
| 17 |
+
|
| 18 |
+
In the first line print a single integer n the maximum number of the chosen integers. In the second line print n distinct non-negative integers — the required integers.
|
| 19 |
+
|
| 20 |
+
If there are multiple solutions, print any of them. You can print the numbers in any order.
|
| 21 |
+
|
| 22 |
+
Examples
|
| 23 |
+
|
| 24 |
+
Input
|
| 25 |
+
|
| 26 |
+
4
|
| 27 |
+
100 10 1 0
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
Output
|
| 31 |
+
|
| 32 |
+
4
|
| 33 |
+
0 1 10 100
|
| 34 |
+
|
| 35 |
+
Input
|
| 36 |
+
|
| 37 |
+
3
|
| 38 |
+
2 70 3
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
Output
|
| 42 |
+
|
| 43 |
+
2
|
| 44 |
+
2 70
|
| 45 |
+
|
| 46 |
+
## Contest Information
|
| 47 |
+
- **Contest ID**: 305
|
| 48 |
+
- **Problem Index**: A
|
| 49 |
+
- **Points**: 500.0
|
| 50 |
+
- **Rating**: 1600
|
| 51 |
+
- **Tags**: brute force, constructive algorithms, implementation
|
| 52 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 53 |
+
- **Memory Limit**: 256000000 bytes
|
| 54 |
+
|
| 55 |
+
## Task
|
| 56 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0570/instruction.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 591_C. Median Smoothing
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.
|
| 5 |
+
|
| 6 |
+
Applying the simplest variant of median smoothing to the sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bn obtained by the following algorithm:
|
| 7 |
+
|
| 8 |
+
* b1 = a1, bn = an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence.
|
| 9 |
+
* For i = 2, ..., n - 1 value bi is equal to the median of three values ai - 1, ai and ai + 1.
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.
|
| 14 |
+
|
| 15 |
+
In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.
|
| 16 |
+
|
| 17 |
+
Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.
|
| 18 |
+
|
| 19 |
+
Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.
|
| 20 |
+
|
| 21 |
+
Input
|
| 22 |
+
|
| 23 |
+
The first input line of the input contains a single integer n (3 ≤ n ≤ 500 000) — the length of the initial sequence.
|
| 24 |
+
|
| 25 |
+
The next line contains n integers a1, a2, ..., an (ai = 0 or ai = 1), giving the initial sequence itself.
|
| 26 |
+
|
| 27 |
+
Output
|
| 28 |
+
|
| 29 |
+
If the sequence will never become stable, print a single number - 1.
|
| 30 |
+
|
| 31 |
+
Otherwise, first print a single integer — the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print n numbers separated by a space — the resulting sequence itself.
|
| 32 |
+
|
| 33 |
+
Examples
|
| 34 |
+
|
| 35 |
+
Input
|
| 36 |
+
|
| 37 |
+
4
|
| 38 |
+
0 0 1 1
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
Output
|
| 42 |
+
|
| 43 |
+
0
|
| 44 |
+
0 0 1 1
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
Input
|
| 48 |
+
|
| 49 |
+
5
|
| 50 |
+
0 1 0 1 0
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
Output
|
| 54 |
+
|
| 55 |
+
2
|
| 56 |
+
0 0 0 0 0
|
| 57 |
+
|
| 58 |
+
Note
|
| 59 |
+
|
| 60 |
+
In the second sample the stabilization occurs in two steps: <image>, and the sequence 00000 is obviously stable.
|
| 61 |
+
|
| 62 |
+
## Contest Information
|
| 63 |
+
- **Contest ID**: 591
|
| 64 |
+
- **Problem Index**: C
|
| 65 |
+
- **Points**: 750.0
|
| 66 |
+
- **Rating**: 1700
|
| 67 |
+
- **Tags**: implementation
|
| 68 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 69 |
+
- **Memory Limit**: 256000000 bytes
|
| 70 |
+
|
| 71 |
+
## Task
|
| 72 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0577/instruction.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 750_F. New Year and Finding Roots
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
This is an interactive problem. In the interaction section below you will find the information about flushing the output.
|
| 5 |
+
|
| 6 |
+
The New Year tree of height h is a perfect binary tree with vertices numbered 1 through 2h - 1 in some order. In this problem we assume that h is at least 2. The drawing below shows one example New Year tree of height 3:
|
| 7 |
+
|
| 8 |
+
<image>
|
| 9 |
+
|
| 10 |
+
Polar bears love decorating the New Year tree and Limak is no exception. To decorate the tree, he must first find its root, i.e. a vertex with exactly two neighbours (assuming that h ≥ 2). It won't be easy because Limak is a little bear and he doesn't even see the whole tree. Can you help him?
|
| 11 |
+
|
| 12 |
+
There are t testcases. In each testcase, you should first read h from the input. Then you can ask at most 16 questions of format "? x" (without quotes), where x is an integer between 1 and 2h - 1, inclusive. As a reply you will get the list of neighbours of vertex x (more details in the "Interaction" section below). For example, for a tree on the drawing above after asking "? 1" you would get a response with 3 neighbours: 4, 5 and 7. Your goal is to find the index of the root y and print it in the format "! y". You will be able to read h for a next testcase only after printing the answer in a previous testcase and flushing the output.
|
| 13 |
+
|
| 14 |
+
Each tree is fixed from the beginning and it doesn't change during your questions.
|
| 15 |
+
|
| 16 |
+
Input
|
| 17 |
+
|
| 18 |
+
The first line of the input contains a single integer t (1 ≤ t ≤ 500) — the number of testcases.
|
| 19 |
+
|
| 20 |
+
At the beginning of each testcase you should read from the input a single integer h (2 ≤ h ≤ 7) — the height of the tree. You can't read the value of h in a next testcase until you answer a previous testcase.
|
| 21 |
+
|
| 22 |
+
Interaction
|
| 23 |
+
|
| 24 |
+
To ask a question about neighbours of vertex x, print "? x" (without quotes) on a separate line. Note, you must print an end-of-line character after the last character of the line and flush your output to get a response.
|
| 25 |
+
|
| 26 |
+
The response will consist of two lines. The first line will contain a single integer k (1 ≤ k ≤ 3) — the number of neighbours of vertex x. The second line will contain k distinct integers t1, ..., tk (1 ≤ t1 < ... < tk ≤ 2h - 1) — indices of neighbours of vertex x, gives in the increasing order.
|
| 27 |
+
|
| 28 |
+
After asking at most 16 questions you have to say y — the index of the root. Print "! y" (without quotes) and an end-of-line character, and flush the output.
|
| 29 |
+
|
| 30 |
+
Each tree is fixed from the beginning and it doesn't change during your questions.
|
| 31 |
+
|
| 32 |
+
You can get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output.
|
| 33 |
+
|
| 34 |
+
To flush you can use (just printing a query/answer and end-of-line):
|
| 35 |
+
|
| 36 |
+
* fflush(stdout) in C++;
|
| 37 |
+
* System.out.flush() in Java;
|
| 38 |
+
* stdout.flush() in Python;
|
| 39 |
+
* flush(output) in Pascal;
|
| 40 |
+
* See the documentation for other languages.
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
In any moment if the program reads h = 0 or k = 0 it should immediately terminate normally (for example, calling exit(0)). It means that the system detected incorrect request/output from your program and printed 0 because if can't process your requests anymore. In this case you'll receive verdict "Wrong Answer", but if you ignore case h = 0 or k = 0 it could lead to "Runtime Error", "Time/Memory limit exceeded" or any other verdict because your program could read a trash from the closed input stream.
|
| 45 |
+
|
| 46 |
+
Hacking. To hack someone, use the following format:
|
| 47 |
+
|
| 48 |
+
The first line should contain a single integer t equal to 1 (only one testcase is allowed in hacks). The second line should contain a single integer h. Each of next 2h - 2 lines should contain two distinct integers ai and bi (1 ≤ ai, bi ≤ 2h - 1), denoting two nodes connected with an edge. The printed edges must form a perfect binary tree of height h.
|
| 49 |
+
|
| 50 |
+
Of course, contestant programs will not be able to see this input.
|
| 51 |
+
|
| 52 |
+
Examples
|
| 53 |
+
|
| 54 |
+
Input
|
| 55 |
+
|
| 56 |
+
1
|
| 57 |
+
3
|
| 58 |
+
3
|
| 59 |
+
4 5 7
|
| 60 |
+
2
|
| 61 |
+
1 2
|
| 62 |
+
1
|
| 63 |
+
2
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
Output
|
| 67 |
+
|
| 68 |
+
? 1
|
| 69 |
+
? 5
|
| 70 |
+
? 6
|
| 71 |
+
! 5
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
Input
|
| 75 |
+
|
| 76 |
+
2
|
| 77 |
+
2
|
| 78 |
+
1
|
| 79 |
+
3
|
| 80 |
+
2
|
| 81 |
+
1 2
|
| 82 |
+
2
|
| 83 |
+
1 2
|
| 84 |
+
4
|
| 85 |
+
3
|
| 86 |
+
3 12 13
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
Output
|
| 90 |
+
|
| 91 |
+
? 1
|
| 92 |
+
? 3
|
| 93 |
+
? 3
|
| 94 |
+
! 3
|
| 95 |
+
? 6
|
| 96 |
+
! 1
|
| 97 |
+
|
| 98 |
+
Note
|
| 99 |
+
|
| 100 |
+
In the first sample, a tree corresponds to the drawing from the statement.
|
| 101 |
+
|
| 102 |
+
In the second sample, there are two two testcases. A tree in the first testcase has height 2 and thus 3 vertices. A tree in the second testcase has height 4 and thus 15 vertices. You can see both trees on the drawing below.
|
| 103 |
+
|
| 104 |
+
<image>
|
| 105 |
+
|
| 106 |
+
## Contest Information
|
| 107 |
+
- **Contest ID**: 750
|
| 108 |
+
- **Problem Index**: F
|
| 109 |
+
- **Points**: 3000.0
|
| 110 |
+
- **Rating**: 2800
|
| 111 |
+
- **Tags**: constructive algorithms, implementation, interactive, trees
|
| 112 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 113 |
+
- **Memory Limit**: 256000000 bytes
|
| 114 |
+
|
| 115 |
+
## Task
|
| 116 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0578/instruction.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 774_D. Lie or Truth
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.
|
| 5 |
+
|
| 6 |
+
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn.
|
| 7 |
+
|
| 8 |
+
Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).
|
| 9 |
+
|
| 10 |
+
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
|
| 11 |
+
|
| 12 |
+
Input
|
| 13 |
+
|
| 14 |
+
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan.
|
| 15 |
+
|
| 16 |
+
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order.
|
| 17 |
+
|
| 18 |
+
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order.
|
| 19 |
+
|
| 20 |
+
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
|
| 21 |
+
|
| 22 |
+
Output
|
| 23 |
+
|
| 24 |
+
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
|
| 25 |
+
|
| 26 |
+
Examples
|
| 27 |
+
|
| 28 |
+
Input
|
| 29 |
+
|
| 30 |
+
5 2 4
|
| 31 |
+
3 4 2 3 1
|
| 32 |
+
3 2 3 4 1
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
Output
|
| 36 |
+
|
| 37 |
+
TRUTH
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
Input
|
| 41 |
+
|
| 42 |
+
3 1 2
|
| 43 |
+
1 2 3
|
| 44 |
+
3 1 2
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
Output
|
| 48 |
+
|
| 49 |
+
LIE
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
Input
|
| 53 |
+
|
| 54 |
+
4 2 4
|
| 55 |
+
1 1 1 1
|
| 56 |
+
1 1 1 1
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
Output
|
| 60 |
+
|
| 61 |
+
TRUTH
|
| 62 |
+
|
| 63 |
+
Note
|
| 64 |
+
|
| 65 |
+
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
|
| 66 |
+
|
| 67 |
+
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
|
| 68 |
+
|
| 69 |
+
In the third example for any values l and r there is a situation when Stepan said the truth.
|
| 70 |
+
|
| 71 |
+
## Contest Information
|
| 72 |
+
- **Contest ID**: 774
|
| 73 |
+
- **Problem Index**: D
|
| 74 |
+
- **Points**: 0.0
|
| 75 |
+
- **Rating**: 1500
|
| 76 |
+
- **Tags**: *special, constructive algorithms, sortings
|
| 77 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 78 |
+
- **Memory Limit**: 256000000 bytes
|
| 79 |
+
|
| 80 |
+
## Task
|
| 81 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0579/instruction.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 799_D. Field expansion
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by ai. Each extension can't be used more than once, the extensions can be used in any order.
|
| 5 |
+
|
| 6 |
+
Now Arkady's field has size h × w. He wants to enlarge it so that it is possible to place a rectangle of size a × b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal.
|
| 7 |
+
|
| 8 |
+
Input
|
| 9 |
+
|
| 10 |
+
The first line contains five integers a, b, h, w and n (1 ≤ a, b, h, w, n ≤ 100 000) — the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions.
|
| 11 |
+
|
| 12 |
+
The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 100 000), where ai equals the integer a side multiplies by when the i-th extension is applied.
|
| 13 |
+
|
| 14 |
+
Output
|
| 15 |
+
|
| 16 |
+
Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0.
|
| 17 |
+
|
| 18 |
+
Examples
|
| 19 |
+
|
| 20 |
+
Input
|
| 21 |
+
|
| 22 |
+
3 3 2 4 4
|
| 23 |
+
2 5 4 10
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
Output
|
| 27 |
+
|
| 28 |
+
1
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
Input
|
| 32 |
+
|
| 33 |
+
3 3 3 3 5
|
| 34 |
+
2 3 5 4 2
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
Output
|
| 38 |
+
|
| 39 |
+
0
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
Input
|
| 43 |
+
|
| 44 |
+
5 5 1 2 3
|
| 45 |
+
2 2 3
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
Output
|
| 49 |
+
|
| 50 |
+
-1
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
Input
|
| 54 |
+
|
| 55 |
+
3 4 1 1 3
|
| 56 |
+
2 3 2
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
Output
|
| 60 |
+
|
| 61 |
+
3
|
| 62 |
+
|
| 63 |
+
Note
|
| 64 |
+
|
| 65 |
+
In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
|
| 66 |
+
|
| 67 |
+
## Contest Information
|
| 68 |
+
- **Contest ID**: 799
|
| 69 |
+
- **Problem Index**: D
|
| 70 |
+
- **Points**: 2000.0
|
| 71 |
+
- **Rating**: 2100
|
| 72 |
+
- **Tags**: brute force, dp, meet-in-the-middle
|
| 73 |
+
- **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
|
| 74 |
+
- **Memory Limit**: 256000000 bytes
|
| 75 |
+
|
| 76 |
+
## Task
|
| 77 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0583/instruction.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 891_D. Sloth
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Sloth is bad, mkay? So we decided to prepare a problem to punish lazy guys.
|
| 5 |
+
|
| 6 |
+
You are given a tree, you should count the number of ways to remove an edge from it and then add an edge to it such that the final graph is a tree and has a perfect matching. Two ways of this operation are considered different if their removed edges or their added edges aren't the same. The removed edge and the added edge can be equal.
|
| 7 |
+
|
| 8 |
+
A perfect matching is a subset of edges such that each vertex is an endpoint of exactly one of these edges.
|
| 9 |
+
|
| 10 |
+
Input
|
| 11 |
+
|
| 12 |
+
The first line contains n (2 ≤ n ≤ 5·105) — the number of vertices.
|
| 13 |
+
|
| 14 |
+
Each of the next n - 1 lines contains two integers a and b (1 ≤ a, b ≤ n) — the endpoints of one edge. It's guaranteed that the graph is a tree.
|
| 15 |
+
|
| 16 |
+
Output
|
| 17 |
+
|
| 18 |
+
Output a single integer — the answer to the problem.
|
| 19 |
+
|
| 20 |
+
Examples
|
| 21 |
+
|
| 22 |
+
Input
|
| 23 |
+
|
| 24 |
+
4
|
| 25 |
+
1 2
|
| 26 |
+
2 3
|
| 27 |
+
3 4
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
Output
|
| 31 |
+
|
| 32 |
+
8
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
Input
|
| 36 |
+
|
| 37 |
+
5
|
| 38 |
+
1 2
|
| 39 |
+
2 3
|
| 40 |
+
3 4
|
| 41 |
+
3 5
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
Output
|
| 45 |
+
|
| 46 |
+
0
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
Input
|
| 50 |
+
|
| 51 |
+
8
|
| 52 |
+
1 2
|
| 53 |
+
2 3
|
| 54 |
+
3 4
|
| 55 |
+
1 5
|
| 56 |
+
5 6
|
| 57 |
+
6 7
|
| 58 |
+
1 8
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
Output
|
| 62 |
+
|
| 63 |
+
22
|
| 64 |
+
|
| 65 |
+
Note
|
| 66 |
+
|
| 67 |
+
In first sample, there are 8 ways:
|
| 68 |
+
|
| 69 |
+
* edge between 2 and 3 turns to edge between 1 and 3,
|
| 70 |
+
* edge between 2 and 3 turns to edge between 1 and 4,
|
| 71 |
+
* edge between 2 and 3 turns to edge between 2 and 3,
|
| 72 |
+
* edge between 2 and 3 turns to edge between 2 and 4,
|
| 73 |
+
* edge between 1 and 2 turns to edge between 1 and 2,
|
| 74 |
+
* edge between 1 and 2 turns to edge between 1 and 4,
|
| 75 |
+
* edge between 3 and 4 turns to edge between 1 and 4,
|
| 76 |
+
* edge between 3 and 4 turns to edge between 3 and 4.
|
| 77 |
+
|
| 78 |
+
## Contest Information
|
| 79 |
+
- **Contest ID**: 891
|
| 80 |
+
- **Problem Index**: D
|
| 81 |
+
- **Points**: 2000.0
|
| 82 |
+
- **Rating**: 3100
|
| 83 |
+
- **Tags**: dfs and similar, dp, graph matchings, trees
|
| 84 |
+
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
|
| 85 |
+
- **Memory Limit**: 256000000 bytes
|
| 86 |
+
|
| 87 |
+
## Task
|
| 88 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|
code_contests-0701/instruction.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# who-will-survive-in-the-end
|
| 2 |
+
|
| 3 |
+
## Problem Description
|
| 4 |
+
Ali baba did a trick on the forty thieves and was able to trap them inside a big cave which was the home of wild wolves. The thieves are without any weapons, only the chief of the thieves has knife. With no weapons they will not be able to fight with the wolves, so they decide to kill themselves rather than being eaten alive.
|
| 5 |
+
|
| 6 |
+
They all decide that they will stand in a circle and they every third person will kill himself but the chief of the thieves does not like this idea and has no intention of killing himself. He calculates where should he stand so that he is the last one left.
|
| 7 |
+
|
| 8 |
+
HackerMan wants to build a game based on this story, but instead of killing he decides that the participant will leave the game, and instead of every 3rd position it will be every 2nd position. Of course the number of participants will be much more than 40 in this game.
|
| 9 |
+
|
| 10 |
+
Input
|
| 11 |
+
|
| 12 |
+
The first line of input is an integer N (1 ≤ N ≤ 1000) that specifies the number of test cases. After that every line contains an integer X (5 ≤ X ≤ 100000000) which is the number of participants in the game.
|
| 13 |
+
|
| 14 |
+
Output
|
| 15 |
+
|
| 16 |
+
For each test case generate a line containing the position of the participant who survives. Assume that the participants have serial numbers from 1 to n and that the counting starts with person 1, i.e., the first person leaving is the one with number 2.
|
| 17 |
+
|
| 18 |
+
SAMPLE INPUT
|
| 19 |
+
4
|
| 20 |
+
5
|
| 21 |
+
11
|
| 22 |
+
45
|
| 23 |
+
23987443
|
| 24 |
+
|
| 25 |
+
SAMPLE OUTPUT
|
| 26 |
+
3
|
| 27 |
+
7
|
| 28 |
+
27
|
| 29 |
+
14420455
|
| 30 |
+
|
| 31 |
+
Explanation
|
| 32 |
+
|
| 33 |
+
Taking the case of the first test case if there are 5 participants in the circle, the first to go is 2, followed by 4, followed by 1, followed by 5 which leaves 3 at the end.
|
| 34 |
+
|
| 35 |
+
## Contest Information
|
| 36 |
+
- **Contest ID**: 0
|
| 37 |
+
- **Problem Index**:
|
| 38 |
+
- **Points**: 0.0
|
| 39 |
+
- **Rating**: 0
|
| 40 |
+
- **Tags**: None
|
| 41 |
+
- **Time Limit**: None seconds
|
| 42 |
+
- **Memory Limit**: 0 bytes
|
| 43 |
+
|
| 44 |
+
## Task
|
| 45 |
+
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
|