-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABC_180_D.cpp
57 lines (46 loc) · 941 Bytes
/
ABC_180_D.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
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
using namespace std;
int main()
{
unsigned long long x, y, a, b;
unsigned long long ans = 0;
cin >> x >> y >> a >> b;
queue< pair<unsigned long long, unsigned long long> >que;
que.push(make_pair(x, 0));
while (!que.empty())
{
unsigned long long str = que.front().first;
unsigned long long exp = que.front().second;
que.pop();
//cout << str << ' ' << exp << endl;
if (str >= y)
{
continue;
}
ans = max(ans, exp);
// overflow
if (!(ULLONG_MAX / a < str))
{
// multiple
que.push(make_pair(str * a, exp + 1));
}
// add....
unsigned long long c = (y - str) / b;
if (c == 0)
{
que.push(make_pair(str + b, exp + 1));
}
else if ((y - str) % b == 0 && c > 1) {
c--;
que.push(make_pair(str + b * c, exp + c));
}
else
{
que.push(make_pair(str + b * c, exp + c));
}
}
cout << ans << endl;
}