-
Notifications
You must be signed in to change notification settings - Fork 2
/
POUR1 - Pouring water.cpp
92 lines (88 loc) · 2.05 KB
/
POUR1 - Pouring water.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int x,y,z,t,X,Y;
cin>>t;
while(t--)
{
cin>>X>>Y>>z;
x=max(X,Y);
y=min(X,Y);
//cout<<x<<" "<<y;
if( (x>=z) && (z%(__gcd(x,y))==0))
{
int a,b,count1,count2,d;
a=0;
b=0;
count1=0;
count2=0;
while(1)
{
//cout<<a<<"a ";
if(a==0)
{
a+=x;
count1++;
}
else if(b==y)
{
b-=y;
count1++;
}
else
{
if(a>y-b)
{
a=a-(y-b);
b=y;
}
else
{
b+=a;
a=0;
}
count1++;
}
if(a==z || b==z)
break;
}
a=0;
b=0;
//cout<<count1<<" "<<count2;
while(1)
{
if(b==0)
{
b+=y;
count2++;
}
else if(a==x)
{
a-=x;
count2++;
}
else
{
if(b>x-a)
{
b=b-(x-a);
a=x;
}
else
{
a+=b;
b=0;
}
count2++;
}
if(a==z || b==z )
break;
}
d=min(count1,count2);
cout<<d<<"\n";
}
else
cout<<"-1\n";
}
}