# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
58820 | 2018-07-19T14:47:20 Z | sansae | 물통 (KOI17_bucket) | C++14 | 4 ms | 728 KB |
#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++; } } 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+=3; } } else if(d==e){ if(aa!=c){ ans2++; } } } printf("%d", min(ans1, ans2)); } } } }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 248 KB | Output is correct |
2 | Correct | 2 ms | 484 KB | Output is correct |
3 | Correct | 4 ms | 484 KB | Output is correct |
4 | Correct | 2 ms | 484 KB | Output is correct |
5 | Correct | 2 ms | 484 KB | Output is correct |
6 | Correct | 2 ms | 484 KB | Output is correct |
7 | Correct | 4 ms | 484 KB | Output is correct |
8 | Correct | 3 ms | 500 KB | Output is correct |
9 | Correct | 3 ms | 584 KB | Output is correct |
10 | Correct | 3 ms | 584 KB | Output is correct |
11 | Correct | 3 ms | 592 KB | Output is correct |
12 | Correct | 3 ms | 592 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 248 KB | Output is correct |
2 | Correct | 2 ms | 484 KB | Output is correct |
3 | Correct | 4 ms | 484 KB | Output is correct |
4 | Correct | 2 ms | 484 KB | Output is correct |
5 | Correct | 2 ms | 484 KB | Output is correct |
6 | Correct | 2 ms | 484 KB | Output is correct |
7 | Correct | 4 ms | 484 KB | Output is correct |
8 | Correct | 3 ms | 500 KB | Output is correct |
9 | Correct | 3 ms | 584 KB | Output is correct |
10 | Correct | 3 ms | 584 KB | Output is correct |
11 | Correct | 3 ms | 592 KB | Output is correct |
12 | Correct | 3 ms | 592 KB | Output is correct |
13 | Correct | 3 ms | 592 KB | Output is correct |
14 | Correct | 3 ms | 592 KB | Output is correct |
15 | Correct | 3 ms | 628 KB | Output is correct |
16 | Correct | 2 ms | 628 KB | Output is correct |
17 | Correct | 3 ms | 628 KB | Output is correct |
18 | Correct | 3 ms | 628 KB | Output is correct |
19 | Correct | 2 ms | 628 KB | Output is correct |
20 | Correct | 2 ms | 628 KB | Output is correct |
21 | Correct | 3 ms | 592 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 628 KB | Output is correct |
2 | Correct | 3 ms | 628 KB | Output is correct |
3 | Correct | 3 ms | 628 KB | Output is correct |
4 | Correct | 2 ms | 628 KB | Output is correct |
5 | Correct | 3 ms | 628 KB | Output is correct |
6 | Correct | 3 ms | 628 KB | Output is correct |
7 | Correct | 2 ms | 628 KB | Output is correct |
8 | Correct | 2 ms | 628 KB | Output is correct |
9 | Correct | 2 ms | 628 KB | Output is correct |
10 | Correct | 3 ms | 628 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 248 KB | Output is correct |
2 | Correct | 2 ms | 484 KB | Output is correct |
3 | Correct | 4 ms | 484 KB | Output is correct |
4 | Correct | 2 ms | 484 KB | Output is correct |
5 | Correct | 2 ms | 484 KB | Output is correct |
6 | Correct | 2 ms | 484 KB | Output is correct |
7 | Correct | 4 ms | 484 KB | Output is correct |
8 | Correct | 3 ms | 500 KB | Output is correct |
9 | Correct | 3 ms | 584 KB | Output is correct |
10 | Correct | 3 ms | 584 KB | Output is correct |
11 | Correct | 3 ms | 592 KB | Output is correct |
12 | Correct | 3 ms | 592 KB | Output is correct |
13 | Correct | 3 ms | 592 KB | Output is correct |
14 | Correct | 3 ms | 592 KB | Output is correct |
15 | Correct | 3 ms | 628 KB | Output is correct |
16 | Correct | 2 ms | 628 KB | Output is correct |
17 | Correct | 3 ms | 628 KB | Output is correct |
18 | Correct | 3 ms | 628 KB | Output is correct |
19 | Correct | 2 ms | 628 KB | Output is correct |
20 | Correct | 2 ms | 628 KB | Output is correct |
21 | Correct | 2 ms | 628 KB | Output is correct |
22 | Correct | 3 ms | 628 KB | Output is correct |
23 | Correct | 3 ms | 628 KB | Output is correct |
24 | Correct | 2 ms | 628 KB | Output is correct |
25 | Correct | 3 ms | 628 KB | Output is correct |
26 | Correct | 3 ms | 628 KB | Output is correct |
27 | Correct | 2 ms | 628 KB | Output is correct |
28 | Correct | 2 ms | 628 KB | Output is correct |
29 | Correct | 2 ms | 628 KB | Output is correct |
30 | Correct | 3 ms | 628 KB | Output is correct |
31 | Correct | 3 ms | 628 KB | Output is correct |
32 | Correct | 3 ms | 628 KB | Output is correct |
33 | Correct | 4 ms | 628 KB | Output is correct |
34 | Correct | 2 ms | 628 KB | Output is correct |
35 | Correct | 3 ms | 636 KB | Output is correct |
36 | Correct | 3 ms | 636 KB | Output is correct |
37 | Correct | 3 ms | 636 KB | Output is correct |
38 | Correct | 4 ms | 688 KB | Output is correct |
39 | Correct | 4 ms | 688 KB | Output is correct |
40 | Correct | 2 ms | 688 KB | Output is correct |
41 | Correct | 2 ms | 728 KB | Output is correct |
42 | Correct | 2 ms | 728 KB | Output is correct |
43 | Correct | 2 ms | 728 KB | Output is correct |
44 | Correct | 3 ms | 728 KB | Output is correct |
45 | Correct | 2 ms | 728 KB | Output is correct |
46 | Correct | 3 ms | 592 KB | Output is correct |