Codeforces Round #547 (Div. 3)
A. Game 23
Description
Polycarp plays "Game 23". Initially he has a number 𝑛n and his goal is to transform it to 𝑚m. In one move, he can multiply 𝑛n by 22 or multiply 𝑛n by 33. He can perform any number of moves.
Print the number of moves needed to transform 𝑛n to 𝑚m. Print -1 if it is impossible to do so.
It is easy to prove that any way to transform 𝑛n to 𝑚m contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation).
Input
The only line of the input contains two integers 𝑛n and 𝑚m (1≤𝑛≤𝑚≤5⋅1081≤n≤m≤5⋅108).
Output
Print the number of moves to transform 𝑛n to 𝑚m, or -1 if there is no solution.
Examples
Input
120 51840
Output
7
C. Polycarp Restores Permutation
Description
An array of integers 𝑝1,𝑝2,…,𝑝𝑛p1,p2,…,pn is called a permutation if it contains each number from 11 to 𝑛n exactly once. For example, the following arrays are permutations: [3,1,2][3,1,2], [1][1], [1,2,3,4,5][1,2,3,4,5] and [4,3,1,2][4,3,1,2]. The following arrays are not permutations: [2][2], [1,1][1,1], [2,3,4][2,3,4].
Polycarp invented a really cool permutation 𝑝1,𝑝2,…,𝑝𝑛p1,p2,…,pn of length 𝑛n. It is very disappointing, but he forgot this permutation. He only remembers the array 𝑞1,𝑞2,…,𝑞𝑛−1q1,q2,…,qn−1 of length 𝑛−1n−1, where 𝑞𝑖=𝑝𝑖+1−𝑝𝑖qi=pi+1−pi.
Given 𝑛n and 𝑞=𝑞1,𝑞2,…,𝑞𝑛−1q=q1,q2,…,qn−1, help Polycarp restore the invented permutation.
Input
The first line contains the integer 𝑛n (2≤𝑛≤2⋅1052≤n≤2⋅105) — the length of the permutation to restore. The second line contains 𝑛−1n−1 integers 𝑞1,𝑞2,…,𝑞𝑛−1q1,q2,…,qn−1 (−𝑛<𝑞𝑖<𝑛−n<qi<n).
Output
Print the integer -1 if there is no such permutation of length 𝑛n which corresponds to the given array 𝑞q. Otherwise, if it exists, print 𝑝1,𝑝2,…,𝑝𝑛p1,p2,…,pn. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
A. Vova and Train
Description
Vova plans to go to the conference by train. Initially, the train is at the point 11 and the destination point of the path is the point LL. The speed of the train is 11 length unit per minute (i.e. at the first minute the train is at the point 11, at the second minute — at the point 22 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by vv (i.e. the first lantern is at the point vv, the second is at the point 2v2v and so on).
There is also exactly one standing train which occupies all the points from ll to rr inclusive.
Vova can see the lantern at the point pp if pp is divisible by vv and there is no standing train at this position (p∉[l;r]p∉[l;r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to tt different conferences, so you should answer tt independent queries.
Input
The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of queries.
Then tt lines follow. The ii-th line contains four integers Li,vi,li,riLi,vi,li,ri (1≤L,v≤1091≤L,v≤109, 1≤l≤r≤L1≤l≤r≤L) — destination point of the ii-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print tt lines. The ii-th line should contain one integer — the answer for the ii-th query.
Examples
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
A. Vova and Train
Description
Vova plans to go to the conference by train. Initially, the train is at the point 11 and the destination point of the path is the point LL. The speed of the train is 11 length unit per minute (i.e. at the first minute the train is at the point 11, at the second minute — at the point 22 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by vv (i.e. the first lantern is at the point vv, the second is at the point 2v2v and so on).
There is also exactly one standing train which occupies all the points from ll to rr inclusive.
Vova can see the lantern at the point pp if pp is divisible by vv and there is no standing train at this position (p∉[l;r]p∉[l;r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to tt different conferences, so you should answer tt independent queries.
Input
The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of queries.
Then tt lines follow. The ii-th line contains four integers Li,vi,li,riLi,vi,li,ri (1≤L,v≤1091≤L,v≤109, 1≤l≤r≤L1≤l≤r≤L) — destination point of the ii-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print tt lines. The ii-th line should contain one integer — the answer for the ii-th query.
Examples
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0