# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
58818 | sansae | 물통 (KOI17_bucket) | C++14 | 4 ms | 840 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <algorithm>
#include <functional>
using namespace std;
int gcd(int a, int b){
if(a==0){
return b;
}
else if(b==0){
return a;
}
else{
return a<b ? gcd(a, b%a) : gcd(b, a%b);
}
}
int main(void){
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
if(c!=0 && d!=0 && c!=a && d!=b){
printf("-1");
return 0;
}
else{
int g=gcd(a, b);
if(c%g!=0 || d%g!=0){
printf("-1");
return 0;
}
else{
c/=g;
d/=g;
a/=g;
b/=g;
if(c==0 && d==0){
printf("0");
return 0;
}
else if(c==a && d==0){
printf("1");
return 0;
}
else if(c==0 && d==b){
printf("1");
return 0;
}
else if(c==a && d==b){
printf("2");
return 0;
}
else{
int e;
if(c!=0 && c!=a){
e=c;
}
if(d!=0 && d!=b){
e=d;
}
int aa, bb;
int ans1=0;
int ans2=0;
aa=0;
bb=0;
while(aa!=e && bb!=e){
if(aa==0){
aa=a;
ans1++;
}
else if(aa==a){
if(aa+bb>=b){
aa=aa+bb-b;
bb=b;
}
else{
aa=0;
bb+=a;
}
ans1++;
}
else if(bb==b){
bb=0;
ans1++;
}
else{
bb=aa;
aa=0;
ans1++;
}
}
if(aa==e){
if(c==e){
if(bb!=d){
ans1++;
}
}
else if(d==e){
if(bb!=0){
ans1++;
}
ans1++;
if(c!=0){
ans1++;
}
}
}
else if(bb==e){
if(c==e){
if(aa!=0){
ans1++;
}
ans1++;
if(d!=0){
ans1++;
}
if(aa==0 && d==0){
ans1--;
}
}
else if(d==e){
if(aa!=c){
ans1++;
}
}
}
aa=0;
bb=0;
while(aa!=e && bb!=e){
if(bb==0){
bb=b;
ans2++;
}
else if(aa==a){
aa=0;
ans2++;
}
else if(aa==0){
if(bb<=a){
aa=bb;
bb=0;
ans2++;
}
else{
bb=bb+aa-a;
aa=a;
ans2++;
}
}
else{
bb=bb+aa-a;
aa=a;
ans2++;
}
}
if(aa==e){
if(c==e){
if(bb!=d){
ans2++;
}
}
else if(d==e){
if(bb==0&&c==0){
ans2++;
}
else if(bb==b && c==0){
ans2+=2;
}
else if(bb==0 && c==a){
ans2+=2;
}
else{
ans2+=3;
}
}
}
else if(bb==e){
if(c==e){
if(aa==0 && d==0){
ans2++;
}
else if(aa==0 && d==b){
ans2+=2;
}
else if(aa==a && d==0){
ans2+=2;
}
else{
ans2+=2;
}
}
else if(d==e){
if(aa!=c){
ans2++;
}
}
}
printf("%d", min(ans1, ans2));
}
}
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |