Submission #12095

# Submission time Handle Problem Language Result Execution time Memory
12095 2014-12-21T04:55:25 Z ainu7 Min-cost GCD (GA9_mcg) C++
0 / 100
660 ms 1720 KB
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <set>
using namespace std;

long long comp(long long a, long long b, long long p, long long q) {
  if (1.0*b*q > a*p*2) return a*p;
  return min(a*p, b*q);
}

int main()
{
  int T;
  cin >> T;
  for (int i=0; i<T; i++) {
    long long a, b, p, q;
    cin >> a >> b >> p >> q;
    long long res = 0;
    while (a && b && (a % b)) {
      long long c = a % b;
      long long d = b % c;
      res += comp(2, a/b + b/c, p, q);
      a = c;
      b = d;
    }
    if (b)
      res += comp(1, a/b, p, q);
    cout << res << endl;
  }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1720 KB Output is correct
2 Incorrect 0 ms 1720 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 340 ms 1720 KB Output is correct
2 Correct 360 ms 1720 KB Output is correct
3 Incorrect 396 ms 1720 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 1720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 660 ms 1720 KB Output isn't correct
2 Halted 0 ms 0 KB -