text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int main() { long long n, p, w, d; cin >> n >> p >> w >> d; for (long long i = 0; i <= w; i++) { long long temp1 = p - i * d, temp2 = -100; if (temp1 % w == 0) { temp2 = (temp1 / w); } if (temp2 >= 0 && i + temp2 <= n) { cout << temp2 << " " << i << " " << n - i - temp2 << endl; return 0; } } puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p, w, d; cin >> n >> p >> w >> d; long long x = p / w, y = 0, z = 0; if (p % w) { x++; } if (p == x * w + y * d) { z = n - x - y; cout << x << " " << y << " " << z; } else { if (w % d == 0) { if (p % d) { cout << -1; return 0; } } long long t = p; for (y = 0; t >= 0; t -= d, y++) { if (t >= 0 && t % w == 0) break; } if (t < 0) { cout << -1; } else { x = t / w; if (x + y > n) { cout << -1; } else { z = n - x - y; cout << x << " " << y << " " << z; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 250050; int n, m; void ex_gcd(long long &x, long long &y, long long a, long long b) { if (b == 0) { x = 1; y = 0; } else { ex_gcd(x, y, b, a % b); long long temp = x; x = y; y = temp - a / b * y; } } int main() { int T = 1; while (T--) { long long n, p, d, w; cin >> n >> p >> w >> d; long long x, y; for (long long i = 0; i < w; i++) { if ((p - d * i) % w == 0) { x = (p - d * i) / w, y = i; if (x >= 0 && y >= 0 && x + y <= n) { cout << x << " " << y << " " << n - x - y << endl; return 0; } } } puts("-1"); } }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const long long mod = 1e9 + 7; signed main() { long long n, p, w, d; cin >> n >> p >> w >> d; for (long long i = p / w; i >= p / w - d; i--) { if (i >= 0 && n >= i + (p - w * i) / d && (p - w * i) % d == 0 && (p - w * i) >= 0) { cout << i << ' ' << (p - w * i) / d << ' ' << n - i - (p - w * i) / d; return 0; } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = (int)2e9; signed main() { cin.tie(0); ios::sync_with_stdio(0); long long n, p, w, d; cin >> n >> p >> w >> d; if (p == 0) { cout << "0 0 " << n; return 0; } for (long long i = 0; i <= w; i++) { long long drew = i * d; long long wins = (p - drew) / w; if (wins * w + i * d > p || wins + i > n) { cout << -1; return 0; } if ((p - drew) % w == 0) { cout << wins << ' ' << i << ' ' << n - i - wins; return 0; } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { while (true) { long long r = a % b; if (r == 0) return b; a = b; b = r; } } struct Num { long long value; long long f, s; Num(long long value, long long f, long long s) : value(value), f(f), s(s) {} Num operator*(long long v) const { return Num(value * v, f * v, s * v); } const Num operator-(const Num& rhs) const { return Num(value - rhs.value, f - rhs.f, s - rhs.s); } const Num operator+(const Num& rhs) const { return Num(value + rhs.value, f + rhs.f, s + rhs.s); } Num& operator+=(const Num& rhs) { value += rhs.value; f += rhs.f; s += rhs.s; return *this; } }; int main() { ios::sync_with_stdio(false); cin.tie(NULL); long long n, p, w, d; cin >> n >> p >> w >> d; long long g = gcd(w, d); if (p % g) { cout << -1 << '\n'; return 0; } long long W = w / g; long long D = d / g; p = p / g; Num ans(0, 0, 0); Num A(W, 1, 0), B(D, 0, 1); while (true) { long long pq = p / A.value; ans += A * pq; p -= pq * A.value; if (p == 0) break; long long q = A.value / B.value; Num R = A - B * q; A = B; B = R; } long long amount; if (ans.s < 0) { amount = (-1 * ans.s + W - 1) / W; ans.f -= amount * D; ans.s += amount * W; } amount = ans.s / W; ans.f += amount * D; ans.s -= amount * W; if (ans.f < 0 || ans.f + ans.s > n) { cout << -1 << '\n'; } else { cout << ans.f << ' ' << ans.s << ' ' << n - ans.f - ans.s << '\n'; } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") #pragma GCC optimize("-fgcse") #pragma GCC optimize("-fgcse-lm") #pragma GCC optimize("-fipa-sra") #pragma GCC optimize("-ftree-pre") #pragma GCC optimize("-ftree-vrp") #pragma GCC optimize("-fpeephole2") #pragma GCC optimize("-ffast-math") #pragma GCC optimize("-fsched-spec") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-falign-jumps") #pragma GCC optimize("-falign-loops") #pragma GCC optimize("-falign-labels") #pragma GCC optimize("-fdevirtualize") #pragma GCC optimize("-fcaller-saves") #pragma GCC optimize("-fcrossjumping") #pragma GCC optimize("-fthread-jumps") #pragma GCC optimize("-funroll-loops") #pragma GCC optimize("-fwhole-program") #pragma GCC optimize("-freorder-blocks") #pragma GCC optimize("-fschedule-insns") #pragma GCC optimize("inline-functions") #pragma GCC optimize("-ftree-tail-merge") #pragma GCC optimize("-fschedule-insns2") #pragma GCC optimize("-fstrict-aliasing") #pragma GCC optimize("-fstrict-overflow") #pragma GCC optimize("-falign-functions") #pragma GCC optimize("-fcse-skip-blocks") #pragma GCC optimize("-fcse-follow-jumps") #pragma GCC optimize("-fsched-interblock") #pragma GCC optimize("-fpartial-inlining") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("-freorder-functions") #pragma GCC optimize("-findirect-inlining") #pragma GCC optimize("-fhoist-adjacent-loads") #pragma GCC optimize("-frerun-cse-after-loop") #pragma GCC optimize("inline-small-functions") #pragma GCC optimize("-finline-small-functions") #pragma GCC optimize("-ftree-switch-conversion") #pragma GCC optimize("-foptimize-sibling-calls") #pragma GCC optimize("-fexpensive-optimizations") #pragma GCC optimize("-funsafe-loop-optimizations") #pragma GCC optimize("inline-functions-called-once") #pragma GCC optimize("-fdelete-null-pointer-checks") using namespace std; const int inf = 0x7fffffff; const int MOD = 1e9 + 7; template <typename tn> inline tn read() { char ch; tn f = 1; while (!isdigit(ch = getchar())) if (ch == '-') f = -1; tn x = ch - '0'; while (isdigit(ch = getchar())) x = x * 10 + ch - '0'; return x * f; } template <typename tn> inline void cmax(tn &a, tn b) { if (a < b) a = b; } template <typename tn> inline void cmin(tn &a, tn b) { if (a > b) a = b; } signed main() { long long n, p, w, d, ans = 0; n = read<long long>(), p = read<long long>(), w = read<long long>(), d = read<long long>(); for (long long i = 0; i < (w); i++) { if ((p - i * d) % w == 0) { long long x = (p - i * d) / w; if (x >= 0 && x + i <= n) cout << x << ' ' << i << ' ' << n - x - i << endl; else cout << "-1" << endl; ans = -1; break; } } if (ans == 0) puts("-1\n"); return 0; }
#include <bits/stdc++.h> const int N = 100010; const int M = 200010; const int maxn = 100000; const long long MOD = 998244353; using namespace std; long long n, p, w, d; long long ex(long long a, long long b, long long &x, long long &y) { if (!b) { x = 1; y = 0; return a; } else { long long ans = ex(b, a % b, x, y); x -= a / b * y; swap(x, y); return ans; } } int SOLVE() { long long x, y; long long g = ex(w, d, x, y); if (p % g != 0) return 0; long long hlp = p / g; long long da = d / g, db = w / g; x = (x % da + da) % da; x *= hlp % da; x %= da; y = (p - 1LL * x * w) / d; if (y < 0) return 0; if (x + y <= n) { printf("%lld %lld %lld\n", x, y, n - (x + y)); return 1; } if (da - db >= 0) return 0; long long ned = (x + y - n) / (db - da); if ((x + y - n) % (db - da) != 0) ned++; x += ned * da; y -= ned * db; if (y < 0) return 0; printf("%lld %lld %lld", x, y, n - (x + y)); return 1; } int main() { scanf("%lld%lld%lld%lld", &n, &p, &w, &d); if (!SOLVE()) printf("-1\n"); return 0; }
#include <bits/stdc++.h> using namespace std; long long arr[1000005]; int main() { long long n, p, w, d, x = 0, y = 0, z = 0, t; cin >> n >> p >> w >> d; if (w * n < p) cout << -1 << endl; else if (p / w > n) cout << -1 << endl; else if (p == 0) { cout << "0" << " 0 " << n << endl; } else if (p % w == 0 and p / w <= n) { cout << (p / w) << " 0 " << (n - (p / w)) << endl; } else if (p % d == 0 and p / d <= n) { cout << "0 " << (p / d) << " " << (n - (p / d)) << endl; } else { long long flag = 1; for (long long i = 1; i <= 10000000; i++) { t = p - (d * i); if (t < 0) break; if (t % w == 0) { x = t / w; y = i; z = n - (x + y); if (z < 0) break; flag = 0; break; } } if (!flag) { cout << x << " " << y << " " << z << endl; } else { cout << -1 << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long n, p, d, w; cin >> n >> p >> w >> d; long long x, y, z; for (y = 0; y <= min(n, 100000ll); ++y) { x = p - y * d; if (x % w || x < 0) continue; x /= w; z = n - x - y; if (z < 0) continue; cout << x << " " << y << " " << z; return 0; } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 7; const int INF = 0x3f3f3f3f; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, p, d, w; cin >> n >> p >> w >> d; long long x, y, z; int ok = 0; for (int i = 0; i < w; ++i) { if ((p - (i * d)) % w == 0) { x = (p - i * d) / w; y = i; z = n - x - y; if (x >= 0 && y >= 0 && z >= 0) { ok = 1; break; } } } if (!ok) cout << -1 << endl; else cout << x << ' ' << y << ' ' << z << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long mulmod(long long a, long long b, long long c) { long long sign = 1; if (a < 0) { a = -a; sign = -sign; } if (b < 0) { b = -b; sign = -sign; } a %= c; b %= c; long long res = 0; while (b > 0) { if (b & 1) { res = (res + a) % c; } a = (a + a) % c; b >>= 1; } if (sign == -1) { res = (-res) % c; } return res; } template <typename T> T extgcd(T a, T b, T &x, T &y) { if (a == 0) { x = 0; y = 1; return b; } T p = b / a; T g = extgcd(b - p * a, a, y, x); x -= p * y; return g; } template <typename T> bool diophantine(T a, T b, T c, T &x, T &y, T &g) { if (a == 0 && b == 0) { if (c == 0) { x = y = g = 0; return true; } return false; } if (a == 0) { if (c % b == 0) { x = 0; y = c / b; g = abs(b); return true; } return false; } if (b == 0) { if (c % a == 0) { x = c / a; y = 0; g = abs(a); return true; } return false; } g = extgcd(a, b, x, y); if (c % g != 0) { return false; } T dx = c / a; c -= dx * a; T dy = c / b; c -= dy * b; x = dx + mulmod(x, c / g, b); y = dy + mulmod(y, c / g, a); g = abs(g); return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, p, d, w; cin >> n >> p >> w >> d; long long x, y, g; bool sol = diophantine(w, d, p, x, y, g); if (sol == 0) { cout << -1 << endl; } else { long long one = w / g; long long two = d / g; long long ymax = (w * n - p) / (w - d); if (x < 0) { long long fixx = ceil((double)(-x) / (double)two); y -= fixx * one; x += fixx * two; if (y < 0) { cout << -1 << endl; return 0; } } if (y < 0) { long long fixy = ceil((double)(-y) / (double)one); y += fixy * one; x -= fixy * two; if (x < 0) { cout << -1 << endl; return 0; } } if (ymax < 0) { cout << -1 << endl; } else if (x + y <= n && x >= 0 && y >= 0) { cout << x << " " << y << " " << n - x - y << endl; return 0; } else { if (y <= ymax) { cout << x << " " << y << " " << n - x - y << endl; return 0; } long long fixy = ceil((double)(y - ymax) / (double)one); y -= fixy * one; x += fixy * two; if (y > 0) { cout << x << " " << y << " " << n - x - y << endl; return 0; } else { cout << -1 << endl; return 0; } } } }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p, w, d; while (~scanf("%lld %lld %lld %lld", &n, &p, &w, &d)) { int flag = 0; for (long long i = 0; i < w; i++) { if ((p - i * d) % w == 0) { long long x = (p - i * d) / w; if (x < 0) continue; long long z = n - x - i; if (z < 0) continue; printf("%lld %lld %lld\n", x, i, z); flag = 1; break; } } if (!flag) printf("-1\n"); } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000002; const int MOD2 = 998244353; const int MOD = 1000000007; const int INF = 1e8; const long double EPS = 1e-7; long long int fastpow(long long int n, long long int x) { if (x == 0) return 1; if (x % 2 == 0) return (fastpow(n * n % MOD2, x / 2) % MOD2); return (x * fastpow(n * n % MOD2, (x - 1) / 2) % MOD2) % MOD2; } int main(int argc, char** argv) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; { long long int n, p, w, d; cin >> n >> p >> w >> d; for (int i = 0; i < (int)w; i++) { if ((p - i * d) % w == 0 && p >= i * d && n - i - (p - i * d) / w >= 0) { cout << (p - i * d) / w << " " << i << " " << n - i - (p - i * d) / w << "\n"; return 0; } } cout << -1 << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, p, d, w; int main() { cin >> n >> p >> w >> d; long long r = p % w; long long x = -1, y = -1, z = -1; for (int k = 0; k < d; k++) if ((k * w + r) % d == 0) { x = p / w - k; y = (p - x * w) / d; z = n - x - y; break; } if (z < 0 || x < 0 || y < 0) cout << "-1\n"; else cout << x << " " << y << " " << z << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; inline long long int gcd(long long int a, long long int b) { return a == 0 ? b : gcd(b % a, a); } inline long long int lcm(long long int a, long long int b) { return a * b / gcd(a, b); } template <class T> inline void rn(T &a) { a = 0; bool f = false; register char c = getchar(); while (c < '0' or c > '9') { if (c == '-') { f = true; } c = getchar(); } while (c >= '0' and c <= '9') { a = (a << 1) + (a << 3) + (c & 15); c = getchar(); } if (f) { a = -a; } } template <class T> inline T mx(T a, T b) { if (a > b) return a; else return b; } int main() { long long int n, p, w, d, y, z; rn(n), rn(p), rn(w), rn(d); for (y = 0; p % w && y < w; y++) p -= d; z = n - p / w - y; if ((p | z) < 0 || y == w) cout << "-1"; else cout << p / w << ' ' << y << ' ' << z; return 0; }
#include <bits/stdc++.h> using namespace std; long long exgcd(long long a, long long b, long long &x1, long long &y1) { if (!b) { x1 = 1, y1 = 0; return a; } long long x2, y2; long long d = exgcd(b, a % b, x2, y2); x1 = y2, y1 = x2 - (a / b) * y2; return d; } int main() { bool ok = true; long long n, p, a, b; cin >> n >> p >> a >> b; long long x0 = -1, y0; for (long long v = 0; v < a && b * v <= p; v++) { if ((p - b * v) % a == 0) { x0 = (p - b * v) / a, y0 = v; break; } } if (x0 != -1 && x0 + y0 <= n) cout << x0 << ' ' << y0 << ' ' << n - x0 - y0 << endl; else cout << -1 << endl; return 0; }
#include <bits/stdc++.h> long long int n, p, d, w, x, y, g; int main() { scanf("%lld%lld%lld%lld", &n, &p, &w, &d); for (long long int y = 0, x; y < w; ++y) { if ((p - d * y) % w == 0) x = (p - d * y) / w; if (x >= 0 && x + y <= n) return 0 & printf("%lld %lld %lld", x, y, n - x - y); } return 0 & printf("-1"); }
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cin.exceptions(cin.failbit); int T; { long long int t, n, i, j, x, y, m, l, d, k, p, w, w1, l1, d1; cin >> n >> p >> w >> d; i = max((n * d - p) / d, 0ll); for (int j = 0; j <= w; i++, j++) { if ((p + i * d - n * d) % (w - d) == 0 && (p + i * d - n * d >= 0)) { l1 = i; w1 = (p + i * d - n * d) / (w - d); d1 = (n - l1 - w1); if (d1 >= 0) { cout << w1 << ' ' << d1 << ' ' << l1 << "\n"; return 0; } } } cout << -1; return 0; } }
#include <bits/stdc++.h> using namespace std; long long n, p, w, d; bool done = false; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } int main() { cin >> n >> p >> w >> d; long long g = gcd(w, d); if (!(p % g)) { p /= g; w /= g; d /= g; for (long long i = p / w; i >= 0 && p - i * w <= (n - i) * d; i--) { if (!((p - i * w) % d)) { cout << i << ' ' << (p - i * w) / d << ' ' << n - i - (p - i * w) / d; done = true; break; } } } if (!done) cout << -1; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p, w, d; cin >> n >> p >> w >> d; for (long long y = 0; y <= w - 1; y++) { long long x = p - y * d; if (x < 0) continue; if (x % w) continue; x /= w; if (y + x <= n) { cout << x << " " << y << " " << n - x - y << endl; return 0; } } cout << "-1" << endl; }
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long n, p, w, d, x, y; int main() { cin >> n >> p >> w >> d; for (long long i = 0; i < w; i++) { long long y = i, x = (p - (y * d)) / w; if (y * d + x * w == p && x + y <= n && x >= 0 && y >= 0) { cout << x << ' ' << y << ' ' << n - (x + y); return 0; } } cout << -1; }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e9; int GCD(int a, int b) { for (; b; a %= b, swap(a, b)) ; return a; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ((void)0); ((void)0); ((void)0); long long N, P, x, y; int d, w, g; cin >> N >> P >> w >> d; g = GCD(d, w); if (P % g || w * N < P) { cout << "-1\n"; return 0; } for (int i = 0; i <= w; i++) if ((P - 1LL * d * i) % w == 0 && (P - 1LL * d * i) / w >= 0 && i + (P - 1LL * d * i) / w <= N) { cout << (P - 1LL * d * i) / w << ' ' << i << ' ' << N - i - (P - 1LL * d * i) / w << '\n'; return 0; } cout << "-1\n"; return 0; }
#include <bits/stdc++.h> using namespace std; map<int, int> ma; int main() { long long n, p, w, d; cin >> n >> p >> w >> d; long long x, y, z = 0; for (long long i = 0;; i += d) { long long x = (p - i) / w; long long y = i / d; if (ma.count(i % w)) { cout << -1 << endl; break; } if (x * w + i == p) { if (x >= 0 && x + y <= n) { cout << x << " " << y << " " << (n - x - y) << endl; } else { cout << -1 << endl; } break; } ma[i % w]++; } return 0; }
#include <bits/stdc++.h> using namespace std; bool fun(int a[], int n, int k, int x, int p) { int l = 0; for (int i = 0; i < n; i++) { if (p) { l++; p = !p; } else if (a[i] <= x) { l++; p = !p; } if (l >= k) return 1; } return 0; } void sol() { long long int n, p, w, d, x, y, k; cin >> n >> p >> w >> d; if (p > n * w || (p && p < d)) { printf("-1"); return; } if (p % w == 0) { printf("%lld ", p / w); printf("0 "); printf("%lld ", n - (p / w)); return; } k = -1; for (int i = 0; i < d; i++) { if (p % d == (i * (w % d)) % d) { k = i; break; } } if (k == -1) { printf("%lld ", k); printf("\n"); return; } x = p / w; if (k <= x % d) x = x - x % d + k; else x = x - x % d - ((d - k) % d); y = (p - (x * w)) / d; printf("%lld ", x); printf("%lld ", y); printf("%lld ", n - (x + y)); printf("\n"); } int main() { sol(); }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); ; long long n, p, w, d; cin >> n >> p >> w >> d; long long x = -1, y = -1, z = -1; const int ITER = 1e7; for (y = 0; y < ITER; ++y) { if ((p - (y * d)) % w == 0) { x = (p - (y * d)) / w; z = n - x - y; break; } } if (x < 0 or y < 0 or z < 0) cout << "-1\n"; else cout << x << " " << y << " " << z << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p, d, w; cin >> n >> p >> w >> d; for (long long y = 0; y < w; y++) { long long t = y * d; if (t > p) break; if ((p - t) % w > 0) continue; long long x = (p - t) / w; if (x + y > n) continue; cout << x << " " << y << " " << n - x - y << endl; return 0; } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long n, p, w, d; cin >> n >> p >> w >> d; long long ans[] = {0, 0, 0}; bool ok = false; for (long long i = 0; i < w; i++) if ((d * i) % w == p % w && i * d <= p) { if (i + (p - d * i) / w <= n) { ans[0] = (p - d * i) / w; ans[1] = i; ans[2] = n - ans[1] - ans[0]; ok = true; break; } } if (ok) cout << ans[0] << " " << ans[1] << " " << ans[2] << "\n"; else cout << "-1\n"; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, p, d, w, x, y, z; long long gcd(long long a, long long b) { while (b) { long long c = b; b = a % b; a = c; } return a; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> p >> w >> d; long long provjera = gcd(w, d); if (p % provjera) { cout << "-1"; return 0; } p /= provjera; w /= provjera; d /= provjera; for (int i = 0; i < d and (p - i * w) >= 0; i++) { if (!((p - i * w) % d)) { x = i; y = (p - i * w) / d; z = n - x - y; if (z >= 0) { cout << x << " " << y << " " << z; return 0; } int korekcija = (-z) / (w - d); if ((-z) % (w - d)) korekcija++; z = z + (w - d) * korekcija; x = x + d * korekcija; y = y - w * korekcija; if (x >= 0 and y >= 0 and z >= 0) { cout << x << " " << y << " " << z; return 0; } else { cout << "-1"; return 0; } } } cout << "-1"; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 2e3 + 5; const int32_t M = 998244353; void solve() { long long n, p, d, w; cin >> n >> p >> w >> d; if (p / w > n) { cout << -1 << "\n"; return; } for (long long i = 0; i <= 100000; i++) { long long val = d * i; if (val > p) { break; } if ((p - val) % w == 0) { if ((p - val) / w + i <= n) { cout << (p - val) / w << " " << i << " " << n - (p - val) / w - i << "\n"; return; } } } cout << -1 << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; long long a = 1; while (t--) { solve(); a += 1; } }
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long MOD = 998244353; inline long long add(long long x, long long y) { x += y; if (x >= mod) x -= mod; return x; } inline long long sub(long long x, long long y) { x -= y; if (x < 0) x += mod; return x; } inline long long mul(long long x, long long y) { return (x * 1ll * y) % mod; } vector<long long> adj[200100]; long long vis[200100]; long long par[200100]; signed main() { long long n, p, w, d; cin >> n >> p >> w >> d; long long a, b, c; for (long long i = 0; i < w; i++) { c = p - i * d; if (c < 0) continue; if (c % w == 0) { if (c / w + i <= n) return cout << c / w << " " << i << " " << n - c / w - i, 0; } } cout << -1; }
#include <bits/stdc++.h> using namespace std; long long n, w, p, d; long long x, y, z; bool flag; int main() { scanf("%lld %lld %lld %lld", &n, &p, &w, &d); x = y = z = -1; flag = 0; while (y < w) { y++; if ((p - y * d) % w != 0) { continue; } x = (p - y * d) / w; if (x + y <= n && x >= 0 && y >= 0) { flag = 1; printf("%lld %lld %lld\n", x, y, n - x - y); break; } } if (!flag) { printf("-1\n"); } }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; long long n, p, d, w, x, y, z; bool sp = false; long long exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1, y = 0; return a; } long long res = exgcd(b, a % b, x, y), t = x; x = y, y = t - (a / b) * y; return res; } int main() { scanf("%lld%lld%lld%lld", &n, &p, &w, &d); if (w > d) swap(w, d), sp = true; long long G = exgcd(w, d, x, y); if (p % G != 0) puts("-1"); else { long long mod = d / G; x = (x % mod * ((p / G) % mod) % mod + mod) % mod; y = (p - x * w) / d; z = n - x - y; if (x >= 0 && y >= 0 && z >= 0) printf("%lld %lld %lld\n", sp ? y : x, sp ? x : y, z); else puts("-1"); } return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { long long n, p, w, d; cin >> n >> p >> w >> d; if (p % w == 0) { if (p / w > n) { cout << -1; return 0; } cout << p / w << " 0 " << n - p / w; return 0; } for (long long i = 0; i < d; i++) { long long k = p / w; if (k % d >= i) { k -= (k % d - i); } else { k -= k % d; k -= (d - i); } if (k < 0) continue; long long pp = p - k * w; if (pp / d + k > n || pp % d) { continue; } cout << k << " " << pp / d << " " << n - k - pp / d; return 0; } if ((p % w) % d) { cout << -1; return 0; } if ((p % w) / d + p / w > n) { cout << -1; return 0; } cout << p / w << " " << (p % w) / d << " " << n - ((p % w) / d + p / w); return 0; }
#include <bits/stdc++.h> void C() { long long n, p, w, d; std::cin >> n >> p >> w >> d; for (long long y = 0; y < w; y++) { long long x = p - y * d; if (x >= 0 && x % w == 0 && x / w + y <= n) { std::cout << x / w << ' ' << y << ' ' << n - x / w - y << '\n'; return; } } std::cout << -1 << '\n'; return; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); long long t; t = 1; while (t--) { C(); } std::cout.flush(); return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") #pragma GCC optimize("fast-math") signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, p, w, d; cin >> n >> p >> w >> d; for (long long i = 0; i < w; i++) { long long l = p - i * d; if (l >= 0 && l % w == 0) { long long s = l / w + i; if (s <= n) return cout << s - i << ' ' << i << ' ' << n - s << '\n', 0; } } cout << -1; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxm = 1e5 + 10; int flag[maxm]; int main() { long long n, p; int w, d; scanf("%lld%lld", &n, &p); scanf("%d%d", &w, &d); int ok = -1; for (int i = 0; i <= n; i++) { if (p - 1ll * i * d < 0) break; if ((p - 1ll * i * d) % w == 0) { ok = i; break; } int pos = (int)((p - 1ll * i * d) % w); if (flag[pos] == 1) break; flag[pos] = 1; } if (ok > -1) { long long tie = ok; long long win = (p - tie * d) / w; if (win + tie <= n) printf("%lld %lld %lld\n", win, tie, n - win - tie); else printf("-1\n"); } else printf("-1\n"); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, a, b, c; void input() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> c >> a >> b; } int main() { input(); for (int i = 0; i < a; i++) { long long temp = c - b * i; if (temp % a == 0 && temp >= 0) { if (temp / a + i <= n) { cout << temp / a << " " << i << " " << n - i - temp / a; return 0; } } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; const long long M = 1e6 + 5; const long long mod = 1e9 + 7; const long long m1 = 1e9 + 7; const long long m2 = 1e9 + 9; const long long p1 = 402653189; const long long p2 = 1610612741; const long long lN = 22; long long powermodm(long long x, long long n, long long M) { long long result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } long long power(long long _a, long long _b) { long long _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a); _b /= 2; _a = (_a * _a); } return _r; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (max(a, b) / gcd(a, b)) * min(a, b); } long long dx[4] = {-1, 1, 0, 0}; long long dy[4] = {0, 0, 1, -1}; long long pw[lN], fact[N], invfact[N]; void pre() { fact[0] = 1; for (long long i = 0; i < lN; i++) pw[i] = power(2, i); for (long long i = 1; i < N; i++) fact[i] = (i * fact[i - 1]) % mod; for (long long i = 0; i < N; i++) invfact[i] = powermodm(fact[i], mod - 2, mod); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long i, j; long long n, p, w, d; cin >> n >> p >> w >> d; for (i = 0; i < w; i++) { long long val = p - i * d; if (val < 0) { cout << "-1"; return 0; } if (val % w == 0 && (val / w) + i <= n) { cout << (val / w) << " " << i << " " << n - i - (val / w); return 0; } } cout << "-1"; return 0; }
#include <bits/stdc++.h> using namespace std; long long mulmod(long long a, long long b, long long c) { long long sign = 1; if (a < 0) { a = -a; sign = -sign; } if (b < 0) { b = -b; sign = -sign; } a %= c; b %= c; long long res = 0; while (b > 0) { if (b & 1) res = (res + a) % c; a = (a + a) % c; b >>= 1; } if (sign == -1) res = (-res) % c; return res; } long long extgcd(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } long long p = b / a; long long g = extgcd(b - p * a, a, y, x); x -= p * y; return g; } bool diophantine(long long a, long long b, long long c, long long &x, long long &y, long long &g) { if (a == 0 && b == 0) { if (c == 0) { x = y = g = 0; return true; } return false; } if (a == 0) { if (c % b == 0) { x = 0; y = c / b; g = abs(b); return true; } return false; } if (b == 0) { if (c % a == 0) { x = c / a; y = 0; g = abs(a); return true; } return false; } g = extgcd(a, b, x, y); if (c % g != 0) { return false; } long long dx = c / a; c -= dx * a; long long dy = c / b; c -= dy * b; x = dx + mulmod(x, c / g, b); y = dy + mulmod(y, c / g, a); g = abs(g); return true; } bool all_diophantine(long long a, long long b, long long c, long long &x, long long &y, long long &g, long long &lowest_x_nn, long long &lowest_y_nn, long long &lowest_x_pos, long long &lowest_y_pos) { long long aa, bb, t; bool possible = diophantine(a, b, c, x, y, g); if (possible) { aa = a / g; bb = b / g; t = y / aa; if (y < 0 && y % aa) t--; lowest_y_nn = y - t * aa; t = x / bb; if (x > 0 && x % bb) t--; lowest_x_nn = x + t * bb; } return possible; } int main() { long long i, j, k, l, n, m, tot, temp, curr, val, prev, a, b, c, t, ans, flag, x, y, siz, ii, jj, xx, yy, ind, len, q, dd, ww, d, p, w, z, g, aa, bb; long long lowest_x_nn, lowest_y_nn, lowest_x_pos, lowest_y_pos; cin >> n >> p >> w >> d; ans = all_diophantine(w, d, p, x, y, g, lowest_x_nn, lowest_y_nn, lowest_x_pos, lowest_y_pos); if (ans) { y = lowest_y_nn; x = (p - d * y) / w; z = n - x - y; if (x >= 0 && y >= 0 && z >= 0) { cout << x << " " << y << " " << z << endl; return 0; } } cout << -1 << endl; }
#include <bits/stdc++.h> using std::cin; using std::cout; long long n, p, w, d, x, y; long long ex_gcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = 1; y = 0; return a; } long long d = ex_gcd(b, a % b, x, y); long long temp = x; x = y; y = temp - a / b * y; return d; } bool liEu(long long a, long long b, long long& x, long long& y) { long long r = ex_gcd(a, b, x, y); if (p % r != 0) return false; long long o = p / r; long long k = w / r; y = (y * (o % k) % k); y = (y % k + k) % k; x = (p - y * d) / w; return true; } int main() { cin >> n >> p >> w >> d; if (liEu(w, d, x, y)) { if (x + y > n) puts("-1"); else { if (x < 0) puts("-1"); else cout << x << " " << y << " " << n - x - y; } return 0; } else { puts("-1"); return 0; } }
#include <bits/stdc++.h> using namespace std; long long n, p, d, w; long long UCLN(long long x, long long y) { if (y == 0) return x; return UCLN(y, x % y); } int main() { cin >> n >> p >> w >> d; if (n * w < p) { cout << -1; return 0; } for (int i = 0; i < 2000000; ++i) { long long dy = i; dy *= d; if ((p - dy) % w == 0) { long long x = (p - dy) / w; if (x < 0) break; if (x + i <= n) { cout << x << " " << i << " " << n - x - i << endl; return 0; } } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p, w, d; scanf("%lld %lld", &n, &p); scanf("%lld %lld", &w, &d); for (long long x = 0, y = 0; x <= 100000; x++, y++) { if (p >= x * w and (p - x * w) % d == 0) { if ((p - x * w) / d + x <= n) printf("%lld %lld %lld\n", x, (p - x * w) / d, n - x - (p - x * w) / d), exit(0); } if (p >= y * d and (p - y * d) % w == 0) { if ((p - y * d) / w + y <= n) printf("%lld %lld %lld\n", (p - y * d) / w, y, n - (p - y * d) / w - y), exit(0); } } printf("-1\n"); return 0; }
#include <bits/stdc++.h> int w, d, y; long long n, p, z; int main() { for (scanf("%lld%lld%d%d", &n, &p, &w, &d); y < w && p % w; y++) p -= d; z = n - p / w - y; if (y == w || p < 0 || z < 0) puts("-1"); else printf("%lld %d %lld\n", p / w, y, z); return 0; }
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); LL n, p, d, w; cin >> n >> p >> d >> w; LL y = 0; while (y < d && (p - w * y) % d != 0) ++y; if (y < d) { LL x = (p - w * y) / d; LL z = n - x - y; if (x >= 0 && z >= 0) cout << x << " " << y << " " << z << "\n"; else cout << "-1\n"; } else cout << "-1\n"; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; template <typename T, typename K> inline void umax(T &a, K b) { a = max(a, (T)b); } template <typename T, typename K> inline void umin(T &a, K b) { a = min(a, (T)b); } const int32_t N = 1e5 + 10; const long long INF = 1e16 + 10; const pair<long long, long long> NINF = make_pair(INF, INF); const long double EPS = 1e-6; const long long II = 1e9; const long long MOD = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, p, w, d; cin >> n >> p >> w >> d; long long mx = p / w; long long ans = 0; for (long long win = mx; win + N > mx && win >= 0; win--) { long long ost = p - win * w; long long draw = ost / d; if (draw + win > n) continue; if (win * w + draw * d == p) { cout << win << ' ' << draw << ' ' << n - win - draw << '\n'; return 0; } } cout << "-1\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = (int)3e3 + 9; const int maxm = (int)1e6 + 9; const long long mod = (long long)1e9 + 7; const double pi = acos(-1); const double eps = 1e-15; int main() { long long n, p, w, d; scanf("%lld%lld%lld%lld", &n, &p, &w, &d); for (long long i = 0; i < w; i++) { long long t = p - i * d; if (t >= 0 && t % w == 0 && t / w + i <= n) { printf("%lld %lld %lld\n", t / w, i, n - t / w - i); return 0; } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p, x = 0, w, d, y = 0, z = 0, cp = 0; cin >> n >> p >> w >> d; if (n * w < p) { cout << -1; exit(0); } else { if (p % w == 0) { x = p / w; y = 0; z = n - x - y; } else { cp = 1; bool flag = 0, flag1 = 0; map<long long, bool> y1; while (p - cp * d > 0) { if (y1[(p - cp * d) % w] == true) break; if ((p - cp * d) % w == 0) { x = (p - cp * d) / w; y = cp; z = (n - x - y); flag = 1; break; } y1[(p - cp * d) % w] = true; cp++; } if (flag == 0) { if (p % d == 0 && n * d >= p) { x = 0; y = p / d; z = n - x - y; flag = 1; } } if (flag == 0) { cout << -1; exit(0); } } cout << x << " " << y << " " << z; } }
#include <bits/stdc++.h> using namespace std; long long mod = 1000000000; int main() { long long n, z, p, w, d, x, y; cin >> n >> p >> w >> d; for (long long i = 0; i < w; i++) { y = i; if ((p - y * d) % w == 0) { x = (p - y * d) / w; if (x >= 0) { z = n - x - y; if (z >= 0) { cout << x << " " << y << " " << z << endl; return 0; } } } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; long long mulmod(long long a, long long b, long long c) { long long sign = 1; if (a < 0) { a = -a; sign = -sign; } if (b < 0) { b = -b; sign = -sign; } a %= c; b %= c; long long res = 0; while (b > 0) { if (b & 1) res = (res + a) % c; a = (a + a) % c; b >>= 1; } if (sign == -1) res = (-res) % c; return res; } long long extgcd(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } long long p = b / a; long long g = extgcd(b - p * a, a, y, x); x -= p * y; return g; } bool diophantine(long long a, long long b, long long c, long long &x, long long &y, long long &g) { if (a == 0 && b == 0) { if (c == 0) { x = y = g = 0; return true; } return false; } if (a == 0) { if (c % b == 0) { x = 0; y = c / b; g = abs(b); return true; } return false; } if (b == 0) { if (c % a == 0) { x = c / a; y = 0; g = abs(a); return true; } return false; } g = extgcd(a, b, x, y); if (c % g != 0) { return false; } long long dx = c / a; c -= dx * a; long long dy = c / b; c -= dy * b; x = dx + mulmod(x, c / g, b); y = dy + mulmod(y, c / g, a); g = abs(g); return true; } int main() { long long i, j, k, l, n, m, tot, temp, curr, val, prev, a, b, c, t, ans, flag, x, y, siz, ii, jj, xx, yy, ind, len, q, dd, ww, d, p, w, z, g, aa, bb; cin >> n >> p >> w >> d; ans = diophantine(w, d, p, x, y, g); if (ans) { aa = w / g; bb = d / g; t = y / aa; if (y < 0 && y % aa) t--; y -= t * aa; x += t * bb; z = n - x - y; if (x >= 0 && y >= 0 && z >= 0) { cout << x << " " << y << " " << z << endl; return 0; } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; long long n, p, w, d; int main() { scanf("%lld%lld%lld%lld", &n, &p, &w, &d); long long resx = -1, resy = -1; for (long long y = 0; y <= w - 1; y++) { if (0 == (p - y * d) % w) { long long x = (p - y * d) / w; if (x >= 0 && y >= 0 && x + y <= n) resx = x, resy = y; } } if (-1 == resx && -1 == resy) printf("-1\n"); else printf("%lld %lld %lld\n", resx, resy, n - resx - resy); return 0; }
#include <bits/stdc++.h> using namespace std; const long long int inf = (long long int)1e18 + 10; const long long int MAX = 200005; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, p, w, d, x, y, z, t, f = 0; cin >> n >> p >> w >> d; long long int win = (p / w); long long int draw = (p / d); long long int rem = p - win * w; long long int rem2 = rem % d; t = w % d; for (long long int i = 0; i <= d; i++) { if ((i * t + rem2) % d == 0) { x = win - i, y = (i * w + rem) / d, z = n - x - y; if (x < 0 || z < 0) continue; f = 1; break; } } if (f) { cout << x << " " << y << " " << z << endl; return 0; } else cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; long long inline read() { long long num = 0, neg = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') neg = -1; c = getchar(); } while (isdigit(c)) { num = (num << 3) + (num << 1) + c - '0'; c = getchar(); } return num * neg; } long long n, p, d, w; long long exgcd(long long &x, long long &y, long long a, long long b) { if (!b) { x = 1; y = 0; return a; } long long r = exgcd(x, y, b, a % b); long long t = x; x = y; y = t - a / b * y; return r; } void work() { long long x, y; long long t = exgcd(x, y, w, d); y = (((p / t) % (w / t)) * (y % (w / t))) % (w / t); y = (y % (w / t) + w / t) % (w / t); x = (p - d * y) / w; if (x + y <= n && x >= 0 && y >= 0 && p % t == 0) cout << x << " " << y << " " << n - x - y; else printf("-1"); } signed main() { n = read(); p = read(); w = read(); d = read(); work(); return 0; }
#include <bits/stdc++.h> using namespace std; long long GCD(long long a, long long b) { if (a == 0) return b; return GCD(b % a, a); } int main() { long long n, p, w, d; cin >> n >> p >> w >> d; long long k = GCD(w, d); if (p % k != 0) { cout << -1; return 0; } else { p /= k; w /= k; d /= k; long long r1 = -1, r2 = -1; for (r1 = 0; r1 < w; r1++) { if (((r1 * d) % w) == (p % w)) break; } for (r2 = 0; r2 < d; r2++) { if (((r2 * w) % d) == (p % d)) break; } if (r1 == -1 || r2 == -1) { cout << -1; return 0; } long long ans1 = r1; long long ans2 = r2; long long sum = w * r2 + d * r1; if (sum > p) { cout << -1; return 0; } if ((p - sum) % (w * d) == 0) { ans2 += (p - sum) / w; sum = p; } if (sum == p) { if (ans1 + ans2 <= n) { cout << ans2 << ' ' << ans1 << ' ' << n - ans1 - ans2; } else { cout << -1; } } else { cout << -1; } } return 0; }
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } int main() { fast(); long long n, p, w, d; cin >> n >> p >> w >> d; long long l = -1; long long r = n; long long h; while (l + 1 < r) { h = (l + r) / 2; if (h * w >= p - ((n - h) * d)) r = h; else l = h; } long long mn = r; for (long long i = mn; i <= mn + 1000000; ++i) { if (i * w <= p) { if ((p - (i * w)) % d == 0 && i + (p - (i * w)) / d <= n) { cout << i << " "; cout << (p - (i * w)) / d << " "; cout << n - (i + (p - (i * w)) / d) << " "; cout << endl; return 0; } } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n, p, w, d; cin >> n >> p >> w >> d; long long x; bool flag = true; if (p % d == 0) { x = 0; long long y = p / d; if (n - y - x >= 0) { cout << x << " " << y << " " << n - x - y << "\n"; flag = false; } } if (flag) { for (long long y = 0; y < 100002; y++) { long long temp = p - (y * d); if (temp % w == 0) { x = temp / w; if (n - x - y >= 0 && x >= 0) { flag = false; cout << x << " " << y << " " << n - x - y << "\n"; break; } } } } if (flag) cout << -1 << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; void exgcd(long long a, long long b, long long& x, long long& y, long long& d) { if (!b) { x = 1, y = 0, d = a; } else { exgcd(b, a % b, y, x, d); y -= x * (a / b); } } int main() { long long n, p, w, d, D, x, y; scanf("%lld%lld%lld%lld", &n, &p, &w, &d); if (n * w < p) { printf("-1"); return 0; } exgcd(w, d, x, y, D); if (p % D) { printf("-1"); return 0; } w /= D, d /= D, p /= D; long long maxn = p / w; for (int i = 0; i <= min(w, maxn); ++i) { if ((p - (maxn - i) * w) % d == 0) { x = maxn - i; y = (p - (maxn - i) * w) / d; break; } } if (x >= 0 && y >= 0 && x + y <= n) { printf("%lld %lld %lld", x, y, n - x - y); } else printf("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; const long long M = 1e6 + 5; const long long mod = 1e9 + 7; const long long m1 = 1e9 + 7; const long long m2 = 1e9 + 9; const long long p1 = 402653189; const long long p2 = 1610612741; const long long lN = 22; long long powermodm(long long x, long long n, long long M) { long long result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } long long power(long long _a, long long _b) { long long _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a); _b /= 2; _a = (_a * _a); } return _r; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (max(a, b) / gcd(a, b)) * min(a, b); } long long dx[4] = {-1, 1, 0, 0}; long long dy[4] = {0, 0, 1, -1}; long long pw[lN], fact[N], invfact[N]; void pre() { fact[0] = 1; for (long long i = 0; i < lN; i++) pw[i] = power(2, i); for (long long i = 1; i < N; i++) fact[i] = (i * fact[i - 1]) % mod; for (long long i = 0; i < N; i++) invfact[i] = powermodm(fact[i], mod - 2, mod); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long i, j; long long n, p, w, d; cin >> n >> p >> w >> d; for (i = 0; i < 1e6; i++) { long long val = p - i * d; if (val < 0) { cout << "-1"; return 0; } if (val % w == 0 && (val / w) + i <= n) { cout << (val / w) << " " << i << " " << n - i - (val / w); return 0; } } cout << "-1"; return 0; }
#include <bits/stdc++.h> using namespace std; const int MX = 1e5; long long n, p, d, w; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> p >> w >> d; long long l = gcd(w, d); if (p % l != 0) { cout << -1 << endl; return 0; } else { long long i = 0, sum = p % w; while (sum <= p) { if (sum % d == 0) { long long x = p / w - i; long long y = sum / d; long long z = n - x - y; if (z < 0) { cout << -1 << endl; return 0; } cout << x << " " << y << " " << z << endl; return 0; } sum += w; i++; } cout << -1 << endl; return 0; } }
#include <bits/stdc++.h> using namespace std; long long n, p, w, d, xmax, xmin, tmp, y; int main() { cin >> n >> p >> w >> d; if (p > n * w) cout << -1 << endl; else { xmax = p / w; if (xmax - d > 0) xmin = xmax - d; else xmin = 0; for (long long i = xmax; i >= xmin; i--) { tmp = p - i * w; if (tmp % d == 0 && i + tmp / d <= n) { y = tmp / d; cout << i << ' ' << y << ' ' << n - i - y << endl; return 0; } } cout << -1 << endl; } }
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); void print_time() {} template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } template <class T> ostream& operator<<(ostream& out, vector<T> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } template <class T> ostream& operator<<(ostream& out, set<T> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } template <class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } void solve() { long long n, p, w, d; cin >> n >> p >> w >> d; long long g = gcd(w, d); if (p % g) { cout << -1 << "\n"; return; } for (long long ds = 0; ds <= w; ++ds) { long long left = p - ds * d; if (left % w || left < 0) continue; long long ws = left / w; if (ds + ws > n) continue; cout << ws << " " << ds << " " << (n - ws - ds) << "\n"; return; } cout << -1 << "\n"; } int main() { cout << fixed << setprecision(20); ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long t; t = 1; while (t--) { solve(); } print_time(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, p, w, d; cin >> n >> p >> w >> d; long long x = -1, y = -1, z = 0; if (p / w > n and p % w != 0) { cout << -1 << endl; return 0; } if (p % w == 0 and p / w <= n) { cout << p / w << " " << 0 << " " << n - (p / w) << endl; return 0; } if (p % d == 0 and p / d <= n) { cout << 0 << " " << p / d << " " << n - (p / d) << endl; return 0; } for (int y = 0; y <= w; y++) { if (y + (p - (d * y)) / w > n) { break; } if ((p - (d * y)) % w == 0 and (p - (d * y)) / w > 0) { cout << (p - (d * y)) / w << " " << y << " " << n - y - (p - (d * y)) / w << endl; return 0; } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void PLAY() { cout << fixed << setprecision(10); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, p, w, d; cin >> n >> p >> w >> d; for (long long i = 0; i < w; i++) { if (i * d > p) { break; } long long rest = p - (i * d); if (rest % w == 0) { long long win = rest / w; if (win + i <= n) { long long lose = n - (win + i); cout << win << " " << i << " " << lose << endl; return 0; } } } cout << "-1" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, p, w, d; long long exgcd(long long a, long long b, long long &x1, long long &y1) { if (!b) { x1 = 1, y1 = 0; return a; } long long x2, y2; long long d = exgcd(b, a % b, x2, y2); x1 = y2, y1 = x2 - (a / b) * y2; return d; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int i, j, flag = 0; cin >> n >> p >> w >> d; if (w > d) { flag = 1; swap(w, d); } long long x, y; long long g = exgcd(w, d, x, y); if ((p % g) != 0) cout << -1 << endl; else { long long s = d / g, t = w / g; x = (s + (p / g) % s * (x % s) % s) % s; y = (p - x * w) / d; if (x >= 0 && y >= 0 && x + y <= n) { if (flag) cout << y << ' ' << x << ' ' << n - x - y << endl; else cout << x << ' ' << y << ' ' << n - x - y << endl; } else cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { long long n, p, w, d; cin >> n >> p >> w >> d; for (long long y = 0; y < w; y++) { long long r = p - y * d; if (r >= 0 && r % w == 0 && y + r / w <= n) { cout << r / w << ' ' << y << ' ' << n - (r / w) - y; return 0; } } cout << -1 << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, p, w, d; cin >> n >> p >> w >> d; long long x, y = -1, z; for (long long i = 0; i < w; i++) { if ((p - i * d) % w == 0) { y = i; break; } } if (y == -1) { cout << -1 << endl; return 0; } x = (p - y * d) / w; z = n - x - y; if (x >= 0 && y >= 0 && z >= 0) { cout << x << " " << y << " " << z << "\n"; return 0; } else { cout << -1 << endl; return 0; } }
#include <bits/stdc++.h> using namespace std; long long n, p, a, b, x, y, t; int main(void) { cin >> n >> p >> a >> b; if (p / a > n) return puts("-1"), 0; x = y = 0x3f3f3f3f3f3f3f3fLL; for (int i = 0; i <= a; ++i) { if (p < 1LL * i * b) break; if ((p - 1LL * i * b) % a == 0 && x + y > i + ((p - 1LL * i * b) / a)) x = i, y = (p - 1LL * i * b) / a; } if (x + y > n) return puts("-1"), 0; cout << y << ' ' << x << ' ' << n - x - y; return 0; }
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } void ghost() { long long int n, i = 0, j, y, k, w, a = 0, b = 0, l, x = 0, t = 0, d = 0, m = 0, p = 0; cin >> n >> p >> w >> d; m = gcd(w, d); if (p % m != 0) cout << "-1"; else if (n * w < p) cout << "-1"; else { while (1) { p = p - i * d; i = 1; if (p % w == 0) { cout << p / w << " " << a << " " << n - p / w - a; break; } a++; if (p < 0 || a > n) { cout << "-1"; break; } } } } signed main() { long long int test = 1; ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (test--) ghost(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p, w, d; cin >> n >> p >> w >> d; for (int i = 0; i <= w; i++) { if ((p - i * d) % w == 0 && p >= i * d && (n - i - (p - i * d) / w) >= 0) { cout << (p - i * d) / w << ' ' << i << ' ' << n - i - (p - i * d) / w; return 0; } } cout << "-1"; }
#include <bits/stdc++.h> using namespace std; long long mulmod(long long a, long long b, long long c) { long long sign = 1; if (a < 0) { a = -a; sign = -sign; } if (b < 0) { b = -b; sign = -sign; } a %= c; b %= c; long long res = 0; while (b > 0) { if (b & 1) res = (res + a) % c; a = (a + a) % c; b >>= 1; } if (sign == -1) res = (-res) % c; return res; } long long extgcd(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } long long p = b / a; long long g = extgcd(b - p * a, a, y, x); x -= p * y; return g; } bool diophantine(long long a, long long b, long long c, long long &x, long long &y, long long &g) { if (a == 0 && b == 0) { if (c == 0) { x = y = g = 0; return true; } return false; } if (a == 0) { if (c % b == 0) { x = 0; y = c / b; g = abs(b); return true; } return false; } if (b == 0) { if (c % a == 0) { x = c / a; y = 0; g = abs(a); return true; } return false; } g = extgcd(a, b, x, y); if (c % g != 0) { return false; } long long dx = c / a; c -= dx * a; long long dy = c / b; c -= dy * b; x = dx + mulmod(x, c / g, b); y = dy + mulmod(y, c / g, a); g = abs(g); return true; } int main() { long long i, j, k, l, n, m, tot, temp, curr, val, prev, a, b, c, t, ans, flag, x, y, siz, ii, jj, xx, yy, ind, len, q, dd, ww, d, p, w, z, g; cin >> n >> p >> w >> d; ans = diophantine(w, d, p, x, y, g); if (ans) { if (x < 0) { x += d; y -= w; } if (y < 0) { y += w; x -= d; } z = n - x - y; if (x >= 0 && y >= 0 && z >= 0) { cout << x << " " << y << " " << z << endl; return 0; } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; bool po[100005]; int main() { long long n, p, w, d; cin >> n >> p >> w >> d; long long c = w - d; long long x = min(p / c, p / w); while (x >= max(0LL, (p - n * d) / c)) { if ((p - x * c) % d == 0) { long long y = (p - x * c) / d - x; cout << x << " " << y << " " << (n - x - y) << endl; return 0; } if (po[(p - x * c) % d]) { cout << -1 << endl; return 0; } po[(p - x * c) % d] = 1; --x; } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; __int128 read() { __int128 x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void print(__int128 x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) print(x / 10); putchar(x % 10 + '0'); } __int128 gcd(__int128 a, __int128 b) { return b ? gcd(b, a % b) : a; } __int128 ex_gcd(__int128 a, __int128 b, __int128& x, __int128& y) { if (b == 0) { x = 1; y = 0; return a; } __int128 d = ex_gcd(b, a % b, x, y); __int128 temp = x; x = y; y = temp - a / b * y; return d; } bool liEu(__int128 a, __int128 b, __int128 c, __int128& x, __int128& y) { __int128 d = ex_gcd(a, b, x, y); if (c % d != 0) return 0; __int128 k = c / d; x *= k; y *= k; return 1; } signed main() { __int128 n, p, w, d; n = read(); p = read(); w = read(); d = read(); __int128 x, y; if (!liEu(w, d, p, x, y)) return (std::cout << "-1\n", 0); __int128 t = d / gcd(w, d); __int128 xt = (x % t + t) % t; __int128 yt = (p - w * xt) / d; if (xt >= 0 && yt >= 0 && xt + yt <= n) { print(xt); printf(" "); print(yt); printf(" "); print(n - xt - yt); printf("\n"); return 0; } t = w / gcd(w, d); yt = (y % t + t) % t; xt = (p - d * yt) / w; if (xt >= 0 && yt >= 0 && xt + yt <= n) { print(xt); printf(" "); print(yt); printf(" "); print(n - xt - yt); printf("\n"); return 0; } std::cout << -1 << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, p, w, d, x, y = 0, z; cin >> n >> p >> w >> d; while (y < w && (p - d * y) % w != 0) ++y; if (y < w) { x = (p - d * y) / w; z = n - x - y; if (x >= 0 && z >= 0) { cout << x << ' ' << y << ' ' << z; return 0; } } cout << "-1"; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, p, w, d; int main() { scanf("%lld%lld%lld%lld", &n, &p, &w, &d); for (register long long i = 0; i <= 10000000; i++) { if ((p - i * d) >= 0 && ((p - i * d) % w) == 0 && (n >= (i + (p - i * d) / w))) { printf("%lld %lld %lld\n", (p - i * d) / w, i, n - (i + (p - i * d) / w)); return 0; } } printf("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, p, w, d, y = -1, x = -1; cin >> n >> p >> w >> d; bool f1 = false, f2 = false, f3 = false; for (long long i = 0; i <= 1000000; ++i) { long long aux = (p - (i * d)) / w; if (aux * w + i * d == p && aux >= 0) { x = aux; y = i; break; } } if (x != -1 && y != -1) { long long res = n - (x + y); if (res >= 0) { cout << x << " " << y << " " << res << endl; f1 = true; } } if (!f1) { if (p % w == 0) { long long win = p / w; long long res = n - win; if (res >= 0) { cout << win << " " << 0 << " " << res << '\n'; f2 = true; } } } if (!f2 && !f1) { if (p % d == 0) { long long win = p / d; long long res = n - win; if (res >= 0) { cout << win << " " << 0 << " " << res << '\n'; f3 = true; } } } if (!f1 && !f2 && !f3) cout << -1 << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, p, w, d; cin >> n >> p >> w >> d; for (int y = 0, ThxDem = w; y < ThxDem; ++y) if (d * y <= p) { long long rem = p - d * y; long long x = rem / w; if (rem % w == 0 && x + y <= n) { cout << x << " " << y << " " << n - x - y << "\n"; return 0; } } cout << "-1\n"; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p, w, d; cin >> n >> p >> w >> d; long long i; bool found = false; for (i = 0; i < w; i++) { if ((p - i * d) % w == 0) { found = true; break; } } if (found) { long long x = (p - i * d) / w; if (x >= 0 && x + i <= n) { cout << x << " " << i << " " << n - x - i << endl; return 0; } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; void solve() { long long n, p, d, w; cin >> n >> p >> w >> d; vector<int> g; int xx = w % d; for (int i = 0; i < d; i++) if (i * 1ll * xx % d == p % d) g.push_back(i); if (g.size() == 0) { cout << -1; return; } long long mx = p / w; int rem = mx % d; int id = lower_bound(g.begin(), g.end(), rem) - g.begin(); if (id >= g.size()) id = g.size() - 1; else if (g[id] > rem) if (mx >= d) { if (id > 0) id--; else id = g.size() - 1, mx = mx / d * d - 1; } else { cout << -1; return; } long long x = mx / d * d + g[id]; if (x * w + (n - x) * d >= p) { long long diff = x * w + (n - x) * d - p; diff /= d; cout << x << " " << n - x - diff << " " << diff; } else cout << -1; } int main() { ; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int tt = 1; for (int i = 1; i <= tt; i++) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int read() { int f = 1, x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -f; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return f * x; } long long n, p, w, d; long long extgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1, y = 0; return a; } else { long long d = extgcd(b, a % b, y, x); y -= (a / b) * x; return d; } } void init() { cin >> n >> p >> w >> d; } void solve() { long long xx, yy; long long dd = extgcd(w, d, xx, yy); if (p % dd != 0) { printf("-1\n"); return; } else { long long ww = w / dd, d_ = d / dd; long long xx1 = (xx * ((p / dd) % d_) % d_ + d_) % d_, yy1 = (p - w * xx1) / d; if (xx1 >= 0 && yy1 >= 0 && xx1 + yy1 <= n) { printf("%I64d %I64d %I64d\n", xx1, yy1, n - xx1 - yy1); return; } long long yy2 = (yy * ((p / dd) % ww) % ww + ww) % ww, xx2 = (p - d * yy2) / w; if (xx2 >= 0 && yy2 >= 0 && xx2 + yy2 <= n) { printf("%I64d %I64d %I64d\n", xx2, yy2, n - xx2 - yy2); return; } } printf("-1\n"); return; } int main() { init(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; void read(int& val) { int x = 0; int bz = 1; char c; for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar()) ; if (c == '-') { bz = -1; c = getchar(); } for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; } const int maxn = 1e2 + 2; int main() { long long n, p, d, w; cin >> n >> p >> w >> d; long long x = -1, y = -1, z; for (int i = 0; i <= w - 1; i++) { if (1LL * i * d > p) break; if ((p - 1LL * i * d) % w == 0) { y = i; x = (p - 1LL * i * d) / w; break; } } if (x + y > n || x == -1) { printf("-1"); return 0; } z = n - x - y; printf("%lld %lld %lld\n", x, y, z); return 0; }
#include <bits/stdc++.h> using namespace std; inline bool scan_d(long long &num) { char in; bool IsN = false; in = getchar(); if (in == EOF) return false; while (in != '-' && (in < '0' || in > '9')) in = getchar(); if (in == '-') { IsN = true; num = 0; } else num = in - '0'; while (in = getchar(), in >= '0' && in <= '9') { num *= 10, num += in - '0'; } if (IsN) num = -num; return true; } int main() { long long n, p; long long w, d; scan_d(n), scan_d(p), scan_d(w), scan_d(d); if (w > d) { long long i = 0; while ((p - i * d) % w && i <= w) { i++; } long long k = (p - i * d) / w; if (i > w || k < 0) { printf("-1\n"); return 0; } if (k + i <= n) { printf("%lld %lld %lld\n", k, i, n - k - i); } else { printf("-1\n"); } } else { long long i = 0; while ((p - i * w) % d && i <= d) { i++; } long long k = (p - i * w) / d; if (i > d || k < 0) { printf("-1\n"); return 0; } if (k + i <= n) { printf("%lld %lld %lld\n", i, k, n - k - i); } else { printf("-1\n"); } } return 0; }
#include <bits/stdc++.h> using namespace std; inline long long llread() { long long x = 0, f = 1; char ch = getchar(); while (!('0' <= ch && ch <= '9')) { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * f; } long long N, P, W, D, x, y, z; long long exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1, y = 0; return a; } long long gcd = exgcd(b, a % b, y, x); y -= a / b * x; return gcd; } inline long long inv(long long x, long long mod) { long long xx, yy; long long gcd = exgcd(x, mod, xx, yy); return (xx % mod + mod) % mod; } int main() { N = llread(), P = llread(), W = llread(), D = llread(); long long gcd = exgcd(W, D, x, y); if (P % gcd != 0) { printf("-1\n"); return 0; } P /= gcd, W /= gcd, D /= gcd; y = P % W * inv(D, W) % W; x = (P - y * D) / W; if (x < 0 || x + y > N) { printf("-1\n"); return 0; } printf("%lld %lld %lld\n", x, y, N - x - y); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 4000 + 50; const int maxm = 2000000 + 1000; const int maxt = 3000000 + 100; const int maxk = 10 + 3; const long long unit = 1LL; const int INF = 0x3f3f3f3f; const long long Inf = 0x3f3f3f3f3f3f3f3fLL; const double eps = 1e-3; const double inf = 1e15; const double pi = acos(-1.0); const long long mod = 998244353LL, gg = 3LL, inv2 = 499122177LL; inline long long read() { long long x = 0; bool t = false; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') t = true, ch = getchar(); while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar(); return t ? -x : x; } long long n, p, w, d; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> p >> w >> d; long long x = 0, y = 0, z = max(0LL, n - p / d); long long ok = false; long long lim = 0; if (!p) lim = n; else lim = n - ((p - 1) / w + 1); for (long long t = 0; t < w - d; ++t) { long long tmp = z + t; if (tmp > lim) break; if ((p - d * (n - tmp)) % (w - d) == 0) { x = (p - d * (n - tmp)) / (w - d); y = n - tmp - x; z = tmp; ok = true; break; } } if (!ok) cout << -1 << '\n'; else cout << x << ' ' << y << ' ' << z << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; long long int X, Y; void modinverse(long long int a, long long int b) { if (b == 0) { X = 1; Y = 0; return; } modinverse(b, a % b); long long int cx = Y; long long int cy = X - (a / b) * Y; X = cx; Y = cy; } long long int gcd(long long int a, long long int b) { return b == 0 ? a : gcd(b, a % b); } void solve() { long long int n, p, w, d; cin >> n >> p >> w >> d; long long int g = gcd(w, d); if (p % g) { cout << -1 << endl; return; } w /= g; d /= g; p /= g; modinverse(d, w); if (X < 0) { X += w; } long long int y = ((p % w) * (X % w)) % w; long long int x = (p - y * d) / w; if (x + y > n) { cout << -1 << endl; return; } if (x < 0) { cout << -1 << endl; return; } long long int z = n - (x + y); cout << x << " " << y << " " << z << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(0); long long int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int inf = 0x3f3f3f3f3f3f3f3fll; long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long int x, y, n, p, w, d, i, j, k; cin >> n >> p >> w >> d; for (i = 0; i < w; i++) { y = i * d; x = p - y; if (x >= 0 && x % w == 0) { if (i + x / w <= n) { cout << x / w << ' ' << i << ' ' << n - i - x / w << endl; return 0; } } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p; long long d, w; cin >> n >> p >> w >> d; for (long long i = 0; i < w; i++) { if ((p - i * d) % w == 0) { if (p - i * d >= 0 && n - i - (p - i * d) / w >= 0) { cout << (p - i * d) / w << " " << i << " " << n - i - (p - i * d) / w << endl; return 0; } } } puts("-1"); }
#include <bits/stdc++.h> long long n, p; int d, w; inline long long read() { long long x = 0; char ch = getchar(); for (; ch > '9' || ch < '0'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x; } inline int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } int main() { n = read(); p = read(); w = read(); d = read(); if (!(p % gcd(w, d))) { for (int y = 0; y < w; ++y) { if ((long long)p - y * d >= 0 && !((p - (long long)y * d) % w)) { long long x = (long long)(p - y * d) / w; if (x + y <= n) { printf("%lld %d %lld", x, y, n - x - y); return 0; } } } } printf("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p, w, d, y, z; cin >> n >> p >> w >> d; for (y = 0; y <= w; y++) { long long kk = y * d; long long pp = p - kk; long long rem = pp % w; long long x = pp / w; if (rem == 0) { if (x >= 0 and x + y <= n) { cout << x << " " << y << " " << n - (x + y) << endl; return 0; } } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; long long n, p, w, d; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> p >> w >> d; for (long long y = 0; y <= 10000000LL; y++) { if (p - y * d >= 0 && (p - y * d) % w == 0) { long long x = (p - y * d) / w; if (x + y <= n) { cout << x << ' ' << y << ' ' << n - (x + y); return 0; } } if (p - y * w >= 0 && (p - y * w) % d == 0) { long long x = (p - y * w) / d; if (x + y <= n) { cout << y << ' ' << x << ' ' << n - (x + y); return 0; } } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, p, w, d; long long exgcd(long long a, long long b, long long &x1, long long &y1) { if (!b) { x1 = 1, y1 = 0; return a; } long long x2, y2; long long d = exgcd(b, a % b, x2, y2); x1 = y2, y1 = x2 - (a / b) * y2; return d; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int i, j, flag = 0; cin >> n >> p >> w >> d; if (w > d) { flag = 1; swap(w, d); } long long x, y; long long g = exgcd(w, d, x, y); if (p % g != 0) cout << -1 << endl; else { long long s = d / g, t = w / g; x = (s + (p / g) % s * (x % s) % s) % s; y = (p - x * w) / d; if (x >= 0 && y >= 0 && x + y <= n) { if (flag) cout << y << ' ' << x << ' ' << n - x - y << endl; else cout << x << ' ' << y << ' ' << n - x - y << endl; } else cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } const long long mod = 1e9 + 7; void test_case() { long long n, p, w, d; cin >> n >> p >> w >> d; for (long long i = (0); i <= (d); i++) { if (p - i * w < 0) break; if ((p - i * w) % d == 0) { long long p1 = i, p2 = ((p - i * w) % (d * w)) / d; long long pg = p - p1 * w - p2 * d; if (p < 0) continue; (w <= d) ? p2 += (pg) / d : p1 += (pg / w); if (p1 + p2 <= n) { cout << p1 << " " << p2 << " " << n - p1 - p2 << "\n"; return; } } } cout << -1 << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t = 1; while (t--) { test_case(); } return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main(void) { long long n, p, w, d; scanf("%lld%lld%lld%lld", &n, &p, &w, &d); long long s = w * d / gcd(w, d); long long y = 0; long long x = 0; while (y * d <= p && y < 2e5) { long long res = p - d * y; if (res % w == 0) { x = res / w; if (x + y <= n && x >= 0 && y >= 0) { printf("%lld %lld %lld", x, y, n - x - y); return 0; } else break; } y++; } y = 0; x = 0; while (x * d <= p && x < 2e5) { long long res = p - w * x; if (res % d == 0) { y = res / d; if (x + y <= n && x >= 0 && y >= 0) { printf("%lld %lld %lld", x, y, n - x - y); return 0; } else break; } x++; } printf("-1"); }
#include <bits/stdc++.h> using namespace std; long long n, k, m, flag, mod, cnt; long long a[500005]; string s; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long x, y, p, w, d; cin >> n >> p >> w >> d; if (p / w > n or (p < d and p)) return cout << "-1" << endl, 0; else { for (int i = 0; i < d; ++i) { x = i, y = (p - w * i) / d; if (p % d == (w * i) % d) { if (x + y <= n) return cout << x << " " << y << " " << n - x - y << endl, 0; else { k = (x + y - n) / (w - d) + ((x + y - n) % (w - d) != 0); if (k == 0) x += d, y -= w; else { x += k * d, y -= k * w; } if (x < 0 or y < 0) return cout << "-1" << endl, 0; return cout << x << " " << y << " " << n - x - y << endl, 0; } } } cout << "-1" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long n, p, w, d; cin >> n >> p >> w >> d; for (long long draws = 0; draws < w; draws++) { long long rem = p - draws * d; if (rem >= 0 && rem % w == 0 && draws + rem / w <= n) { cout << rem / w << " " << draws << " " << n - rem / w - draws << "\n"; return; } } cout << "-1\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; inline void prep() { cin.tie(0); cin.sync_with_stdio(0); }; const double pi = acos(-1.); long long mod = 1000000007; long long mymod(long long num) { if (num < 0 && !(num % mod == 0)) { if (mod == 1) { return 0; } return mod - abs(num) % mod; } else { return num % mod; } } const int biggg = 2000000000; const long long bigggest = 1000000000000000000LL; const double eps = .0000000001; int main() { prep(); long long n, p, w, d; cin >> n >> p >> w >> d; for (int i = 0; i <= 100000 && d * i <= p; i++) { long long mult = d * i; long long rem = p - mult; if (n - rem / w - i < 0) break; if (rem % w == 0) { cout << rem / w << " " << i << " " << n - rem / w - i; return 0; } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; const long long M = 1e7 + 7; long long mul(long long x, long long y, long long m) { long long res = 0; while (y) { if (y & 1) res = (res + x) % m; x = (x + x) % m; y >>= 1; } return res; } long long eGCD(long long a, long long b, long long &x0, long long &y0) { long long tmp, y = x0 = 1, x = y0 = 0; while (b) tmp = x, x = x0 - (a / b) * x, x0 = tmp, tmp = y, y = y0 - (a / b) * y, y0 = tmp, tmp = b, b = a - (a / b) * b, a = tmp; return a; } bool LDE(long long a, long long b, long long &x, long long &y, long long c, long long &g, bool minimizex) { g = eGCD(a, b, x, y); if (c % g) return false; a /= g, b /= g, c /= g; if (minimizex) { x = mul(x, c, b); if (x < 0) x = (x + b) % b; y = (c - a * x) / b; } else { y = mul(y, c, a); if (y < 0) y = (y + a) % a; x = (c - b * y) / a; }; return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, p, w, d; cin >> n >> p >> w >> d; long long x, y, g; if (!LDE(w, d, x, y, p, g, false)) { cout << -1 << endl; } else { if (x < 0) { LDE(w, d, x, y, p, g, true); } if (x >= 0 && y >= 0 && x + y <= n) { cout << x << " " << y << " " << n - x - y << endl; } else { cout << -1 << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int64_t n, p, w, d; cin >> n >> p >> w >> d; int64_t good_x = -1, good_y = -1; if (w == 1) { good_x = 1; } if (d == 1) { good_y = 1; } int64_t p_ost_w = p % w, p_ost_d = p % d; for (int i = 1; i <= d; ++i) { if (i * w % d == p_ost_d) { good_x = i; break; } } for (int i = 1; i <= w; ++i) { if (i * d % w == p_ost_w) { good_y = i; break; } } int64_t try_ans_x = (p - good_y * d) / w, try_ans_y = (p - good_x * w) / d, minus_one = -1, zero_zero = 0; int64_t second_ans_x = (p - try_ans_x * w) / d; int64_t third_ans_x = n - try_ans_x - second_ans_x; int64_t second_ans_y = (p - try_ans_y * d) / w; int64_t third_ans_y = n - second_ans_y - try_ans_y; if (p == 0) { cout << zero_zero << " " << zero_zero << " " << n; } else if (p % w == 0 && p / w <= n) { cout << p / w << " " << zero_zero << " " << n - p / w; } else if (p % d == 0 && p / d <= n) { cout << zero_zero << " " << p / d << " " << n - p / d; } else if (min(good_x, good_y) == -1) { cout << minus_one; } else if (third_ans_x > -1 && (p - try_ans_x * w) % d == 0 && try_ans_x > 0 && second_ans_x > 0) { cout << try_ans_x << " " << second_ans_x << " " << third_ans_x; } else { cout << minus_one; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, p, w, d; cin >> n >> p >> w >> d; long long x = p / w; for (int j = 0; j < 100000000; j++) { if ((p - (x - j) * w) % d == 0) { long long y = (p - (x - j) * w) / d; if (x + y <= n && x - j >= 0 && y >= 0) { cout << x - j << " " << y << " " << n - x - y + j; return 0; } } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, p, w, d; cin >> n >> p >> w >> d; long long above = n * w - p; if (p == 0) { cout << "0 0 " << n << endl; } else if (above < 0 || d > p) { cout << -1 << endl; } else { long long xx = p / w; for (long long k = 0; k <= 1e6; k++) { long long x = xx - k; if (x < 0) break; long long sheng = p - x * w; if (sheng % d != 0) continue; long long y = sheng / d; if (x + y > n) continue; cout << x << " " << y << " " << n - x - y << endl; return 0; } cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long int n, i, ans, p, w, l, d, wp, dp, pp; bool flag; int main() { cin >> n >> p >> wp >> dp; if (p / wp > n) { cout << -1; return 0; } w = p / wp; for (i = 0; i < wp; i++) { if ((p - wp * (w - i)) % dp == 0) { flag = 1; break; } } w -= i; d = (p - wp * w) / dp; l = n - w - d; if (w > -1 and d > -1 and l > -1 and flag) cout << w << " " << d << " " << l; else cout << -1; return 0; }