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𝑛𝑚51081≤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

Note

In the first example, the possible sequence of moves is: 12024072014404320129602592051840.120→240→720→1440→4320→12960→25920→51840. The are 77 steps in total.

正确解法:

发现自己总有一种把难题想得特别复杂的那种,总之就是不敢去放手写。下一阶段要改正,不敢写也要写,哪怕错,哪怕超时,总能发现和正解错在哪里。

大半夜写CF神志不清。A题写不出来真是人才。

求m/n的 2的个数和3的个数,就是他可能无法变换乘几个2和几个3相乘。

 

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𝑛21052≤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 

正确解法:

设ans[1]=x,则ans[2]=x+p[1],ans[3]=x+p[1]+p[2],ans[4]=x+p[1]+p[2]+p[3];

则其中最小的就是1.因为是1-n的序列。

我们手上有p的序列,然后求前缀和。得到 { 0, p[1], p[1]+p[2],.....}

最小的与1求差值。 x+minn=1.  x=1-minn。

然后所有都加上x就是答案。

 1 include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef long long ll;
 8 const int inf=0x7fffffff;
 9 const int N=200000+100;
10 int n,a[N],b[N],minn=0;
11 int main()
12 {
13     scanf("%d",&n);
14     for(int i=2;i<=n;i++)
15     {
16         scanf("%d",&a[i]);
17         a[i]=a[i-1]+a[i];
18         minn=min(minn,a[i]);
19     }
20     minn=-minn+1;
21     for(int i=1;i<=n;i++)
22     {
23         a[i]+=minn;
24         b[i]=a[i];
25     }
26     sort(b+1,b+n+1);
27     for(int i=1;i<=n;i++)
28         if(b[i]!=i)
29         {
30             cout<<-1<<endl;
31             return 0;
32         }
33     for(int i=1;i<=n;i++)
34         cout<<a[i]<<" ";
35     cout<<endl;
36 
37     return 0;
38 }
View Code

 

 

 

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 tindependent queries.

Input

The first line of the input contains one integer tt (1t1041≤t≤104) — the number of queries.

Then tt lines follow. The ii-th line contains four integers Li,vi,li,riLi,vi,li,ri (1L,v1091≤L,v≤109, 1lrL1≤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

Note

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 tindependent queries.

Input

The first line of the input contains one integer tt (1t1041≤t≤104) — the number of queries.

Then tt lines follow. The ii-th line contains four integers Li,vi,li,riLi,vi,li,ri (1L,v1091≤L,v≤109, 1lrL1≤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

Note

 
 
posted @ 2019-03-21 17:20  kaike  阅读(434)  评论(0编辑  收藏  举报