output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; inline int read() { int ans = 0, fh = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') fh = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0', ch = getchar(); return ans * fh; } const int maxn = 4e5; int n, ql[maxn], qr[maxn], b[maxn], mx, du[maxn]; int head[maxn], nex[maxn], v[maxn], num = 1, vis[maxn], ans[maxn]; inline void lsh() { for (int i = 1; i <= n; i++) { b[++mx] = ql[i], b[++mx] = ql[i] - 1; b[++mx] = qr[i], b[++mx] = qr[i] + 1; } sort(b + 1, b + mx + 1), mx = unique(b + 1, b + mx + 1) - b - 1; for (int i = 1; i <= n; i++) { ql[i] = lower_bound(b + 1, b + mx + 1, ql[i]) - b; qr[i] = lower_bound(b + 1, b + mx + 1, qr[i]) - b; } } inline void add(int x, int y) { v[++num] = y, du[x]++; nex[num] = head[x], head[x] = num; v[++num] = x, du[y]++; nex[num] = head[y], head[y] = num; } void dfs(int x) { for (int &i = head[x]; i; i = nex[i]) { if (vis[i >> 1]) continue; int y = v[i]; vis[i >> 1] = 1; du[x]--, du[y]--; ans[i >> 1] = y > x, dfs(y); } } int main() { n = read(); for (int i = 1; i <= n; i++) ql[i] = read(), qr[i] = read(); lsh(); for (int i = 1; i <= n; i++) add(ql[i], qr[i] + 1); for (int i = 1, las = 0; i <= mx; i++) if (du[i] & 1) { if (!las) las = i; else add(las, i), las = 0; } for (int i = 1; i <= mx; i++) if (du[i]) dfs(i); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }
### Prompt Generate a cpp solution to the following problem: Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| ≀ 1 must be satisfied. A segment [l, r] contains a point x if and only if l ≀ x ≀ r. Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. Input The first line of input contains integer n (1 ≀ n ≀ 105) β€” the number of segments. The i-th of the next n lines contains two integers li and ri (0 ≀ li ≀ ri ≀ 109) β€” the borders of the i-th segment. It's guaranteed that all the segments are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 2 0 2 2 3 Output 0 1 Input 6 1 5 1 3 3 5 2 10 11 11 12 12 Output 0 1 0 1 0 0 ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int read() { int ans = 0, fh = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') fh = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0', ch = getchar(); return ans * fh; } const int maxn = 4e5; int n, ql[maxn], qr[maxn], b[maxn], mx, du[maxn]; int head[maxn], nex[maxn], v[maxn], num = 1, vis[maxn], ans[maxn]; inline void lsh() { for (int i = 1; i <= n; i++) { b[++mx] = ql[i], b[++mx] = ql[i] - 1; b[++mx] = qr[i], b[++mx] = qr[i] + 1; } sort(b + 1, b + mx + 1), mx = unique(b + 1, b + mx + 1) - b - 1; for (int i = 1; i <= n; i++) { ql[i] = lower_bound(b + 1, b + mx + 1, ql[i]) - b; qr[i] = lower_bound(b + 1, b + mx + 1, qr[i]) - b; } } inline void add(int x, int y) { v[++num] = y, du[x]++; nex[num] = head[x], head[x] = num; v[++num] = x, du[y]++; nex[num] = head[y], head[y] = num; } void dfs(int x) { for (int &i = head[x]; i; i = nex[i]) { if (vis[i >> 1]) continue; int y = v[i]; vis[i >> 1] = 1; du[x]--, du[y]--; ans[i >> 1] = y > x, dfs(y); } } int main() { n = read(); for (int i = 1; i <= n; i++) ql[i] = read(), qr[i] = read(); lsh(); for (int i = 1; i <= n; i++) add(ql[i], qr[i] + 1); for (int i = 1, las = 0; i <= mx; i++) if (du[i] & 1) { if (!las) las = i; else add(las, i), las = 0; } for (int i = 1; i <= mx; i++) if (du[i]) dfs(i); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 10; int n, l[MAXN], r[MAXN], sec[MAXN], sz; int ptr[MAXN]; vector<int> adj[MAXN]; short used[MAXN], cur = 1; void dfs(int v, int p = -1) { for (; ptr[v] < adj[v].size();) { int e = adj[v][ptr[v]++]; if (!used[e]) { used[e] = 1; dfs(l[e] ^ r[e] ^ v, e); } } if (~p) { if (v == l[p]) used[p] = 2; else used[p] = 1; } } int main() { scanf("%d", &n); int m = n; for (int i = 0; i < n; i++) { scanf("%d %d", &l[i], &r[i]), l[i] <<= 1, r[i] <<= 1, r[i] |= 1; sec[sz++] = l[i], sec[sz++] = r[i]; } sort(sec, sec + sz); sz = unique(sec, sec + sz) - sec; for (int i = 0; i < n; i++) { l[i] = lower_bound(sec, sec + sz, l[i]) - sec, r[i] = lower_bound(sec, sec + sz, r[i]) - sec; adj[l[i]].push_back(i); adj[r[i]].push_back(i); } int lst = -1; for (int i = 0; i < sz; i++) if (adj[i].size() & 1) { if (~lst) { l[m] = lst, r[m] = i; adj[lst].push_back(m); adj[i].push_back(m); m++; lst = -1; } else lst = i; } for (int i = 0; i < sz; i++) if (ptr[i] < adj[i].size()) dfs(i); for (int e = 0; e < n; e++) printf("%d ", used[e] - 1); printf("\n"); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| ≀ 1 must be satisfied. A segment [l, r] contains a point x if and only if l ≀ x ≀ r. Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. Input The first line of input contains integer n (1 ≀ n ≀ 105) β€” the number of segments. The i-th of the next n lines contains two integers li and ri (0 ≀ li ≀ ri ≀ 109) β€” the borders of the i-th segment. It's guaranteed that all the segments are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 2 0 2 2 3 Output 0 1 Input 6 1 5 1 3 3 5 2 10 11 11 12 12 Output 0 1 0 1 0 0 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 10; int n, l[MAXN], r[MAXN], sec[MAXN], sz; int ptr[MAXN]; vector<int> adj[MAXN]; short used[MAXN], cur = 1; void dfs(int v, int p = -1) { for (; ptr[v] < adj[v].size();) { int e = adj[v][ptr[v]++]; if (!used[e]) { used[e] = 1; dfs(l[e] ^ r[e] ^ v, e); } } if (~p) { if (v == l[p]) used[p] = 2; else used[p] = 1; } } int main() { scanf("%d", &n); int m = n; for (int i = 0; i < n; i++) { scanf("%d %d", &l[i], &r[i]), l[i] <<= 1, r[i] <<= 1, r[i] |= 1; sec[sz++] = l[i], sec[sz++] = r[i]; } sort(sec, sec + sz); sz = unique(sec, sec + sz) - sec; for (int i = 0; i < n; i++) { l[i] = lower_bound(sec, sec + sz, l[i]) - sec, r[i] = lower_bound(sec, sec + sz, r[i]) - sec; adj[l[i]].push_back(i); adj[r[i]].push_back(i); } int lst = -1; for (int i = 0; i < sz; i++) if (adj[i].size() & 1) { if (~lst) { l[m] = lst, r[m] = i; adj[lst].push_back(m); adj[i].push_back(m); m++; lst = -1; } else lst = i; } for (int i = 0; i < sz; i++) if (ptr[i] < adj[i].size()) dfs(i); for (int e = 0; e < n; e++) printf("%d ", used[e] - 1); printf("\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 222222; int n, p[N], c[N], a, b, i; pair<int, int> s[N]; int dp(int x, int t) { if (c[x]) return c[x]; c[x] = t; dp(p[x << 1] >> 1, p[x << 1] & 1 ^ t ^ 1); dp(p[x << 1 | 1] >> 1, p[x << 1 | 1] & 1 ^ t); return t; } int main() { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &a, &b); s[i << 1] = make_pair(a, i << 1); s[i << 1 | 1] = make_pair(b + 1, i << 1 | 1); } sort(s, s + n + n); for (i = 0; i < n; i++) { a = s[i << 1].second, b = s[i << 1 | 1].second; p[a] = b, p[b] = a; } for (int i = 0; i < n; i++) printf("%d ", dp(i, 2) ^ 2); return 0; }
### Prompt Create a solution in CPP for the following problem: Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| ≀ 1 must be satisfied. A segment [l, r] contains a point x if and only if l ≀ x ≀ r. Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. Input The first line of input contains integer n (1 ≀ n ≀ 105) β€” the number of segments. The i-th of the next n lines contains two integers li and ri (0 ≀ li ≀ ri ≀ 109) β€” the borders of the i-th segment. It's guaranteed that all the segments are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 2 0 2 2 3 Output 0 1 Input 6 1 5 1 3 3 5 2 10 11 11 12 12 Output 0 1 0 1 0 0 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 222222; int n, p[N], c[N], a, b, i; pair<int, int> s[N]; int dp(int x, int t) { if (c[x]) return c[x]; c[x] = t; dp(p[x << 1] >> 1, p[x << 1] & 1 ^ t ^ 1); dp(p[x << 1 | 1] >> 1, p[x << 1 | 1] & 1 ^ t); return t; } int main() { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &a, &b); s[i << 1] = make_pair(a, i << 1); s[i << 1 | 1] = make_pair(b + 1, i << 1 | 1); } sort(s, s + n + n); for (i = 0; i < n; i++) { a = s[i << 1].second, b = s[i << 1 | 1].second; p[a] = b, p[b] = a; } for (int i = 0; i < n; i++) printf("%d ", dp(i, 2) ^ 2); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; int n, fir[MAXN], nxt[MAXN << 1], to[MAXN << 1], ans[MAXN << 1], vis[MAXN << 1], tot = 1, d[MAXN << 1]; int d1[MAXN << 1], d2[MAXN << 1]; int l[MAXN], r[MAXN], t[MAXN << 1], cnt; void add(int u, int v) { to[++tot] = v; nxt[tot] = fir[u]; fir[u] = tot; } void dfs(int x) { for (int &i = fir[x]; i; i = nxt[i]) { int v = to[i]; if (vis[i >> 1]) continue; vis[i >> 1] = 1; if ((i >> 1) <= n) ans[i >> 1] = i & 1; dfs(v); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d %d", &l[i], &r[i]); r[i]++; t[i] = l[i]; t[i + n] = r[i]; } sort(t + 1, t + 2 * n + 1); int k = unique(t + 1, t + 2 * n + 1) - t - 1; for (int i = 1; i <= n; i++) { l[i] = lower_bound(t + 1, t + k + 1, l[i]) - t; r[i] = lower_bound(t + 1, t + k + 1, r[i]) - t; add(l[i], r[i]); add(r[i], l[i]); d1[l[i]]++; d2[r[i]]++; } for (int i = 1; i <= k; i++) { d[i] = d[i - 1] + d1[i] - d2[i]; if (d[i] & 1) { add(i, i + 1); add(i + 1, i); } } for (int i = 1; i <= k; i++) dfs(i); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }
### Prompt Develop a solution in cpp to the problem described below: Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| ≀ 1 must be satisfied. A segment [l, r] contains a point x if and only if l ≀ x ≀ r. Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. Input The first line of input contains integer n (1 ≀ n ≀ 105) β€” the number of segments. The i-th of the next n lines contains two integers li and ri (0 ≀ li ≀ ri ≀ 109) β€” the borders of the i-th segment. It's guaranteed that all the segments are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 2 0 2 2 3 Output 0 1 Input 6 1 5 1 3 3 5 2 10 11 11 12 12 Output 0 1 0 1 0 0 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; int n, fir[MAXN], nxt[MAXN << 1], to[MAXN << 1], ans[MAXN << 1], vis[MAXN << 1], tot = 1, d[MAXN << 1]; int d1[MAXN << 1], d2[MAXN << 1]; int l[MAXN], r[MAXN], t[MAXN << 1], cnt; void add(int u, int v) { to[++tot] = v; nxt[tot] = fir[u]; fir[u] = tot; } void dfs(int x) { for (int &i = fir[x]; i; i = nxt[i]) { int v = to[i]; if (vis[i >> 1]) continue; vis[i >> 1] = 1; if ((i >> 1) <= n) ans[i >> 1] = i & 1; dfs(v); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d %d", &l[i], &r[i]); r[i]++; t[i] = l[i]; t[i + n] = r[i]; } sort(t + 1, t + 2 * n + 1); int k = unique(t + 1, t + 2 * n + 1) - t - 1; for (int i = 1; i <= n; i++) { l[i] = lower_bound(t + 1, t + k + 1, l[i]) - t; r[i] = lower_bound(t + 1, t + k + 1, r[i]) - t; add(l[i], r[i]); add(r[i], l[i]); d1[l[i]]++; d2[r[i]]++; } for (int i = 1; i <= k; i++) { d[i] = d[i - 1] + d1[i] - d2[i]; if (d[i] & 1) { add(i, i + 1); add(i + 1, i); } } for (int i = 1; i <= k; i++) dfs(i); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; } ```
#include <bits/stdc++.h> const int maxn = 1e6 + 9; int Read() { int x(0), f(1); char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3ll) + (x << 1ll) + c - '0'; c = getchar(); } return x * f; } struct node { int to, nxt, id; } dis[maxn]; struct Edge { int u, v, id; }; int n, num, tot; int head[maxn], ans[maxn], l[maxn], r[maxn], a[maxn], deg[maxn], vis[maxn], col[2][maxn], mark[maxn]; std::vector<Edge> edge; std::vector<int> V[maxn]; void Add(int u, int v, int id0) { dis[++num] = (node){v, head[u], id0}; head[u] = num; } void Dfs(int u) { mark[u] = 1; for (int i = head[u]; i != -1; i = dis[i].nxt) if (!vis[i]) { int v(dis[i].to); ans[dis[i].id] = (u < v); vis[i] = vis[i ^ 1] = 1; Dfs(v); } } int main() { n = Read(); for (int i = 1; i <= n; ++i) { l[i] = Read(); r[i] = Read() + 1; a[++tot] = l[i]; a[++tot] = r[i]; } std::sort(a + 1, a + 1 + tot); tot = std::unique(a + 1, a + 1 + tot) - a - 1; num = -1; for (int i = 1; i <= tot; ++i) head[i] = -1; for (int i = 1; i <= n; ++i) { l[i] = std::lower_bound(a + 1, a + 1 + tot, l[i]) - a; r[i] = std::lower_bound(a + 1, a + 1 + tot, r[i]) - a; deg[l[i]]++; deg[r[i]]++; Add(l[i], r[i], i); Add(r[i], l[i], i); } int lst(0); for (int i = 1; i <= tot; ++i) if (deg[i] & 1) { if (!lst) lst = i; else { Add(lst, i, 0); Add(i, lst, 0); lst = 0; } } for (int i = 1; i <= tot; ++i) if (!mark[i]) Dfs(i); for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); puts(""); return 0; }
### Prompt Develop a solution in cpp to the problem described below: Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| ≀ 1 must be satisfied. A segment [l, r] contains a point x if and only if l ≀ x ≀ r. Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. Input The first line of input contains integer n (1 ≀ n ≀ 105) β€” the number of segments. The i-th of the next n lines contains two integers li and ri (0 ≀ li ≀ ri ≀ 109) β€” the borders of the i-th segment. It's guaranteed that all the segments are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 2 0 2 2 3 Output 0 1 Input 6 1 5 1 3 3 5 2 10 11 11 12 12 Output 0 1 0 1 0 0 ### Response ```cpp #include <bits/stdc++.h> const int maxn = 1e6 + 9; int Read() { int x(0), f(1); char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3ll) + (x << 1ll) + c - '0'; c = getchar(); } return x * f; } struct node { int to, nxt, id; } dis[maxn]; struct Edge { int u, v, id; }; int n, num, tot; int head[maxn], ans[maxn], l[maxn], r[maxn], a[maxn], deg[maxn], vis[maxn], col[2][maxn], mark[maxn]; std::vector<Edge> edge; std::vector<int> V[maxn]; void Add(int u, int v, int id0) { dis[++num] = (node){v, head[u], id0}; head[u] = num; } void Dfs(int u) { mark[u] = 1; for (int i = head[u]; i != -1; i = dis[i].nxt) if (!vis[i]) { int v(dis[i].to); ans[dis[i].id] = (u < v); vis[i] = vis[i ^ 1] = 1; Dfs(v); } } int main() { n = Read(); for (int i = 1; i <= n; ++i) { l[i] = Read(); r[i] = Read() + 1; a[++tot] = l[i]; a[++tot] = r[i]; } std::sort(a + 1, a + 1 + tot); tot = std::unique(a + 1, a + 1 + tot) - a - 1; num = -1; for (int i = 1; i <= tot; ++i) head[i] = -1; for (int i = 1; i <= n; ++i) { l[i] = std::lower_bound(a + 1, a + 1 + tot, l[i]) - a; r[i] = std::lower_bound(a + 1, a + 1 + tot, r[i]) - a; deg[l[i]]++; deg[r[i]]++; Add(l[i], r[i], i); Add(r[i], l[i], i); } int lst(0); for (int i = 1; i <= tot; ++i) if (deg[i] & 1) { if (!lst) lst = i; else { Add(lst, i, 0); Add(i, lst, 0); lst = 0; } } for (int i = 1; i <= tot; ++i) if (!mark[i]) Dfs(i); for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); puts(""); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxN = 101000 * 2; const int maxM = maxN * 4; int n, m; int num, Num[maxN], L[maxN], R[maxN], Dg[maxN]; int edgecnt = -1, Hd[maxN], Nt[maxM], V[maxM], vis[maxM]; void Add_Edge(int u, int v); void dfs(int u); int main() { memset(Hd, -1, sizeof(Hd)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &L[i], &R[i]), ++R[i], Num[++num] = L[i], Num[++num] = R[i]; sort(&Num[1], &Num[num + 1]); num = unique(&Num[1], &Num[num + 1]) - Num - 1; for (int i = 1; i <= n; i++) L[i] = (lower_bound(&Num[1], &Num[num + 1], L[i]) - Num), R[i] = (lower_bound(&Num[1], &Num[num + 1], R[i]) - Num), Add_Edge(L[i], R[i]); for (int i = 1, lst = 0; i <= num; i++) if (Dg[i] & 1) if (lst) Add_Edge(lst, i), lst = 0; else lst = i; for (int i = 0; i < n; i++) if (!vis[i]) dfs(V[i << 1]); for (int i = 0; i < n; i++) printf("%d ", vis[i] - 1); printf("\n"); return 0; } void Add_Edge(int u, int v) { ++Dg[u]; ++Dg[v]; Nt[++edgecnt] = Hd[u]; Hd[u] = edgecnt; V[edgecnt] = v; Nt[++edgecnt] = Hd[v]; Hd[v] = edgecnt; V[edgecnt] = u; return; } void dfs(int u) { for (int &i = Hd[u]; i != -1;) if (!vis[i >> 1]) { int j = i; i = Nt[i]; vis[j >> 1] = ((j & 1) ? 1 : 2); dfs(V[j]); } else i = Nt[i]; return; }
### Prompt Your task is to create a CPP solution to the following problem: Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| ≀ 1 must be satisfied. A segment [l, r] contains a point x if and only if l ≀ x ≀ r. Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. Input The first line of input contains integer n (1 ≀ n ≀ 105) β€” the number of segments. The i-th of the next n lines contains two integers li and ri (0 ≀ li ≀ ri ≀ 109) β€” the borders of the i-th segment. It's guaranteed that all the segments are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 2 0 2 2 3 Output 0 1 Input 6 1 5 1 3 3 5 2 10 11 11 12 12 Output 0 1 0 1 0 0 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxN = 101000 * 2; const int maxM = maxN * 4; int n, m; int num, Num[maxN], L[maxN], R[maxN], Dg[maxN]; int edgecnt = -1, Hd[maxN], Nt[maxM], V[maxM], vis[maxM]; void Add_Edge(int u, int v); void dfs(int u); int main() { memset(Hd, -1, sizeof(Hd)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &L[i], &R[i]), ++R[i], Num[++num] = L[i], Num[++num] = R[i]; sort(&Num[1], &Num[num + 1]); num = unique(&Num[1], &Num[num + 1]) - Num - 1; for (int i = 1; i <= n; i++) L[i] = (lower_bound(&Num[1], &Num[num + 1], L[i]) - Num), R[i] = (lower_bound(&Num[1], &Num[num + 1], R[i]) - Num), Add_Edge(L[i], R[i]); for (int i = 1, lst = 0; i <= num; i++) if (Dg[i] & 1) if (lst) Add_Edge(lst, i), lst = 0; else lst = i; for (int i = 0; i < n; i++) if (!vis[i]) dfs(V[i << 1]); for (int i = 0; i < n; i++) printf("%d ", vis[i] - 1); printf("\n"); return 0; } void Add_Edge(int u, int v) { ++Dg[u]; ++Dg[v]; Nt[++edgecnt] = Hd[u]; Hd[u] = edgecnt; V[edgecnt] = v; Nt[++edgecnt] = Hd[v]; Hd[v] = edgecnt; V[edgecnt] = u; return; } void dfs(int u) { for (int &i = Hd[u]; i != -1;) if (!vis[i >> 1]) { int j = i; i = Nt[i]; vis[j >> 1] = ((j & 1) ? 1 : 2); dfs(V[j]); } else i = Nt[i]; return; } ```
#include <bits/stdc++.h> using namespace std; const int N = 100030; pair<int, int> l[N], r[N], tot[N * 2]; int n, cnt; vector<int> mp[N << 1]; int vis[N << 1]; int read() { char c = getchar(); int k = 0; for (; c < 48 || c > 57; c = getchar()) ; for (; c > 47 && c < 58; c = getchar()) k = (k << 3) + (k << 1) + c - 48; return k; } void dfs(int p, int mat) { if (vis[p] != -1) return; vis[p] = mat; for (int i = 0; i < (int)mp[p].size(); i++) dfs(mp[p][i], mat ^ 1); } int main() { n = read(); for (int i = 0, a, b; i < n; i++) { a = read(), b = read(); b++; l[i] = make_pair(a, i << 1); r[i] = make_pair(b, i << 1 | 1); tot[cnt++] = l[i]; tot[cnt++] = r[i]; mp[i * 2].push_back(i * 2 + 1); mp[i * 2 + 1].push_back(i * 2); } memset(vis, -1, sizeof vis); sort(tot, tot + cnt); for (int i = 0; i < cnt; i += 2) { mp[tot[i].second].push_back(tot[i + 1].second); mp[tot[i + 1].second].push_back(tot[i].second); } for (int i = 0; i < cnt; i++) dfs(i, 0); for (int i = 0; i < cnt; i += 2) printf("%d ", vis[i]); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| ≀ 1 must be satisfied. A segment [l, r] contains a point x if and only if l ≀ x ≀ r. Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. Input The first line of input contains integer n (1 ≀ n ≀ 105) β€” the number of segments. The i-th of the next n lines contains two integers li and ri (0 ≀ li ≀ ri ≀ 109) β€” the borders of the i-th segment. It's guaranteed that all the segments are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 2 0 2 2 3 Output 0 1 Input 6 1 5 1 3 3 5 2 10 11 11 12 12 Output 0 1 0 1 0 0 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 100030; pair<int, int> l[N], r[N], tot[N * 2]; int n, cnt; vector<int> mp[N << 1]; int vis[N << 1]; int read() { char c = getchar(); int k = 0; for (; c < 48 || c > 57; c = getchar()) ; for (; c > 47 && c < 58; c = getchar()) k = (k << 3) + (k << 1) + c - 48; return k; } void dfs(int p, int mat) { if (vis[p] != -1) return; vis[p] = mat; for (int i = 0; i < (int)mp[p].size(); i++) dfs(mp[p][i], mat ^ 1); } int main() { n = read(); for (int i = 0, a, b; i < n; i++) { a = read(), b = read(); b++; l[i] = make_pair(a, i << 1); r[i] = make_pair(b, i << 1 | 1); tot[cnt++] = l[i]; tot[cnt++] = r[i]; mp[i * 2].push_back(i * 2 + 1); mp[i * 2 + 1].push_back(i * 2); } memset(vis, -1, sizeof vis); sort(tot, tot + cnt); for (int i = 0; i < cnt; i += 2) { mp[tot[i].second].push_back(tot[i + 1].second); mp[tot[i + 1].second].push_back(tot[i].second); } for (int i = 0; i < cnt; i++) dfs(i, 0); for (int i = 0; i < cnt; i += 2) printf("%d ", vis[i]); return 0; } ```
#include <bits/stdc++.h> const int N = 400050; int l[N], r[N], num[N], nk; int n, tot; int id(int v) { int L = 1, R = nk, mid; while (L <= R) { mid = (L + R) / 2; if (num[mid] == v) return mid; if (num[mid] < v) L = mid + 1; else R = mid - 1; } } int first[2 * N], to[4 * N], next[4 * N], real[4 * N], size; int d[2 * N]; void put(int x, int y, int rr) { next[++size] = first[x]; to[first[x] = size] = y; real[size] = rr; d[x]++; } int ans[2 * N], V[2 * N]; void dfs(int v) { for (int &k = first[v]; k; k = next[k]) if (!ans[real[k]]) { if (v < to[k]) ans[real[k]] = 1; else ans[real[k]] = -1; dfs(to[k]); } V[v] = 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &l[i], &r[i]); for (int i = 1; i <= n; i++) num[i] = l[i] = l[i] * 2, num[i + n] = r[i] = r[i] * 2 + 1; std::sort(num + 1, num + 2 * n + 1); num[0] = -1; for (int i = 1; i <= 2 * n; i++) if (num[i] != num[i - 1]) num[++nk] = num[i]; for (int i = 1; i <= n; i++) l[i] = id(l[i]), r[i] = id(r[i]); for (int i = 1; i <= n; i++) { put(l[i], r[i], ++tot); put(r[i], l[i], tot); } printf("\n"); for (int i = 1, t = 0, last = 0; i <= nk + 1; i++) { if (d[i] & 1) { t ^= 1; if (!t) put(i, last, ++tot), put(last, i, tot); last = i; } } for (int i = 1; i <= nk + 1; i++) dfs(i); for (int i = 1; i <= n; i++) { if (ans[i] + 1) printf("1 "); else printf("0 "); } }
### Prompt Your challenge is to write a Cpp solution to the following problem: Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| ≀ 1 must be satisfied. A segment [l, r] contains a point x if and only if l ≀ x ≀ r. Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. Input The first line of input contains integer n (1 ≀ n ≀ 105) β€” the number of segments. The i-th of the next n lines contains two integers li and ri (0 ≀ li ≀ ri ≀ 109) β€” the borders of the i-th segment. It's guaranteed that all the segments are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 2 0 2 2 3 Output 0 1 Input 6 1 5 1 3 3 5 2 10 11 11 12 12 Output 0 1 0 1 0 0 ### Response ```cpp #include <bits/stdc++.h> const int N = 400050; int l[N], r[N], num[N], nk; int n, tot; int id(int v) { int L = 1, R = nk, mid; while (L <= R) { mid = (L + R) / 2; if (num[mid] == v) return mid; if (num[mid] < v) L = mid + 1; else R = mid - 1; } } int first[2 * N], to[4 * N], next[4 * N], real[4 * N], size; int d[2 * N]; void put(int x, int y, int rr) { next[++size] = first[x]; to[first[x] = size] = y; real[size] = rr; d[x]++; } int ans[2 * N], V[2 * N]; void dfs(int v) { for (int &k = first[v]; k; k = next[k]) if (!ans[real[k]]) { if (v < to[k]) ans[real[k]] = 1; else ans[real[k]] = -1; dfs(to[k]); } V[v] = 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &l[i], &r[i]); for (int i = 1; i <= n; i++) num[i] = l[i] = l[i] * 2, num[i + n] = r[i] = r[i] * 2 + 1; std::sort(num + 1, num + 2 * n + 1); num[0] = -1; for (int i = 1; i <= 2 * n; i++) if (num[i] != num[i - 1]) num[++nk] = num[i]; for (int i = 1; i <= n; i++) l[i] = id(l[i]), r[i] = id(r[i]); for (int i = 1; i <= n; i++) { put(l[i], r[i], ++tot); put(r[i], l[i], tot); } printf("\n"); for (int i = 1, t = 0, last = 0; i <= nk + 1; i++) { if (d[i] & 1) { t ^= 1; if (!t) put(i, last, ++tot), put(last, i, tot); last = i; } } for (int i = 1; i <= nk + 1; i++) dfs(i); for (int i = 1; i <= n; i++) { if (ans[i] + 1) printf("1 "); else printf("0 "); } } ```
#include <bits/stdc++.h> using namespace std; struct sweep { pair<int, int> P; int idx; }; int N, L; sweep B[2 * 100005]; vector<int> G[2 * 100005]; int color[2 * 100005]; bool qx(sweep a, sweep b) { return a.P < b.P; } void doIt(int node) { for (auto it : G[node]) if (!color[it]) color[it] = -1 * color[node], doIt(it); } int main() { cin >> N; for (int i = 1; i <= N; i++) { int l, r; cin >> l >> r; L++; B[L] = {{l, -1}, i}; L++; B[L] = {{r, 1}, N + i}; G[i].push_back(N + i); G[N + i].push_back(i); } sort(B + 1, B + L + 1, qx); for (int i = 1; i < L; i += 2) G[B[i].idx].push_back(B[i + 1].idx), G[B[i + 1].idx].push_back(B[i].idx); for (int i = 1; i <= L; i++) if (!color[i]) { color[i] = 1; doIt(i); } for (int i = 1; i <= N; i++) cout << max(0, color[i]) << " "; return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct segments [li, ri] on the OX axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point x of the OX axis consider all the segments that contains point x; suppose, that rx red segments and bx blue segments contain point x; for each point x inequality |rx - bx| ≀ 1 must be satisfied. A segment [l, r] contains a point x if and only if l ≀ x ≀ r. Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. Input The first line of input contains integer n (1 ≀ n ≀ 105) β€” the number of segments. The i-th of the next n lines contains two integers li and ri (0 ≀ li ≀ ri ≀ 109) β€” the borders of the i-th segment. It's guaranteed that all the segments are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers; each integer must be 0 or 1. The i-th number denotes the color of the i-th segment (0 is red and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 2 0 2 2 3 Output 0 1 Input 6 1 5 1 3 3 5 2 10 11 11 12 12 Output 0 1 0 1 0 0 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct sweep { pair<int, int> P; int idx; }; int N, L; sweep B[2 * 100005]; vector<int> G[2 * 100005]; int color[2 * 100005]; bool qx(sweep a, sweep b) { return a.P < b.P; } void doIt(int node) { for (auto it : G[node]) if (!color[it]) color[it] = -1 * color[node], doIt(it); } int main() { cin >> N; for (int i = 1; i <= N; i++) { int l, r; cin >> l >> r; L++; B[L] = {{l, -1}, i}; L++; B[L] = {{r, 1}, N + i}; G[i].push_back(N + i); G[N + i].push_back(i); } sort(B + 1, B + L + 1, qx); for (int i = 1; i < L; i += 2) G[B[i].idx].push_back(B[i + 1].idx), G[B[i + 1].idx].push_back(B[i].idx); for (int i = 1; i <= L; i++) if (!color[i]) { color[i] = 1; doIt(i); } for (int i = 1; i <= N; i++) cout << max(0, color[i]) << " "; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long q, t, n, k, d1, d2, x, y; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; q = k; k = n - k; if (k - (d1 + d2) >= 0) { x = (d1 + d2 + q) / 3; y = (q - d1 - d2) / 3; if ((k - (d1 + d2)) % 3 == 0 && (d1 + d2 + q) % 3 == 0 && (x - d1 >= 0) && (x - d2) >= 0) { cout << "yes\n"; continue; } } if ((k - (2 * max(d1, d2) - min(d1, d2))) >= 0) { x = (q - d1 - d2) / 3; if ((k - (2 * max(d1, d2) - min(d1, d2))) % 3 == 0 && (q - d1 - d2) % 3 == 0 && x >= 0) { cout << "yes\n"; continue; } } if (k - (d1 + 2 * d2) >= 0) { x = (q + d1 - d2) / 3; if ((k - (d1 + 2 * d2)) % 3 == 0 && (q + d1 - d2) % 3 == 0 && (x - d1) >= 0) { cout << "yes\n"; continue; } } if (k - (2 * d1 + d2) >= 0) { x = (q - d1 + d2) / 3; if ((k - (2 * d1 + d2)) % 3 == 0 && (q - d1 + d2) % 3 == 0 && (x - d2) >= 0) { cout << "yes\n"; continue; } } cout << "no\n"; } }
### Prompt Generate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long q, t, n, k, d1, d2, x, y; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; q = k; k = n - k; if (k - (d1 + d2) >= 0) { x = (d1 + d2 + q) / 3; y = (q - d1 - d2) / 3; if ((k - (d1 + d2)) % 3 == 0 && (d1 + d2 + q) % 3 == 0 && (x - d1 >= 0) && (x - d2) >= 0) { cout << "yes\n"; continue; } } if ((k - (2 * max(d1, d2) - min(d1, d2))) >= 0) { x = (q - d1 - d2) / 3; if ((k - (2 * max(d1, d2) - min(d1, d2))) % 3 == 0 && (q - d1 - d2) % 3 == 0 && x >= 0) { cout << "yes\n"; continue; } } if (k - (d1 + 2 * d2) >= 0) { x = (q + d1 - d2) / 3; if ((k - (d1 + 2 * d2)) % 3 == 0 && (q + d1 - d2) % 3 == 0 && (x - d1) >= 0) { cout << "yes\n"; continue; } } if (k - (2 * d1 + d2) >= 0) { x = (q - d1 + d2) / 3; if ((k - (2 * d1 + d2)) % 3 == 0 && (q - d1 + d2) % 3 == 0 && (x - d2) >= 0) { cout << "yes\n"; continue; } } cout << "no\n"; } } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2, ave, x; int main() { int T; cin >> T; while (T--) { cin >> n >> k >> d1 >> d2; if (n % 3) { puts("no"); continue; } ave = n / 3; bool flag = false; if ((k - d1 - 2 * d2) % 3 == 0 && (k - d1 - 2 * d2) >= 0) { x = (k - d1 - 2 * d2) / 3; if (x + d1 + d2 <= ave) flag = true; } if ((k - d1 - d2) % 3 == 0 && (k - d1 - d2) >= 0) { x = (k - d1 - d2) / 3; if (x + d1 <= ave && x + d2 <= ave) flag = true; } if ((k - 2 * d1 - d2) % 3 == 0 && (k - 2 * d1 - d2) >= 0) { x = (k - 2 * d1 - d2) / 3; if (x + d1 + d2 <= ave) flag = true; } if ((k + d2 - 2 * d1) % 3 == 0 && (k + d2 - 2 * d1) >= 0 && (k + d1 - 2 * d2) % 3 == 0 && (k + d1 - 2 * d2) >= 0) { x = (k + d2 - 2 * d1) / 3; if (x + d1 <= ave) flag = true; } if (flag) puts("yes"); else puts("no"); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2, ave, x; int main() { int T; cin >> T; while (T--) { cin >> n >> k >> d1 >> d2; if (n % 3) { puts("no"); continue; } ave = n / 3; bool flag = false; if ((k - d1 - 2 * d2) % 3 == 0 && (k - d1 - 2 * d2) >= 0) { x = (k - d1 - 2 * d2) / 3; if (x + d1 + d2 <= ave) flag = true; } if ((k - d1 - d2) % 3 == 0 && (k - d1 - d2) >= 0) { x = (k - d1 - d2) / 3; if (x + d1 <= ave && x + d2 <= ave) flag = true; } if ((k - 2 * d1 - d2) % 3 == 0 && (k - 2 * d1 - d2) >= 0) { x = (k - 2 * d1 - d2) / 3; if (x + d1 + d2 <= ave) flag = true; } if ((k + d2 - 2 * d1) % 3 == 0 && (k + d2 - 2 * d1) >= 0 && (k + d1 - 2 * d2) % 3 == 0 && (k + d1 - 2 * d2) >= 0) { x = (k + d2 - 2 * d1) / 3; if (x + d1 <= ave) flag = true; } if (flag) puts("yes"); else puts("no"); } return 0; } ```
#include <bits/stdc++.h> int inf = 1e9 + 7; using namespace std; long long n, k, d1, d2; long long a[4]; int judge() { long long r = n - k, sum = 0; sort(a, a + 3); if (a[0] < 0) { long long ad = -a[0]; for (int i = 0; i < (3); i++) a[i] += ad; } for (int i = 0; i < (3); i++) sum += a[i]; if (sum > k) return 0; if ((k - sum) % 3) return 0; sum = 2 * a[2] - a[0] - a[1]; if (sum > r) return 0; if ((r - sum) % 3) return 0; return 1; } int main() { int rep; cin >> rep; while (rep--) { cin >> n >> k >> d1 >> d2; long long ans = 0; for (long long i = -1; i <= 1; i += 2) for (long long j = -1; j <= 1; j += 2) a[0] = i * d1, a[1] = 0, a[2] = j * d2, ans += judge(); if (ans) puts("yes"); else puts("no"); } }
### Prompt Generate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> int inf = 1e9 + 7; using namespace std; long long n, k, d1, d2; long long a[4]; int judge() { long long r = n - k, sum = 0; sort(a, a + 3); if (a[0] < 0) { long long ad = -a[0]; for (int i = 0; i < (3); i++) a[i] += ad; } for (int i = 0; i < (3); i++) sum += a[i]; if (sum > k) return 0; if ((k - sum) % 3) return 0; sum = 2 * a[2] - a[0] - a[1]; if (sum > r) return 0; if ((r - sum) % 3) return 0; return 1; } int main() { int rep; cin >> rep; while (rep--) { cin >> n >> k >> d1 >> d2; long long ans = 0; for (long long i = -1; i <= 1; i += 2) for (long long j = -1; j <= 1; j += 2) a[0] = i * d1, a[1] = 0, a[2] = j * d2, ans += judge(); if (ans) puts("yes"); else puts("no"); } } ```
#include <bits/stdc++.h> using namespace std; int check(long long int n, long long int k, long long int d1, long long int d2) { long long int A[4] = {1, -1, -1, 1}, B[4] = {1, -1, 1, -1}, x, a, b, c, D1, D2; x = n / 3; if (n % 3 != 0) return 0; for (int i = (0); i < (4); ++i) { D1 = d1 * A[i]; D2 = d2 * B[i]; if ((k + 2 * D1 + D2) % 3 == 0) { a = (k + 2 * D1 + D2) / 3; b = a - D1; c = b - D2; if (a <= x && b <= x && c <= x && a >= 0 && b >= 0 && c >= 0) return 1; } } return 0; } int main() { long long int t, n, k, d1, d2; scanf("%llu", &t); while (t--) { cin >> n >> k >> d1 >> d2; if (check(n, k, d1, d2)) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int check(long long int n, long long int k, long long int d1, long long int d2) { long long int A[4] = {1, -1, -1, 1}, B[4] = {1, -1, 1, -1}, x, a, b, c, D1, D2; x = n / 3; if (n % 3 != 0) return 0; for (int i = (0); i < (4); ++i) { D1 = d1 * A[i]; D2 = d2 * B[i]; if ((k + 2 * D1 + D2) % 3 == 0) { a = (k + 2 * D1 + D2) / 3; b = a - D1; c = b - D2; if (a <= x && b <= x && c <= x && a >= 0 && b >= 0 && c >= 0) return 1; } } return 0; } int main() { long long int t, n, k, d1, d2; scanf("%llu", &t); while (t--) { cin >> n >> k >> d1 >> d2; if (check(n, k, d1, d2)) cout << "yes" << endl; else cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> int t; long long n, k, x, y, z; int chk(long long d1, long long d2) { y = (k + d2 - d1); if (y % 3 != 0) { return 0; } y /= 3; x = y + d1; z = y - d2; if (x < 0 || y < 0 || z < 0) { return 0; } if (x > n || y > n || z > n) { return 0; } return 1; } int main() { scanf("%d", &t); long long d1, d2; while (t--) { scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } n /= 3; if (chk(d1, -d2) || chk(d1, d2) || chk(-d1, d2) || chk(-d1, -d2)) { printf("yes\n"); continue; } printf("no\n"); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> int t; long long n, k, x, y, z; int chk(long long d1, long long d2) { y = (k + d2 - d1); if (y % 3 != 0) { return 0; } y /= 3; x = y + d1; z = y - d2; if (x < 0 || y < 0 || z < 0) { return 0; } if (x > n || y > n || z > n) { return 0; } return 1; } int main() { scanf("%d", &t); long long d1, d2; while (t--) { scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } n /= 3; if (chk(d1, -d2) || chk(d1, d2) || chk(-d1, d2) || chk(-d1, -d2)) { printf("yes\n"); continue; } printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; bool solve(int64_t n, int64_t k, int64_t d1, int64_t d2) { if (n % 3 != 0) return false; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 || j == 0) continue; int64_t D1 = d1 * i; int64_t D2 = d2 * j; int64_t x2 = (k + D2 - D1) / 3; if ((k + D2 - D1) % 3 != 0) continue; if (x2 >= 0 && x2 <= k) { int64_t x1 = D1 + x2; int64_t x3 = x2 - D2; if (x1 >= 0 && x3 >= 0 && x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) return true; } } } return false; } int main() { int t; cin >> t; for (int m = 0; m < t; m++) { int64_t n, k, d1, d2; cin >> n; cin >> k; cin >> d1; cin >> d2; bool ans = solve(n, k, d1, d2); if (ans) cout << "yes\n"; else cout << "no\n"; } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool solve(int64_t n, int64_t k, int64_t d1, int64_t d2) { if (n % 3 != 0) return false; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 || j == 0) continue; int64_t D1 = d1 * i; int64_t D2 = d2 * j; int64_t x2 = (k + D2 - D1) / 3; if ((k + D2 - D1) % 3 != 0) continue; if (x2 >= 0 && x2 <= k) { int64_t x1 = D1 + x2; int64_t x3 = x2 - D2; if (x1 >= 0 && x3 >= 0 && x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) return true; } } } return false; } int main() { int t; cin >> t; for (int m = 0; m < t; m++) { int64_t n, k, d1, d2; cin >> n; cin >> k; cin >> d1; cin >> d2; bool ans = solve(n, k, d1, d2); if (ans) cout << "yes\n"; else cout << "no\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { long long n, k, d1, d2; scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); if (n % 3 != 0) { cout << "no\n"; continue; } long long gamesRem = n - k; bool poss = false; for (int sign1 = -1; sign1 <= 1; sign1 += 2) { for (int sign2 = -1; sign2 <= 1; sign2 += 2) { long long x1 = (sign1 * d1 * 2 + sign2 * d2 + k) / 3; long long x3 = sign1 * d1 + k - 2 * x1; long long x2 = k - x1 - x3; if (abs(x1 - x2) != d1 || abs(x2 - x3) != d2 || x1 + x2 + x3 != k) { continue; } if (x1 <= n / 3 && x1 >= 0 && x2 >= 0 && x2 <= n / 3 && x3 >= 0 && x3 <= n / 3) { poss = true; } } } if (poss) { printf("yes\n"); } else { printf("no\n"); } } }
### Prompt Create a solution in Cpp for the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { long long n, k, d1, d2; scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); if (n % 3 != 0) { cout << "no\n"; continue; } long long gamesRem = n - k; bool poss = false; for (int sign1 = -1; sign1 <= 1; sign1 += 2) { for (int sign2 = -1; sign2 <= 1; sign2 += 2) { long long x1 = (sign1 * d1 * 2 + sign2 * d2 + k) / 3; long long x3 = sign1 * d1 + k - 2 * x1; long long x2 = k - x1 - x3; if (abs(x1 - x2) != d1 || abs(x2 - x3) != d2 || x1 + x2 + x3 != k) { continue; } if (x1 <= n / 3 && x1 >= 0 && x2 >= 0 && x2 <= n / 3 && x3 >= 0 && x3 <= n / 3) { poss = true; } } } if (poss) { printf("yes\n"); } else { printf("no\n"); } } } ```
#include <bits/stdc++.h> using namespace std; long long num; long long n, k, d1, d2; bool solve() { if (n % 3) return false; long long aim = n / 3; bool res = false; if ((k + d1 + d2) % 3 == 0) { long long x2 = (k + d1 + d2) / 3; long long x1 = x2 - d1; long long x3 = x2 - d2; if (x1 <= aim && x2 <= aim && x3 <= aim) if (x2 >= 0 && x1 >= 0 && x3 >= 0 && 3 * aim - x1 - x2 - x3 == n - k) { res = true; } } if ((k - d1 + d2) % 3 == 0) { long long x2 = (k - d1 + d2) / 3; long long x1 = x2 + d1; long long x3 = x2 - d2; if (x1 <= aim && x2 <= aim && x3 <= aim) if (x2 >= 0 && x1 >= 0 && x3 >= 0 && 3 * aim - x1 - x2 - x3 == n - k) { res = true; } } if ((k + d1 - d2) % 3 == 0) { long long x2 = (k + d1 - d2) / 3; long long x1 = x2 - d1; long long x3 = x2 + d2; if ((x1 <= aim && x2 <= aim && x3 <= aim)) if (x2 >= 0 && x1 >= 0 && x3 >= 0 && 3 * aim - x1 - x2 - x3 == n - k) { res = true; } } if ((k - d1 - d2) % 3 == 0) { long long x2 = (k - d1 - d2) / 3; long long x1 = x2 + d1; long long x3 = x2 + d2; if ((x1 <= aim && x2 <= aim && x3 <= aim)) if (x2 >= 0 && x1 >= 0 && x3 >= 0 && 3 * aim - x1 - x2 - x3 == n - k) { res = true; } } return res; } int main() { cin >> num; for (int i = 0; i < num; i++) { cin >> n >> k >> d1 >> d2; if (solve()) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
### Prompt Generate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long num; long long n, k, d1, d2; bool solve() { if (n % 3) return false; long long aim = n / 3; bool res = false; if ((k + d1 + d2) % 3 == 0) { long long x2 = (k + d1 + d2) / 3; long long x1 = x2 - d1; long long x3 = x2 - d2; if (x1 <= aim && x2 <= aim && x3 <= aim) if (x2 >= 0 && x1 >= 0 && x3 >= 0 && 3 * aim - x1 - x2 - x3 == n - k) { res = true; } } if ((k - d1 + d2) % 3 == 0) { long long x2 = (k - d1 + d2) / 3; long long x1 = x2 + d1; long long x3 = x2 - d2; if (x1 <= aim && x2 <= aim && x3 <= aim) if (x2 >= 0 && x1 >= 0 && x3 >= 0 && 3 * aim - x1 - x2 - x3 == n - k) { res = true; } } if ((k + d1 - d2) % 3 == 0) { long long x2 = (k + d1 - d2) / 3; long long x1 = x2 - d1; long long x3 = x2 + d2; if ((x1 <= aim && x2 <= aim && x3 <= aim)) if (x2 >= 0 && x1 >= 0 && x3 >= 0 && 3 * aim - x1 - x2 - x3 == n - k) { res = true; } } if ((k - d1 - d2) % 3 == 0) { long long x2 = (k - d1 - d2) / 3; long long x1 = x2 + d1; long long x3 = x2 + d2; if ((x1 <= aim && x2 <= aim && x3 <= aim)) if (x2 >= 0 && x1 >= 0 && x3 >= 0 && 3 * aim - x1 - x2 - x3 == n - k) { res = true; } } return res; } int main() { cin >> num; for (int i = 0; i < num; i++) { cin >> n >> k >> d1 >> d2; if (solve()) cout << "yes" << endl; else cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long G(long long a, long long b) { if (b == 0) return a; return G(b, a % b); } int main() { int t; cin >> t; while (t--) { long long n, k, d1, d2, a, b, c, t, kk; cin >> n >> k >> d1 >> d2; kk = k - d1 - d1 - d2; if (kk >= 0 && kk % 3 == 0) { a = 0, b = d1, c = d1 + d2, t = n - k; t = t - d1 - d2 - d2; if (t >= 0 && t % 3 == 0) { cout << "yes" << endl; continue; } } kk = k - d2 - d1 - d2 - d1 + 3 * min(d1, d2); if (kk >= 0 && kk % 3 == 0) { a = d2, b = d1 + d2, c = d1, t = n - k; t = t - d1 - d2; if (t >= 0 && t % 3 == 0) { cout << "yes" << endl; continue; } } kk = k - d1 - d2; if (kk >= 0 && kk % 3 == 0) { a = d1, b = 0, c = d2, t = n - k; t = t - max(d1, d2) - max(d1, d2) + min(d1, d2); if (t >= 0 && t % 3 == 0) { cout << "yes" << endl; continue; } } kk = k - d1 - d2 - d2; if (kk >= 0 && kk % 3 == 0) { a = d1 + d2, b = d2, c = 0, t = n - k; t = t - d1 - d1 - d2; if (t >= 0 && t % 3 == 0) { cout << "yes" << endl; continue; } } cout << "no" << endl; continue; } }
### Prompt Please provide a Cpp coded solution to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long G(long long a, long long b) { if (b == 0) return a; return G(b, a % b); } int main() { int t; cin >> t; while (t--) { long long n, k, d1, d2, a, b, c, t, kk; cin >> n >> k >> d1 >> d2; kk = k - d1 - d1 - d2; if (kk >= 0 && kk % 3 == 0) { a = 0, b = d1, c = d1 + d2, t = n - k; t = t - d1 - d2 - d2; if (t >= 0 && t % 3 == 0) { cout << "yes" << endl; continue; } } kk = k - d2 - d1 - d2 - d1 + 3 * min(d1, d2); if (kk >= 0 && kk % 3 == 0) { a = d2, b = d1 + d2, c = d1, t = n - k; t = t - d1 - d2; if (t >= 0 && t % 3 == 0) { cout << "yes" << endl; continue; } } kk = k - d1 - d2; if (kk >= 0 && kk % 3 == 0) { a = d1, b = 0, c = d2, t = n - k; t = t - max(d1, d2) - max(d1, d2) + min(d1, d2); if (t >= 0 && t % 3 == 0) { cout << "yes" << endl; continue; } } kk = k - d1 - d2 - d2; if (kk >= 0 && kk % 3 == 0) { a = d1 + d2, b = d2, c = 0, t = n - k; t = t - d1 - d1 - d2; if (t >= 0 && t % 3 == 0) { cout << "yes" << endl; continue; } } cout << "no" << endl; continue; } } ```
#include <bits/stdc++.h> using namespace std; long long d1, d2; bool res(long long n, long long k, long long a, long long b, long long c) { long long maxi = max(max(a, b), c), still = (n - k) + (a + b + c); if (((a + b + c) > k) || (a < 0) || (b < 0) || (c < 0) || (n % 3 != 0) || ((k - a - b - c) % 3 != 0)) { return 0; } long long con = k - (a + b + c); con += (n - k), con -= ((maxi - a) + (maxi - b) + (maxi - c)); if ((a <= still / 3) && (b <= still / 3) && (c <= still / 3) && (still % 3 == 0)) return 1; return 0; } int main() { long long t, a, b, c, n, k; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; bool x = res(n, k, d1, 0, d2); x |= res(n, k, 0, d1, d1 - d2), x |= res(n, k, 0, d2, d2 - d1), x |= res(n, k, 0, d2, d1 + d2), x |= res(n, k, 0, d1, d1 + d2); if (x) printf("yes\n"); else printf("no\n"); } }
### Prompt Your challenge is to write a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long d1, d2; bool res(long long n, long long k, long long a, long long b, long long c) { long long maxi = max(max(a, b), c), still = (n - k) + (a + b + c); if (((a + b + c) > k) || (a < 0) || (b < 0) || (c < 0) || (n % 3 != 0) || ((k - a - b - c) % 3 != 0)) { return 0; } long long con = k - (a + b + c); con += (n - k), con -= ((maxi - a) + (maxi - b) + (maxi - c)); if ((a <= still / 3) && (b <= still / 3) && (c <= still / 3) && (still % 3 == 0)) return 1; return 0; } int main() { long long t, a, b, c, n, k; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; bool x = res(n, k, d1, 0, d2); x |= res(n, k, 0, d1, d1 - d2), x |= res(n, k, 0, d2, d2 - d1), x |= res(n, k, 0, d2, d1 + d2), x |= res(n, k, 0, d1, d1 + d2); if (x) printf("yes\n"); else printf("no\n"); } } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool f(long long a, long long b) { long long c = k - a - b * 2; if (c < 0 || c % 3) return 0; return n / 3 >= a + b + c / 3; } int main() { int t; cin >> t; for (int i = 1; i <= t; i++) { cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no\n"; continue; } if (f(d1, d2) || f(abs(d1 - d2), min(d1, d2)) || f(d2, d1) || f(min(d1, d2), abs(d1 - d2))) cout << "yes\n"; else cout << "no\n"; } }
### Prompt Please formulate a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool f(long long a, long long b) { long long c = k - a - b * 2; if (c < 0 || c % 3) return 0; return n / 3 >= a + b + c / 3; } int main() { int t; cin >> t; for (int i = 1; i <= t; i++) { cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no\n"; continue; } if (f(d1, d2) || f(abs(d1 - d2), min(d1, d2)) || f(d2, d1) || f(min(d1, d2), abs(d1 - d2))) cout << "yes\n"; else cout << "no\n"; } } ```
#include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2; bool solve(long long t1, long long t2, long long t3) { long long sum = t1 + t2 + t3; if (k < sum || (k - sum) % 3) return false; long long t = n - k - (3 * max(max(t1, t2), t3) - sum); if (t < 0 || t % 3) return false; return true; } int main() { ios_base::sync_with_stdio(0); cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; bool res = false; long long mx = max(d1, d2); res = res || solve(d1, 0, d2); res = res || solve(d1 + d2, d2, 0); res = res || solve(0, d1, d1 + d2); res = res || solve(mx - d1, mx, mx - d2); if (res) cout << "yes\n"; else cout << "no\n"; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2; bool solve(long long t1, long long t2, long long t3) { long long sum = t1 + t2 + t3; if (k < sum || (k - sum) % 3) return false; long long t = n - k - (3 * max(max(t1, t2), t3) - sum); if (t < 0 || t % 3) return false; return true; } int main() { ios_base::sync_with_stdio(0); cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; bool res = false; long long mx = max(d1, d2); res = res || solve(d1, 0, d2); res = res || solve(d1 + d2, d2, 0); res = res || solve(0, d1, d1 + d2); res = res || solve(mx - d1, mx, mx - d2); if (res) cout << "yes\n"; else cout << "no\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline void Boost() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long n, k, d1, d2, T; bool solve(long long &n, long long &k, long long &d1, long long &d2) { if (n % 3 != 0) return 0; short sign, sign2; long long x1, x2, x3, dd1, dd2, v[3], cnt; for (sign = -1; sign <= 1; sign += 2) for (sign2 = -1; sign2 <= 1; sign2 += 2) { dd1 = sign * d1; dd2 = sign2 * d2; cnt = k + 2 * dd2 + dd1; if (cnt % 3 != 0) continue; x3 = cnt / 3; x1 = x3 - dd1 - dd2; x2 = x3 - dd2; if (x1 + x2 + x3 <= k && x1 >= 0 && x2 >= 0 && x3 >= 0 && x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) return 1; } return 0; } int main() { Boost(); cin >> T; while (T--) { cin >> n >> k >> d1 >> d2; if (solve(n, k, d1, d2)) cout << "yes\n"; else cout << "no\n"; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline void Boost() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long n, k, d1, d2, T; bool solve(long long &n, long long &k, long long &d1, long long &d2) { if (n % 3 != 0) return 0; short sign, sign2; long long x1, x2, x3, dd1, dd2, v[3], cnt; for (sign = -1; sign <= 1; sign += 2) for (sign2 = -1; sign2 <= 1; sign2 += 2) { dd1 = sign * d1; dd2 = sign2 * d2; cnt = k + 2 * dd2 + dd1; if (cnt % 3 != 0) continue; x3 = cnt / 3; x1 = x3 - dd1 - dd2; x2 = x3 - dd2; if (x1 + x2 + x3 <= k && x1 >= 0 && x2 >= 0 && x3 >= 0 && x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) return 1; } return 0; } int main() { Boost(); cin >> T; while (T--) { cin >> n >> k >> d1 >> d2; if (solve(n, k, d1, d2)) cout << "yes\n"; else cout << "no\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2, a, b, c; bool make(long long n, long long k, long long d1, long long d2) { long long a, b, c; a = (k + d1 + d1 + d2) / 3; b = a - d1; c = a - d1 - d2; if (a >= b && b >= c && a >= 0 && b >= 0 && c >= 0 && a <= n / 3 && a + b + c == k) return true; b = (k - d1 - d2) / 3; a = b + d1; c = b + d2; if (a >= b && b <= c && a >= 0 && b >= 0 && c >= 0 && max(a, c) <= n / 3 && a + b + c == k) return true; b = (k + d1 + d2) / 3; a = b - d1; c = b - d2; if (a <= b && b >= c && a >= 0 && b >= 0 && c >= 0 && b <= n / 3 && a + b + c == k) return true; c = (k + d1 + d2 + d2) / 3; a = c - d1 - d2; b = c - d2; if (a <= b && b <= c && a >= 0 && b >= 0 && c >= 0 && c <= n / 3 && a + b + c == k) return true; return false; } int main() { string ans; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no" << endl; continue; } bool ans = make(n, k, d1, d2); if (ans) cout << "yes" << endl; else { cout << "no" << endl; } } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2, a, b, c; bool make(long long n, long long k, long long d1, long long d2) { long long a, b, c; a = (k + d1 + d1 + d2) / 3; b = a - d1; c = a - d1 - d2; if (a >= b && b >= c && a >= 0 && b >= 0 && c >= 0 && a <= n / 3 && a + b + c == k) return true; b = (k - d1 - d2) / 3; a = b + d1; c = b + d2; if (a >= b && b <= c && a >= 0 && b >= 0 && c >= 0 && max(a, c) <= n / 3 && a + b + c == k) return true; b = (k + d1 + d2) / 3; a = b - d1; c = b - d2; if (a <= b && b >= c && a >= 0 && b >= 0 && c >= 0 && b <= n / 3 && a + b + c == k) return true; c = (k + d1 + d2 + d2) / 3; a = c - d1 - d2; b = c - d2; if (a <= b && b <= c && a >= 0 && b >= 0 && c >= 0 && c <= n / 3 && a + b + c == k) return true; return false; } int main() { string ans; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no" << endl; continue; } bool ans = make(n, k, d1, d2); if (ans) cout << "yes" << endl; else { cout << "no" << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, k, d1, d2; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } long long int x, y, z; y = k + d1 - d2; if (y % 3 == 0 && y >= 0 && y - d1 >= 0) { y = y / 3; x = y - d1; z = d2 + y; if (y <= n / 3 && x <= n / 3 && z <= n / 3 && x >= 0 && y >= 0 && z >= 0) { cout << "yes\n"; continue; } } y = k - d1 + d2; if (y % 3 == 0 && y >= 0) { y = y / 3; x = y + d1; z = y - d2; if (y <= n / 3 && x <= n / 3 && z <= n / 3 && x >= 0 && y >= 0 && z >= 0) { cout << "yes\n"; continue; } } y = k - d1 - d2; if (y % 3 == 0 && y >= 0) { y = y / 3; x = y + d1; z = y + d2; if (y <= n / 3 && x <= n / 3 && z <= n / 3 && x >= 0 && y >= 0 && z >= 0) { cout << "yes\n"; continue; } } y = k + d1 + d2; if (y % 3 == 0 && y >= 0) { y = y / 3; x = y - d1; z = y - d2; if (y <= n / 3 && x <= n / 3 && z <= n / 3 && x >= 0 && y >= 0 && z >= 0) { cout << "yes\n"; continue; } } cout << "no\n"; } }
### Prompt Please provide a Cpp coded solution to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int t, n, k, d1, d2; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } long long int x, y, z; y = k + d1 - d2; if (y % 3 == 0 && y >= 0 && y - d1 >= 0) { y = y / 3; x = y - d1; z = d2 + y; if (y <= n / 3 && x <= n / 3 && z <= n / 3 && x >= 0 && y >= 0 && z >= 0) { cout << "yes\n"; continue; } } y = k - d1 + d2; if (y % 3 == 0 && y >= 0) { y = y / 3; x = y + d1; z = y - d2; if (y <= n / 3 && x <= n / 3 && z <= n / 3 && x >= 0 && y >= 0 && z >= 0) { cout << "yes\n"; continue; } } y = k - d1 - d2; if (y % 3 == 0 && y >= 0) { y = y / 3; x = y + d1; z = y + d2; if (y <= n / 3 && x <= n / 3 && z <= n / 3 && x >= 0 && y >= 0 && z >= 0) { cout << "yes\n"; continue; } } y = k + d1 + d2; if (y % 3 == 0 && y >= 0) { y = y / 3; x = y - d1; z = y - d2; if (y <= n / 3 && x <= n / 3 && z <= n / 3 && x >= 0 && y >= 0 && z >= 0) { cout << "yes\n"; continue; } } cout << "no\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; long long n, k, d1, d2; int solve(long long d1, long long d2) { long long x, y, z; y = (k - d1 + d2) / 3; if ((k - d1 + d2) % 3 != 0) return 0; x = y + d1; z = y - d2; if (x >= 0 && x <= n / 3 && y >= 0 && y <= n / 3 && z >= 0 && z <= n / 3) return 1; return 0; } int main() { int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) cout << "no" << endl; else if (solve(d1, d2) || solve(-d1, d2) || solve(d1, -d2) || solve(-d1, -d2)) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; long long n, k, d1, d2; int solve(long long d1, long long d2) { long long x, y, z; y = (k - d1 + d2) / 3; if ((k - d1 + d2) % 3 != 0) return 0; x = y + d1; z = y - d2; if (x >= 0 && x <= n / 3 && y >= 0 && y <= n / 3 && z >= 0 && z <= n / 3) return 1; return 0; } int main() { int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) cout << "no" << endl; else if (solve(d1, d2) || solve(-d1, d2) || solve(d1, -d2) || solve(-d1, -d2)) cout << "yes" << endl; else cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int calc(long long n, long long k, long long a1, long long a2, long long a3) { long long sum; if (a2 < a3) { swap(a2, a3); } if (a1 < a2) { swap(a1, a2); } if (a2 < a3) { swap(a2, a3); } if (a1 < 0 || a2 < 0 || a3 < 0) { return 0; } sum = a1 - a2 + a1 - a3; if (sum > n - k) { return 0; } if ((n - k - sum) % 3 == 0) { return 1; } return 0; } int task() { long long i, n, k, d1, d2, a1, a2, a3; cin >> n; ; cin >> k; ; cin >> d1; ; cin >> d2; ; a1 = k + 2 * d1 + d2; if (a1 % 3 == 0) { a1 = a1 / 3; a2 = a1 - d1; a3 = a2 - d2; if (calc(n, k, a1, a2, a3)) { return 1; } } a1 = k + 2 * d1 - d2; if (a1 % 3 == 0) { a1 = a1 / 3; a2 = a1 - d1; a3 = a2 + d2; if (calc(n, k, a1, a2, a3)) { return 1; } } a1 = k - 2 * d1 + d2; if (a1 % 3 == 0) { a1 = a1 / 3; a2 = a1 + d1; a3 = a2 - d2; if (calc(n, k, a1, a2, a3)) { return 1; } } a1 = k - 2 * d1 - d2; if (a1 % 3 == 0) { a1 = a1 / 3; a2 = a1 + d1; a3 = a2 + d2; if (calc(n, k, a1, a2, a3)) { return 1; } } return 0; } int main(int argc, char const *argv[]) { int i, t; string yes("yes"); string no("no"); cin >> t; ; for (i = 0; i < t; i++) { if (task()) { cout << yes << endl; ; } else { cout << no << endl; ; } } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int calc(long long n, long long k, long long a1, long long a2, long long a3) { long long sum; if (a2 < a3) { swap(a2, a3); } if (a1 < a2) { swap(a1, a2); } if (a2 < a3) { swap(a2, a3); } if (a1 < 0 || a2 < 0 || a3 < 0) { return 0; } sum = a1 - a2 + a1 - a3; if (sum > n - k) { return 0; } if ((n - k - sum) % 3 == 0) { return 1; } return 0; } int task() { long long i, n, k, d1, d2, a1, a2, a3; cin >> n; ; cin >> k; ; cin >> d1; ; cin >> d2; ; a1 = k + 2 * d1 + d2; if (a1 % 3 == 0) { a1 = a1 / 3; a2 = a1 - d1; a3 = a2 - d2; if (calc(n, k, a1, a2, a3)) { return 1; } } a1 = k + 2 * d1 - d2; if (a1 % 3 == 0) { a1 = a1 / 3; a2 = a1 - d1; a3 = a2 + d2; if (calc(n, k, a1, a2, a3)) { return 1; } } a1 = k - 2 * d1 + d2; if (a1 % 3 == 0) { a1 = a1 / 3; a2 = a1 + d1; a3 = a2 - d2; if (calc(n, k, a1, a2, a3)) { return 1; } } a1 = k - 2 * d1 - d2; if (a1 % 3 == 0) { a1 = a1 / 3; a2 = a1 + d1; a3 = a2 + d2; if (calc(n, k, a1, a2, a3)) { return 1; } } return 0; } int main(int argc, char const *argv[]) { int i, t; string yes("yes"); string no("no"); cin >> t; ; for (i = 0; i < t; i++) { if (task()) { cout << yes << endl; ; } else { cout << no << endl; ; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int t; scanf("%lld", &t); while (t--) { long long int n, k, d1, d2, x, y, z; scanf("%lld", &n); scanf("%lld", &k); scanf("%lld", &d1); scanf("%lld", &d2); x = (k + 2 * d1 + d2) / 3; y = x - d1; z = y - d2; if (((n / 3 - x) + (n / 3 - y) + (n / 3 - z) == n - k) && (n / 3 - x) >= 0 && (n / 3 - y) >= 0 && (n / 3 - z) >= 0 && x >= 0 && y >= 0 && z >= 0 && !((k + 2 * d1 + d2) % 3)) cout << "yes"; else { x = (k + 2 * d1 - d2) / 3; y = x - d1; z = y + d2; if (((n / 3 - x) + (n / 3 - y) + (n / 3 - z) == n - k) && (n / 3 - x) >= 0 && (n / 3 - y) >= 0 && (n / 3 - z) >= 0 && x >= 0 && y >= 0 && z >= 0 && !((k + 2 * d1 - d2) % 3)) cout << "yes"; else { x = (k - 2 * d1 - d2) / 3; y = x + d1; z = y + d2; if (((n / 3 - x) + (n / 3 - y) + (n / 3 - z) == n - k) && (n / 3 - x) >= 0 && (n / 3 - y) >= 0 && (n / 3 - z) >= 0 && x >= 0 && y >= 0 && z >= 0 && !((k - 2 * d1 - d2) % 3)) cout << "yes"; else { x = (k - 2 * d1 + d2) / 3; y = x + d1; z = y - d2; if (((n / 3 - x) + (n / 3 - y) + (n / 3 - z) == n - k) && (n / 3 - x) >= 0 && (n / 3 - y) >= 0 && (n / 3 - z) >= 0 && x >= 0 && y >= 0 && z >= 0 && !((k - 2 * d1 + d2) % 3)) cout << "yes"; else cout << "no"; } } } cout << endl; } }
### Prompt Create a solution in Cpp for the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int t; scanf("%lld", &t); while (t--) { long long int n, k, d1, d2, x, y, z; scanf("%lld", &n); scanf("%lld", &k); scanf("%lld", &d1); scanf("%lld", &d2); x = (k + 2 * d1 + d2) / 3; y = x - d1; z = y - d2; if (((n / 3 - x) + (n / 3 - y) + (n / 3 - z) == n - k) && (n / 3 - x) >= 0 && (n / 3 - y) >= 0 && (n / 3 - z) >= 0 && x >= 0 && y >= 0 && z >= 0 && !((k + 2 * d1 + d2) % 3)) cout << "yes"; else { x = (k + 2 * d1 - d2) / 3; y = x - d1; z = y + d2; if (((n / 3 - x) + (n / 3 - y) + (n / 3 - z) == n - k) && (n / 3 - x) >= 0 && (n / 3 - y) >= 0 && (n / 3 - z) >= 0 && x >= 0 && y >= 0 && z >= 0 && !((k + 2 * d1 - d2) % 3)) cout << "yes"; else { x = (k - 2 * d1 - d2) / 3; y = x + d1; z = y + d2; if (((n / 3 - x) + (n / 3 - y) + (n / 3 - z) == n - k) && (n / 3 - x) >= 0 && (n / 3 - y) >= 0 && (n / 3 - z) >= 0 && x >= 0 && y >= 0 && z >= 0 && !((k - 2 * d1 - d2) % 3)) cout << "yes"; else { x = (k - 2 * d1 + d2) / 3; y = x + d1; z = y - d2; if (((n / 3 - x) + (n / 3 - y) + (n / 3 - z) == n - k) && (n / 3 - x) >= 0 && (n / 3 - y) >= 0 && (n / 3 - z) >= 0 && x >= 0 && y >= 0 && z >= 0 && !((k - 2 * d1 + d2) % 3)) cout << "yes"; else cout << "no"; } } } cout << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { long long T, n, k, d1, d2, x, y, z; scanf("%lld", &T); while (T--) { scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); int flag = 0; if ((k - d1 - d2) % 3 == 0 && k - d1 - d2 >= 0) { y = (k - d1 - d2) / 3; x = y + d1; z = y + d2; long long mm = 0; mm = max(mm, x); mm = max(mm, y); mm = max(mm, z); if (x + y + z == k) { long long ok = mm - x + mm - y + mm - z; ok = n - k - ok; if (ok >= 0 && ok % 3 == 0) flag = 1; } } if ((k - d1 - 2 * d2) % 3 == 0 && k - d1 - 2 * d2 >= 0) { z = (k - d1 - 2 * d2) / 3; y = z + d2; x = y + d1; long long mm = -1; mm = max(mm, x); mm = max(mm, y); mm = max(mm, z); if (x + y + z == k) { long long ok = mm - x + mm - y + mm - z; ok = n - k - ok; if (ok >= 0 && ok % 3 == 0) flag = 1; } } if ((k + d1 + d2) % 3 == 0 && k + d1 + d2 >= 0) { y = (k + d1 + d2) / 3; x = y - d1; z = y - d2; long long mm = 0; mm = max(mm, x); mm = max(mm, y); mm = max(mm, z); if (x + y + z == k && x >= 0 && y >= 0 && z >= 0) { long long ok = mm - x + mm - y + mm - z; ok = n - k - ok; if (ok >= 0 && ok % 3 == 0) flag = 1; } } if ((k - d1 * 2 - d2) % 3 == 0 && k - d1 * 2 - d2 >= 0) { x = (k - 2 * d1 - d2) / 3; y = x + d1; z = y + d2; long long mm = 0; mm = max(mm, x); mm = max(mm, y); mm = max(mm, z); if (x + y + z == k) { long long ok = mm - x + mm - y + mm - z; ok = n - k - ok; if (ok >= 0 && ok % 3 == 0) flag = 1; } } if (flag) puts("yes"); else puts("no"); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long T, n, k, d1, d2, x, y, z; scanf("%lld", &T); while (T--) { scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); int flag = 0; if ((k - d1 - d2) % 3 == 0 && k - d1 - d2 >= 0) { y = (k - d1 - d2) / 3; x = y + d1; z = y + d2; long long mm = 0; mm = max(mm, x); mm = max(mm, y); mm = max(mm, z); if (x + y + z == k) { long long ok = mm - x + mm - y + mm - z; ok = n - k - ok; if (ok >= 0 && ok % 3 == 0) flag = 1; } } if ((k - d1 - 2 * d2) % 3 == 0 && k - d1 - 2 * d2 >= 0) { z = (k - d1 - 2 * d2) / 3; y = z + d2; x = y + d1; long long mm = -1; mm = max(mm, x); mm = max(mm, y); mm = max(mm, z); if (x + y + z == k) { long long ok = mm - x + mm - y + mm - z; ok = n - k - ok; if (ok >= 0 && ok % 3 == 0) flag = 1; } } if ((k + d1 + d2) % 3 == 0 && k + d1 + d2 >= 0) { y = (k + d1 + d2) / 3; x = y - d1; z = y - d2; long long mm = 0; mm = max(mm, x); mm = max(mm, y); mm = max(mm, z); if (x + y + z == k && x >= 0 && y >= 0 && z >= 0) { long long ok = mm - x + mm - y + mm - z; ok = n - k - ok; if (ok >= 0 && ok % 3 == 0) flag = 1; } } if ((k - d1 * 2 - d2) % 3 == 0 && k - d1 * 2 - d2 >= 0) { x = (k - 2 * d1 - d2) / 3; y = x + d1; z = y + d2; long long mm = 0; mm = max(mm, x); mm = max(mm, y); mm = max(mm, z); if (x + y + z == k) { long long ok = mm - x + mm - y + mm - z; ok = n - k - ok; if (ok >= 0 && ok % 3 == 0) flag = 1; } } if (flag) puts("yes"); else puts("no"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int t; long long n, k, d1, d2; long long A, W; long long add1, add2, add3; bool moze; bool try1() { A = k - d1 - d2 - d2; if (A % 3 != 0 || A < 0) return false; A /= 3; if (A + d1 + d2 > W) return false; if (A + d2 > W) return false; if (A > W) return false; add1 = W - A; add2 = W - A - d2; add3 = W - A - d1 - d2; if (add1 + add2 + add3 + k != n) return false; return true; } bool try2() { A = k - d1 - d1 - d2; if (A % 3 != 0 || A < 0) return false; A /= 3; if (A + d1 + d2 > W) return false; if (A + d1 > W) return false; if (A > W) return false; add1 = W - A; add2 = W - A - d1; add3 = W - A - d1 - d2; if (add1 + add2 + add3 + k != n) return false; return true; } bool try3() { A = k - d1 - d2; if (A % 3 != 0 || A < 0) return false; A /= 3; if (A + d2 > W) return false; if (A + d1 > W) return false; if (A > W) return false; add1 = W - A; add2 = W - A - d1; add3 = W - A - d2; if (add1 + add2 + add3 + k != n) return false; return true; } bool try4() { A = k + d1 + d2; if (A % 3 != 0 || A < 0) return false; A /= 3; if (A - d2 > W || A - d2 < 0) return false; if (A - d1 > W || A - d1 < 0) return false; if (A > W) return false; add1 = W - A; add2 = W - A + d1; add3 = W - A + d2; if (add1 + add2 + add3 + k != n) return false; return true; } int main() { scanf("%d", &t); for (int tt = 1; tt <= t; tt++) { scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { cout << "no" << endl; continue; } W = n / 3; if (try1()) { cout << "yes" << endl; continue; } if (try2()) { cout << "yes" << endl; continue; } if (try3()) { cout << "yes" << endl; continue; } if (try4()) { cout << "yes" << endl; continue; } cout << "no" << endl; } return 0; }
### Prompt Please create a solution in cpp to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int t; long long n, k, d1, d2; long long A, W; long long add1, add2, add3; bool moze; bool try1() { A = k - d1 - d2 - d2; if (A % 3 != 0 || A < 0) return false; A /= 3; if (A + d1 + d2 > W) return false; if (A + d2 > W) return false; if (A > W) return false; add1 = W - A; add2 = W - A - d2; add3 = W - A - d1 - d2; if (add1 + add2 + add3 + k != n) return false; return true; } bool try2() { A = k - d1 - d1 - d2; if (A % 3 != 0 || A < 0) return false; A /= 3; if (A + d1 + d2 > W) return false; if (A + d1 > W) return false; if (A > W) return false; add1 = W - A; add2 = W - A - d1; add3 = W - A - d1 - d2; if (add1 + add2 + add3 + k != n) return false; return true; } bool try3() { A = k - d1 - d2; if (A % 3 != 0 || A < 0) return false; A /= 3; if (A + d2 > W) return false; if (A + d1 > W) return false; if (A > W) return false; add1 = W - A; add2 = W - A - d1; add3 = W - A - d2; if (add1 + add2 + add3 + k != n) return false; return true; } bool try4() { A = k + d1 + d2; if (A % 3 != 0 || A < 0) return false; A /= 3; if (A - d2 > W || A - d2 < 0) return false; if (A - d1 > W || A - d1 < 0) return false; if (A > W) return false; add1 = W - A; add2 = W - A + d1; add3 = W - A + d2; if (add1 + add2 + add3 + k != n) return false; return true; } int main() { scanf("%d", &t); for (int tt = 1; tt <= t; tt++) { scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { cout << "no" << endl; continue; } W = n / 3; if (try1()) { cout << "yes" << endl; continue; } if (try2()) { cout << "yes" << endl; continue; } if (try3()) { cout << "yes" << endl; continue; } if (try4()) { cout << "yes" << endl; continue; } cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long a[10], b[10]; int main() { long long t, n, k, d1, d2; scanf("%I64d", &t); for (int i = 1; i <= t; i++) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (d1 > d2) { swap(d1, d2); } a[1] = d1 * 2 + d2; a[2] = d1 + d2 * 2; a[3] = d1 + d2; a[4] = d2 + d2 - d1; b[1] = d2 * 2 + d1; b[2] = d1 * 2 + d2; b[3] = d2 + d2 - d1; b[4] = d1 + d2; n -= k; for (int j = 1; j <= 4; j++) { if (k < b[j]) { continue; } else { if ((k - b[j]) % 3 != 0) { continue; } } long long kk = n - a[j]; if (kk >= 0) { if (kk % 3 == 0) { n = -1; printf("yes\n"); break; } } } if (n != -1) { printf("no\n"); } } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a[10], b[10]; int main() { long long t, n, k, d1, d2; scanf("%I64d", &t); for (int i = 1; i <= t; i++) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (d1 > d2) { swap(d1, d2); } a[1] = d1 * 2 + d2; a[2] = d1 + d2 * 2; a[3] = d1 + d2; a[4] = d2 + d2 - d1; b[1] = d2 * 2 + d1; b[2] = d1 * 2 + d2; b[3] = d2 + d2 - d1; b[4] = d1 + d2; n -= k; for (int j = 1; j <= 4; j++) { if (k < b[j]) { continue; } else { if ((k - b[j]) % 3 != 0) { continue; } } long long kk = n - a[j]; if (kk >= 0) { if (kk % 3 == 0) { n = -1; printf("yes\n"); break; } } } if (n != -1) { printf("no\n"); } } return 0; } ```
#include <bits/stdc++.h> bool solve(long long k, long long y, long long z, long long &x1, long long &x2, long long &x3) { if ((k - y - 2 * z) % 3 != 0) return (false); x3 = (k - y - 2 * z) / 3; x2 = x3 + z; x1 = x2 + y; return (x1 >= 0 && x2 >= 0 && x3 >= 0); } bool canwin(long long n, long long x1, long long x2, long long x3) { if (n % 3 != 0) return (false); long long t = n / 3; if (x1 > t) return (false); if (x2 > t) return (false); if (x3 > t) return (false); return (true); } bool test(void) { long long n, k, d1, d2; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); long long x1, x2, x3; for (int i1 = 0; i1 < (2); i1 = i1 + 1) for (int i2 = 0; i2 < (2); i2 = i2 + 1) if (solve(k, i1 ? d1 : -d1, i2 ? d2 : -d2, x1, x2, x3) && canwin(n, x1, x2, x3)) return (true); return (false); } int main(void) { int t; scanf("%d", &t); for (int zz = 0; zz < (t); zz = zz + 1) if (test()) printf("yes\n"); else printf("no\n"); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> bool solve(long long k, long long y, long long z, long long &x1, long long &x2, long long &x3) { if ((k - y - 2 * z) % 3 != 0) return (false); x3 = (k - y - 2 * z) / 3; x2 = x3 + z; x1 = x2 + y; return (x1 >= 0 && x2 >= 0 && x3 >= 0); } bool canwin(long long n, long long x1, long long x2, long long x3) { if (n % 3 != 0) return (false); long long t = n / 3; if (x1 > t) return (false); if (x2 > t) return (false); if (x3 > t) return (false); return (true); } bool test(void) { long long n, k, d1, d2; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); long long x1, x2, x3; for (int i1 = 0; i1 < (2); i1 = i1 + 1) for (int i2 = 0; i2 < (2); i2 = i2 + 1) if (solve(k, i1 ? d1 : -d1, i2 ? d2 : -d2, x1, x2, x3) && canwin(n, x1, x2, x3)) return (true); return (false); } int main(void) { int t; scanf("%d", &t); for (int zz = 0; zz < (t); zz = zz + 1) if (test()) printf("yes\n"); else printf("no\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = (1 << 30) - 1; const int mod = 1000000007; const int maxn = 1000005; int flag; long long n, k, d1, d2; void f(long long a, long long b, long long c) { if (n % 3 || a % 3 || b % 3 || c % 3) { flag = 0; return; } long long ans = n / 3; a = a / 3; b = b / 3; c = c / 3; if ((a >= 0 && a <= ans) && (b >= 0 && b <= ans) && (c >= 0 && c <= ans)) { flag = 1; return; } else { flag = 0; return; } } int main() { int T; scanf("%d", &T); while (T--) { cin >> n >> k >> d1 >> d2; flag = 1; for (int i = 1; i <= 4; i++) { if (i == 1) f(k + 2 * d1 + d2, k - d1 + d2, k - d1 - 2 * d2); if (flag) break; if (i == 2) f(k - 2 * d1 + d2, k + d1 + d2, k + d1 - 2 * d2); if (flag) break; if (i == 3) f(k + 2 * d1 - d2, k - d1 - d2, k - d1 + 2 * d2); if (flag) break; if (i == 4) f(k - 2 * d1 - d2, k + d1 - d2, k + d1 + 2 * d2); } if (flag) puts("yes"); else puts("no"); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = (1 << 30) - 1; const int mod = 1000000007; const int maxn = 1000005; int flag; long long n, k, d1, d2; void f(long long a, long long b, long long c) { if (n % 3 || a % 3 || b % 3 || c % 3) { flag = 0; return; } long long ans = n / 3; a = a / 3; b = b / 3; c = c / 3; if ((a >= 0 && a <= ans) && (b >= 0 && b <= ans) && (c >= 0 && c <= ans)) { flag = 1; return; } else { flag = 0; return; } } int main() { int T; scanf("%d", &T); while (T--) { cin >> n >> k >> d1 >> d2; flag = 1; for (int i = 1; i <= 4; i++) { if (i == 1) f(k + 2 * d1 + d2, k - d1 + d2, k - d1 - 2 * d2); if (flag) break; if (i == 2) f(k - 2 * d1 + d2, k + d1 + d2, k + d1 - 2 * d2); if (flag) break; if (i == 3) f(k + 2 * d1 - d2, k - d1 - d2, k - d1 + 2 * d2); if (flag) break; if (i == 4) f(k - 2 * d1 - d2, k + d1 - d2, k + d1 + 2 * d2); } if (flag) puts("yes"); else puts("no"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; bool checking(long long x, long long y, long long z, long long n, long long k) { if (x < 0 || y < 0 || z < 0) return false; if ((x + y + z + n - k) % 3) return false; else { long long q = (x + y + z + n - k) / 3; if (q >= x && q >= y && q >= z) return true; else return false; } } int main() { int t; cin >> t; while (t--) { bool flag = false; long long n, k, l, p, x, y, z; cin >> n >> k >> l >> p; if ((k + 2 * l + p) % 3 == 0) { x = (k + 2 * l + p) / 3; y = x - l; z = x - (l + p); if (checking(x, y, z, n, k)) flag = true; } if ((k + 2 * l - p) % 3 == 0) { x = (k + 2 * l - p) / 3; y = x - l; z = x - (l - p); if (checking(x, y, z, n, k)) flag = true; } if ((k + l + 2 * p) % 3 == 0) { z = (k + l + 2 * p) / 3; x = z - (l + p); y = z - p; if (checking(x, y, z, n, k)) flag = true; } if ((k + l - 2 * p) % 3 == 0) { z = (k + l - 2 * p) / 3; y = p + z; x = z - (l - p); if (checking(x, y, z, n, k)) flag = true; } if (flag) cout << "yes\n"; else cout << "no\n"; } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool checking(long long x, long long y, long long z, long long n, long long k) { if (x < 0 || y < 0 || z < 0) return false; if ((x + y + z + n - k) % 3) return false; else { long long q = (x + y + z + n - k) / 3; if (q >= x && q >= y && q >= z) return true; else return false; } } int main() { int t; cin >> t; while (t--) { bool flag = false; long long n, k, l, p, x, y, z; cin >> n >> k >> l >> p; if ((k + 2 * l + p) % 3 == 0) { x = (k + 2 * l + p) / 3; y = x - l; z = x - (l + p); if (checking(x, y, z, n, k)) flag = true; } if ((k + 2 * l - p) % 3 == 0) { x = (k + 2 * l - p) / 3; y = x - l; z = x - (l - p); if (checking(x, y, z, n, k)) flag = true; } if ((k + l + 2 * p) % 3 == 0) { z = (k + l + 2 * p) / 3; x = z - (l + p); y = z - p; if (checking(x, y, z, n, k)) flag = true; } if ((k + l - 2 * p) % 3 == 0) { z = (k + l - 2 * p) / 3; y = p + z; x = z - (l - p); if (checking(x, y, z, n, k)) flag = true; } if (flag) cout << "yes\n"; else cout << "no\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; bool check(long long n, long long k, long long d1, long long d2) { long long b = k; long long a = b - d1; long long c = b + d2; long long sum = a + b + c; if (sum % 3 != k % 3) { return false; } a -= (sum - k) / 3; b -= (sum - k) / 3; c -= (sum - k) / 3; if (0 > a || 0 > b || 0 > c || a > n / 3 || b > n / 3 || c > n / 3) { return false; } return true; } int main() { long long t, n, k, d1, d2; for (cin >> t; t; t--) { cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no\n"; continue; } if (check(n, k, d1, d2)) { cout << "yes\n"; continue; } if (check(n, k, 0 - d1, d2)) { cout << "yes\n"; continue; } if (check(n, k, d1, 0 - d2)) { cout << "yes\n"; continue; } if (check(n, k, 0 - d1, 0 - d2)) { cout << "yes\n"; continue; } cout << "no\n"; } return 0; }
### Prompt Generate a CPP solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool check(long long n, long long k, long long d1, long long d2) { long long b = k; long long a = b - d1; long long c = b + d2; long long sum = a + b + c; if (sum % 3 != k % 3) { return false; } a -= (sum - k) / 3; b -= (sum - k) / 3; c -= (sum - k) / 3; if (0 > a || 0 > b || 0 > c || a > n / 3 || b > n / 3 || c > n / 3) { return false; } return true; } int main() { long long t, n, k, d1, d2; for (cin >> t; t; t--) { cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no\n"; continue; } if (check(n, k, d1, d2)) { cout << "yes\n"; continue; } if (check(n, k, 0 - d1, d2)) { cout << "yes\n"; continue; } if (check(n, k, d1, 0 - d2)) { cout << "yes\n"; continue; } if (check(n, k, 0 - d1, 0 - d2)) { cout << "yes\n"; continue; } cout << "no\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; long long valid(long long w1, long long w2, long long w3) { if (w1 < 0 || w2 < 0 || w3 < 0) { return 0; } if (w1 + w2 + w3 != k) { return 0; } if (n % 3 != 0) { return 0; } long long d = n / 3; if (w1 > d || w2 > d || w3 > d) { return 0; } return 1; } int main() { long long t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; long long w1 = 0, w2 = 0, w3 = 0; long long flag = 0; if ((k - 2 * d2 - d1) >= 0 && (k - 2 * d2 - d1) % 3 == 0) { w3 = (k - 2 * d2 - d1) / 3; w1 = w3 + d2 + d1; w2 = w3 + d2; if (valid(w1, w2, w3) == 1) { flag = 1; } } if ((k - 2 * d2 + d1) >= 0 && (k - 2 * d2 + d1) % 3 == 0) { w3 = (k - 2 * d2 + d1) / 3; w2 = w3 + d2; w1 = w2 - d1; if (valid(w1, w2, w3) == 1) { flag = 1; } } if ((k + 2 * d2 - d1) >= 0 && (k + 2 * d2 - d1) % 3 == 0) { w3 = (k + 2 * d2 - d1) / 3; w2 = w3 - d2; w1 = w2 + d1; if (valid(w1, w2, w3) == 1) { flag = 1; } } if ((k + 2 * d2 + d1) >= 0 && (k + 2 * d2 + d1) % 3 == 0) { w3 = (k + 2 * d2 + d1) / 3; w2 = w3 - d2; w1 = w2 - d1; if (valid(w1, w2, w3) == 1) { flag = 1; } } if (flag == 1) { cout << "yes\n"; } else { cout << "no\n"; } } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; long long valid(long long w1, long long w2, long long w3) { if (w1 < 0 || w2 < 0 || w3 < 0) { return 0; } if (w1 + w2 + w3 != k) { return 0; } if (n % 3 != 0) { return 0; } long long d = n / 3; if (w1 > d || w2 > d || w3 > d) { return 0; } return 1; } int main() { long long t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; long long w1 = 0, w2 = 0, w3 = 0; long long flag = 0; if ((k - 2 * d2 - d1) >= 0 && (k - 2 * d2 - d1) % 3 == 0) { w3 = (k - 2 * d2 - d1) / 3; w1 = w3 + d2 + d1; w2 = w3 + d2; if (valid(w1, w2, w3) == 1) { flag = 1; } } if ((k - 2 * d2 + d1) >= 0 && (k - 2 * d2 + d1) % 3 == 0) { w3 = (k - 2 * d2 + d1) / 3; w2 = w3 + d2; w1 = w2 - d1; if (valid(w1, w2, w3) == 1) { flag = 1; } } if ((k + 2 * d2 - d1) >= 0 && (k + 2 * d2 - d1) % 3 == 0) { w3 = (k + 2 * d2 - d1) / 3; w2 = w3 - d2; w1 = w2 + d1; if (valid(w1, w2, w3) == 1) { flag = 1; } } if ((k + 2 * d2 + d1) >= 0 && (k + 2 * d2 + d1) % 3 == 0) { w3 = (k + 2 * d2 + d1) / 3; w2 = w3 - d2; w1 = w2 - d1; if (valid(w1, w2, w3) == 1) { flag = 1; } } if (flag == 1) { cout << "yes\n"; } else { cout << "no\n"; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; void FastIO() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } int64_t modpow(int64_t a, int64_t p, int64_t mod) { int64_t ret = 1; while (p) { if (p & 1) ret = (ret * a) % mod; a = (a * a) % mod; p /= 2; } return ret; } int64_t power(int64_t a, int64_t p) { int64_t ret = 1; while (p) { if (p & 1) ret = (ret * a); a = (a * a); p /= 2; } return ret; } int main() { FastIO(); int t; scanf("%d", &t); while (t--) { int64_t n, k, d1, d2; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); bool f = 0; int64_t a, b, c; a = (k + d1 * 2 + d2); if (a % 3 == 0) { a /= 3; b = a - d1; c = a - d1 - d2; if (a >= 0 && b >= 0 && c >= 0) if (k + d1 * 2 + d2 <= n && (n - k - d1 * 2 - d2) % 3 == 0) f = 1; } b = (k - d1 - d2); if (b >= 0 && b % 3 == 0) { b /= 3; a = b + d1; c = b + d2; int64_t mx = max(a, c); int64_t req = mx - a + mx - b + mx - c; if (a >= 0 && b >= 0 && c >= 0) if (k + req <= n && (n - k - req) % 3 == 0) f = 1; } b = (k + d1 + d2); if (b % 3 == 0) { b /= 3; a = b - d1; c = b - d2; if (a >= 0 && b >= 0 && c >= 0) if (k + d1 + d2 <= n && (n - k - d1 - d2) % 3 == 0) f = 1; } c = (k + d1 + d2 * 2); if (c % 3 == 0) { c /= 3; b = c - d2; a = c - d1 - d2; if (a >= 0 && b >= 0 && c >= 0) if (k + d1 + d2 * 2 <= n && (n - k - d1 - d2 * 2) % 3 == 0) f = 1; } printf(f ? "yes\n" : "no\n"); } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void FastIO() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } int64_t modpow(int64_t a, int64_t p, int64_t mod) { int64_t ret = 1; while (p) { if (p & 1) ret = (ret * a) % mod; a = (a * a) % mod; p /= 2; } return ret; } int64_t power(int64_t a, int64_t p) { int64_t ret = 1; while (p) { if (p & 1) ret = (ret * a); a = (a * a); p /= 2; } return ret; } int main() { FastIO(); int t; scanf("%d", &t); while (t--) { int64_t n, k, d1, d2; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); bool f = 0; int64_t a, b, c; a = (k + d1 * 2 + d2); if (a % 3 == 0) { a /= 3; b = a - d1; c = a - d1 - d2; if (a >= 0 && b >= 0 && c >= 0) if (k + d1 * 2 + d2 <= n && (n - k - d1 * 2 - d2) % 3 == 0) f = 1; } b = (k - d1 - d2); if (b >= 0 && b % 3 == 0) { b /= 3; a = b + d1; c = b + d2; int64_t mx = max(a, c); int64_t req = mx - a + mx - b + mx - c; if (a >= 0 && b >= 0 && c >= 0) if (k + req <= n && (n - k - req) % 3 == 0) f = 1; } b = (k + d1 + d2); if (b % 3 == 0) { b /= 3; a = b - d1; c = b - d2; if (a >= 0 && b >= 0 && c >= 0) if (k + d1 + d2 <= n && (n - k - d1 - d2) % 3 == 0) f = 1; } c = (k + d1 + d2 * 2); if (c % 3 == 0) { c /= 3; b = c - d2; a = c - d1 - d2; if (a >= 0 && b >= 0 && c >= 0) if (k + d1 + d2 * 2 <= n && (n - k - d1 - d2 * 2) % 3 == 0) f = 1; } printf(f ? "yes\n" : "no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct comp { bool operator()(const pair<int, int> &a, const pair<int, int> &b) { return a.second > b.second; } }; struct myclass { bool operator()(const pair<int, int> &a, const pair<int, int> &b) { return a.first > b.first; } } myobject; inline void in(int &n) { n = 0; int ch = getchar(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { n = (n << 3) + (n << 1) + ch - '0', ch = getchar(); } n = n * sign; } int yes; void func(long long a, long long b, long long c, long long k) { vector<long long> v; v.push_back(a); v.push_back(b); v.push_back(c); sort(v.begin(), v.end()); k = k - (v[2] - v[0]); v[0] = v[2]; k = k - (v[2] - v[1]); v[1] = v[2]; if (k < 0) return; if (k % 3 == 0) { yes = 1; } return; } int main() { int t; long long n, k, d1, d2, d3, a, b, c; in(t); while (t--) { yes = 0; cin >> n >> k >> d1 >> d2; d3 = d1 + d2; a = d3; b = d2; c = 0; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); if (d1 >= d2) { d3 = d1 - d2; a = d1; b = 0; c = d2; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); } if (d2 >= d1) { d3 = d2 - d1; a = d3; c = 0; b = d2; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); } if (d1 >= d2) { d3 = d1 - d2; a = 0; b = d1; c = d3; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); } if (d2 >= d1) { d3 = d2 - d1; a = d1; b = 0; c = d2; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); } d3 = d1 + d2; a = 0; b = d1; c = d3; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); if (yes == 1) printf("yes\n"); else printf("no\n"); } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct comp { bool operator()(const pair<int, int> &a, const pair<int, int> &b) { return a.second > b.second; } }; struct myclass { bool operator()(const pair<int, int> &a, const pair<int, int> &b) { return a.first > b.first; } } myobject; inline void in(int &n) { n = 0; int ch = getchar(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { n = (n << 3) + (n << 1) + ch - '0', ch = getchar(); } n = n * sign; } int yes; void func(long long a, long long b, long long c, long long k) { vector<long long> v; v.push_back(a); v.push_back(b); v.push_back(c); sort(v.begin(), v.end()); k = k - (v[2] - v[0]); v[0] = v[2]; k = k - (v[2] - v[1]); v[1] = v[2]; if (k < 0) return; if (k % 3 == 0) { yes = 1; } return; } int main() { int t; long long n, k, d1, d2, d3, a, b, c; in(t); while (t--) { yes = 0; cin >> n >> k >> d1 >> d2; d3 = d1 + d2; a = d3; b = d2; c = 0; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); if (d1 >= d2) { d3 = d1 - d2; a = d1; b = 0; c = d2; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); } if (d2 >= d1) { d3 = d2 - d1; a = d3; c = 0; b = d2; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); } if (d1 >= d2) { d3 = d1 - d2; a = 0; b = d1; c = d3; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); } if (d2 >= d1) { d3 = d2 - d1; a = d1; b = 0; c = d2; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); } d3 = d1 + d2; a = 0; b = d1; c = d3; if (a + b + c <= k && (k - a - b - c) % 3 == 0) func(a, b, c, n - k); if (yes == 1) printf("yes\n"); else printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, d1, d2; int t; scanf("%d", &t); while (t--) { cin >> n >> k >> d1 >> d2; long long left = n - k; long long d = d1 + 2 * d2; long long twins = d1 + d1 + d2; if (d <= left && twins <= k && (k - twins) % 3 == 0) { if ((left - d) % 3 == 0) { printf("yes\n"); continue; } } d = d1 + d2; if (d1 < d2) twins = d2 - d1 + d2; else twins = d1 - d2 + d1; if (d <= left && twins <= k && (k - twins) % 3 == 0) { if ((left - d) % 3 == 0) { printf("yes\n"); continue; } } d = 2 * d1 + d2; twins = d2 + d1 + d2; if (d <= left && twins <= k && (k - twins) % 3 == 0) { if ((left - d) % 3 == 0) { printf("yes\n"); continue; } } d = max(0ll, d2 - d1) + max(d1, d2) + max(0ll, d1 - d2); twins = d1 + d2; if (d <= left && twins <= k && (k - twins) % 3 == 0) { left -= d; if (left % 3 == 0) { printf("yes\n"); continue; } } printf("no\n"); } }
### Prompt Create a solution in CPP for the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n, k, d1, d2; int t; scanf("%d", &t); while (t--) { cin >> n >> k >> d1 >> d2; long long left = n - k; long long d = d1 + 2 * d2; long long twins = d1 + d1 + d2; if (d <= left && twins <= k && (k - twins) % 3 == 0) { if ((left - d) % 3 == 0) { printf("yes\n"); continue; } } d = d1 + d2; if (d1 < d2) twins = d2 - d1 + d2; else twins = d1 - d2 + d1; if (d <= left && twins <= k && (k - twins) % 3 == 0) { if ((left - d) % 3 == 0) { printf("yes\n"); continue; } } d = 2 * d1 + d2; twins = d2 + d1 + d2; if (d <= left && twins <= k && (k - twins) % 3 == 0) { if ((left - d) % 3 == 0) { printf("yes\n"); continue; } } d = max(0ll, d2 - d1) + max(d1, d2) + max(0ll, d1 - d2); twins = d1 + d2; if (d <= left && twins <= k && (k - twins) % 3 == 0) { left -= d; if (left % 3 == 0) { printf("yes\n"); continue; } } printf("no\n"); } } ```
#include <bits/stdc++.h> using namespace std; int main() { long long T, n, k, a, b; cin >> T; while (T--) { cin >> n >> k >> a >> b; n -= k; bool flag = 0; if (a < b) swap(a, b); if ((2 * a + b) <= n && (n - 2 * a - b) % 3 == 0 && (2 * b + a) <= k && (k - 2 * b - a) % 3 == 0) flag = 1; if (2 * b + a <= n && (n - 2 * b - a) % 3 == 0 && (2 * a + b) <= k && (k - 2 * a - b) % 3 == 0) flag = 1; if (2 * a - b <= n && (n - 2 * a + b) % 3 == 0 && (a + b) <= k && (k - a - b) % 3 == 0) flag = 1; if (a + b <= n && (n - a - b) % 3 == 0 && (2 * a - b) <= k && (k - 2 * a + b) % 3 == 0) flag = 1; if (flag) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long T, n, k, a, b; cin >> T; while (T--) { cin >> n >> k >> a >> b; n -= k; bool flag = 0; if (a < b) swap(a, b); if ((2 * a + b) <= n && (n - 2 * a - b) % 3 == 0 && (2 * b + a) <= k && (k - 2 * b - a) % 3 == 0) flag = 1; if (2 * b + a <= n && (n - 2 * b - a) % 3 == 0 && (2 * a + b) <= k && (k - 2 * a - b) % 3 == 0) flag = 1; if (2 * a - b <= n && (n - 2 * a + b) % 3 == 0 && (a + b) <= k && (k - a - b) % 3 == 0) flag = 1; if (a + b <= n && (n - a - b) % 3 == 0 && (2 * a - b) <= k && (k - 2 * a + b) % 3 == 0) flag = 1; if (flag) cout << "yes" << endl; else cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-11; const int INFINITE = 0x3f3f3f3f; template <class T> inline void checkmin(T &a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T &a, T b) { if (b > a) a = b; } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline T lowbit(T n) { return (n ^ (n - 1)) & n; } template <class T> inline int countbit(T n) { return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); } typedef vector<int> VI; typedef vector<VI> VII; typedef vector<string> VS; long long n, k, d1, d2; bool c1() { long long x3 = (long long)(k - d1 - 2 * d2) / 3; long long x2 = x3 + d2, x1 = x3 + d1 + d2; if (x1 < 0 || x2 < 0 || x3 < 0) return false; if (x1 + x2 + x3 != k) return false; long long rem = n - k; rem -= 2 * d1 + d2; return ((rem >= 0) && (rem % 3 == 0)); } bool c2() { long long x2 = (long long)(k - d1 - d2) / 3; long long x1 = x2 + d1, x3 = x2 + d2; if (x1 < 0 || x2 < 0 || x3 < 0) return false; if (x1 + x2 + x3 != k) return false; if (d1 < d2) d1 ^= d2 ^= d1 ^= d2; long long rem = n - k; rem -= 2 * d1 - d2; return ((rem >= 0) && (rem % 3 == 0)); } bool c3() { long long x1 = (long long)(k - 2 * d1 + d2) / 3; long long x2 = x1 + d1, x3 = x1 + d1 - d2; if (x1 < 0 || x2 < 0 || x3 < 0) return false; if (x1 + x2 + x3 != k) return false; long long rem = n - k; rem -= d1 + d2; return ((rem >= 0) && (rem % 3 == 0)); } bool c4() { long long x1 = (long long)(k - 2 * d1 - d2) / 3; long long x2 = x1 + d1, x3 = x1 + d1 + d2; if (x1 < 0 || x2 < 0 || x3 < 0) return false; if (x1 + x2 + x3 != k) return false; long long rem = n - k; rem -= d1 + 2 * d2; return ((rem >= 0) && (rem % 3 == 0)); } void solve() { cin >> n >> k >> d1 >> d2; if (c1() || c2() || c3() || c4()) cout << "yes\n"; else cout << "no\n"; } int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) solve(); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-11; const int INFINITE = 0x3f3f3f3f; template <class T> inline void checkmin(T &a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T &a, T b) { if (b > a) a = b; } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline T lowbit(T n) { return (n ^ (n - 1)) & n; } template <class T> inline int countbit(T n) { return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); } typedef vector<int> VI; typedef vector<VI> VII; typedef vector<string> VS; long long n, k, d1, d2; bool c1() { long long x3 = (long long)(k - d1 - 2 * d2) / 3; long long x2 = x3 + d2, x1 = x3 + d1 + d2; if (x1 < 0 || x2 < 0 || x3 < 0) return false; if (x1 + x2 + x3 != k) return false; long long rem = n - k; rem -= 2 * d1 + d2; return ((rem >= 0) && (rem % 3 == 0)); } bool c2() { long long x2 = (long long)(k - d1 - d2) / 3; long long x1 = x2 + d1, x3 = x2 + d2; if (x1 < 0 || x2 < 0 || x3 < 0) return false; if (x1 + x2 + x3 != k) return false; if (d1 < d2) d1 ^= d2 ^= d1 ^= d2; long long rem = n - k; rem -= 2 * d1 - d2; return ((rem >= 0) && (rem % 3 == 0)); } bool c3() { long long x1 = (long long)(k - 2 * d1 + d2) / 3; long long x2 = x1 + d1, x3 = x1 + d1 - d2; if (x1 < 0 || x2 < 0 || x3 < 0) return false; if (x1 + x2 + x3 != k) return false; long long rem = n - k; rem -= d1 + d2; return ((rem >= 0) && (rem % 3 == 0)); } bool c4() { long long x1 = (long long)(k - 2 * d1 - d2) / 3; long long x2 = x1 + d1, x3 = x1 + d1 + d2; if (x1 < 0 || x2 < 0 || x3 < 0) return false; if (x1 + x2 + x3 != k) return false; long long rem = n - k; rem -= d1 + 2 * d2; return ((rem >= 0) && (rem % 3 == 0)); } void solve() { cin >> n >> k >> d1 >> d2; if (c1() || c2() || c3() || c4()) cout << "yes\n"; else cout << "no\n"; } int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; bool cond(long long num, long long lim) { if (num <= lim && num >= 0 && num % 3 == 0) return true; return false; } int main() { int t; scanf("%d", &t); while (t--) { long long n, x, y, z, a, b, c; bool ans = false; cin >> n >> x >> y >> z; if (n % 3 == 0) { for (int i = 0; i < 4; i++) { if (i == 2) z *= -1; a = x + 2 * y + z; b = x - y + z; c = x - y - 2 * z; if (cond(a, n) && cond(b, n) && cond(c, n)) { ans = true; break; } y *= -1; } } if (ans) printf("yes\n"); else printf("no\n"); } }
### Prompt Construct a Cpp code solution to the problem outlined: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool cond(long long num, long long lim) { if (num <= lim && num >= 0 && num % 3 == 0) return true; return false; } int main() { int t; scanf("%d", &t); while (t--) { long long n, x, y, z, a, b, c; bool ans = false; cin >> n >> x >> y >> z; if (n % 3 == 0) { for (int i = 0; i < 4; i++) { if (i == 2) z *= -1; a = x + 2 * y + z; b = x - y + z; c = x - y - 2 * z; if (cond(a, n) && cond(b, n) && cond(c, n)) { ans = true; break; } y *= -1; } } if (ans) printf("yes\n"); else printf("no\n"); } } ```
#include <bits/stdc++.h> using namespace std; int check(long long n, long long k, long long d1, long long d2) { return (k - d1 - d2 - d2) >= 0 && (k - d1 - d2 - d2) % 3 == 0 && (n - k - d1 - d1 - d2) >= 0 && (n - k - d1 - d1 - d2) % 3 == 0; } int main() { int ca; long long n, k, d1, d2; while (~scanf("%d", &ca)) { while (ca-- > 0) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); bool ok = check(n, k, d1, d2) || check(n, k, d2, d1); if (d1 > d2) { ok = ok || check(n, k, d2, d1 - d2) || check(n, k, d1 - d2, d2); } else { ok = ok || check(n, k, d1, d2 - d1) || check(n, k, d2 - d1, d1); } if (n % 3 == 0 && ok) { printf("yes\n"); } else { printf("no\n"); } } } }
### Prompt Please create a solution in cpp to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int check(long long n, long long k, long long d1, long long d2) { return (k - d1 - d2 - d2) >= 0 && (k - d1 - d2 - d2) % 3 == 0 && (n - k - d1 - d1 - d2) >= 0 && (n - k - d1 - d1 - d2) % 3 == 0; } int main() { int ca; long long n, k, d1, d2; while (~scanf("%d", &ca)) { while (ca-- > 0) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); bool ok = check(n, k, d1, d2) || check(n, k, d2, d1); if (d1 > d2) { ok = ok || check(n, k, d2, d1 - d2) || check(n, k, d1 - d2, d2); } else { ok = ok || check(n, k, d1, d2 - d1) || check(n, k, d2 - d1, d1); } if (n % 3 == 0 && ok) { printf("yes\n"); } else { printf("no\n"); } } } } ```
#include <bits/stdc++.h> using namespace std; const int LIM = 100005; int main(int argc, char* argv[]) { long long T, n, k, d1, d2, t1, t2, t3; long long m; int r; bool flag; cin >> T; while (T-- > 0LL) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { printf("no\n"); continue; } m = n / 3; flag = false; t1 = k + 2 * d1 + d2; t2 = k - d1 + d2; t3 = 3 * k - t1 - t2; if (t1 % 3 == 0 && t2 % 3 == 0 && t3 % 3 == 0 && t1 >= 0 && t2 >= 0 && t3 >= 0) { t1 /= 3; t2 /= 3; t3 /= 3; if (m >= t1 && m >= t2 && m >= t3) flag = true, r = 1; } t1 = k + d2 - 2 * d1; t2 = k + d1 + d2; t3 = 3 * k - t1 - t2; if (t1 % 3 == 0 && t2 % 3 == 0 && t3 % 3 == 0 && t1 >= 0 && t2 >= 0 && t3 >= 0) { t1 /= 3; t2 /= 3; t3 /= 3; if (m >= t1 && m >= t2 && m >= t3) flag = true, r = 2; } t1 = k - 2 * d1 - d2; t2 = k - d2 + d1; t3 = 3 * k - t1 - t2; if (t1 % 3 == 0 && t2 % 3 == 0 && t3 % 3 == 0 && t1 >= 0 && t2 >= 0 && t3 >= 0) { t1 /= 3; t2 /= 3; t3 /= 3; if (m >= t1 && m >= t2 && m >= t3) flag = true, r = 3; } t1 = k + 2 * d1 - d2; t2 = k - d1 - d2; t3 = 3 * k - t1 - t2; if (t1 % 3 == 0 && t2 % 3 == 0 && t3 % 3 == 0 && t1 >= 0 && t2 >= 0 && t3 >= 0) { t1 /= 3; t2 /= 3; t3 /= 3; if (m >= t1 && m >= t2 && m >= t3) flag = true, r = 4; } if (flag) printf("yes\n"); else printf("no\n"); } return 0; }
### Prompt Please create a solution in cpp to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int LIM = 100005; int main(int argc, char* argv[]) { long long T, n, k, d1, d2, t1, t2, t3; long long m; int r; bool flag; cin >> T; while (T-- > 0LL) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { printf("no\n"); continue; } m = n / 3; flag = false; t1 = k + 2 * d1 + d2; t2 = k - d1 + d2; t3 = 3 * k - t1 - t2; if (t1 % 3 == 0 && t2 % 3 == 0 && t3 % 3 == 0 && t1 >= 0 && t2 >= 0 && t3 >= 0) { t1 /= 3; t2 /= 3; t3 /= 3; if (m >= t1 && m >= t2 && m >= t3) flag = true, r = 1; } t1 = k + d2 - 2 * d1; t2 = k + d1 + d2; t3 = 3 * k - t1 - t2; if (t1 % 3 == 0 && t2 % 3 == 0 && t3 % 3 == 0 && t1 >= 0 && t2 >= 0 && t3 >= 0) { t1 /= 3; t2 /= 3; t3 /= 3; if (m >= t1 && m >= t2 && m >= t3) flag = true, r = 2; } t1 = k - 2 * d1 - d2; t2 = k - d2 + d1; t3 = 3 * k - t1 - t2; if (t1 % 3 == 0 && t2 % 3 == 0 && t3 % 3 == 0 && t1 >= 0 && t2 >= 0 && t3 >= 0) { t1 /= 3; t2 /= 3; t3 /= 3; if (m >= t1 && m >= t2 && m >= t3) flag = true, r = 3; } t1 = k + 2 * d1 - d2; t2 = k - d1 - d2; t3 = 3 * k - t1 - t2; if (t1 % 3 == 0 && t2 % 3 == 0 && t3 % 3 == 0 && t1 >= 0 && t2 >= 0 && t3 >= 0) { t1 /= 3; t2 /= 3; t3 /= 3; if (m >= t1 && m >= t2 && m >= t3) flag = true, r = 4; } if (flag) printf("yes\n"); else printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { long long n, k, d1, d2; long long x1, x2, x3, av; bool tag = false; cin >> n >> k >> d1 >> d2; if (n % 3) { printf("no\n"); continue; } av = n / 3; if ((2 * d1 + d2 + k) % 3 == 0 && (-d1 + d2 + k) % 3 == 0 && (-d1 - 2 * d2 + k) % 3 == 0) { x1 = (2 * d1 + d2 + k) / 3; x2 = (-d1 + d2 + k) / 3; x3 = (-d1 - 2 * d2 + k) / 3; if (x1 >= 0 && x1 <= av && x2 >= 0 && x2 <= av && x3 >= 0 && x3 <= av) tag = true; } if ((2 * d1 - d2 + k) % 3 == 0 && (-d1 - d2 + k) % 3 == 0 && (-d1 + 2 * d2 + k) % 3 == 0) { x1 = (2 * d1 - d2 + k) / 3; x2 = (-d1 - d2 + k) / 3; x3 = (-d1 + 2 * d2 + k) / 3; if (x1 >= 0 && x1 <= av && x2 >= 0 && x2 <= av && x3 >= 0 && x3 <= av) tag = true; } if ((-2 * d1 + d2 + k) % 3 == 0 && (d1 + d2 + k) % 3 == 0 && (d1 - 2 * d2 + k) % 3 == 0) { x1 = (-2 * d1 + d2 + k) / 3; x2 = (d1 + d2 + k) / 3; x3 = (d1 - 2 * d2 + k) / 3; if (x1 >= 0 && x1 <= av && x2 >= 0 && x2 <= av && x3 >= 0 && x3 <= av) tag = true; } if ((-2 * d1 - d2 + k) % 3 == 0 && (d1 - d2 + k) % 3 == 0 && (d1 + 2 * d2 + k) % 3 == 0) { x1 = (-2 * d1 - d2 + k) / 3; x2 = (d1 - d2 + k) / 3; x3 = (d1 + 2 * d2 + k) / 3; if (x1 >= 0 && x1 <= av && x2 >= 0 && x2 <= av && x3 >= 0 && x3 <= av) tag = true; } if (tag) printf("yes\n"); else printf("no\n"); } }
### Prompt Generate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { long long n, k, d1, d2; long long x1, x2, x3, av; bool tag = false; cin >> n >> k >> d1 >> d2; if (n % 3) { printf("no\n"); continue; } av = n / 3; if ((2 * d1 + d2 + k) % 3 == 0 && (-d1 + d2 + k) % 3 == 0 && (-d1 - 2 * d2 + k) % 3 == 0) { x1 = (2 * d1 + d2 + k) / 3; x2 = (-d1 + d2 + k) / 3; x3 = (-d1 - 2 * d2 + k) / 3; if (x1 >= 0 && x1 <= av && x2 >= 0 && x2 <= av && x3 >= 0 && x3 <= av) tag = true; } if ((2 * d1 - d2 + k) % 3 == 0 && (-d1 - d2 + k) % 3 == 0 && (-d1 + 2 * d2 + k) % 3 == 0) { x1 = (2 * d1 - d2 + k) / 3; x2 = (-d1 - d2 + k) / 3; x3 = (-d1 + 2 * d2 + k) / 3; if (x1 >= 0 && x1 <= av && x2 >= 0 && x2 <= av && x3 >= 0 && x3 <= av) tag = true; } if ((-2 * d1 + d2 + k) % 3 == 0 && (d1 + d2 + k) % 3 == 0 && (d1 - 2 * d2 + k) % 3 == 0) { x1 = (-2 * d1 + d2 + k) / 3; x2 = (d1 + d2 + k) / 3; x3 = (d1 - 2 * d2 + k) / 3; if (x1 >= 0 && x1 <= av && x2 >= 0 && x2 <= av && x3 >= 0 && x3 <= av) tag = true; } if ((-2 * d1 - d2 + k) % 3 == 0 && (d1 - d2 + k) % 3 == 0 && (d1 + 2 * d2 + k) % 3 == 0) { x1 = (-2 * d1 - d2 + k) / 3; x2 = (d1 - d2 + k) / 3; x3 = (d1 + 2 * d2 + k) / 3; if (x1 >= 0 && x1 <= av && x2 >= 0 && x2 <= av && x3 >= 0 && x3 <= av) tag = true; } if (tag) printf("yes\n"); else printf("no\n"); } } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000000") using namespace std; const long long int INF = 2e9 + 1; int main() { int T; cin >> T; for (int(Q) = 0; (Q) < (T); (Q)++) { long long int n, k, d1, d2; cin >> n >> k >> d1 >> d2; bool can = false; for (int(i) = 0; (i) < (2); (i)++) for (int(j) = 0; (j) < (2); (j)++) { long long int a = 0; long long int b = (i ? -1 : 1) * d1; long long int c = b + (j ? -1 : 1) * d2; if (k - b - c >= 0 && (k - b - c) % 3 == 0) { long long int x = (k - b - c) / 3; if (x >= 0 && x + min(b, c) >= 0) { long long int maxi = max(a, max(b, c)); long long int val = 3 * maxi - a - b - c; long long int delta = n - k; if (delta >= val && (delta - val) % 3 == 0) can = true; } } } printf(can ? "yes\n" : "no\n"); } }
### Prompt Your challenge is to write a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000000") using namespace std; const long long int INF = 2e9 + 1; int main() { int T; cin >> T; for (int(Q) = 0; (Q) < (T); (Q)++) { long long int n, k, d1, d2; cin >> n >> k >> d1 >> d2; bool can = false; for (int(i) = 0; (i) < (2); (i)++) for (int(j) = 0; (j) < (2); (j)++) { long long int a = 0; long long int b = (i ? -1 : 1) * d1; long long int c = b + (j ? -1 : 1) * d2; if (k - b - c >= 0 && (k - b - c) % 3 == 0) { long long int x = (k - b - c) / 3; if (x >= 0 && x + min(b, c) >= 0) { long long int maxi = max(a, max(b, c)); long long int val = 3 * maxi - a - b - c; long long int delta = n - k; if (delta >= val && (delta - val) % 3 == 0) can = true; } } } printf(can ? "yes\n" : "no\n"); } } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool isok(long long d1, long long d2) { long long x, y, z; if (k - 2 * d2 - d1 < 0) return false; if ((k - 2 * d2 - d1) % 3) return false; z = (k - 2 * d2 - d1) / 3; y = d2 + z; x = d1 + y; if (x < 0 || y < 0) return false; long long l = max(max(x, y), z), r = max(max(x, y), z) + n; while (l <= r) { long long mid = (l + r) >> 1; long long tmp = (mid - x) + (mid - y) + (mid - z); if (tmp == n) return true; if (tmp < n) l = mid + 1; else r = mid - 1; } return false; } int main() { int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; n -= k; int flag = 0; flag = flag | isok(d1, d2) | isok(-d1, d2) | isok(d1, -d2) | isok(-d1, d2) | isok(-d1, -d2); if (!flag) { puts("no"); } else puts("yes"); } }
### Prompt Generate a CPP solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool isok(long long d1, long long d2) { long long x, y, z; if (k - 2 * d2 - d1 < 0) return false; if ((k - 2 * d2 - d1) % 3) return false; z = (k - 2 * d2 - d1) / 3; y = d2 + z; x = d1 + y; if (x < 0 || y < 0) return false; long long l = max(max(x, y), z), r = max(max(x, y), z) + n; while (l <= r) { long long mid = (l + r) >> 1; long long tmp = (mid - x) + (mid - y) + (mid - z); if (tmp == n) return true; if (tmp < n) l = mid + 1; else r = mid - 1; } return false; } int main() { int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; n -= k; int flag = 0; flag = flag | isok(d1, d2) | isok(-d1, d2) | isok(d1, -d2) | isok(-d1, d2) | isok(-d1, -d2); if (!flag) { puts("no"); } else puts("yes"); } } ```
#include <bits/stdc++.h> using namespace std; bool check(long long n, long long k, long long d1, long long d2) { if (n % 3) return 0; long long win = n / 3; for (long long sgn1 = -1; sgn1 <= 1; sgn1++) { for (long long sgn2 = -1; sgn2 <= 1; sgn2++) { if (sgn1 == 0 || sgn2 == 0) continue; long long dd1 = d1 * sgn1; long long dd2 = d2 * sgn2; long long x2 = (k - dd1 + dd2) / 3; if ((k - dd1 + dd2) % 3) continue; if (x2 >= 0 && x2 <= k) { long long x1 = dd1 + x2; long long x3 = x2 - dd2; if (x1 >= 0 && x1 <= k && x3 >= 0 && x3 <= k) { if (x1 <= win && x2 <= win && x3 <= win) { if (abs(x1 - x2) == d1 && abs(x2 - x3) == d2) return true; } } } } } return false; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (check(n, k, d1, d2)) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool check(long long n, long long k, long long d1, long long d2) { if (n % 3) return 0; long long win = n / 3; for (long long sgn1 = -1; sgn1 <= 1; sgn1++) { for (long long sgn2 = -1; sgn2 <= 1; sgn2++) { if (sgn1 == 0 || sgn2 == 0) continue; long long dd1 = d1 * sgn1; long long dd2 = d2 * sgn2; long long x2 = (k - dd1 + dd2) / 3; if ((k - dd1 + dd2) % 3) continue; if (x2 >= 0 && x2 <= k) { long long x1 = dd1 + x2; long long x3 = x2 - dd2; if (x1 >= 0 && x1 <= k && x3 >= 0 && x3 <= k) { if (x1 <= win && x2 <= win && x3 <= win) { if (abs(x1 - x2) == d1 && abs(x2 - x3) == d2) return true; } } } } } return false; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (check(n, k, d1, d2)) cout << "yes" << endl; else cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool Possibility(long long n, long long k, long long d1, long long d2) { if (n % 3) return false; if (d1 + d2 > k) return false; if (n - k - 3 * max(d1, d2) + d1 + d2 < 0) return false; if ((n - k - 3 * max(d1, d2) + d1 + d2) % 3 > 0) return false; return true; } void Read() { int t; scanf("%d", &t); while (t--) { scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2); bool judge = false; if (Possibility(n, k, d1, d2)) { judge = true; } if (Possibility(n, k, d1 + d2, d1)) { judge = true; } if (Possibility(n, k, max(d1, d2), max(d1, d2) - min(d1, d2))) { judge = true; } if (Possibility(n, k, d1 + d2, d2)) { judge = true; } if (judge) printf("yes\n"); else printf("no\n"); } } int main() { Read(); return 0; }
### Prompt Create a solution in CPP for the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool Possibility(long long n, long long k, long long d1, long long d2) { if (n % 3) return false; if (d1 + d2 > k) return false; if (n - k - 3 * max(d1, d2) + d1 + d2 < 0) return false; if ((n - k - 3 * max(d1, d2) + d1 + d2) % 3 > 0) return false; return true; } void Read() { int t; scanf("%d", &t); while (t--) { scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2); bool judge = false; if (Possibility(n, k, d1, d2)) { judge = true; } if (Possibility(n, k, d1 + d2, d1)) { judge = true; } if (Possibility(n, k, max(d1, d2), max(d1, d2) - min(d1, d2))) { judge = true; } if (Possibility(n, k, d1 + d2, d2)) { judge = true; } if (judge) printf("yes\n"); else printf("no\n"); } } int main() { Read(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2; bool _(long long a, long long b) { return (a / 3 >= b && a / 3 * 3 == a && a <= n); } bool x() { if (n % 3) return false; if (_(n - k + d1 + d2 * 2, d1 + d2)) return true; if (_(n - k + d2 + d1 * 2, d1 + d2)) return true; if (_(n - k + d1 + d2, max(d1, d2))) return true; if (_(n - k - min(d1, d2) + max(d1, d2) * 2, max(d1, d2))) return true; return false; } int main() { scanf("%I64d", &t); for (long long i = 0; i < t; i++) { cin >> n >> k >> d1 >> d2; printf("%s\n", x() ? "yes" : "no"); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2; bool _(long long a, long long b) { return (a / 3 >= b && a / 3 * 3 == a && a <= n); } bool x() { if (n % 3) return false; if (_(n - k + d1 + d2 * 2, d1 + d2)) return true; if (_(n - k + d2 + d1 * 2, d1 + d2)) return true; if (_(n - k + d1 + d2, max(d1, d2))) return true; if (_(n - k - min(d1, d2) + max(d1, d2) * 2, max(d1, d2))) return true; return false; } int main() { scanf("%I64d", &t); for (long long i = 0; i < t; i++) { cin >> n >> k >> d1 >> d2; printf("%s\n", x() ? "yes" : "no"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool check(long long ofsa, long long ofsb) { long long c_3 = (k - ofsa - ofsb); if (c_3 % 3 != 0) return 0; long long c = c_3 / 3, a = c + ofsa, b = c + ofsb; if (a < 0 || b < 0 || c < 0) return 0; if (a > n / 3 || b > n / 3 || c > n / 3) return 0; return 1; } bool check() { if (n % 3 != 0) return 0; if (check(d2 + d1, d2)) return 1; if (check(d2 - d1, d2)) return 1; if (check(-d2 - d1, -d2)) return 1; if (check(-d2 + d1, -d2)) return 1; return 0; } void solve() { cin >> n >> k >> d1 >> d2; ; ; cout << (check() ? "yes" : "no") << endl; } void contest_main() { int t; cin >> t; while (t--) solve(); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); contest_main(); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool check(long long ofsa, long long ofsb) { long long c_3 = (k - ofsa - ofsb); if (c_3 % 3 != 0) return 0; long long c = c_3 / 3, a = c + ofsa, b = c + ofsb; if (a < 0 || b < 0 || c < 0) return 0; if (a > n / 3 || b > n / 3 || c > n / 3) return 0; return 1; } bool check() { if (n % 3 != 0) return 0; if (check(d2 + d1, d2)) return 1; if (check(d2 - d1, d2)) return 1; if (check(-d2 - d1, -d2)) return 1; if (check(-d2 + d1, -d2)) return 1; return 0; } void solve() { cin >> n >> k >> d1 >> d2; ; ; cout << (check() ? "yes" : "no") << endl; } void contest_main() { int t; cin >> t; while (t--) solve(); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); contest_main(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long tc, n, k, d1, d2; cin >> tc; while (tc--) { cin >> n >> k >> d1 >> d2; if (false) { printf("no\n"); } else { if (false) { printf("no\n"); } else { const long long r = n - k; long long x; bool yes = false; long long a, b, c, sum; a = d1 + d2, b = d2, c = 0; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; x = r - ((a - c) + (a - b)); if (x >= 0 && x % 3 == 0) yes = true; } if (!yes) { a = 0, b = d1, c = d1 + d2; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; x = r - ((c - a) + (c - b)); if (x >= 0 && x % 3 == 0) yes = true; } } if (!yes) { a = (d1 < d2) ? d2 - d1 : 0, b = max(d1, d2), c = (d1 < d2) ? 0 : d1 - d2; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; x = r - ((b - a) + (b - c)); if (x >= 0 && x % 3 == 0) yes = true; } } if (!yes) { a = d1, b = 0, c = d2; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; if (a > c) x = r - ((a - b) + (a - c)); else x = r - ((c - b) + (c - a)); if (x >= 0 && x % 3 == 0) yes = true; } } if (yes) printf("yes\n"); else printf("no\n"); } } } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long tc, n, k, d1, d2; cin >> tc; while (tc--) { cin >> n >> k >> d1 >> d2; if (false) { printf("no\n"); } else { if (false) { printf("no\n"); } else { const long long r = n - k; long long x; bool yes = false; long long a, b, c, sum; a = d1 + d2, b = d2, c = 0; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; x = r - ((a - c) + (a - b)); if (x >= 0 && x % 3 == 0) yes = true; } if (!yes) { a = 0, b = d1, c = d1 + d2; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; x = r - ((c - a) + (c - b)); if (x >= 0 && x % 3 == 0) yes = true; } } if (!yes) { a = (d1 < d2) ? d2 - d1 : 0, b = max(d1, d2), c = (d1 < d2) ? 0 : d1 - d2; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; x = r - ((b - a) + (b - c)); if (x >= 0 && x % 3 == 0) yes = true; } } if (!yes) { a = d1, b = 0, c = d2; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; if (a > c) x = r - ((a - b) + (a - c)); else x = r - ((c - b) + (c - a)); if (x >= 0 && x % 3 == 0) yes = true; } } if (yes) printf("yes\n"); else printf("no\n"); } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long Max(long long a, long long b) { return a > b ? a : b; } int main() { long long t, n, k, d1, d2, m; int flag; cin >> t; while (t--) { flag = 1; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } if (k < (d1 + d2 + d2) || (k - d2 - d1 - d2) % 3 != 0) { flag = 0; } if (flag) { m = (k - d2 - d1 - d2) / 3; if (n % 3 != 0 || (n / 3) < (d1 + d2 + m)) flag = 0; } if (flag) cout << "yes\n"; else { flag = 1; if (k < (d1 + d2) || (k - d1 - d2) % 3 != 0) flag = 0; if (flag) { m = (k - d1 - d2) / 3; if (n % 3 != 0 || (n / 3) < Max(d1 + m, d2 + m)) flag = 0; } if (flag) cout << "yes\n"; else { flag = 1; if (k < (d1 + d1 + d2) || (k - d1 - d1 - d2) % 3 != 0) flag = 0; if (flag) { m = (k - d1 - d1 - d2) / 3; if (n % 3 != 0 || (n / 3) < (d1 + d2 + m)) flag = 0; } if (flag) cout << "yes\n"; else { if (d1 < d2) { m = d1; d1 = d2; d2 = m; } flag = 1; if (k < (2 * d1 - d2) || (k - d1 + d2 - d1) % 3 != 0) { flag = 0; } if (flag) { m = (k - d1 + d2 - d1) / 3; if (n % 3 != 0 || (n / 3) < (d1 + m)) flag = 0; } if (flag) cout << "yes\n"; else cout << "no\n"; } } } } return 0; }
### Prompt In CPP, your task is to solve the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long Max(long long a, long long b) { return a > b ? a : b; } int main() { long long t, n, k, d1, d2, m; int flag; cin >> t; while (t--) { flag = 1; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } if (k < (d1 + d2 + d2) || (k - d2 - d1 - d2) % 3 != 0) { flag = 0; } if (flag) { m = (k - d2 - d1 - d2) / 3; if (n % 3 != 0 || (n / 3) < (d1 + d2 + m)) flag = 0; } if (flag) cout << "yes\n"; else { flag = 1; if (k < (d1 + d2) || (k - d1 - d2) % 3 != 0) flag = 0; if (flag) { m = (k - d1 - d2) / 3; if (n % 3 != 0 || (n / 3) < Max(d1 + m, d2 + m)) flag = 0; } if (flag) cout << "yes\n"; else { flag = 1; if (k < (d1 + d1 + d2) || (k - d1 - d1 - d2) % 3 != 0) flag = 0; if (flag) { m = (k - d1 - d1 - d2) / 3; if (n % 3 != 0 || (n / 3) < (d1 + d2 + m)) flag = 0; } if (flag) cout << "yes\n"; else { if (d1 < d2) { m = d1; d1 = d2; d2 = m; } flag = 1; if (k < (2 * d1 - d2) || (k - d1 + d2 - d1) % 3 != 0) { flag = 0; } if (flag) { m = (k - d1 + d2 - d1) / 3; if (n % 3 != 0 || (n / 3) < (d1 + m)) flag = 0; } if (flag) cout << "yes\n"; else cout << "no\n"; } } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2, t, a, b, c; void solve() { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3) { printf("no\n"); return; } t = n / 3; bool ok = false; if ((d1 * 2 + d2 + k) % 3 == 0) { a = (k + d1 * 2 + d2) / 3; b = a - d1; c = b - d2; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } if ((k - d1 - d2) % 3 == 0) { b = (k - d1 - d2) / 3; a = b + d1; c = b + d2; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } if ((k + d1 + d2) % 3 == 0) { b = (k + d1 + d2) / 3; a = b - d1; c = b - d2; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } if ((k + d2 * 2 + d1) % 3 == 0) { c = (k + d2 * 2 + d1) / 3; b = c - d2; a = b - d1; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } printf("no\n"); } int main() { int q; scanf("%d", &q); while (q--) solve(); return 0; }
### Prompt Please create a solution in Cpp to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2, t, a, b, c; void solve() { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3) { printf("no\n"); return; } t = n / 3; bool ok = false; if ((d1 * 2 + d2 + k) % 3 == 0) { a = (k + d1 * 2 + d2) / 3; b = a - d1; c = b - d2; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } if ((k - d1 - d2) % 3 == 0) { b = (k - d1 - d2) / 3; a = b + d1; c = b + d2; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } if ((k + d1 + d2) % 3 == 0) { b = (k + d1 + d2) / 3; a = b - d1; c = b - d2; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } if ((k + d2 * 2 + d1) % 3 == 0) { c = (k + d2 * 2 + d1) / 3; b = c - d2; a = b - d1; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } printf("no\n"); } int main() { int q; scanf("%d", &q); while (q--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int T, i, j, ok; long long n, k, d1, d2, r, a, b, c; for (scanf("%d", &T); T--;) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3) { puts("no"); continue; } ok = 0; for (i = -1; i <= 1; i += 2) for (j = -1; j <= 2; j += 2) { r = k - i * d1 - j * d2 - i * d1; if (r % 3) continue; a = r / 3; b = a + i * d1; c = b + j * d2; if (a < 0 || b < 0 || c < 0) continue; if (a > n / 3) continue; if (b > n / 3) continue; if (c > n / 3) continue; ok = 1; } if (ok) puts("yes"); else puts("no"); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int T, i, j, ok; long long n, k, d1, d2, r, a, b, c; for (scanf("%d", &T); T--;) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3) { puts("no"); continue; } ok = 0; for (i = -1; i <= 1; i += 2) for (j = -1; j <= 2; j += 2) { r = k - i * d1 - j * d2 - i * d1; if (r % 3) continue; a = r / 3; b = a + i * d1; c = b + j * d2; if (a < 0 || b < 0 || c < 0) continue; if (a > n / 3) continue; if (b > n / 3) continue; if (c > n / 3) continue; ok = 1; } if (ok) puts("yes"); else puts("no"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int max(long long int a, long long int b) { if (a > b) return a; else return b; } long long int min(long long int a, long long int b) { if (a < b) return a; else return b; } const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1}; long long int func(long long int x) { long long int ans = 0; for (long long int i = 2; i * i * i <= x; i++) { ans += (x / (i * i * i)); } return ans; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int k, i, j, m, n, x, y, t, d1, d2, a, b, c; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; long long int x, y, z = 0; z = k - (d1 + d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1; b = x; c = x + d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (d1 - d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1; b = x; c = x - d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d1 - d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1; b = x; c = x - d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d1 + d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1; b = x; c = x + d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (d2 + d1 + d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1 + d2; b = x + d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (d2 + d2 - d1); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1 + d2; b = x + d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d2 - d2 + d1); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1 - d2; b = x - d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d2 - d2 - d1); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1 - d2; b = x - d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } cout << "no" << "\n"; } }
### Prompt Please formulate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int max(long long int a, long long int b) { if (a > b) return a; else return b; } long long int min(long long int a, long long int b) { if (a < b) return a; else return b; } const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1}; long long int func(long long int x) { long long int ans = 0; for (long long int i = 2; i * i * i <= x; i++) { ans += (x / (i * i * i)); } return ans; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int k, i, j, m, n, x, y, t, d1, d2, a, b, c; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; long long int x, y, z = 0; z = k - (d1 + d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1; b = x; c = x + d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (d1 - d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1; b = x; c = x - d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d1 - d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1; b = x; c = x - d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d1 + d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1; b = x; c = x + d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (d2 + d1 + d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1 + d2; b = x + d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (d2 + d2 - d1); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1 + d2; b = x + d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d2 - d2 + d1); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1 - d2; b = x - d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d2 - d2 - d1); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1 - d2; b = x - d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } cout << "no" << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; long long n, k; inline bool Solve(const long long d1, const long long d2) { long long p = k + 2 * d1 + d2; if (p % 3) return 0; long long x1 = p / 3; if (x1 < 0 || x1 > n / 3) return 0; long long x2 = x1 - d1; if (x2 < 0 || x2 > n / 3) return 0; long long x3 = x2 - d2; if (x3 < 0 || x3 > n / 3) return 0; return 1; } int main() { int t; cin.sync_with_stdio(false); cin >> t; long long d1, d2; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no\n"; continue; } bool ok = 0; ok |= Solve(d1, d2); ok |= Solve(-d1, d2); ok |= Solve(-d1, -d2); ok |= Solve(d1, -d2); if (ok) cout << "yes\n"; else cout << "no\n"; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k; inline bool Solve(const long long d1, const long long d2) { long long p = k + 2 * d1 + d2; if (p % 3) return 0; long long x1 = p / 3; if (x1 < 0 || x1 > n / 3) return 0; long long x2 = x1 - d1; if (x2 < 0 || x2 > n / 3) return 0; long long x3 = x2 - d2; if (x3 < 0 || x3 > n / 3) return 0; return 1; } int main() { int t; cin.sync_with_stdio(false); cin >> t; long long d1, d2; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no\n"; continue; } bool ok = 0; ok |= Solve(d1, d2); ok |= Solve(-d1, d2); ok |= Solve(-d1, -d2); ok |= Solve(d1, -d2); if (ok) cout << "yes\n"; else cout << "no\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void calc() { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no" << endl; return; } long long a[3]; for (int i = 0; i < 2; i++) for (int j = 1; j < 3; j++) { a[0] = a[1] = a[2] = 0; if (i == j) { a[0] = -d1; a[2] = -d2; } else if (i == 0 && j == 1) { a[0] = d1; a[2] = -d2; } else { a[i] += d1; a[2] = a[1]; a[j] += d2; } long long mi = min(a[0], min(a[1], a[2])); a[0] -= mi; a[1] -= mi; a[2] -= mi; long long ma = max(a[0], max(a[1], a[2])); if ((a[0] + a[1] + a[2]) % 3 == k % 3 && a[0] + a[1] + a[2] <= k && 3 * ma + k - a[0] - a[1] - a[2] <= n) { cerr << a[0] << ' ' << a[1] << ' ' << a[2] << endl; cout << "yes" << endl; return; } } cout << "no" << endl; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) calc(); }
### Prompt In CPP, your task is to solve the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void calc() { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no" << endl; return; } long long a[3]; for (int i = 0; i < 2; i++) for (int j = 1; j < 3; j++) { a[0] = a[1] = a[2] = 0; if (i == j) { a[0] = -d1; a[2] = -d2; } else if (i == 0 && j == 1) { a[0] = d1; a[2] = -d2; } else { a[i] += d1; a[2] = a[1]; a[j] += d2; } long long mi = min(a[0], min(a[1], a[2])); a[0] -= mi; a[1] -= mi; a[2] -= mi; long long ma = max(a[0], max(a[1], a[2])); if ((a[0] + a[1] + a[2]) % 3 == k % 3 && a[0] + a[1] + a[2] <= k && 3 * ma + k - a[0] - a[1] - a[2] <= n) { cerr << a[0] << ' ' << a[1] << ' ' << a[2] << endl; cout << "yes" << endl; return; } } cout << "no" << endl; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) calc(); } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2, a, b, ans; void work() { scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2), ans = 0; a = k + 2 * d1 + d2, b = k - d1 - 2 * d2; if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; a = k + 2 * max(d1, d2) - min(d1, d2), b = k - d1 - d2; if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; a = k + d1 + d2, b = k - 2 * max(d1, d2) + min(d1, d2); if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; a = k + d1 + 2 * d2, b = k - 2 * d1 - d2; if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; if (ans) printf("yes\n"); else printf("no\n"); } int main() { int t; scanf("%d", &t); while (t--) work(); return 0; }
### Prompt Please formulate a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2, a, b, ans; void work() { scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2), ans = 0; a = k + 2 * d1 + d2, b = k - d1 - 2 * d2; if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; a = k + 2 * max(d1, d2) - min(d1, d2), b = k - d1 - d2; if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; a = k + d1 + d2, b = k - 2 * max(d1, d2) + min(d1, d2); if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; a = k + d1 + 2 * d2, b = k - 2 * d1 - d2; if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; if (ans) printf("yes\n"); else printf("no\n"); } int main() { int t; scanf("%d", &t); while (t--) work(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, k, d1, d2, i, j; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { long long int cd1 = d1 * (i == 0 ? 1 : -1); long long int cd2 = d2 * (j == 0 ? 1 : -1); long long int y = (k - cd2 + cd1) / 3; long long int x = y - cd1; long long int z = k - x - y; if (llabs(y - x) == d1 && llabs(y - z) == d2 && x >= 0 && y >= 0 && z >= 0) { if (x <= n / 3 && y <= n / 3 && z <= n / 3 && n % 3 == 0) { printf("yes\n"); i = j = 100; break; } } } } if (i < 10) { printf("no\n"); } } return 0; }
### Prompt Generate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int t, n, k, d1, d2, i, j; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { long long int cd1 = d1 * (i == 0 ? 1 : -1); long long int cd2 = d2 * (j == 0 ? 1 : -1); long long int y = (k - cd2 + cd1) / 3; long long int x = y - cd1; long long int z = k - x - y; if (llabs(y - x) == d1 && llabs(y - z) == d2 && x >= 0 && y >= 0 && z >= 0) { if (x <= n / 3 && y <= n / 3 && z <= n / 3 && n % 3 == 0) { printf("yes\n"); i = j = 100; break; } } } } if (i < 10) { printf("no\n"); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n; bool solve(long long k, long long d1, long long d2, long long m) { long long t2 = (k + d2 - d1) / 3; if ((k + d2 - d1) % 3 != 0) return false; long long t1 = d1 + t2; long long t3 = t2 - d2; long long maxV = max(t1, max(t2, t3)); long long need = 0; if (maxV == t1) { need = (maxV - t2) + (maxV - t3); } else if (maxV == t2) { need = (maxV - t1) + (maxV - t3); } else if (maxV == t3) { need = (maxV - t1) + (maxV - t2); } if (t1 < 0 || t2 < 0 || t3 < 0) return false; m -= k; m -= need; if (m < 0 || m % 3 != 0) { return false; } return true; } int main() { long long t; cin >> t; for (long long i = 0; i < t; i++) { long long k, d1, d2; cin >> n >> k >> d1 >> d2; if (solve(k, d1, d2, n)) cout << "yes\n"; else if (solve(k, -1 * d1, d2, n)) cout << "yes\n"; else if (solve(k, -1 * d1, -1 * d2, n)) cout << "yes\n"; else if (solve(k, d1, -1 * d2, n)) cout << "yes\n"; else { cout << "no\n"; } } return 0; }
### Prompt Generate a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n; bool solve(long long k, long long d1, long long d2, long long m) { long long t2 = (k + d2 - d1) / 3; if ((k + d2 - d1) % 3 != 0) return false; long long t1 = d1 + t2; long long t3 = t2 - d2; long long maxV = max(t1, max(t2, t3)); long long need = 0; if (maxV == t1) { need = (maxV - t2) + (maxV - t3); } else if (maxV == t2) { need = (maxV - t1) + (maxV - t3); } else if (maxV == t3) { need = (maxV - t1) + (maxV - t2); } if (t1 < 0 || t2 < 0 || t3 < 0) return false; m -= k; m -= need; if (m < 0 || m % 3 != 0) { return false; } return true; } int main() { long long t; cin >> t; for (long long i = 0; i < t; i++) { long long k, d1, d2; cin >> n >> k >> d1 >> d2; if (solve(k, d1, d2, n)) cout << "yes\n"; else if (solve(k, -1 * d1, d2, n)) cout << "yes\n"; else if (solve(k, -1 * d1, -1 * d2, n)) cout << "yes\n"; else if (solve(k, d1, -1 * d2, n)) cout << "yes\n"; else { cout << "no\n"; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long d1, d2, tests, n, k, d, ans; long long tob; long long v1, v2, v3, has, need; long long up, dwn; void check() { long long temp = k - has; if (temp < 0 || temp % 3 > 0) return; long long rem = n - k; rem -= need; if (rem < 0) return; if (rem % 3) return; ans = 1; } int main() { ios_base::sync_with_stdio(0); cin >> tests; for (; tests; --tests) { cin >> n >> k >> d1 >> d2; ans = 0; for (int mask = 0; mask < 4; mask++) { v1 = 0; v2 = v1; if (mask & 1) v2 -= d1; else v2 += d1; v3 = v2; if (mask & 2) v3 -= d2; else v3 += d2; up = max(max(v1, v2), v3); dwn = min(min(v1, v2), v3); has = v1 + v2 + v3 - dwn * 3; need = up * 3 - v1 - v2 - v3; check(); } if (ans) cout << "yes" << endl; else cout << "no" << endl; } cin.get(); cin.get(); return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long d1, d2, tests, n, k, d, ans; long long tob; long long v1, v2, v3, has, need; long long up, dwn; void check() { long long temp = k - has; if (temp < 0 || temp % 3 > 0) return; long long rem = n - k; rem -= need; if (rem < 0) return; if (rem % 3) return; ans = 1; } int main() { ios_base::sync_with_stdio(0); cin >> tests; for (; tests; --tests) { cin >> n >> k >> d1 >> d2; ans = 0; for (int mask = 0; mask < 4; mask++) { v1 = 0; v2 = v1; if (mask & 1) v2 -= d1; else v2 += d1; v3 = v2; if (mask & 2) v3 -= d2; else v3 += d2; up = max(max(v1, v2), v3); dwn = min(min(v1, v2), v3); has = v1 + v2 + v3 - dwn * 3; need = up * 3 - v1 - v2 - v3; check(); } if (ans) cout << "yes" << endl; else cout << "no" << endl; } cin.get(); cin.get(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool check(long long a, long long b, long long c) { if (a > b) swap(a, b); if (b > c) swap(b, c); if (a > b) swap(a, b); if (a < 0) { b -= a, c -= a; a = 0; } if (a + b + c > k) return 0; if ((k - (a + b + c)) % 3) return 0; long long t = n - k - (2 * c - b - a); if (n - k - (2 * c - b - a) < 0) return 0; if (t % 3 == 0) return 1; return 0; } int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); bool flag = 0; if (check(0, d1, d1 + d2) || check(0, d1, d1 - d2) || check(0, -d1, -d1 + d2) || check(0, -d1, -d1 - d2)) flag = 1; if (flag) puts("yes"); else puts("no"); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool check(long long a, long long b, long long c) { if (a > b) swap(a, b); if (b > c) swap(b, c); if (a > b) swap(a, b); if (a < 0) { b -= a, c -= a; a = 0; } if (a + b + c > k) return 0; if ((k - (a + b + c)) % 3) return 0; long long t = n - k - (2 * c - b - a); if (n - k - (2 * c - b - a) < 0) return 0; if (t % 3 == 0) return 1; return 0; } int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); bool flag = 0; if (check(0, d1, d1 + d2) || check(0, d1, d1 - d2) || check(0, -d1, -d1 + d2) || check(0, -d1, -d1 - d2)) flag = 1; if (flag) puts("yes"); else puts("no"); } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; const int MAX = 2e5 + 5; const long long MAX2 = 11; const int MOD = 1000000000 + 7; const long long INF = 20000; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; const double pi = acos(-1); long long tc, a, b, c, d, x[3]; bool ans; inline bool cek() { sort(x, x + 3); if (x[0] < 0 || 2 * x[2] - x[0] - x[1] > a || (a - 2 * x[2] + x[0] + x[1]) % 3 || x[0] + x[1] + x[2] > b || (x[0] + x[1] + x[2]) % 3 != b % 3) return 0; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> tc; while (tc--) { cin >> a >> b >> c >> d; if (c > d) c ^= d ^= c ^= d; a -= b; ans = 0; x[0] = 0, x[1] = c, x[2] = d; ans |= cek(); x[0] = 0, x[1] = c, x[2] = c + d; ans |= cek(); x[0] = 0, x[1] = d, x[2] = c + d; ans |= cek(); x[0] = 0, x[1] = c - d, x[2] = c; ans |= cek(); x[0] = 0, x[1] = d - c, x[2] = d; ans |= cek(); if (ans) cout << "yes\n"; else cout << "no\n"; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; const int MAX = 2e5 + 5; const long long MAX2 = 11; const int MOD = 1000000000 + 7; const long long INF = 20000; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; const double pi = acos(-1); long long tc, a, b, c, d, x[3]; bool ans; inline bool cek() { sort(x, x + 3); if (x[0] < 0 || 2 * x[2] - x[0] - x[1] > a || (a - 2 * x[2] + x[0] + x[1]) % 3 || x[0] + x[1] + x[2] > b || (x[0] + x[1] + x[2]) % 3 != b % 3) return 0; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> tc; while (tc--) { cin >> a >> b >> c >> d; if (c > d) c ^= d ^= c ^= d; a -= b; ans = 0; x[0] = 0, x[1] = c, x[2] = d; ans |= cek(); x[0] = 0, x[1] = c, x[2] = c + d; ans |= cek(); x[0] = 0, x[1] = d, x[2] = c + d; ans |= cek(); x[0] = 0, x[1] = c - d, x[2] = c; ans |= cek(); x[0] = 0, x[1] = d - c, x[2] = d; ans |= cek(); if (ans) cout << "yes\n"; else cout << "no\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, k; bool yes(long long a, long long b) { long long x; x = k - 2 * a - b; if (x < 0 || x % 3) return false; x /= 3; if (x < 0 || x + a < 0 || x + a + b < 0) return false; if (n % 3) return false; if (x > n / 3 || x + a > n / 3 || x + a + b > n / 3) return false; return true; } int main() { int cn; cin >> cn; while (cn--) { long long a, b; cin >> n >> k >> a >> b; if (yes(a, b) || yes(-a, b) || yes(a, -b) || yes(-a, -b)) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k; bool yes(long long a, long long b) { long long x; x = k - 2 * a - b; if (x < 0 || x % 3) return false; x /= 3; if (x < 0 || x + a < 0 || x + a + b < 0) return false; if (n % 3) return false; if (x > n / 3 || x + a > n / 3 || x + a + b > n / 3) return false; return true; } int main() { int cn; cin >> cn; while (cn--) { long long a, b; cin >> n >> k >> a >> b; if (yes(a, b) || yes(-a, b) || yes(a, -b) || yes(-a, -b)) cout << "yes" << endl; else cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2; int solve(long long a, long long b, long long c) { long long m = min(min(a, b), c); a -= m; b -= m; c -= m; if (a + b + c > k || (a + b + c - k) % 3) return 0; m = max(max(a, b), c); long long x = m - a + m - b + m - c; if (x <= n - k && (n - k - x) % 3 == 0) return 1; return 0; } int main() { ios::sync_with_stdio(false); cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; int ok = 0; ok |= solve(0, d1, d1 + d2); ok |= solve(-d1, 0, -d2); ok |= solve(0, -d1, -d1 - d2); ok |= solve(d1, 0, d2); cout << (ok ? "yes" : "no") << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2; int solve(long long a, long long b, long long c) { long long m = min(min(a, b), c); a -= m; b -= m; c -= m; if (a + b + c > k || (a + b + c - k) % 3) return 0; m = max(max(a, b), c); long long x = m - a + m - b + m - c; if (x <= n - k && (n - k - x) % 3 == 0) return 1; return 0; } int main() { ios::sync_with_stdio(false); cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; int ok = 0; ok |= solve(0, d1, d1 + d2); ok |= solve(-d1, 0, -d2); ok |= solve(0, -d1, -d1 - d2); ok |= solve(d1, 0, d2); cout << (ok ? "yes" : "no") << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline long long read() { long long 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; } long long n, m, d1, d2; long long a[5]; inline bool jud() { if ((m - a[1] - a[2]) % 3 || (m - a[1] - a[2]) < 0) return 0; long long t = (m - a[1] - a[2]) / 3; a[1] += t; a[2] += t; a[3] += t; t = n / 3; long long tot = n - m; for (int i = 1; i <= 3; i++) if (a[i] > t) return 0; else tot -= t - a[i]; if (tot != 0) return 0; return 1; } inline bool solve() { if (d1 < d2) swap(d1, d2); a[1] = d1; a[2] = d2; a[3] = 0; if (jud()) return 1; a[1] = d1; a[2] = d1 - d2; a[3] = 0; if (jud()) return 1; a[1] = d1 + d2; a[2] = d1; a[3] = 0; if (jud()) return 1; a[1] = d1 + d2; a[2] = d2; a[3] = 0; if (jud()) return 1; return 0; } int main() { int T = read(); while (T--) { n = read(); m = read(); d1 = read(); d2 = read(); if (n % 3 || !solve()) puts("no"); else puts("yes"); } return 0; }
### Prompt Please create a solution in CPP to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline long long read() { long long 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; } long long n, m, d1, d2; long long a[5]; inline bool jud() { if ((m - a[1] - a[2]) % 3 || (m - a[1] - a[2]) < 0) return 0; long long t = (m - a[1] - a[2]) / 3; a[1] += t; a[2] += t; a[3] += t; t = n / 3; long long tot = n - m; for (int i = 1; i <= 3; i++) if (a[i] > t) return 0; else tot -= t - a[i]; if (tot != 0) return 0; return 1; } inline bool solve() { if (d1 < d2) swap(d1, d2); a[1] = d1; a[2] = d2; a[3] = 0; if (jud()) return 1; a[1] = d1; a[2] = d1 - d2; a[3] = 0; if (jud()) return 1; a[1] = d1 + d2; a[2] = d1; a[3] = 0; if (jud()) return 1; a[1] = d1 + d2; a[2] = d2; a[3] = 0; if (jud()) return 1; return 0; } int main() { int T = read(); while (T--) { n = read(); m = read(); d1 = read(); d2 = read(); if (n % 3 || !solve()) puts("no"); else puts("yes"); } return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; bool judge(long long k, long long x) { if (k >= x && (k - x) % 3 == 0) return true; return false; } int main() { int T; scanf("%d", &T); while (T--) { long long n, k, d1, d2, a, b, c, flag = 0; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n - k >= (2 * d1 + d2) && (n - k - 2 * d1 - d2) % 3 == 0 && 3 * (d1 + d2) <= n && judge(k, 2 * d2 + d1)) flag = 1; if (d1 >= d2) { if (n - k >= (2 * d1 - d2) && (n - k - 2 * d1 + d2) % 3 == 0 && 3 * d1 <= n && judge(k, d1 + d2)) flag = 1; } else { if (n - k >= (2 * d2 - d1) && (n - k - 2 * d2 + d1) % 3 == 0 && 3 * d2 <= n && judge(k, d1 + d2)) flag = 1; } if (n - k >= (d1 + d2) && (n - k - d1 - d2) % 3 == 0 && d1 >= d2 && 3 * d1 <= n && judge(k, 2 * d1 - d2)) flag = 1; if (n - k >= (d1 + d2) && (n - k - d1 - d2) % 3 == 0 && d1 < d2 && 3 * d2 <= n && judge(k, 2 * d2 - d1)) flag = 1; if (n - k >= (2 * d2 + d1) && (n - k - 2 * d2 - d1) % 3 == 0 && 3 * (d1 + d2) <= n && judge(k, 2 * d1 + d2)) flag = 1; if (flag) puts("yes"); else puts("no"); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; bool judge(long long k, long long x) { if (k >= x && (k - x) % 3 == 0) return true; return false; } int main() { int T; scanf("%d", &T); while (T--) { long long n, k, d1, d2, a, b, c, flag = 0; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n - k >= (2 * d1 + d2) && (n - k - 2 * d1 - d2) % 3 == 0 && 3 * (d1 + d2) <= n && judge(k, 2 * d2 + d1)) flag = 1; if (d1 >= d2) { if (n - k >= (2 * d1 - d2) && (n - k - 2 * d1 + d2) % 3 == 0 && 3 * d1 <= n && judge(k, d1 + d2)) flag = 1; } else { if (n - k >= (2 * d2 - d1) && (n - k - 2 * d2 + d1) % 3 == 0 && 3 * d2 <= n && judge(k, d1 + d2)) flag = 1; } if (n - k >= (d1 + d2) && (n - k - d1 - d2) % 3 == 0 && d1 >= d2 && 3 * d1 <= n && judge(k, 2 * d1 - d2)) flag = 1; if (n - k >= (d1 + d2) && (n - k - d1 - d2) % 3 == 0 && d1 < d2 && 3 * d2 <= n && judge(k, 2 * d2 - d1)) flag = 1; if (n - k >= (2 * d2 + d1) && (n - k - 2 * d2 - d1) % 3 == 0 && 3 * (d1 + d2) <= n && judge(k, 2 * d1 + d2)) flag = 1; if (flag) puts("yes"); else puts("no"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const double eps(1e-8); int t; long long n, k, d1, d2, x1, x2, x3; bool check(long long x) { if (0 <= x && x <= n / 3) { return true; } return false; } int main() { cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no" << endl; continue; } if ((k + d2 - d1) % 3 == 0 && (k + d2 + 2 * d1) % 3 == 0 && (k - 2 * d2 - d1) % 3 == 0) { x1 = (k + d2 + 2 * d1) / 3; x2 = (k + d2 - d1) / 3; x3 = (k - 2 * d2 - d1) / 3; if (x1 >= x2 && x2 >= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } if ((k - d1 - d2) % 3 == 0 && (k + 2 * d1 - d2) % 3 == 0 && (k - d1 + 2 * d2) % 3 == 0) { x1 = (k + 2 * d1 - d2) / 3; x2 = (k - d1 - d2) / 3; x3 = (k - d1 + 2 * d2) / 3; if (x1 >= x2 && x2 <= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } if ((k + d2 + d1) % 3 == 0 && (k + d2 - 2 * d1) % 3 == 0 && (k - 2 * d2 + d1) % 3 == 0) { x1 = (k + d2 - 2 * d1) / 3; x2 = (k + d2 + d1) / 3; x3 = (k - 2 * d2 + d1) / 3; if (x1 <= x2 && x2 >= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } if ((k + d1 - d2) % 3 == 0 && (k - 2 * d1 - d2) % 3 == 0 && (k + d1 + 2 * d2) % 3 == 0) { x1 = (k - 2 * d1 - d2) / 3; x2 = (k + d1 - d2) / 3; x3 = (k + d1 + 2 * d2) / 3; if (x1 <= x2 && x2 <= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } cout << "no" << endl; } return 0; }
### Prompt Please create a solution in cpp to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double eps(1e-8); int t; long long n, k, d1, d2, x1, x2, x3; bool check(long long x) { if (0 <= x && x <= n / 3) { return true; } return false; } int main() { cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no" << endl; continue; } if ((k + d2 - d1) % 3 == 0 && (k + d2 + 2 * d1) % 3 == 0 && (k - 2 * d2 - d1) % 3 == 0) { x1 = (k + d2 + 2 * d1) / 3; x2 = (k + d2 - d1) / 3; x3 = (k - 2 * d2 - d1) / 3; if (x1 >= x2 && x2 >= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } if ((k - d1 - d2) % 3 == 0 && (k + 2 * d1 - d2) % 3 == 0 && (k - d1 + 2 * d2) % 3 == 0) { x1 = (k + 2 * d1 - d2) / 3; x2 = (k - d1 - d2) / 3; x3 = (k - d1 + 2 * d2) / 3; if (x1 >= x2 && x2 <= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } if ((k + d2 + d1) % 3 == 0 && (k + d2 - 2 * d1) % 3 == 0 && (k - 2 * d2 + d1) % 3 == 0) { x1 = (k + d2 - 2 * d1) / 3; x2 = (k + d2 + d1) / 3; x3 = (k - 2 * d2 + d1) / 3; if (x1 <= x2 && x2 >= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } if ((k + d1 - d2) % 3 == 0 && (k - 2 * d1 - d2) % 3 == 0 && (k + d1 + 2 * d2) % 3 == 0) { x1 = (k - 2 * d1 - d2) / 3; x2 = (k + d1 - d2) / 3; x3 = (k + d1 + 2 * d2) / 3; if (x1 <= x2 && x2 <= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void _fill_int(int* p, int val, int rep) { int i; for (i = 0; i < rep; i++) p[i] = val; } int T; signed long long N, K, D1, D2; void solve() { int f, i, j, k, l, x, y; cin >> T; for (i = 0; i < T; i++) { cin >> N >> K >> D1 >> D2; int ok = 0; for (j = 0; j < 4; j++) { x = (j % 2) ? 1 : -1; y = (j / 2) ? 1 : -1; signed long long t = K + x * D1 + y * D2; signed long long b = t / 3; signed long long a = b - x * D1; signed long long c = b - y * D2; if (t % 3 == 0 && a <= N / 3 && a >= 0 && b <= N / 3 && b >= 0 && c <= N / 3 && c >= 0) ok++; } if (N % 3 == 0 && ok) (void)printf("yes\n"); else (void)printf("no\n"); } } int main(int argc, char** argv) { string s; if (argc == 1) ios::sync_with_stdio(false); for (int i = 1; i < argc; i++) s += argv[i], s += '\n'; for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin); solve(); return 0; }
### Prompt Please create a solution in CPP to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void _fill_int(int* p, int val, int rep) { int i; for (i = 0; i < rep; i++) p[i] = val; } int T; signed long long N, K, D1, D2; void solve() { int f, i, j, k, l, x, y; cin >> T; for (i = 0; i < T; i++) { cin >> N >> K >> D1 >> D2; int ok = 0; for (j = 0; j < 4; j++) { x = (j % 2) ? 1 : -1; y = (j / 2) ? 1 : -1; signed long long t = K + x * D1 + y * D2; signed long long b = t / 3; signed long long a = b - x * D1; signed long long c = b - y * D2; if (t % 3 == 0 && a <= N / 3 && a >= 0 && b <= N / 3 && b >= 0 && c <= N / 3 && c >= 0) ok++; } if (N % 3 == 0 && ok) (void)printf("yes\n"); else (void)printf("no\n"); } } int main(int argc, char** argv) { string s; if (argc == 1) ios::sync_with_stdio(false); for (int i = 1; i < argc; i++) s += argv[i], s += '\n'; for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int T; long long n, k, d1, d2; for (scanf("%d", &T); T--;) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } bool flag = false; if ((k - (d1 + d2)) % 3 == 0) { long long base = (k - d1 - d2) / 3; if (k - d1 - d2 >= 0) if (base + max(d1, d2) <= n / 3) flag = true; } if ((k - (2 * d1 + d2)) % 3 == 0) { long long base = (k - 2 * d1 - d2) / 3; if (k - 2 * d1 - d2 >= 0) if (base + d1 + d2 <= n / 3) flag = true; } if ((k - (d1 + 2 * d2)) % 3 == 0) { long long base = (k - d1 - 2 * d2) / 3; if (k - d1 - 2 * d2 >= 0) if (base + d1 + d2 <= n / 3) flag = true; } if ((k - (2 * d1 - d2)) % 3 == 0 && d1 >= d2) { long long base = (k - 2 * d1 + d2) / 3; if (k - 2 * d1 + d2 >= 0) if (base + d1 <= n / 3) flag = true; } if ((k - (2 * d2 - d1)) % 3 == 0 && d1 <= d2) { long long base = (k + d1 - 2 * d2) / 3; if (k + d1 - 2 * d2 >= 0) if (base + d2 <= n / 3) flag = true; } printf(flag ? "yes\n" : "no\n"); } return 0; }
### Prompt Generate a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int T; long long n, k, d1, d2; for (scanf("%d", &T); T--;) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } bool flag = false; if ((k - (d1 + d2)) % 3 == 0) { long long base = (k - d1 - d2) / 3; if (k - d1 - d2 >= 0) if (base + max(d1, d2) <= n / 3) flag = true; } if ((k - (2 * d1 + d2)) % 3 == 0) { long long base = (k - 2 * d1 - d2) / 3; if (k - 2 * d1 - d2 >= 0) if (base + d1 + d2 <= n / 3) flag = true; } if ((k - (d1 + 2 * d2)) % 3 == 0) { long long base = (k - d1 - 2 * d2) / 3; if (k - d1 - 2 * d2 >= 0) if (base + d1 + d2 <= n / 3) flag = true; } if ((k - (2 * d1 - d2)) % 3 == 0 && d1 >= d2) { long long base = (k - 2 * d1 + d2) / 3; if (k - 2 * d1 + d2 >= 0) if (base + d1 <= n / 3) flag = true; } if ((k - (2 * d2 - d1)) % 3 == 0 && d1 <= d2) { long long base = (k + d1 - 2 * d2) / 3; if (k + d1 - 2 * d2 >= 0) if (base + d2 <= n / 3) flag = true; } printf(flag ? "yes\n" : "no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int i, j, _; long long n, k, d1, d2; long long win[3]; scanf("%d", &_); while (_--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } int flag = 0; for (i = 0; i < 4; i++) { if (i == 0) { win[0] = d1 + d2; win[1] = d2; win[2] = 0; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((d1 + d2 + (k - tmp) / 3) * 3 <= n) flag = 1; } if (i == 1) { win[0] = d1; win[1] = 0; win[2] = d2; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((max(d1, d2) + (k - tmp) / 3) * 3 <= n) flag = 1; } if (i == 2) { win[1] = max(d1, d2); win[0] = win[1] - d1; win[2] = win[1] - d2; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((max(d1, d2) + (k - tmp) / 3) * 3 <= n) flag = 1; } if (i == 3) { win[0] = 0; win[1] = d1; win[2] = d1 + d2; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((d1 + d2 + (k - tmp) / 3) * 3 <= n) flag = 1; } } if (flag) printf("yes\n"); else printf("no\n"); } return 0; }
### Prompt Please formulate a CPP solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int i, j, _; long long n, k, d1, d2; long long win[3]; scanf("%d", &_); while (_--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } int flag = 0; for (i = 0; i < 4; i++) { if (i == 0) { win[0] = d1 + d2; win[1] = d2; win[2] = 0; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((d1 + d2 + (k - tmp) / 3) * 3 <= n) flag = 1; } if (i == 1) { win[0] = d1; win[1] = 0; win[2] = d2; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((max(d1, d2) + (k - tmp) / 3) * 3 <= n) flag = 1; } if (i == 2) { win[1] = max(d1, d2); win[0] = win[1] - d1; win[2] = win[1] - d2; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((max(d1, d2) + (k - tmp) / 3) * 3 <= n) flag = 1; } if (i == 3) { win[0] = 0; win[1] = d1; win[2] = d1 + d2; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((d1 + d2 + (k - tmp) / 3) * 3 <= n) flag = 1; } } if (flag) printf("yes\n"); else printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, k; bool f(long long a, long long b) { long long p[3] = {}; long long r = k - b - a - b; if (r % 3 != 0) return false; p[2] = r / 3; p[1] = p[2] + b; p[0] = p[1] + a; if (p[1] < 0 || p[2] < 0 || p[0] < 0) return false; if (n % 3 != 0) return false; long long m = n / 3; return m >= p[0] && m >= p[1] && m >= p[2]; } int main() { long long t, a, b; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> k >> a >> b; if (f(a, b) || f(a, -b) || f(-a, b) || f(-a, -b)) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k; bool f(long long a, long long b) { long long p[3] = {}; long long r = k - b - a - b; if (r % 3 != 0) return false; p[2] = r / 3; p[1] = p[2] + b; p[0] = p[1] + a; if (p[1] < 0 || p[2] < 0 || p[0] < 0) return false; if (n % 3 != 0) return false; long long m = n / 3; return m >= p[0] && m >= p[1] && m >= p[2]; } int main() { long long t, a, b; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> k >> a >> b; if (f(a, b) || f(a, -b) || f(-a, b) || f(-a, -b)) cout << "yes" << endl; else cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int dx[] = {1, 1, -1, -1}; int dy[] = {1, -1, -1, 1}; long long int n, k, d1, d2, rem; bool _solve(long long int w1, long long int w2, long long int w3) { long long int tar = n / 3; if (w1 < 0 || w2 < 0 || w3 < 0) return false; if (tar >= w1 && tar >= w2 && tar >= w3 && abs(w1 - w2) == d1 && abs(w2 - w3) == d2) return true; return false; } bool solve() { long long int _d1, w1, w2, w3, _d2; for (int i = 0; i < 4; i++) { _d1 = d1 * dx[i]; _d2 = d2 * dy[i]; w1 = (k + 2 * _d1 + _d2) / 3; w2 = w1 - _d1; w3 = w2 - _d2; if (w1 + w2 + w3 != k) continue; if (_solve(w1, w2, w3)) return true; } return false; } int main() { ios_base ::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 == 0) { if (solve()) cout << "yes" << endl; else cout << "no" << endl; } else cout << "no" << endl; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dx[] = {1, 1, -1, -1}; int dy[] = {1, -1, -1, 1}; long long int n, k, d1, d2, rem; bool _solve(long long int w1, long long int w2, long long int w3) { long long int tar = n / 3; if (w1 < 0 || w2 < 0 || w3 < 0) return false; if (tar >= w1 && tar >= w2 && tar >= w3 && abs(w1 - w2) == d1 && abs(w2 - w3) == d2) return true; return false; } bool solve() { long long int _d1, w1, w2, w3, _d2; for (int i = 0; i < 4; i++) { _d1 = d1 * dx[i]; _d2 = d2 * dy[i]; w1 = (k + 2 * _d1 + _d2) / 3; w2 = w1 - _d1; w3 = w2 - _d2; if (w1 + w2 + w3 != k) continue; if (_solve(w1, w2, w3)) return true; } return false; } int main() { ios_base ::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 == 0) { if (solve()) cout << "yes" << endl; else cout << "no" << endl; } else cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long t, i, j, n, k, d1, d2; while (cin >> t) { while (t--) { scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2); if (n % 3) { puts("no"); continue; } int f = 0; if (d1 + 2 * d2 <= k) if ((k - (d1 + 2 * d2)) % 3 == 0) if ((n - k) >= (d2 + 2 * d1)) if ((n - k - (d2 + 2 * d1)) % 3 == 0) f = 1; if (d1 + d2 <= k) if ((k - (d1 + d2)) % 3 == 0) { if ((n - k) >= (max(d1, d2) + abs(d1 - d2))) { if ((n - k - (max(d1, d2) + abs(d1 - d2))) % 3 == 0) { f = 2; } } } long long tmp = max(d1, d2); long long x = (d1 > d2 ? d1 - d2 : d2 - d1); if (tmp + x <= k) { if ((k - tmp - x) % 3 == 0) { if (tmp - x + tmp <= n - k) { if ((n - k - (tmp - x + tmp)) % 3 == 0) f = 1; } } } if (2 * d1 + d2 <= k) if ((k - (2 * d1 + d2)) % 3 == 0) if ((n - k) >= (2 * d2 + d1)) if ((n - k - (2 * d2 + d1)) % 3 == 0) f = 4; if (f) puts("yes"); else puts("no"); } } }
### Prompt Construct a Cpp code solution to the problem outlined: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long t, i, j, n, k, d1, d2; while (cin >> t) { while (t--) { scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2); if (n % 3) { puts("no"); continue; } int f = 0; if (d1 + 2 * d2 <= k) if ((k - (d1 + 2 * d2)) % 3 == 0) if ((n - k) >= (d2 + 2 * d1)) if ((n - k - (d2 + 2 * d1)) % 3 == 0) f = 1; if (d1 + d2 <= k) if ((k - (d1 + d2)) % 3 == 0) { if ((n - k) >= (max(d1, d2) + abs(d1 - d2))) { if ((n - k - (max(d1, d2) + abs(d1 - d2))) % 3 == 0) { f = 2; } } } long long tmp = max(d1, d2); long long x = (d1 > d2 ? d1 - d2 : d2 - d1); if (tmp + x <= k) { if ((k - tmp - x) % 3 == 0) { if (tmp - x + tmp <= n - k) { if ((n - k - (tmp - x + tmp)) % 3 == 0) f = 1; } } } if (2 * d1 + d2 <= k) if ((k - (2 * d1 + d2)) % 3 == 0) if ((n - k) >= (2 * d2 + d1)) if ((n - k - (2 * d2 + d1)) % 3 == 0) f = 4; if (f) puts("yes"); else puts("no"); } } } ```
#include <bits/stdc++.h> using namespace std; bool solve(long long n, long long k, long long d1, long long d2) { n -= k; long long tmp = k - d1 - d1 - d2; if (tmp % 3 || tmp < 0) return false; long long a[3] = {tmp / 3, tmp / 3 + d1, tmp / 3 + d1 + d2}; sort(a, a + 3); if (a[0] < 0) return false; long long eq = a[2] - a[0] + a[2] - a[1]; if (eq <= n && ((n - eq) % 3) == 0) return true; else return false; } int main() { int t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (solve(n, k, d1, d2) || solve(n, k, -d1, d2) || solve(n, k, d1, -d2) || solve(n, k, -d1, -d2)) cout << "yes\n"; else cout << "no\n"; } return 0; }
### Prompt Generate a CPP solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool solve(long long n, long long k, long long d1, long long d2) { n -= k; long long tmp = k - d1 - d1 - d2; if (tmp % 3 || tmp < 0) return false; long long a[3] = {tmp / 3, tmp / 3 + d1, tmp / 3 + d1 + d2}; sort(a, a + 3); if (a[0] < 0) return false; long long eq = a[2] - a[0] + a[2] - a[1]; if (eq <= n && ((n - eq) % 3) == 0) return true; else return false; } int main() { int t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (solve(n, k, d1, d2) || solve(n, k, -d1, d2) || solve(n, k, d1, -d2) || solve(n, k, -d1, -d2)) cout << "yes\n"; else cout << "no\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<long long int> bin_s(long long int k, long long int d1, long long int d2, long long int a, long long int b) { long long int lo = 0, hi = k + 1; while (lo < hi) { long long int mid = (lo + hi) / 2; long long int x = mid + (a * d1); long long int y = mid; long long int z = mid + (b * d2); if (x >= 0 && y >= 0 && z >= 0 && (x + y + z) == k) return vector<long long int>({x, y, z}); else if (x < 0 || y < 0 || z < 0 || (x + y + z) < k) lo = mid + 1; else hi = mid; } return vector<long long int>({-1000}); } bool check(vector<long long int> A, long long int n) { sort(A.begin(), A.end()); n -= (A[2] - A[0]); n -= (A[2] - A[1]); return n >= 0 && (n % 3 == 0); } int main() { long long int t; cin >> t; while (t--) { long long int n, k, d1, d2; cin >> n >> k >> d1 >> d2; vector<long long int> A = bin_s(k, d1, d2, 1, 1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } A = bin_s(k, d1, d2, -1, 1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } A = bin_s(k, d1, d2, 1, -1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } A = bin_s(k, d1, d2, -1, -1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } cout << "no" << endl; } }
### Prompt In Cpp, your task is to solve the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<long long int> bin_s(long long int k, long long int d1, long long int d2, long long int a, long long int b) { long long int lo = 0, hi = k + 1; while (lo < hi) { long long int mid = (lo + hi) / 2; long long int x = mid + (a * d1); long long int y = mid; long long int z = mid + (b * d2); if (x >= 0 && y >= 0 && z >= 0 && (x + y + z) == k) return vector<long long int>({x, y, z}); else if (x < 0 || y < 0 || z < 0 || (x + y + z) < k) lo = mid + 1; else hi = mid; } return vector<long long int>({-1000}); } bool check(vector<long long int> A, long long int n) { sort(A.begin(), A.end()); n -= (A[2] - A[0]); n -= (A[2] - A[1]); return n >= 0 && (n % 3 == 0); } int main() { long long int t; cin >> t; while (t--) { long long int n, k, d1, d2; cin >> n >> k >> d1 >> d2; vector<long long int> A = bin_s(k, d1, d2, 1, 1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } A = bin_s(k, d1, d2, -1, 1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } A = bin_s(k, d1, d2, 1, -1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } A = bin_s(k, d1, d2, -1, -1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } cout << "no" << endl; } } ```
#include <bits/stdc++.h> using namespace std; bool isPossible(long long n, long long k, long long d1, long long d2) { int i, j; if (n % 3 != 0) return false; for (i = -1; i <= 1; i += 2) for (j = -1; j <= 1; j += 2) { long long D1 = d1 * i; long long D2 = d2 * j; if ((k - 2 * D2 - D1) % 3 != 0) continue; long long x[3]; x[2] = (k - 2 * D2 - D1) / 3; x[1] = x[2] + D2; x[0] = x[1] + D1; if ((x[2] >= 0 && x[2] <= k) && (x[1] >= 0 && x[1] <= k) && (x[0] >= 0 && x[0] <= k)) if (x[2] <= n / 3 && x[1] <= n / 3 && x[0] <= n / 3) return true; } return false; } int main(void) { int T; scanf("%i", &T); while (T--) { long long n, k, d1, d2; scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2); if (isPossible(n, k, d1, d2)) printf("yes\n"); else printf("no\n"); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool isPossible(long long n, long long k, long long d1, long long d2) { int i, j; if (n % 3 != 0) return false; for (i = -1; i <= 1; i += 2) for (j = -1; j <= 1; j += 2) { long long D1 = d1 * i; long long D2 = d2 * j; if ((k - 2 * D2 - D1) % 3 != 0) continue; long long x[3]; x[2] = (k - 2 * D2 - D1) / 3; x[1] = x[2] + D2; x[0] = x[1] + D1; if ((x[2] >= 0 && x[2] <= k) && (x[1] >= 0 && x[1] <= k) && (x[0] >= 0 && x[0] <= k)) if (x[2] <= n / 3 && x[1] <= n / 3 && x[0] <= n / 3) return true; } return false; } int main(void) { int T; scanf("%i", &T); while (T--) { long long n, k, d1, d2; scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2); if (isPossible(n, k, d1, d2)) printf("yes\n"); else printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int T; scanf("%d", &T); for (int tt = 0; tt < (T); ++tt) { long long n, k, d1, d2; cin >> n, cin >> k, cin >> d1, cin >> d2; long long cur; bool ok = false; cur = 2 * d1 + d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a + d1; long long c = a + d1 + d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } cur = 2 * d1 - d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a + d1; long long c = a + d1 - d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } cur = -2 * d1 + d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a - d1; long long c = a - d1 + d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } cur = -2 * d1 - d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a - d1; long long c = a - d1 - d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } puts(ok ? "yes" : "no"); } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int T; scanf("%d", &T); for (int tt = 0; tt < (T); ++tt) { long long n, k, d1, d2; cin >> n, cin >> k, cin >> d1, cin >> d2; long long cur; bool ok = false; cur = 2 * d1 + d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a + d1; long long c = a + d1 + d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } cur = 2 * d1 - d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a + d1; long long c = a + d1 - d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } cur = -2 * d1 + d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a - d1; long long c = a - d1 + d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } cur = -2 * d1 - d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a - d1; long long c = a - d1 - d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } puts(ok ? "yes" : "no"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; int main() { int t; cin >> t; for (int i = 0, _n = (t); i < _n; i++) { long long n, k, d1, d2; cin >> n >> k; cin >> d1 >> d2; long long t1, t2, t3; t2 = k - d1 + d2; if (t2 >= 0 && t2 % 3 == 0) { t2 /= 3; t3 = t2 - d2; long long m = n - k - (d1 + d2 + d1); if (t3 >= 0 && m >= 0 && m % 3 == 0) { puts("yes"); continue; } } t2 = k - d1 - d2; if (t2 >= 0 && t2 % 3 == 0) { long long m = n - k - (max(d1, d2) + abs(d1 - d2)); if (m >= 0 && m % 3 == 0) { puts("yes"); continue; } } t2 = k + d1 + d2; if (t2 >= 0 && t2 % 3 == 0) { t2 /= 3; long long m = n - k - (d1 + d2); t1 = t2 - d1; t3 = t2 - d2; if (t1 >= 0 && t3 >= 0 && m >= 0 && m % 3 == 0) { puts("yes"); continue; } } t2 = k + d1 - d2; if (t2 >= 0 && t2 % 3 == 0) { t2 /= 3; long long m = n - k - (d1 + d2 + d2); t1 = t2 - d1; if (t1 >= 0 && m >= 0 && m % 3 == 0) { puts("yes"); continue; } } puts("no"); } return 0; }
### Prompt In Cpp, your task is to solve the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; int main() { int t; cin >> t; for (int i = 0, _n = (t); i < _n; i++) { long long n, k, d1, d2; cin >> n >> k; cin >> d1 >> d2; long long t1, t2, t3; t2 = k - d1 + d2; if (t2 >= 0 && t2 % 3 == 0) { t2 /= 3; t3 = t2 - d2; long long m = n - k - (d1 + d2 + d1); if (t3 >= 0 && m >= 0 && m % 3 == 0) { puts("yes"); continue; } } t2 = k - d1 - d2; if (t2 >= 0 && t2 % 3 == 0) { long long m = n - k - (max(d1, d2) + abs(d1 - d2)); if (m >= 0 && m % 3 == 0) { puts("yes"); continue; } } t2 = k + d1 + d2; if (t2 >= 0 && t2 % 3 == 0) { t2 /= 3; long long m = n - k - (d1 + d2); t1 = t2 - d1; t3 = t2 - d2; if (t1 >= 0 && t3 >= 0 && m >= 0 && m % 3 == 0) { puts("yes"); continue; } } t2 = k + d1 - d2; if (t2 >= 0 && t2 % 3 == 0) { t2 /= 3; long long m = n - k - (d1 + d2 + d2); t1 = t2 - d1; if (t1 >= 0 && m >= 0 && m % 3 == 0) { puts("yes"); continue; } } puts("no"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int ans; long long n, k, d1, d2, a, b, c; void gettin(long long x, long long y) { if (n % 3) return; long long aa, bb, cc; if ((k - x - y) < 0) return; if ((k - x - y) % 3) return; aa = (k - x - y) / 3; bb = aa + x; cc = aa + y; if (aa > n / 3) return; if (aa < 0) return; if (bb > n / 3) return; if (cc > n / 3) return; if (bb < 0) return; if (cc < 0) return; ans = 1; } int main() { int t; scanf("%d", &t); while (t--) { ans = 0; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); gettin(d1, d1 + d2); gettin(d1, d1 - d2); gettin(-d1, -d1 + d2); gettin(-d1, -d1 - d2); if (ans) puts("yes"); else puts("no"); } }
### Prompt Please create a solution in cpp to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int ans; long long n, k, d1, d2, a, b, c; void gettin(long long x, long long y) { if (n % 3) return; long long aa, bb, cc; if ((k - x - y) < 0) return; if ((k - x - y) % 3) return; aa = (k - x - y) / 3; bb = aa + x; cc = aa + y; if (aa > n / 3) return; if (aa < 0) return; if (bb > n / 3) return; if (cc > n / 3) return; if (bb < 0) return; if (cc < 0) return; ans = 1; } int main() { int t; scanf("%d", &t); while (t--) { ans = 0; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); gettin(d1, d1 + d2); gettin(d1, d1 - d2); gettin(-d1, -d1 + d2); gettin(-d1, -d1 - d2); if (ans) puts("yes"); else puts("no"); } } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; void solve() { scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); if (d1 < d2) swap(d1, d2); long long mi[] = {d1 + d2, d1 + 2 * d2, d1 * 2 - d2, d2 + 2 * d1}; long long ned[] = {d1 * 2 - d2, d1 * 2 + d2, d1 + d2, d2 * 2 + d1}; long long has = n - k; for (int i = (0); i < (4); ++i) if (k >= mi[i] && (k - mi[i]) % 3 == 0 && has >= ned[i] && (has - ned[i]) % 3 == 0) { puts("yes"); return; } puts("no"); } int main() { int t; scanf("%d", &t); for (int i = (0); i < (t); ++i) solve(); return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; void solve() { scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); if (d1 < d2) swap(d1, d2); long long mi[] = {d1 + d2, d1 + 2 * d2, d1 * 2 - d2, d2 + 2 * d1}; long long ned[] = {d1 * 2 - d2, d1 * 2 + d2, d1 + d2, d2 * 2 + d1}; long long has = n - k; for (int i = (0); i < (4); ++i) if (k >= mi[i] && (k - mi[i]) % 3 == 0 && has >= ned[i] && (has - ned[i]) % 3 == 0) { puts("yes"); return; } puts("no"); } int main() { int t; scanf("%d", &t); for (int i = (0); i < (t); ++i) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long total, n, k, d1, d2; int main() { cin >> total; while (total--) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } long long tar = n / 3; if ((d2 + k - d1) % 3 == 0) { long long x1 = (d2 + k - d1) / 3; long long x2 = d1 + x1; long long x3 = x1 - d2; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } if ((k - d1 - d2) % 3 == 0) { long long x2 = (k - d1 - d2) / 3; long long x1 = d1 + x2; long long x3 = d2 + x2; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } if ((k + d1 - d2) % 3 == 0) { long long x1 = (d1 + k - d2) / 3; long long x2 = d2 + x1; long long x3 = x1 - d1; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } if ((k + d1 + d2) % 3 == 0) { long long x2 = (k + d1 + d2) / 3; long long x1 = x2 - d1; long long x3 = x2 - d2; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } cout << "no\n"; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long total, n, k, d1, d2; int main() { cin >> total; while (total--) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } long long tar = n / 3; if ((d2 + k - d1) % 3 == 0) { long long x1 = (d2 + k - d1) / 3; long long x2 = d1 + x1; long long x3 = x1 - d2; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } if ((k - d1 - d2) % 3 == 0) { long long x2 = (k - d1 - d2) / 3; long long x1 = d1 + x2; long long x3 = d2 + x2; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } if ((k + d1 - d2) % 3 == 0) { long long x1 = (d1 + k - d2) / 3; long long x2 = d2 + x1; long long x3 = x1 - d1; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } if ((k + d1 + d2) % 3 == 0) { long long x2 = (k + d1 + d2) / 3; long long x1 = x2 - d1; long long x3 = x2 - d2; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } cout << "no\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2; int main() { cin >> t; for (__typeof(t) i = 0; i < (t); i++) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } bool flag = false; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 || j == 0) continue; long long td1 = d1 * i, td2 = d2 * j; long long num = k - td1 + td2; if (num % 3 != 0) continue; long long n2 = num / 3; if (0 <= n2 && n2 <= k) { long long n1 = td1 + n2, n3 = n2 - td2; if (n1 >= 0 && n1 <= k && n3 >= 0 && n3 <= k && n1 <= n / 3 && n2 <= n / 3 && n3 <= n / 3) { flag = 1; break; } } } if (flag) break; } if (flag) { cout << "yes\n"; } else cout << "no\n"; } }
### Prompt Your task is to create a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2; int main() { cin >> t; for (__typeof(t) i = 0; i < (t); i++) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } bool flag = false; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 || j == 0) continue; long long td1 = d1 * i, td2 = d2 * j; long long num = k - td1 + td2; if (num % 3 != 0) continue; long long n2 = num / 3; if (0 <= n2 && n2 <= k) { long long n1 = td1 + n2, n3 = n2 - td2; if (n1 >= 0 && n1 <= k && n3 >= 0 && n3 <= k && n1 <= n / 3 && n2 <= n / 3 && n3 <= n / 3) { flag = 1; break; } } } if (flag) break; } if (flag) { cout << "yes\n"; } else cout << "no\n"; } } ```
#include <bits/stdc++.h> using namespace std; const long long N = 10; bool valid(long long x, long long y, long long z) { if (x < 0) return false; if (y < 0) return false; if (z < 0) return false; return true; } int main() { long long t; cin >> t; while (t--) { long long n, k, d1, d2, diff, sum, mx; long long i, w1, w2, w3; bool flag = false; cin >> n >> k >> d1 >> d2; diff = n - k; if (n % 3) { cout << "no\n"; continue; } for (i = 1; i <= 4; i++) { if (i == 1) { w2 = (k - d1 - d2) / 3; w1 = w2 + d1; w3 = w2 + d2; } if (i == 2) { w2 = (k + d1 - d2) / 3; w1 = w2 - d1; w3 = w2 + d2; } if (i == 3) { w2 = (k - d1 + d2) / 3; w1 = w2 + d1; w3 = w2 - d2; } if (i == 4) { w2 = (k + d1 + d2) / 3; w1 = w2 - d1; w3 = w2 - d2; } mx = max({w1, w2, w3}); if (valid(w1, w2, w3)) { sum = mx * 3 - w1 - w2 - w3; flag |= (sum <= diff && !((diff - sum) % 3)); } } cout << (flag ? ("yes\n") : ("no\n")); } return 0; }
### Prompt Generate a cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 10; bool valid(long long x, long long y, long long z) { if (x < 0) return false; if (y < 0) return false; if (z < 0) return false; return true; } int main() { long long t; cin >> t; while (t--) { long long n, k, d1, d2, diff, sum, mx; long long i, w1, w2, w3; bool flag = false; cin >> n >> k >> d1 >> d2; diff = n - k; if (n % 3) { cout << "no\n"; continue; } for (i = 1; i <= 4; i++) { if (i == 1) { w2 = (k - d1 - d2) / 3; w1 = w2 + d1; w3 = w2 + d2; } if (i == 2) { w2 = (k + d1 - d2) / 3; w1 = w2 - d1; w3 = w2 + d2; } if (i == 3) { w2 = (k - d1 + d2) / 3; w1 = w2 + d1; w3 = w2 - d2; } if (i == 4) { w2 = (k + d1 + d2) / 3; w1 = w2 - d1; w3 = w2 - d2; } mx = max({w1, w2, w3}); if (valid(w1, w2, w3)) { sum = mx * 3 - w1 - w2 - w3; flag |= (sum <= diff && !((diff - sum) % 3)); } } cout << (flag ? ("yes\n") : ("no\n")); } return 0; } ```
#include <bits/stdc++.h> using namespace std; class C_ {}; template <typename T> C_& operator<<(C_& __m, const T& __s) { if (!1) cerr << "\E[91m" << __s << "\E[0m"; return __m; } C_ merr; struct __s { __s() { if (1) { ios_base::Init i; cin.sync_with_stdio(0); cin.tie(0); } } ~__s() { merr << "Execution time: " << fixed << setprecision(3) << (double)clock() / CLOCKS_PER_SEC << " s.\n"; } } __S; int main(void) { int t; cin >> t; for (int tt = 0; tt < t; tt++) { long long n, k, d1, d2; long long left; long long d3; long long a, b, c; cin >> n >> k >> d1 >> d2; if (n % 3) { } d3 = (d1 - d2 >= 0 ? d1 - d2 : d2 - d1); left = n - k; a = max(d1, d2); b = a - d1, c = a - d2; if (k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d1 + d2 <= left && (left - (d1 + d2)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = d1 + d2; b = 0, c = d2; if (k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d1 + d1 + d2 <= left && (left - (d1 + d1 + d2)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = max(d1, d3); b = a - d1, c = a - d3; if (abs(b - c) == d2 && (b <= min(a, c) || b >= max(a, c)) && k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d1 + d3 <= left && (left - (d1 + d3)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = d1 + d2; b = 0, c = d1; if (k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d2 + d1 + d2 <= left && (left - (d2 + d1 + d2)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = max(d2, d3); b = a - d2, c = a - d3; if (abs(b - c) == d1 && (b <= min(a, c) || b >= max(a, c)) && k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d2 + d3 <= left && (left - (d2 + d3)) % 3 == 0) { cout << "yes" << '\n'; continue; } cout << "no" << '\n'; } return 0; }
### Prompt Please create a solution in cpp to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; class C_ {}; template <typename T> C_& operator<<(C_& __m, const T& __s) { if (!1) cerr << "\E[91m" << __s << "\E[0m"; return __m; } C_ merr; struct __s { __s() { if (1) { ios_base::Init i; cin.sync_with_stdio(0); cin.tie(0); } } ~__s() { merr << "Execution time: " << fixed << setprecision(3) << (double)clock() / CLOCKS_PER_SEC << " s.\n"; } } __S; int main(void) { int t; cin >> t; for (int tt = 0; tt < t; tt++) { long long n, k, d1, d2; long long left; long long d3; long long a, b, c; cin >> n >> k >> d1 >> d2; if (n % 3) { } d3 = (d1 - d2 >= 0 ? d1 - d2 : d2 - d1); left = n - k; a = max(d1, d2); b = a - d1, c = a - d2; if (k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d1 + d2 <= left && (left - (d1 + d2)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = d1 + d2; b = 0, c = d2; if (k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d1 + d1 + d2 <= left && (left - (d1 + d1 + d2)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = max(d1, d3); b = a - d1, c = a - d3; if (abs(b - c) == d2 && (b <= min(a, c) || b >= max(a, c)) && k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d1 + d3 <= left && (left - (d1 + d3)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = d1 + d2; b = 0, c = d1; if (k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d2 + d1 + d2 <= left && (left - (d2 + d1 + d2)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = max(d2, d3); b = a - d2, c = a - d3; if (abs(b - c) == d1 && (b <= min(a, c) || b >= max(a, c)) && k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d2 + d3 <= left && (left - (d2 + d3)) % 3 == 0) { cout << "yes" << '\n'; continue; } cout << "no" << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (d1 < d2) swap(d1, d2); if (k >= d1 + d1 + d2 && (k - (d1 + d1 + d2)) % 3 == 0 && n - k >= d2 + d1 + d2 && (n - k - (d2 + d1 + d2)) % 3 == 0) { cout << "yes" << endl; } else if (k >= d1 + d1 - d2 && (k - (d1 + d1 - d2)) % 3 == 0 && n - k >= d1 + d2 && (n - k - (d1 + d2)) % 3 == 0) { cout << "yes" << endl; } else if (k >= d1 + d2 + d2 && (k - (d1 + d2 + d2)) % 3 == 0 && n - k >= d2 + d1 + d1 && (n - k - (d2 + d1 + d1)) % 3 == 0) { cout << "yes" << endl; } else if (k >= d1 + d2 && (k - (d1 + d2)) % 3 == 0 && n - k >= d1 + d1 - d2 && (n - k - (d1 + d1 - d2)) % 3 == 0) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (d1 < d2) swap(d1, d2); if (k >= d1 + d1 + d2 && (k - (d1 + d1 + d2)) % 3 == 0 && n - k >= d2 + d1 + d2 && (n - k - (d2 + d1 + d2)) % 3 == 0) { cout << "yes" << endl; } else if (k >= d1 + d1 - d2 && (k - (d1 + d1 - d2)) % 3 == 0 && n - k >= d1 + d2 && (n - k - (d1 + d2)) % 3 == 0) { cout << "yes" << endl; } else if (k >= d1 + d2 + d2 && (k - (d1 + d2 + d2)) % 3 == 0 && n - k >= d2 + d1 + d1 && (n - k - (d2 + d1 + d1)) % 3 == 0) { cout << "yes" << endl; } else if (k >= d1 + d2 && (k - (d1 + d2)) % 3 == 0 && n - k >= d1 + d1 - d2 && (n - k - (d1 + d1 - d2)) % 3 == 0) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> inline T gcd(T a, T b) { return (b) == 0 ? (a) : gcd((b), ((a) % (b))); } template <class T> inline T lcm(T a, T b) { return ((a) / gcd((a), (b)) * (b)); } template <class T> inline T BigMod(T Base, T power, T M = 1000000007) { if (power == 0) return 1; if (power & 1) return ((Base % M) * (BigMod(Base, power - 1, M) % M)) % M; else { T y = BigMod(Base, power / 2, M) % M; return (y * y) % M; } } template <class T> inline T ModInv(T A, T M = 1000000007) { return BigMod(A, M - 2, M); } int fx[] = {-1, +0, +1, +0, +1, +1, -1, -1, +0}; int fy[] = {+0, -1, +0, +1, +1, -1, +1, -1, +0}; int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k, d1, d2; int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; long long x = max(d1, d2); if (x > n / 3 || n % 3 != 0) { cout << "no" << endl; continue; } x += abs(d1 - d2); if (n - x - k >= 0 && (n - x - k) % 3 == 0 && max(d1, d2) + (n - x - k) / 3 <= n / 3) { cout << "yes" << endl; continue; } x = d1 + d2 + d1; if (n - x - k >= 0 && (n - x - k) % 3 == 0 && (d1 + d2 + (n - x - k) / 3) <= n / 3) { cout << "yes" << endl; continue; } x = d1 + d2; if (n - x - k >= 0 && (n - x - k) % 3 == 0 && max(d1, d2) + (n - x - k) / 3 <= n / 3) { cout << "yes" << endl; continue; } x = d1 + d2 + d2; if (n - x - k >= 0 && (n - x - k) % 3 == 0 && (d1 + d2 + (n - x - k) / 3) <= n / 3) { cout << "yes" << endl; continue; } cout << "no" << endl; } return 0; }
### Prompt Generate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> inline T gcd(T a, T b) { return (b) == 0 ? (a) : gcd((b), ((a) % (b))); } template <class T> inline T lcm(T a, T b) { return ((a) / gcd((a), (b)) * (b)); } template <class T> inline T BigMod(T Base, T power, T M = 1000000007) { if (power == 0) return 1; if (power & 1) return ((Base % M) * (BigMod(Base, power - 1, M) % M)) % M; else { T y = BigMod(Base, power / 2, M) % M; return (y * y) % M; } } template <class T> inline T ModInv(T A, T M = 1000000007) { return BigMod(A, M - 2, M); } int fx[] = {-1, +0, +1, +0, +1, +1, -1, -1, +0}; int fy[] = {+0, -1, +0, +1, +1, -1, +1, -1, +0}; int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k, d1, d2; int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; long long x = max(d1, d2); if (x > n / 3 || n % 3 != 0) { cout << "no" << endl; continue; } x += abs(d1 - d2); if (n - x - k >= 0 && (n - x - k) % 3 == 0 && max(d1, d2) + (n - x - k) / 3 <= n / 3) { cout << "yes" << endl; continue; } x = d1 + d2 + d1; if (n - x - k >= 0 && (n - x - k) % 3 == 0 && (d1 + d2 + (n - x - k) / 3) <= n / 3) { cout << "yes" << endl; continue; } x = d1 + d2; if (n - x - k >= 0 && (n - x - k) % 3 == 0 && max(d1, d2) + (n - x - k) / 3 <= n / 3) { cout << "yes" << endl; continue; } x = d1 + d2 + d2; if (n - x - k >= 0 && (n - x - k) % 3 == 0 && (d1 + d2 + (n - x - k) / 3) <= n / 3) { cout << "yes" << endl; continue; } cout << "no" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = 2000000000; static inline int Rint() { struct X { int dig[256]; X() { for (int i = '0'; i <= '9'; ++i) dig[i] = 1; dig['-'] = 1; } }; static X fuck; int s = 1, v = 0, c; for (; !fuck.dig[c = getchar()];) ; if (c == '-') s = 0; else if (fuck.dig[c]) v = c ^ 48; for (; fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48)) ; return s ? v : -v; } template <typename T> static inline void cmax(T& a, const T& b) { if (b > a) a = b; } template <typename T> static inline void cmin(T& a, const T& b) { if (b < a) a = b; } const int maxn = 100005; long long check0(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = 2 * d1 + d2 + k; if (temp % 3 != 0) return 0; long long w1 = temp / 3; long long w2 = w1 - d1; long long w3 = w2 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } long long check1(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = d1 + d2 + k; if (temp % 3 != 0) return 0; long long w1 = temp / 3; long long w2 = w1 - d1; long long w3 = w1 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } long long check2(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = k - d1 - d2; if (temp % 3 != 0) return 0; long long w2 = temp / 3; long long w1 = w2 + d1; long long w3 = w2 + d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } long long check3(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = d1 + 2 * d2 + k; if (temp % 3 != 0) return 0; long long w1 = temp / 3; long long w2 = w1 - d1; long long w3 = w2 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } int main() { int q = Rint(); while (q--) { long long n, k, d1, d2; scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { puts("no"); continue; } if (check0(n, k, d1, d2) || check1(n, k, d1, d2) || check2(n, k, d1, d2) || check3(n, k, d1, d2)) puts("yes"); else puts("no"); } return 0; }
### Prompt In cpp, your task is to solve the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 2000000000; static inline int Rint() { struct X { int dig[256]; X() { for (int i = '0'; i <= '9'; ++i) dig[i] = 1; dig['-'] = 1; } }; static X fuck; int s = 1, v = 0, c; for (; !fuck.dig[c = getchar()];) ; if (c == '-') s = 0; else if (fuck.dig[c]) v = c ^ 48; for (; fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48)) ; return s ? v : -v; } template <typename T> static inline void cmax(T& a, const T& b) { if (b > a) a = b; } template <typename T> static inline void cmin(T& a, const T& b) { if (b < a) a = b; } const int maxn = 100005; long long check0(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = 2 * d1 + d2 + k; if (temp % 3 != 0) return 0; long long w1 = temp / 3; long long w2 = w1 - d1; long long w3 = w2 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } long long check1(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = d1 + d2 + k; if (temp % 3 != 0) return 0; long long w1 = temp / 3; long long w2 = w1 - d1; long long w3 = w1 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } long long check2(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = k - d1 - d2; if (temp % 3 != 0) return 0; long long w2 = temp / 3; long long w1 = w2 + d1; long long w3 = w2 + d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } long long check3(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = d1 + 2 * d2 + k; if (temp % 3 != 0) return 0; long long w1 = temp / 3; long long w2 = w1 - d1; long long w3 = w2 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } int main() { int q = Rint(); while (q--) { long long n, k, d1, d2; scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { puts("no"); continue; } if (check0(n, k, d1, d2) || check1(n, k, d1, d2) || check2(n, k, d1, d2) || check3(n, k, d1, d2)) puts("yes"); else puts("no"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long t, ti, d1, d2, n, k, p, ok = 0, maxim, v; int check(long long s1, long long s2, long long s3) { if (s1 + s2 + s3 > k) return 0; if ((k - s1 - s2 - s3) % 3 != 0) return 0; maxim = s1; if (s2 > maxim) maxim = s2; if (s3 > maxim) maxim = s3; v = 3 * maxim - s1 - s2 - s3; if (v > n - k) return 0; if ((n - k - v) % 3 != 0) return 0; ok = 1; return 1; } int main() { cin >> t; for (ti = 1; ti <= t; ti++) { cin >> n >> k >> d1 >> d2; ok = 0; check(d1, 0, d2); check(d1 + d2, d2, 0); check(0, d1, d1 + d2); p = d1; if (d2 > d1) p = d2; check(-d1 + p, p, -d2 + p); if (ok) cout << "yes\n"; else cout << "no\n"; } return 0; }
### Prompt Generate a CPP solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long t, ti, d1, d2, n, k, p, ok = 0, maxim, v; int check(long long s1, long long s2, long long s3) { if (s1 + s2 + s3 > k) return 0; if ((k - s1 - s2 - s3) % 3 != 0) return 0; maxim = s1; if (s2 > maxim) maxim = s2; if (s3 > maxim) maxim = s3; v = 3 * maxim - s1 - s2 - s3; if (v > n - k) return 0; if ((n - k - v) % 3 != 0) return 0; ok = 1; return 1; } int main() { cin >> t; for (ti = 1; ti <= t; ti++) { cin >> n >> k >> d1 >> d2; ok = 0; check(d1, 0, d2); check(d1 + d2, d2, 0); check(0, d1, d1 + d2); p = d1; if (d2 > d1) p = d2; check(-d1 + p, p, -d2 + p); if (ok) cout << "yes\n"; else cout << "no\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int t, i; long long n, k, d1, d2; bool solve(long long _d1, long long _d2) { long long y = k - _d1 - _d2; if (y % 3) return false; long long x = y; if (x < 0 || x > n) return false; x = 3 * _d1 + y; if (x < 0 || x > n) return false; x = 3 * _d2 + y; if (x < 0 || x > n) return false; return true; } int main() { cin >> t; for (i = 0; i < t; ++i) { cin >> n >> k >> d1 >> d2; if (n % 3 || (!solve(d1, d2) && !solve(d1, -d2) && !solve(-d1, d2) && !solve(-d1, -d2))) printf("no\n"); else printf("yes\n"); } return 0; }
### Prompt Please create a solution in CPP to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int t, i; long long n, k, d1, d2; bool solve(long long _d1, long long _d2) { long long y = k - _d1 - _d2; if (y % 3) return false; long long x = y; if (x < 0 || x > n) return false; x = 3 * _d1 + y; if (x < 0 || x > n) return false; x = 3 * _d2 + y; if (x < 0 || x > n) return false; return true; } int main() { cin >> t; for (i = 0; i < t; ++i) { cin >> n >> k >> d1 >> d2; if (n % 3 || (!solve(d1, d2) && !solve(d1, -d2) && !solve(-d1, d2) && !solve(-d1, -d2))) printf("no\n"); else printf("yes\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; int T; bool check(long long a, long long b, long long c) { if (a + b + c > k) return 0; if ((k - a - b - c) % 3) return 0; if (a < b) swap(a, b); if (a < c) swap(a, c); long long nd = a - b + a - c; if (nd > n - k) return 0; if ((n - k - nd) % 3) return 0; return 1; } int main() { scanf("%d", &T); while (T--) { scanf("%I64d", &n), scanf("%I64d", &k), scanf("%I64d", &d1), scanf("%I64d", &d2); bool ok = 0; if (check(d1 + d2, d2, 0)) ok = 1; if (check(d1, 0, d2)) ok = 1; if (d1 > d2 && check(0, d1, d1 - d2)) ok = 1; if (d2 >= d1 && check(d2 - d1, d2, 0)) ok = 1; if (check(0, d1, d1 + d2)) ok = 1; if (ok) printf("yes\n"); else printf("no\n"); } return 0; }
### Prompt Generate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; int T; bool check(long long a, long long b, long long c) { if (a + b + c > k) return 0; if ((k - a - b - c) % 3) return 0; if (a < b) swap(a, b); if (a < c) swap(a, c); long long nd = a - b + a - c; if (nd > n - k) return 0; if ((n - k - nd) % 3) return 0; return 1; } int main() { scanf("%d", &T); while (T--) { scanf("%I64d", &n), scanf("%I64d", &k), scanf("%I64d", &d1), scanf("%I64d", &d2); bool ok = 0; if (check(d1 + d2, d2, 0)) ok = 1; if (check(d1, 0, d2)) ok = 1; if (d1 > d2 && check(0, d1, d1 - d2)) ok = 1; if (d2 >= d1 && check(d2 - d1, d2, 0)) ok = 1; if (check(0, d1, d1 + d2)) ok = 1; if (ok) printf("yes\n"); else printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; class Codeforce { public: bool task(long long n, long long k, long long d1, long long d2) { if (n % 3 != 0) return false; long long t = n / 3; if (d1 > t || d2 > t) return false; if (((k - d1 + d2) % 3 == 0 && (k - d1 + d2) / 3 >= 0 && (k - d1 + d2) / 3 <= t) && ((k - d1 - 2 * d2) % 3 == 0 && (k - d1 - 2 * d2) / 3 >= 0 && (k - d1 - 2 * d2) / 3 <= t)) { if ((k + 2 * d1 + d2) % 3 == 0 && (k + 2 * d1 + d2) / 3 >= 0 && (k + 2 * d1 + d2) / 3 <= t) return true; } if (((k - d1 - d2) % 3 == 0 && (k - d1 - d2) / 3 >= 0 && (k - d1 - d2) / 3 <= t) && ((k - d1 + 2 * d2) % 3 == 0 && (k - d1 + 2 * d2) / 3 >= 0 && (k - d1 + 2 * d2) / 3 <= t)) { if ((k + 2 * d1 - d2) % 3 == 0 && (k + 2 * d1 - d2) / 3 >= 0 && (k + 2 * d1 - d2) / 3 <= t) return true; } if (((k + d1 + d2) % 3 == 0 && (k + d1 + d2) / 3 >= 0 && (k + d1 + d2) / 3 <= t) && ((k + d1 - 2 * d2) % 3 == 0 && (k + d1 - 2 * d2) / 3 >= 0 && (k + d1 - 2 * d2) / 3 <= t)) { if ((k - 2 * d1 + d2) % 3 == 0 && (k - 2 * d1 + d2) / 3 >= 0 && (k - 2 * d1 + d2) / 3 <= t) return true; } if (((k + d1 - d2) % 3 == 0 && (k + d1 - d2) / 3 >= 0 && (k + d1 - d2) / 3 <= t) && ((k + d1 + 2 * d2) % 3 == 0 && (k + d1 + 2 * d2) / 3 >= 0 && (k + d1 + 2 * d2) / 3 <= t)) { if ((k - 2 * d1 - d2) % 3 == 0 && (k - 2 * d1 - d2) / 3 >= 0 && (k - 2 * d1 - d2) / 3 <= t) return true; } return false; } }; int main() { Codeforce cf = Codeforce(); int t; long long n, k, d1, d2; scanf("%d", &t); while (t--) { cin >> n >> k >> d1 >> d2; if (cf.task(n, k, d1, d2)) printf("yes\n"); else printf("no\n"); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; class Codeforce { public: bool task(long long n, long long k, long long d1, long long d2) { if (n % 3 != 0) return false; long long t = n / 3; if (d1 > t || d2 > t) return false; if (((k - d1 + d2) % 3 == 0 && (k - d1 + d2) / 3 >= 0 && (k - d1 + d2) / 3 <= t) && ((k - d1 - 2 * d2) % 3 == 0 && (k - d1 - 2 * d2) / 3 >= 0 && (k - d1 - 2 * d2) / 3 <= t)) { if ((k + 2 * d1 + d2) % 3 == 0 && (k + 2 * d1 + d2) / 3 >= 0 && (k + 2 * d1 + d2) / 3 <= t) return true; } if (((k - d1 - d2) % 3 == 0 && (k - d1 - d2) / 3 >= 0 && (k - d1 - d2) / 3 <= t) && ((k - d1 + 2 * d2) % 3 == 0 && (k - d1 + 2 * d2) / 3 >= 0 && (k - d1 + 2 * d2) / 3 <= t)) { if ((k + 2 * d1 - d2) % 3 == 0 && (k + 2 * d1 - d2) / 3 >= 0 && (k + 2 * d1 - d2) / 3 <= t) return true; } if (((k + d1 + d2) % 3 == 0 && (k + d1 + d2) / 3 >= 0 && (k + d1 + d2) / 3 <= t) && ((k + d1 - 2 * d2) % 3 == 0 && (k + d1 - 2 * d2) / 3 >= 0 && (k + d1 - 2 * d2) / 3 <= t)) { if ((k - 2 * d1 + d2) % 3 == 0 && (k - 2 * d1 + d2) / 3 >= 0 && (k - 2 * d1 + d2) / 3 <= t) return true; } if (((k + d1 - d2) % 3 == 0 && (k + d1 - d2) / 3 >= 0 && (k + d1 - d2) / 3 <= t) && ((k + d1 + 2 * d2) % 3 == 0 && (k + d1 + 2 * d2) / 3 >= 0 && (k + d1 + 2 * d2) / 3 <= t)) { if ((k - 2 * d1 - d2) % 3 == 0 && (k - 2 * d1 - d2) / 3 >= 0 && (k - 2 * d1 - d2) / 3 <= t) return true; } return false; } }; int main() { Codeforce cf = Codeforce(); int t; long long n, k, d1, d2; scanf("%d", &t); while (t--) { cin >> n >> k >> d1 >> d2; if (cf.task(n, k, d1, d2)) printf("yes\n"); else printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; const int IT_MAX = 131072; const long long MOD = 1000000007; const int INF = 2034567891; const long long LL_INF = 1234567890123456789ll; int main() { int T; scanf("%d", &T); for (int tc = 1; tc <= T; tc++) { long long N, K, d1, d2, i, j; scanf("%I64d %I64d %I64d %I64d", &N, &K, &d1, &d2); if (N % 3 != 0) { printf("no\n"); continue; } for (i = -1; i <= 1; i += 2) { for (j = -1; j <= 1; j += 2) { long long t1 = d1 * i, t2 = d2 * j; long long x1, x2, x3; if ((K - t1 - 2 * t2) % 3 != 0 || (K - t1 - 2 * t2) < 0) continue; x3 = (K - t1 - 2 * t2) / 3; x2 = x3 + t2; x1 = x2 + t1; if (max(max(x1, x2), x3) <= N / 3 && min(min(x1, x2), x3) >= 0) break; } if (j <= 1) break; } if (i <= 1) printf("yes\n"); else printf("no\n"); } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; const int IT_MAX = 131072; const long long MOD = 1000000007; const int INF = 2034567891; const long long LL_INF = 1234567890123456789ll; int main() { int T; scanf("%d", &T); for (int tc = 1; tc <= T; tc++) { long long N, K, d1, d2, i, j; scanf("%I64d %I64d %I64d %I64d", &N, &K, &d1, &d2); if (N % 3 != 0) { printf("no\n"); continue; } for (i = -1; i <= 1; i += 2) { for (j = -1; j <= 1; j += 2) { long long t1 = d1 * i, t2 = d2 * j; long long x1, x2, x3; if ((K - t1 - 2 * t2) % 3 != 0 || (K - t1 - 2 * t2) < 0) continue; x3 = (K - t1 - 2 * t2) / 3; x2 = x3 + t2; x1 = x2 + t1; if (max(max(x1, x2), x3) <= N / 3 && min(min(x1, x2), x3) >= 0) break; } if (j <= 1) break; } if (i <= 1) printf("yes\n"); else printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; bool Check(long long n, long long k, long long d1, long long d2) { if ((k + d1 + d2) % 3) return false; long long w2 = (k + d1 + d2) / 3, w1 = w2 - d1, w3 = w2 - d2; return w1 >= 0 && w1 <= n && w2 >= 0 && w2 <= n && w3 >= 0 && w3 <= n; } int main() { int cas; cin >> cas; while (cas--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (n % 3) { puts("no"); continue; } n /= 3; bool ok = false; for (int i = -1; i <= 1; ++i) { if (i == 0) continue; for (int j = -1; j <= 1; ++j) { if (j == 0) continue; if (Check(n, k, d1 * i, d2 * j)) { ok = true; break; } } if (ok) break; } puts(ok ? "yes" : "no"); } return 0; }
### Prompt In Cpp, your task is to solve the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool Check(long long n, long long k, long long d1, long long d2) { if ((k + d1 + d2) % 3) return false; long long w2 = (k + d1 + d2) / 3, w1 = w2 - d1, w3 = w2 - d2; return w1 >= 0 && w1 <= n && w2 >= 0 && w2 <= n && w3 >= 0 && w3 <= n; } int main() { int cas; cin >> cas; while (cas--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (n % 3) { puts("no"); continue; } n /= 3; bool ok = false; for (int i = -1; i <= 1; ++i) { if (i == 0) continue; for (int j = -1; j <= 1; ++j) { if (j == 0) continue; if (Check(n, k, d1 * i, d2 * j)) { ok = true; break; } } if (ok) break; } puts(ok ? "yes" : "no"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int f[4][2] = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; int main() { int t; scanf("%d", &t); while (t--) { long long n, k, _d1, _d2; scanf("%I64d%I64d%I64d%I64d", &n, &k, &_d1, &_d2); if (n % 3 != 0) { printf("no\n"); continue; } bool flag = false; for (int i = 0; i < 4; i++) { long long d1 = f[i][0] * _d1; long long d2 = f[i][1] * _d2; long long a = (k + d1 * 2 + d2) / 3; long long b = a - d1; long long c = b - d2; if (a >= 0 && a <= k && b >= 0 && b <= k && c >= 0 && c <= k && a + b + c == k) if (a <= n / 3 && b <= n / 3 && c <= n / 3) { printf("yes\n"); flag = true; break; } } if (!flag) printf("no\n"); } return 0; }
### Prompt Please create a solution in Cpp to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int f[4][2] = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; int main() { int t; scanf("%d", &t); while (t--) { long long n, k, _d1, _d2; scanf("%I64d%I64d%I64d%I64d", &n, &k, &_d1, &_d2); if (n % 3 != 0) { printf("no\n"); continue; } bool flag = false; for (int i = 0; i < 4; i++) { long long d1 = f[i][0] * _d1; long long d2 = f[i][1] * _d2; long long a = (k + d1 * 2 + d2) / 3; long long b = a - d1; long long c = b - d2; if (a >= 0 && a <= k && b >= 0 && b <= k && c >= 0 && c <= k && a + b + c == k) if (a <= n / 3 && b <= n / 3 && c <= n / 3) { printf("yes\n"); flag = true; break; } } if (!flag) printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } long long N, K, D1, D2; bool check(long long a, long long b, long long c) { if (N % 3) return false; long long temp = a + b + c; temp = K - temp; if (temp % 3) return false; temp /= 3; long long cur_a = temp + a; long long cur_b = temp + b; long long cur_c = temp + c; ; ; ; if (cur_a < 0 || cur_a > N / 3) return false; if (cur_b < 0 || cur_b > N / 3) return false; if (cur_c < 0 || cur_c > N / 3) return false; return true; } void solve() { scanf("%I64d %I64d %I64d %I64d", &N, &K, &D1, &D2); if (check(0, D1, D1 + D2) || check(0, D1, D1 - D2) || check(0, -D1, -D1 + D2) || check(0, -D1, -D1 - D2)) { printf("yes\n"); } else { printf("no\n"); } } int main() { int test; scanf("%d", &test); for (int tt = 0; tt < test; tt++) { solve(); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } long long N, K, D1, D2; bool check(long long a, long long b, long long c) { if (N % 3) return false; long long temp = a + b + c; temp = K - temp; if (temp % 3) return false; temp /= 3; long long cur_a = temp + a; long long cur_b = temp + b; long long cur_c = temp + c; ; ; ; if (cur_a < 0 || cur_a > N / 3) return false; if (cur_b < 0 || cur_b > N / 3) return false; if (cur_c < 0 || cur_c > N / 3) return false; return true; } void solve() { scanf("%I64d %I64d %I64d %I64d", &N, &K, &D1, &D2); if (check(0, D1, D1 + D2) || check(0, D1, D1 - D2) || check(0, -D1, -D1 + D2) || check(0, -D1, -D1 - D2)) { printf("yes\n"); } else { printf("no\n"); } } int main() { int test; scanf("%d", &test); for (int tt = 0; tt < test; tt++) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int T; long long n, k, d1, d2, a, b, c, fuck; scanf("%d", &T); while (T--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } long long ave = n / 3; a = d1; b = 0; c = d2; fuck = (k - a - c) / 3; if (a + b + c <= k && (k - a - c) % 3 == 0 && a + fuck >= 0 && c + fuck >= 0 && fuck >= 0 && fuck <= ave && a <= ave - fuck && c <= ave - fuck) { printf("yes\n"); continue; } a = d1 + d2; b = d2; c = 0; fuck = (k - a - b) / 3; if (a + b + c <= k && (k - a - b) % 3 == 0 && a + fuck >= 0 && b + fuck >= 0 && fuck <= ave && fuck >= 0 && a <= ave - fuck && b <= ave - fuck) { printf("yes\n"); continue; } a = 0; b = d1; c = d1 + d2; fuck = (k - b - c) / 3; if (a + b + c <= k && (k - b - c) % 3 == 0 && fuck <= ave && fuck >= 0 && b + fuck >= 0 && c + fuck >= 0 && c <= ave - fuck && b <= ave - fuck) { printf("yes\n"); continue; } a = 0; b = d1; c = d1 - d2; fuck = (k - c - b) / 3; if (a + b + c <= k && (k - c - b) % 3 == 0 && fuck <= ave && fuck >= 0 && b + fuck <= ave && b + fuck >= 0 && c + fuck <= ave && c + fuck >= 0) { printf("yes\n"); continue; } c = 0; b = d2; a = d2 - d1; fuck = (k - b - a) / 3; if (a + b + c <= k && (k - b - a) % 3 == 0 && fuck <= ave && fuck >= 0 && b + fuck <= ave && b + fuck >= 0 && a + fuck <= ave && a + fuck >= 0) { printf("yes\n"); continue; } printf("no\n"); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int T; long long n, k, d1, d2, a, b, c, fuck; scanf("%d", &T); while (T--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } long long ave = n / 3; a = d1; b = 0; c = d2; fuck = (k - a - c) / 3; if (a + b + c <= k && (k - a - c) % 3 == 0 && a + fuck >= 0 && c + fuck >= 0 && fuck >= 0 && fuck <= ave && a <= ave - fuck && c <= ave - fuck) { printf("yes\n"); continue; } a = d1 + d2; b = d2; c = 0; fuck = (k - a - b) / 3; if (a + b + c <= k && (k - a - b) % 3 == 0 && a + fuck >= 0 && b + fuck >= 0 && fuck <= ave && fuck >= 0 && a <= ave - fuck && b <= ave - fuck) { printf("yes\n"); continue; } a = 0; b = d1; c = d1 + d2; fuck = (k - b - c) / 3; if (a + b + c <= k && (k - b - c) % 3 == 0 && fuck <= ave && fuck >= 0 && b + fuck >= 0 && c + fuck >= 0 && c <= ave - fuck && b <= ave - fuck) { printf("yes\n"); continue; } a = 0; b = d1; c = d1 - d2; fuck = (k - c - b) / 3; if (a + b + c <= k && (k - c - b) % 3 == 0 && fuck <= ave && fuck >= 0 && b + fuck <= ave && b + fuck >= 0 && c + fuck <= ave && c + fuck >= 0) { printf("yes\n"); continue; } c = 0; b = d2; a = d2 - d1; fuck = (k - b - a) / 3; if (a + b + c <= k && (k - b - a) % 3 == 0 && fuck <= ave && fuck >= 0 && b + fuck <= ave && b + fuck >= 0 && a + fuck <= ave && a + fuck >= 0) { printf("yes\n"); continue; } printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; long long p = n - k; if (p >= 2 * d1 + d2 && k >= d1 + 2 * d2) { if ((p - (2 * d1 + d2)) % 3 == 0 && (k - d1 - 2 * d2) % 3 == 0) { printf("yes\n"); continue; } } if (p >= d1 + d2 && k >= (max(d1, d2) + abs(d1 - d2))) { if ((p - d1 - d2) % 3 == 0 && (k - (max(d1, d2) + abs(d1 - d2))) % 3 == 0) { printf("yes\n"); continue; } } if (p >= (max(d1, d2) + abs(d1 - d2)) && k >= d1 + d2) { if ((p - (max(d1, d2) + abs(d1 - d2))) % 3 == 0 && (k - (d1 + d2)) % 3 == 0) { printf("yes\n"); continue; } } if (p >= (d1 + 2 * d2) && k >= 2 * d1 + d2) { if ((p - (d1 + 2 * d2)) % 3 == 0 && (k - 2 * d1 - d2) % 3 == 0) { printf("yes\n"); continue; } } printf("no\n"); } return 0; }
### Prompt In Cpp, your task is to solve the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; long long p = n - k; if (p >= 2 * d1 + d2 && k >= d1 + 2 * d2) { if ((p - (2 * d1 + d2)) % 3 == 0 && (k - d1 - 2 * d2) % 3 == 0) { printf("yes\n"); continue; } } if (p >= d1 + d2 && k >= (max(d1, d2) + abs(d1 - d2))) { if ((p - d1 - d2) % 3 == 0 && (k - (max(d1, d2) + abs(d1 - d2))) % 3 == 0) { printf("yes\n"); continue; } } if (p >= (max(d1, d2) + abs(d1 - d2)) && k >= d1 + d2) { if ((p - (max(d1, d2) + abs(d1 - d2))) % 3 == 0 && (k - (d1 + d2)) % 3 == 0) { printf("yes\n"); continue; } } if (p >= (d1 + 2 * d2) && k >= 2 * d1 + d2) { if ((p - (d1 + 2 * d2)) % 3 == 0 && (k - 2 * d1 - d2) % 3 == 0) { printf("yes\n"); continue; } } printf("no\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, K, d1, d2, x, y, z, tmp, r; void doit() { scanf("%I64d%I64d%I64d%I64d", &n, &K, &d1, &d2); if (n % 3) { printf("no\n"); return; } for (int key1 = -1; key1 <= 1; key1 += 2) for (int key2 = -1; key2 <= 1; key2 += 2) { tmp = K - key1 * d1 - key2 * d2; if (tmp % 3 || tmp < 0) continue; x = tmp / 3; y = x + key1 * d1; z = x + key2 * d2; if (y < 0 || z < 0) continue; if (x + y + z > K) continue; r = max(max(x, y), z); tmp = K; K += 3 * r - x - y - z; if (K <= n && (n - K) % 3 == 0) { printf("yes\n", x, y, z); return; } K = tmp; } printf("no\n"); } int main() { int T; scanf("%d", &T); while (T--) { doit(); } }
### Prompt Create a solution in CPP for the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, K, d1, d2, x, y, z, tmp, r; void doit() { scanf("%I64d%I64d%I64d%I64d", &n, &K, &d1, &d2); if (n % 3) { printf("no\n"); return; } for (int key1 = -1; key1 <= 1; key1 += 2) for (int key2 = -1; key2 <= 1; key2 += 2) { tmp = K - key1 * d1 - key2 * d2; if (tmp % 3 || tmp < 0) continue; x = tmp / 3; y = x + key1 * d1; z = x + key2 * d2; if (y < 0 || z < 0) continue; if (x + y + z > K) continue; r = max(max(x, y), z); tmp = K; K += 3 * r - x - y - z; if (K <= n && (n - K) % 3 == 0) { printf("yes\n", x, y, z); return; } K = tmp; } printf("no\n"); } int main() { int T; scanf("%d", &T); while (T--) { doit(); } } ```
#include <bits/stdc++.h> const int mod = 1000000007; using namespace std; long long x, y, z, t, n, k, d1, d2; ; bool check() { if (x >= 0 && y >= 0 && z >= 0 && x % 3 == 0 && y % 3 == 0 && z % 3 == 0) { x /= 3; y /= 3; z /= 3; long long mx = max(max(x, y), z); if ((n - (mx - x) - (mx - y) - (mx - z)) >= 0 && (n - (mx - x) - (mx - y) - (mx - z)) % 3 == 0) { cout << "yes" << endl; return 1; } } return 0; } int main() { cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; n -= k; x = 2 * d1 + d2 + k; y = k - d1 + d2; z = k - d1 - 2 * d2; if (check()) continue; x = d2 - 2 * d1 + k; y = k + d1 + d2; z = k + d1 - 2 * d2; if (check()) continue; x = 2 * d1 - d2 + k; y = k - d1 - d2; z = k + 2 * d2 - d1; if (check()) continue; x = k - 2 * d1 - d2; y = k - d2 + d1; z = k + 2 * d2 + d1; if (check()) continue; cout << "no" << endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). ### Response ```cpp #include <bits/stdc++.h> const int mod = 1000000007; using namespace std; long long x, y, z, t, n, k, d1, d2; ; bool check() { if (x >= 0 && y >= 0 && z >= 0 && x % 3 == 0 && y % 3 == 0 && z % 3 == 0) { x /= 3; y /= 3; z /= 3; long long mx = max(max(x, y), z); if ((n - (mx - x) - (mx - y) - (mx - z)) >= 0 && (n - (mx - x) - (mx - y) - (mx - z)) % 3 == 0) { cout << "yes" << endl; return 1; } } return 0; } int main() { cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; n -= k; x = 2 * d1 + d2 + k; y = k - d1 + d2; z = k - d1 - 2 * d2; if (check()) continue; x = d2 - 2 * d1 + k; y = k + d1 + d2; z = k + d1 - 2 * d2; if (check()) continue; x = 2 * d1 - d2 + k; y = k - d1 - d2; z = k + 2 * d2 - d1; if (check()) continue; x = k - 2 * d1 - d2; y = k - d2 + d1; z = k + 2 * d2 + d1; if (check()) continue; cout << "no" << endl; } return 0; } ```