You are given a ternary string (it is a string which consists only of characters '0', '1' and '2').
You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace "01" with "10" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace "12" with "21" or vice versa).
For example, for string "010210" we can perform the following moves:
Note than you cannot swap "02" →→ "20" and vice versa. You cannot perform any other operations with the given string excluding described above.
You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).
String aa is lexicographically less than string bb (if strings aa and bb have the same length) if there exists some position ii (1≤i≤|a|1≤i≤|a|, where |s||s| is the length of the string ss) such that for every j<ij holds aj=bjaj=bj, and ai<biai
The first line of the input contains the string ss consisting only of characters '0', '1' and '2', its length is between 11 and 105105 (inclusive).
Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).
100210
001120
11222121
11112222
20
20
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
Alice got an array of length nn as a birthday present once again! This is the third year in a row!
And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice.
Bob has chosen mm changes of the following form. For some integer numbers xx and dd, he chooses an arbitrary position ii (1≤i≤n1≤i≤n) and for every j∈[1,n]j∈[1,n] adds x+d⋅dist(i,j)x+d⋅dist(i,j) to the value of the jj-th cell. dist(i,j)dist(i,j) is the distance between positions ii and jj (i.e. dist(i,j)=|i−j|dist(i,j)=|i−j|, where |x||x| is an absolute value of xx).
For example, if Alice currently has an array [2,1,2,2][2,1,2,2] and Bob chooses position 33 for x=−1x=−1 and d=2d=2 then the array will become [2−1+2⋅2, 1−1+2⋅1, 2−1+2⋅0, 2−1+2⋅1][2−1+2⋅2, 1−1+2⋅1, 2−1+2⋅0, 2−1+2⋅1] = [5,2,1,3][5,2,1,3]. Note that Bob can't choose position ii outside of the array (that is, smaller than 11 or greater than nn).
Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric.
What is the maximum arithmetic mean value Bob can achieve?
The first line contains two integers nn and mm (1≤n,m≤1051≤n,m≤105) — the number of elements of the array and the number of changes.
Each of the next mm lines contains two integers xixi and didi (−103≤xi,di≤103−103≤xi,di≤103) — the parameters for the ii-th change.
Print the maximal average arithmetic mean of the elements Bob can achieve.
Your answer is considered correct if its absolute or relative error doesn't exceed 10−610−6.
2 3 -1 3 0 0 -1 -4
-2.500000000000000
3 2 0 2 5 0
7.000000000000000
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E(v,u)∈E GCD(v,u)=1GCD(v,u)=1 (the greatest common divisor of vv and uu is 11). If there is no edge between some pair of vertices vv and uu then the value of GCD(v,u)GCD(v,u) doesn't matter. The vertices are numbered from 11 to |V||V|.
Construct a relatively prime graph with nn vertices and mm edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
The only line contains two integers nn and mm (1≤n,m≤1051≤n,m≤105) — the number of vertices and the number of edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The ii-th of the next mm lines should contain the ii-th edge (vi,ui)(vi,ui) of the resulting graph (1≤vi,ui≤n,vi≠ui1≤vi,ui≤n,vi≠ui). For each pair (v,u)(v,u) there can be no more pairs (v,u)(v,u) or (u,v)(u,v). The vertices are numbered from 11 to nn.
If there are multiple answers then print any of them.
5 6
Possible 2 5 3 2 5 1 3 4 4 1 5 4
6 12
Impossible
Here is the representation of the graph from the first example:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is nn km. Let's say that Moscow is situated at the point with coordinate 00 km, and Saratov — at coordinate nn km.
Driving for a long time may be really difficult. Formally, if Leha has already covered ii kilometers since he stopped to have a rest, he considers the difficulty of covering (i+1)(i+1)-th kilometer as ai+1ai+1. It is guaranteed that for every i∈[1,n−1]i∈[1,n−1] ai≤ai+1ai≤ai+1. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 11 to n−1n−1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a1a1, the kilometer after it — difficulty a2a2, and so on.
For example, if n=5n=5 and there is a rest site in coordinate 22, the difficulty of journey will be 2a1+2a2+a32a1+2a2+a3: the first kilometer will have difficulty a1a1, the second one — a2a2, then Leha will have a rest, and the third kilometer will have difficulty a1a1, the fourth — a2a2, and the last one — a3a3. Another example: if n=7n=7 and there are rest sites in coordinates 11 and 55, the difficulty of Leha's journey is 3a1+2a2+a3+a43a1+2a2+a3+a4.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2n−12n−1 different distributions of rest sites (two distributions are different if there exists some point xx such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate pp — the expected value of difficulty of his journey.
Obviously, p⋅2n−1p⋅2n−1 is an integer number. You have to calculate it modulo 998244353998244353.
The first line contains one number nn (1≤n≤1061≤n≤106) — the distance from Moscow to Saratov.
The second line contains nn integer numbers a1a1, a2a2, ..., anan (1≤a1≤a2≤⋯≤an≤1061≤a1≤a2≤⋯≤an≤106), where aiai is the difficulty of ii-th kilometer after Leha has rested.
Print one number — p⋅2n−1p⋅2n−1, taken modulo 998244353998244353.
2 1 2
5
4 1 3 3 7
60
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include