-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSB.cpp
78 lines (64 loc) · 1.04 KB
/
MSB.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
int main() {
ll t;
cin>>t;
while(t--)
{
ll a,b,x,y;
cin>>a>>b>>x>>y;
if(a*x==a and b/y==b and a<b)
{
cout<<"-1"<<endl;
continue;
}
//if x and y are equal
int cnt=0;
if(x==y)
{
int cnt1=0;
int cnt2=0;
ll _a=a;
ll _b=b;
//dividing
while(_b>a)
{
cnt1++;
_b/=y;
}
//multiplying
while(_a<b)
{
cnt2++;
_a*=x;
if(_a>=b)
break;
if(b/_a + bool((b%_a)) <= x){
++cnt2;break;
}
}
cout<<min(cnt2,cnt1)<<endl;
continue;
}
if(y > x){
while(a < b){
b /= y;
++cnt;
}
}
else{
while(a < b){
a *= x;
++cnt;
if(a >=b)break;
if(b/a + bool((b%a))<= x){
++cnt;break;
}
}
}
cout<<cnt<<endl;
}
return 0;
}