A. Aziz and Sardor's Game

Xotira: 32 MB, Vaqt: 1000 ms
Masala

Aziz and Sardor are playing a simple game. Aziz thought of number xx between 11 and nn, and Sardor tries to guess the number.

Sardor can ask questions like: "Is the unknown number divisible by number yy?".

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 aa 'yes' or aa '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 yiy_i, he should ask the questions about.

Kiruvchi ma'lumotlar:

A single line contains number n(1n103). n (1 ≤ n ≤ 10^3).

Chiquvchi ma'lumotlar:

Print the length of the sequence of questions k(0kn)k (0 ≤ k ≤ n), followed by kk numbers — the questions yi(1yin).y_i (1 ≤ y_i ≤ n).

If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.

Izoh:

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.

Misollar:
# 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 ms
Masala

A tree of size nn is an undirected connected graph consisting of nn vertices without cycles.

Consider some tree with nn vertices. We call a tree invariant relative to permutation p=p1 p2...pnp = p_1  p_2... p_n, 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 pup_u and pvp_v are connected by an edge".

You are given permutation p of size nn. Find some tree size nn, invariant relative to the given permutation.

Kiruvchi ma'lumotlar:

The first line contains number n(1n105)n (1 ≤ n ≤ 10^5) — the size of the permutation (also equal to the size of the sought tree).

The second line contains permutation pi(1pin).p_i (1 ≤ p_i ≤ n).

Chiquvchi ma'lumotlar:

If the sought tree does not exist, print "NO""NO" (without the quotes).

Otherwise, print "YES", and then print n1n - 1 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.

Izoh:

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.

Misollar:
# 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 ms
Masala

On a plane are n points xi,yix_i,y_i with integer coordinates between 0 and 10610^6. The distance between the two points with numbers a and b is said to be the following value: 

dist(a,b)=xaxb+yaybdist(a,b) = |x_a - x_b| + |y_a - y_b|

 (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 .

i=1n1dist(pi,pi+1)∑ ^ {n-1} _ {i=1} dist(p_i, p_{i+1})

Find some hamiltonian path with a length of no more than 25×10825 × 10^8. Note that you do not have to minimize the path length.

Kiruvchi ma'lumotlar:

The first line contains integer n(1n106)n (1 ≤ n ≤ 10^6).

The i+1i+1 -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.


 

Chiquvchi ma'lumotlar:

Print the permutation of numbers pip_i from 1 to n — the sought Hamiltonian path. The permutation must meet the inequality

i=1n1dist(pi,pi+1)<=25108​  ​ ∑ ^ {n-1} _ {i=1} dist(p_i, p_{i+1}) <= 25 * 10^8 ​    ​

If there are multiple possible answers, print any of them.

It is guaranteed that the answer exists.

Izoh:

In the sample test the total distance is:

dist(4,3)+dist(3,1)+dist(1,2)+dist(2,5)=dist(4,3) + dist(3,1) + dist(1,2) + dist(2,5) =

(|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

Misollar:
# 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 ms
Masala

In 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 mm flights. Unfortunately, to use them, you need to be a regular customer of this company, namely, you have the opportunity to enjoy flight ii from city aia_i to city bib_i only if you have already made at least did_i flights before that.

Please note that flight ii flies exactly from city aia_i to city bib_i. It can not be used to fly from city bib_i to city aia_i. 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.

Kiruvchi ma'lumotlar:

The first line contains two integers, n and m(2n150,1m150)n  and   m (2 ≤ n ≤ 150, 1 ≤ m ≤ 150) — the number of cities in the country and the number of flights the company provides.

Next m lines contain numbers ai,bi,di(1ai,bin,0di109)a_i, b_i, d_i (1 ≤ a_i, b_i ≤ n, 0 ≤ d_i ≤ 10^9), representing flight number i from city aia_i to city bib_i, accessible to only the clients who have made at least did_i flights.

Chiquvchi ma'lumotlar:

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.

Misollar:
# 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 ms
Masala

You are given an undirected graph consisting of nn ertices and mm edges. The vertices are numbered with integers from 1 to nn, the edges are numbered with integers from 1 to mm. Each edge can be unpainted or be painted in one of the kk colors, which are numbered with integers from 1 to kk. Initially, none of the edges is painted in any of the colors.

You get queries of the form "Repaint edge eie_i to color cic_i". 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 eie_i keeps its color. Otherwise, edge eie_i is repainted in color cic_i, 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 qq queries. For each query, you should either apply it, and report that the query is valid, or report that the query is invalid.

Kiruvchi ma'lumotlar:

The first line contains integers n,m,k,q(2n5105,1m,q5105,1k50)n, m, k, q (2 ≤ n ≤ 5·10^5, 1 ≤ m, q ≤ 5·10^5, 1 ≤ k ≤ 50) — the number of vertices, the number of edges, the number of colors and the number of queries.

Then follow mm edges of the graph in the form ai,bi(1ai,bin).a_i, b_i (1 ≤ a_i, b_i ≤ n).

Then follow qq queries of the form ei,ci(1eim,1cik).e_i, c_i (1 ≤ e_i ≤ m, 1 ≤ c_i ≤ k).

It is guaranteed that the graph doesn't contain multiple edges and loops.

Chiquvchi ma'lumotlar:

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.

Misollar:
# 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 ms
Masala

This 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 ss of length nn. Kamron can perform the following operation:

  • Choose two adjacent blocks of ss and swap them.

A block is a maximal substringsubstring∗ of identical characters. Formally, denote s[l,r]s[l,r] as the substring slsl+1sr.s_ls_{l+1}…s_r. A block is s[l,r]s[l,r] satisfying:

  • l=1orslsl1.l=1 or s_l≠s_{l-1}.
  • sl=sl+1==sr.s_l=s_{l+1}=…=s_r.
  • r=norsrsr+1.r=n or s_r≠s_{r+1}.

Adjacent blocks are two blocks s[l1,r1]s[l_1,r_1] and s[l2,r2]s[l_2,r_2] satisfying r1+1=l2r_1+1=l_2.

For example, if s=0001100111s=0001100111, Kamron can choose the two blocks s[4,5]s[4,5] and s[6,7]s[6,7] and swap them, transforming ss into 00000111110000011111.

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 i(1in)i (1≤i≤n), if ti?t_i≠ '?' then si=tis_i=t_i. If it is impossible, output 1−1.

∗∗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.
Kiruvchi ma'lumotlar:

Each test contains multiple test cases. The first line contains the number of test cases t(1t104)t (1≤t≤10^4). 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 41054⋅10^5.

Chiquvchi ma'lumotlar:

For each test case, output one integer — the minimum number of operations required. If it is impossible, output −1.

Izoh:

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 [2,2],[3,3][2,2],[3,3], s will become 001101001101.
  • Swap blocks [3,4],[5,5][3,4],[5,5], s will become 000111000111.
  • Swap blocks [1,3],[4,6][1,3],[4,6], s will become 111000111000.

 


 

Misollar:
# 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 ms
Masala

This 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 ss of length nn. Kamron can perform the following operation:

  • Choose two adjacent blocks of ss and swap them.

A block is a maximal substringsubstring∗ of identical characters. Formally, denote s[l,r]s[l,r] as the substring slsl+1sr.s_ls_{l+1}…s_r. A block is s[l,r]s[l,r] satisfying:

  • l=1orslsl1.l=1 or s_l≠s_{l-1}.
  • sl=sl+1==sr.s_l=s_{l+1}=…=s_r.
  • r=norsrsr+1.r=n or s_r≠s_{r+1}.

Adjacent blocks are two blocks s[l1,r1]s[l_1,r_1] and s[l2,r2]s[l_2,r_2] satisfying r1+1=l2r_1+1=l_2.

For example, if s=0001100111s=0001100111, Kamron can choose the two blocks s[4,5]s[4,5] and s[6,7]s[6,7] and swap them, transforming ss into 00000111110000011111.

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 i(1in)i (1≤i≤n), if ti?t_i≠ '?' then si=tis_i=t_i. If it is impossible, output 1−1.

∗∗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.
Kiruvchi ma'lumotlar:

Each test contains multiple test cases. The first line contains the number of test cases t(1t104)t (1≤t≤10^4). 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 41054⋅10^5.

Chiquvchi ma'lumotlar:

For each test case, output one integer — the minimum number of operations required. If it is impossible, output −1.

Izoh:

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 [2,2],[3,3][2,2],[3,3], s will become 001101001101.
  • Swap blocks [3,4],[5,5][3,4],[5,5], s will become 000111000111.
  • Swap blocks [1,3],[4,6][1,3],[4,6], s will become 111000111000.

In the first test case of the second example, one possible way could be:

  • Swap blocks [1,1],[2,2][1,1],[2,2], ss will become 100101100101.
  • Swap blocks [4,4],[5,5][4,4],[5,5], ss will become 100011100011.

 


 

Misollar:
# 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
Kitob yaratilingan sana: 05-Jul-25 08:38