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

 
 

转载于:https://www.cnblogs.com/Kaike/p/10573035.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值