fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. ll x, y, d;
  6.  
  7. void extendedEuclid(ll a, ll b) {
  8. if (b == 0) {
  9. x = 1;
  10. y = 0;
  11. d = a;
  12. return;
  13. }
  14. extendedEuclid(b, a % b);
  15. ll y1 = x - (a / b) * y;
  16. x = y;
  17. y = y1;
  18. }
  19.  
  20. int main() {
  21. ll v, n1, n2, c1, c2;
  22. while (cin >> v && v) {
  23. cin >> c1 >> n1 >> c2 >> n2;
  24. extendedEuclid(n1, n2);
  25.  
  26. if (v % d != 0) {
  27. cout << "failed\n";
  28. } else {
  29. x *= v / d;
  30. y *= v / d;
  31.  
  32. n2 /= d;
  33. n1 /= d;
  34.  
  35. ll l = ceil(-(double)x / n2);
  36. ll r = floor((double)y / n1);
  37.  
  38. if (l <= r) {
  39. ll x1 = x + n2 * l;
  40. ll y1 = y - n1 * l;
  41. ll cost1 = c1 * x1 + c2 * y1;
  42.  
  43. ll x2 = x + n2 * r;
  44. ll y2 = y - n1 * r;
  45. ll cost2 = c1 * x2 + c2 * y2;
  46.  
  47. if (cost1 < cost2) {
  48. cout << x1 << " " << y1 << '\n';
  49. } else {
  50. cout << x2 << " " << y2 << '\n';
  51. }
  52. } else {
  53. cout << "failed\n";
  54. }
  55. }
  56. }
  57. return 0;
  58. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty