A. Aziz and Sardor's Game
Xotira: 32 MB, Vaqt: 1000 msAziz and Sardor are playing a simple game. Aziz thought of number between and , and Sardor tries to guess the number.
Sardor can ask questions like: "Is the unknown number divisible by number ?".
The game is played by the following rules: first Sardor asks all the questions that interest him (also, he can ask no questions), and then Aziz responds to each question with 'yes' or 'no'. After receiving all the answers Sardor should determine the number that Aziz thought of.
Unfortunately, Sardor is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Aziz's number, and the numbers , he should ask the questions about.
A single line contains number
Print the length of the sequence of questions , followed by numbers — the questions
If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.
The sequence from the answer to the first sample test is actually correct.
If the unknown number is not divisible by one of the sequence numbers, it is equal to 1.
If the unknown number is divisible by 4, it is 4.
If the unknown number is divisible by 3, then the unknown number is 3.
Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.
# | INPUT.TXT | OUTPUT.TXT |
---|---|---|
1 |
4 |
3 2 4 3 |
2 |
6 |
4 2 4 3 5 |
B. Tree Consistency
Xotira: 32 MB, Vaqt: 1000 msA tree of size is an undirected connected graph consisting of vertices without cycles.
Consider some tree with vertices. We call a tree invariant relative to permutation , if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices and are connected by an edge".
You are given permutation p of size . Find some tree size , invariant relative to the given permutation.
The first line contains number — the size of the permutation (also equal to the size of the sought tree).
The second line contains permutation
If the sought tree does not exist, print (without the quotes).
Otherwise, print "YES", and then print lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter.
If there are multiple solutions, output any of them.
In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree.
It can be shown that in the second sample test no tree satisfies the given condition.
# | INPUT.TXT | OUTPUT.TXT |
---|---|---|
1 |
4 4 3 2 1 |
YES 4 1 4 2 1 3 |
2 |
3 3 1 2 |
NO |
C. Coordinates on a Plane
Xotira: 32 MB, Vaqt: 1000 msOn a plane are n points with integer coordinates between 0 and . The distance between the two points with numbers a and b is said to be the following value:
(the distance calculated by such formula is called Manhattan distance).
We call a hamiltonian path to be some permutation pi of numbers from 1 to n. We say that the length of this path is value .
Find some hamiltonian path with a length of no more than . Note that you do not have to minimize the path length.
The first line contains integer .
The -th line contains the coordinates of the i-th point: \(x_i and y_i (0 ≤ x_i, y_i ≤ 10^6)\).
It is guaranteed that no two points coincide.
Print the permutation of numbers from 1 to n — the sought Hamiltonian path. The permutation must meet the inequality
If there are multiple possible answers, print any of them.
It is guaranteed that the answer exists.
In the sample test the total distance is:
(|5 - 3| + |0 - 4|) + (|3 - 0| + |4 - 7|) + (|0 - 8| + |7 - 10|) + (|8 - 9| + |10 - 12|) = 2 + 4 + 3 + 3 + 8 + 3 + 1 + 2 = 26
# | INPUT.TXT | OUTPUT.TXT |
---|---|---|
1 |
5 0 7 8 10 3 4 5 0 9 12 |
4 3 1 2 5 |
D. Frequent Flyer Flights
Xotira: 32 MB, Vaqt: 1000 msIn the country there are exactly n cities numbered with positive integers from 1 to n. In each city there is an airport is located.
Also, there is the only one airline, which makes flights. Unfortunately, to use them, you need to be a regular customer of this company, namely, you have the opportunity to enjoy flight from city to city only if you have already made at least flights before that.
Please note that flight flies exactly from city to city . It can not be used to fly from city to city . An interesting fact is that there may possibly be recreational flights with a beautiful view of the sky, which begin and end in the same city.
You need to get from city 1 to city n. Unfortunately, you've never traveled by plane before. What minimum number of flights you have to perform in order to get to city n?
Note that the same flight can be used multiple times.
The first line contains two integers, — the number of cities in the country and the number of flights the company provides.
Next m lines contain numbers , representing flight number i from city to city , accessible to only the clients who have made at least flights.
Print "Impossible" (without the quotes), if it is impossible to get from city 1 to city n using the airways.
But if there is at least one way, print a single integer — the minimum number of flights you need to make to get to the destination point.
# | INPUT.TXT | OUTPUT.TXT |
---|---|---|
1 |
3 2 1 2 0 2 3 1 |
2 |
2 |
2 1 1 2 100500 |
Impossible |
3 |
3 3 2 1 0 2 3 6 1 2 0 |
8 |
E. Outlining Borders
Xotira: 60 MB, Vaqt: 6000 msYou are given an undirected graph consisting of ertices and edges. The vertices are numbered with integers from 1 to , the edges are numbered with integers from 1 to . Each edge can be unpainted or be painted in one of the colors, which are numbered with integers from 1 to . Initially, none of the edges is painted in any of the colors.
You get queries of the form "Repaint edge to color ". At any time the graph formed by the edges of the same color must be bipartite. If after the repaint this condition is violated, then the query is considered to be invalid and edge keeps its color. Otherwise, edge is repainted in color , and the query is considered to valid.
Recall that the graph is called bipartite if the set of its vertices can be divided into two parts so that no edge connected vertices of the same parts.
For example, suppose you are given a triangle graph, that is a graph with three vertices and edges (1, 2), (2, 3) and (3, 1). Suppose that the first two edges are painted color 1, and the third one is painted color 2. Then the query of "repaint the third edge in color 1" will be incorrect because after its execution the graph formed by the edges of color 1 will not be bipartite. On the other hand, it is possible to repaint the second edge in color 2.
You receive queries. For each query, you should either apply it, and report that the query is valid, or report that the query is invalid.
The first line contains integers — the number of vertices, the number of edges, the number of colors and the number of queries.
Then follow edges of the graph in the form
Then follow queries of the form
It is guaranteed that the graph doesn't contain multiple edges and loops.
For each query print "YES" (without the quotes), if it is valid, or "NO" (without the quotes), if this query destroys the bipartivity of the graph formed by the edges of some color.
# | INPUT.TXT | OUTPUT.TXT |
---|---|---|
1 |
3 3 2 5 1 2 2 3 1 3 1 1 2 1 3 2 3 1 2 2 |
YES YES YES NO YES |
F. Kamron and Binary String (Easy Version)
Xotira: 256 MB, Vaqt: 2000 msThis is the easy version of the problem. The difference between the versions is that in this version, string tt consists of only '0' and '1'. You can hack only if you solved all versions of this problem.
Kamron has a binary string of length . Kamron can perform the following operation:
- Choose two adjacent blocks of and swap them.
A block is a maximal of identical characters. Formally, denote as the substring A block is satisfying:
Adjacent blocks are two blocks and satisfying .
For example, if , Kamron can choose the two blocks and and swap them, transforming into .
Given a string t of length n consisting of '0', '1' and '?', Kamron wants to determine the minimum number of operations required to perform such that for any index , if then . If it is impossible, output .
∗∗A string a is a substring of a string b if a can be obtained from b by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Each test contains multiple test cases. The first line contains the number of test cases . The description of the test cases follows.
The first line of each test case contains a string s consisting of '0' and '1'.
The second line of each test case contains a string t consisting of '0' and '1'.
It is guaranteed that the lengths of s and tt are the same.
It is guaranteed that the sum of the length of ss over all test cases will not exceed .
For each test case, output one integer — the minimum number of operations required. If it is impossible, output −1.
In the first test case, the possible way is shown in the statement.
In the second test case, one possible way could be:
- Swap blocks , s will become .
- Swap blocks , s will become .
- Swap blocks , s will become .
# | INPUT.TXT | OUTPUT.TXT |
---|---|---|
1 |
6 0001100111 0000011111 010101 111000 0101 0110 0101 1010 011001 001110 0 1 |
1 3 1 -1 -1 -1 |
G. Kamron and Binary String (Hard Version)
Xotira: 32 MB, Vaqt: 1000 msThis is the hard version of the problem. The difference between the versions is that in this version, string tt consists of '0', '1' and '?'. You can hack only if you solved all versions of this problem.
Kamron has a binary string of length . Kamron can perform the following operation:
- Choose two adjacent blocks of and swap them.
A block is a maximal of identical characters. Formally, denote as the substring A block is satisfying:
Adjacent blocks are two blocks and satisfying .
For example, if , Kamron can choose the two blocks and and swap them, transforming into .
Given a string t of length n consisting of '0', '1' and '?', Kamron wants to determine the minimum number of operations required to perform such that for any index , if then . If it is impossible, output .
∗∗A string a is a substring of a string b if a can be obtained from b by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Each test contains multiple test cases. The first line contains the number of test cases . The description of the test cases follows.
The first line of each test case contains a string s consisting of '0' and '1'.
The second line of each test case contains a string t consisting of '0' and '1'.
It is guaranteed that the lengths of s and tt are the same.
It is guaranteed that the sum of the length of ss over all test cases will not exceed .
For each test case, output one integer — the minimum number of operations required. If it is impossible, output −1.
In the first test case, the possible way is shown in the statement.
In the second test case, one possible way could be:
- Swap blocks , s will become .
- Swap blocks , s will become .
- Swap blocks , s will become .
In the first test case of the second example, one possible way could be:
- Swap blocks , will become .
- Swap blocks , will become .
# | INPUT.TXT | OUTPUT.TXT |
---|---|---|
1 |
6 0001100111 0000011111 010101 111000 0101 0110 0101 1010 011001 001110 0 1 |
1 3 1 -1 -1 -1 |
2 |
6 010101 ?0?0?? 0101 ?0?0 11100101 ???????? 11100101 ???11?1? 1000100011 ?11?000?0? 10101 ?1011 |
2 -1 0 2 2 -1 |