Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
429_E. Points and Segments
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
2
11
#include <bits/stdc++.h> using namespace std; const long long mx = 2e5 + 10; set<pair<long long, long long> > ad[mx]; vector<long long> ver; long long ans[mx], l[mx], r[mx]; void dfst(long long v) { while ((long long)ad[v].size()) { long long u = ad[v].begin()->first, yal = ad[v].begin()->second; ad[v].erase(ad[v].begin()); ans[yal] = v < u; ad[u].erase({v, yal}); dfst(u); } return; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; for (long long i = 0; i < n; i++) { cin >> l[i] >> r[i]; r[i]++; ver.push_back(l[i]); ver.push_back(r[i]); } sort(ver.begin(), ver.end()); ver.resize(unique(ver.begin(), ver.end()) - ver.begin()); for (long long i = 0; i < n; i++) { l[i] = lower_bound(ver.begin(), ver.end(), l[i]) - ver.begin(); r[i] = lower_bound(ver.begin(), ver.end(), r[i]) - ver.begin(); ad[l[i]].insert({r[i], i}); ad[r[i]].insert({l[i], i}); } long long fard = -1; for (long long i = 0; i < (long long)ver.size(); i++) if ((long long)ad[i].size() % 2) { if (fard != -1) { ad[i].insert({fard, n}); ad[fard].insert({i, n}); fard = -1; } else fard = i; } for (long long i = 0; i < (long long)ver.size(); i++) dfst(i); for (long long i = 0; i < n; i++) cout << ans[i] << " "; cout << endl; return 0; }
CPP
429_E. Points and Segments
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
2
11
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; inline long long rd() { long long x = 0, w = 1; char ch = 0; while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return x * w; } int to[N << 2], nt[N << 2], hd[N << 1], dg[N << 1], tot = 1; void add(int x, int y) { ++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot, ++dg[x]; ++tot, to[tot] = x, nt[tot] = hd[y], hd[y] = tot, ++dg[y]; } bool cs[N << 2], ban[N << 2], v[N << 1]; int n, m, a[N][2], b[N << 1]; void dfs(int x) { v[x] = 1; for (int &i = hd[x]; i; i = nt[i]) { if (ban[i]) continue; int y = to[i]; ban[i] = ban[i ^ 1] = 1, cs[i] = 1; dfs(y); } } int main() { n = rd(); for (int i = 1; i <= n; ++i) a[i][0] = b[i * 2 - 1] = rd(), a[i][1] = b[i * 2] = rd() + 1; sort(b + 1, b + n + n + 1), m = unique(b + 1, b + n + n + 1) - b - 1; for (int i = 1; i <= n; ++i) { a[i][0] = lower_bound(b + 1, b + m + 1, a[i][0]) - b; a[i][1] = lower_bound(b + 1, b + m + 1, a[i][1]) - b; add(a[i][0], a[i][1]); } for (int i = 1, la = 0; i <= m; ++i) if (dg[i] & 1) { if (!la) la = i; else add(la, i), la = 0; } for (int i = 1; i <= m; ++i) if (!v[i]) dfs(i); for (int i = 1; i <= n; ++i) printf("%d ", cs[i << 1]); return 0; }
CPP
429_E. Points and Segments
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
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; inline int gi() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int sum = 0; while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar(); return sum; } int n, l[maxn], r[maxn], ans[maxn], *q[maxn], num, cnt; struct edge { int to, next, Id; } e[maxn * 2]; int h[maxn], vis[maxn], deg[maxn], tot = 1; inline void add(int u, int v, int Id) { e[++tot] = (edge){v, h[u], Id}; h[u] = tot; e[++tot] = (edge){u, h[v], Id}; h[v] = tot; } void dfs(int u) { for (int &i = h[u]; i; i = e[i].next) if (!vis[i >> 1]) vis[i >> 1] = 1, ans[e[i].Id] = i & 1, dfs(e[i].to); } int main() { n = gi(); for (int i = 1; i <= n; ++i) l[i] = gi(), r[i] = gi() + 1, q[++cnt] = &l[i], q[++cnt] = &r[i]; sort(q + 1, q + cnt + 1, [](int *a, int *b) { return *a < *b; }); num = 0; for (int k = -1, i = 1; i <= cnt; ++i) if (*q[i] == k) *q[i] = num; else k = *q[i], *q[i] = ++num; for (int i = 1; i <= n; ++i) deg[l[i]] ^= 1, deg[r[i]] ^= 1, add(l[i], r[i], i); for (int lst = 0, i = 1; i <= num; ++i) if (deg[i] & 1) { if (lst) add(lst, i, 0), lst = 0; else lst = i; } for (int i = 1; i <= num; ++i) if (h[i]) dfs(i); for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); return 0; }
CPP
429_E. Points and Segments
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
2
11
#include <bits/stdc++.h> using namespace std; const int _ = 1e5 + 5; inline int read() { char ch = '!'; int z = 1, num = 0; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') z = -1, ch = getchar(); while (ch <= '9' && ch >= '0') num = (num << 3) + (num << 1) + ch - '0', ch = getchar(); return z * num; } int n, l[_], r[_], o[_ << 1], m, ans[_]; struct ed { int to, next, id; } e[_ << 2]; int cnt = 1, head[_ << 1], d[_ << 1]; bool vis[_ << 2]; void link(int u, int v, int id) { d[u]++, d[v]++; e[++cnt] = (ed){v, head[u], id}; head[u] = cnt; e[++cnt] = (ed){u, head[v], id}; head[v] = cnt; } void dfs(int u) { for (int i = head[u]; i; i = e[i].next) if (!vis[i]) { ans[e[i].id] = (u < e[i].to); vis[i] = vis[i ^ 1] = 1; dfs(e[i].to); } } int main() { n = read(); for (int i = 1; i <= n; ++i) l[i] = read(), r[i] = read() + 1, o[++m] = l[i], o[++m] = r[i]; sort(o + 1, o + 1 + m); m = unique(o + 1, o + 1 + m) - o - 1; for (int i = 1; i <= n; ++i) l[i] = lower_bound(o + 1, o + 1 + m, l[i]) - o, r[i] = lower_bound(o + 1, o + 1 + m, r[i]) - o; for (int i = 1; i <= n; ++i) link(l[i], r[i], i); int lst = 0; for (int i = 1; i <= m; ++i) if (d[i] & 1) { if (lst) link(lst, i, 0), lst = 0; else lst = i; } if (lst) return puts("-1"), 0; for (int i = 1; i <= m; ++i) if (d[i]) dfs(i); for (int i = 1; i <= n; ++i) if (!ans[i]) printf("0 "); else printf("1 "); puts(""); return 0; }
CPP
429_E. Points and Segments
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
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int n; int l[maxn]; int r[maxn]; int clr[maxn]; bool mark[maxn]; vector<int> cmp; map<int, int> compress; stack<pair<int, int>> adj[maxn]; void in(); void solve(); void dfs(int u); void out(); int main() { ios::sync_with_stdio(false); cout.tie(0); cin.tie(0); in(); solve(); out(); } void in() { cin >> n; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; r[i]++; cmp.push_back(l[i]); cmp.push_back(r[i]); } sort(cmp.begin(), cmp.end()); cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin()); for (int i = 0; i < cmp.size(); i++) compress[cmp[i]] = i; for (int i = 0; i < n; i++) { l[i] = compress[l[i]]; r[i] = compress[r[i]]; adj[l[i]].push({i, r[i]}); adj[r[i]].push({i, l[i]}); } } void solve() { int last = -1; for (int i = 0; i < maxn; i++) { if (adj[i].size() % 2) { if (last == -1) last = i; else { adj[i].push({maxn - i, last}); adj[last].push({maxn - i, i}); last = -1; } } } for (int i = 0; i < maxn; i++) while (adj[i].size()) dfs(i); } void dfs(int u) { if (adj[u].size()) { auto x = adj[u].top(); adj[u].pop(); if (mark[x.first]) dfs(u); mark[x.first] = 1; clr[x.first] = x.second > u; dfs(x.second); } } void out() { for (int i = 0; i < n; i++) cout << clr[i] << ' '; }
CPP
429_E. Points and Segments
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
2
11
#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; }
CPP
429_E. Points and Segments
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
2
11
#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; }
CPP
429_E. Points and Segments
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
2
11
#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; }
CPP
429_E. Points and Segments
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
2
11
import java.io.*; import java.util.*; /** * Created by Katushka on 14.02.2021. */ public class Main { static void sortArray(long[] a, boolean rev) { Random random = new Random(); for (int i = 0; i < a.length; i++) { int randomPos = random.nextInt(a.length); long t = a[i]; a[i] = a[randomPos]; a[randomPos] = t; } Arrays.sort(a); if (rev) { for (int i = 0; i < a.length / 2; i++) { long t = a[i]; a[i] = a[a.length - i - 1]; a[a.length - i - 1] = t; } } } static int[] readArray(int size, InputReader in, boolean subOne) { int[] a = new int[size]; for (int i = 0; i < size; i++) { a[i] = in.nextInt() + (subOne ? -1 : 0); } return a; } static long[] readLongArray(int size, InputReader in) { long[] a = new long[size]; for (int i = 0; i < size; i++) { a[i] = in.nextLong(); } return a; } public static final Comparator<int[]> TRIPLE_COMPARATOR = (o1, o2) -> { if (o1[0] == o2[0]) { if (o1[1] == o2[1]) { return Long.compare(o1[2], o2[2]); } return Long.compare(o1[1], o2[1]); } return Long.compare(o1[0], o2[0]); }; private static class Edge { int v; int u; int segNum; int edgeNum; Edge(int v, int u, int segNum, int edgeNum) { this.u = u; this.v = v; this.segNum = segNum; this.edgeNum = edgeNum; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Edge edge = (Edge) o; return u == edge.u && v == edge.v && segNum == edge.segNum; } @Override public int hashCode() { return Objects.hash(u, v, segNum); } } public static void main(String[] args) throws FileNotFoundException { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = in.nextInt(); long[][] segments = new long[n][2]; long[] points = new long[n << 1]; long[] ls = new long[n]; long[] rs = new long[n]; for (int i = 0; i < n; i++) { ls[i] = in.nextInt(); rs[i] = in.nextInt(); segments[i][0] = ls[i]; segments[i][1] = rs[i]; points[i << 1] = (ls[i] << 1); points[(i << 1) + 1] = (rs[i] << 1); } sortArray(ls, false); sortArray(rs, false); sortArray(points, false); long[] vs = new long[points.length << 1]; Map<Long, Integer> occs = new HashMap<>(); int i = 0; for (int j = 0; j < points.length; j++) { long point = points[j]; if (j > 0 && point == points[j - 1]) { continue; } if (i > 0) { vs[i] = (point + vs[i - 1]) / 2; i++; } vs[i] = point; occs.put(point, i); i++; } vs[i] = vs[i - 1] + 1; List<Edge>[] edges = new List[vs.length]; for (i = 0; i < vs.length; i++) { edges[i] = new ArrayList<>(); } int edgeCnt = 0; for (i = 0; i < segments.length; i++) { int li = occs.get(segments[i][0] * 2); int ri = occs.get(segments[i][1] * 2); edges[li].add(new Edge(li, ri + 1, i, edgeCnt)); edges[ri + 1].add(new Edge(ri + 1, li, i, edgeCnt)); edgeCnt++; } int li = 0; int ri = 0; int cnt = 0; for (i = 0; i < vs.length; i++) { while (li < n && ls[li] * 2 == vs[i]) { cnt++; li++; } while (ri < n && i > 0 && rs[ri] * 2 == vs[i - 1]) { cnt--; ri++; } if (cnt % 2 != 0) { edges[i].add(new Edge(i, i + 1, -1, edgeCnt)); edges[i + 1].add(new Edge(i + 1, i, -1, edgeCnt)); edgeCnt++; } } boolean[] visited = new boolean[vs.length]; boolean[] visitedEdges = new boolean[edgeCnt]; int[] colors = new int[n]; for (i = 0; i < vs.length; i++) { if (!visited[i]) { ArrayList<Edge> path = new ArrayList<>(); dfs(i, edges, visited, visitedEdges, path); for (int j = 0; j < path.size(); j++) { if (path.get(j).segNum >= 0) { if (path.get(j).v < path.get(j).u) { colors[path.get(j).segNum] = 1; } } } } } outputArray(colors, out, false); out.close(); } private static void dfs(int v, List<Edge>[] edges, boolean[] visited, boolean[] visitedEdges, List<Edge> path) { visited[v] = true; for (int i = 0; i < edges[v].size(); i++) { Edge vu = edges[v].get(i); if (!visitedEdges[vu.edgeNum]) { visitedEdges[vu.edgeNum] = true; dfs(vu.u, edges, visited, visitedEdges, path); if (vu.v < vu.u) { path.add(vu); } } } } private static void outputArray(int[] ans, PrintWriter out, boolean addOne) { StringBuilder str = new StringBuilder(); for (int i = 0; i < ans.length; i++) { long an = ans[i] + (addOne ? 1 : 0); str.append(an).append(' '); } out.println(str); out.flush(); } private static void outputArray(List<Integer> ans, PrintWriter out, boolean addOne) { StringBuilder str = new StringBuilder(); for (int i : ans) { long an = i + (addOne ? 1 : 0); str.append(an).append(' '); } out.println(str); out.flush(); } private static int[] getBinary(long a, int size) { int[] result = new int[size]; for (int i = 0; i < size; i++) { result[i] = (int) ((a >> i) & 1); } return result; } private static int fromBinary(int[] binary) { int res = 0; int d = 1; for (int i = 0; i < binary.length; i++) { if (binary[i] == 1) { res = res | d; } d = d << 1; } return res; } private static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextString() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public char nextChar() { return next().charAt(0); } public String nextWord() { return next(); } private List<Integer>[] readTree(int n) { List<Integer>[] result = new ArrayList[n]; for (int i = 0; i < n; i++) { result[i] = new ArrayList<>(); } for (int i = 0; i < n - 1; i++) { int u = nextInt() - 1; int v = nextInt() - 1; result[u].add(v); result[v].add(u); } return result; } } }
JAVA
429_E. Points and Segments
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
2
11
#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; }
CPP
429_E. Points and Segments
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
2
11
#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; }
CPP
429_E. Points and Segments
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
2
11
#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; }
CPP
429_E. Points and Segments
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
2
11
#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; }
CPP
429_E. Points and Segments
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
2
11
#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 "); } }
CPP
429_E. Points and Segments
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
2
11
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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"; } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner s=new Scanner(System.in); int t=s.nextInt(); for(int i=0;i<t;i++) { long n=s.nextLong(); long k=s.nextLong(); long d1=s.nextLong(); long d2=s.nextLong(); boolean ans1=true; ans1 = ans1 && ((2*d1) + d2 + k)%3==0; ans1 = ans1 && (d2 + k - d1)%3==0 ; ans1 = ans1 && (k - d1 - (2*d2))>=0 && (k - d1 - (2*d2))%3==0; long w11 = ((2*d1) + d2 + k)/3; long w12 = (d2 + k - d1)/3; long w13 = (k - d1 - (2*d2))/3; ans1 = ans1 && w11<=(n/3); ans1 = ans1 && w12<=(n/3); ans1 = ans1 && w13<=(n/3); boolean ans2=true; ans2 = ans2 && (k - d2 - (2*d1))>=0 && (k - d2 - (2*d1))%3==0; ans2 = ans2 && (d1 + k - d2)%3==0; ans2 = ans2 && ((2*d2) + (d1) + k)%3==0; long w21 = (k - d2 - (2*d1))/3; long w22 = (d1 + k - d2)/3; long w23 = ((2*d2) + (d1) + k)/3; ans2 = ans2 && w21<=(n/3); ans2 = ans2 && w22<=(n/3); ans2 = ans2 && w23<=(n/3); boolean ans3=true; ans3 = ans3 && ((2*d2) - d1 + k)%3==0; ans3 = ans3 && ((2*d1) + k - d2)%3==0; ans3 = ans3 && (k-d1-d2>=0) && (k-d1-d2)%3==0; long w31 = ((2*d1) + k - d2)/3; long w32 = (k-d1-d2)/3; long w33 = ((2*d2) - d1 + k)/3; ans3 = ans3 && w31<=(n/3); ans3 = ans3 && w32<=(n/3); ans3 = ans3 && w33<=(n/3); boolean ans4=true; ans4 = ans4 && (d2 - (2*d1) + k)>=0 && (d2 - (2*d1) + k)%3==0; ans4 = ans4 && (d1 + k - (2*d2))>=0 && (d1 + k - (2*d2))%3==0; ans4 = ans4 && (k+d1+d2)%3==0; long w41 = (d2 - (2*d1) + k)/3; long w42 = (d1 + k - (2*d2))/3; long w43 = (k+d1+d2)/3; ans4 = ans4 && w41<=(n/3); ans4 = ans4 && w42<=(n/3); ans4 = ans4 && w43<=(n/3); boolean ans = ans1 || ans2 || ans3 || ans4; if(ans && n%3==0) { System.out.println("yes"); } else { System.out.println("no"); } } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top * @author Rubanenko */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int tc = in.nextInt(); while (tc > 0) { tc--; long n = in.nextLong(); long k = in.nextLong(); long d1 = in.nextLong(); long d2 = in.nextLong(); if (d1 < d2) { long tmp = d1; d1 = d2; d2 = tmp; } if (n % 3 != 0) { out.println("no"); continue; } long t = k - 2 * d1 + d2; if (t % 3 == 0 && t >= 0 && t / 3+ d1 <= n / 3) { out.println("yes"); continue; } t = k -2 * d1 - d2; if (t % 3 == 0 && t >= 0 && t / 3 + d1 + d2 <= n / 3) { out.println("yes"); continue; } t = k - d1 - d2; if (t % 3 == 0 && t >= 0 && t / 3 + d1 <= n / 3) { out.println("yes"); continue; } t = k - 2 * d2 - d1; if (t % 3 == 0 && t >= 0 && t / 3 + d1 + d2 <= n / 3) { out.println("yes"); continue; } out.println("no"); } } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); } public String nextLine() { String line = null; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(nextLine()); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.PrintWriter; import java.io.StreamTokenizer; public class Solution { public static void main(String[] args) throws Exception { solve(System.in, System.out); } public static void solve(InputStream inputStream, PrintStream outputStream) throws Exception { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(inputStream))); // BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); PrintWriter out = new PrintWriter(new OutputStreamWriter(outputStream)); int t = readInteger(in); for (int i = 0; i < t; i++) { long n = readLong(in); long k = readLong(in); long d1 = readLong(in); long d2 = readLong(in); if (n % 3 != 0) { out.println("no"); continue; } long n3 = n / 3; long v1, v2, v3; long x3; x3 = d2 + k - d1; if (x3 % 3 == 0) { v2 = x3 / 3; v1 = d1 + v2; v3 = k - v1 - v2; assert Math.abs(v1 - v2) == d1; assert Math.abs(v2 - v3) == d2; if (check(v1, v2, v3, n3)) { out.println("yes"); continue; } } x3 = -d2 + k - d1; if (x3 % 3 == 0) { v2 = x3 / 3; v1 = d1 + v2; v3 = k - v1 - v2; assert Math.abs(v1 - v2) == d1; assert Math.abs(v2 - v3) == d2; if (check(v1, v2, v3, n3)) { out.println("yes"); continue; } } x3 = -d2 + k + d1; if (x3 % 3 == 0) { v2 = x3 / 3; v1 = -d1 + v2; v3 = k - v1 - v2; assert Math.abs(v1 - v2) == d1; assert Math.abs(v2 - v3) == d2; if (check(v1, v2, v3, n3)) { out.println("yes"); continue; } } x3 = d2 + k + d1; if (x3 % 3 == 0) { v2 = x3 / 3; v1 = -d1 + v2; v3 = k - v1 - v2; assert Math.abs(v1 - v2) == d1; assert Math.abs(v2 - v3) == d2; if (check(v1, v2, v3, n3)) { out.println("yes"); continue; } } out.println("no"); } out.flush(); } private static boolean check(long v1, long v2, long v3, long n3) { return v1 >= 0 && v2 >= 0 && v3 >= 0 && v1 <= n3 && v2 <= n3 && v3 <= n3; } private static double readDouble(StreamTokenizer in) throws Exception { in.nextToken(); return in.nval; } private static float readFloat(StreamTokenizer in) throws Exception { return (float) readDouble(in); } private static int readInteger(StreamTokenizer in) throws Exception { return (int) readDouble(in); } private static long readLong(StreamTokenizer in) throws Exception { return (long) readDouble(in); } private static byte readByte(StreamTokenizer in) throws Exception { return (byte) readDouble(in); } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
from sys import * t=int(stdin.readline()) for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) vars=((2*d1+d2,2*d2+d1),(2*max(d1,d2)-min(d1,d2),d1+d2)) y=False for i in vars: if i[0]<=k and i[0]%3==k%3 and n-k-i[1]>=0 and (n-i[1]-k)%3==0: print("yes") y=True break if i[1]<=k and i[1]%3==k%3 and n-k-i[0]>=0 and (n-i[0]-k)%3==0: print("yes") y=True break if not y: print("no")
PYTHON3
451_C. Predict Outcome of the Game
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).
2
9
import java.util.Scanner; public class Sample { public static void main(String[] args) { long i,j,n,k,d1,d2,t; long n1, n2, n3; Scanner sc = new Scanner(System.in); t = sc.nextInt(); while (t-- != 0) { n = sc.nextLong(); k = sc.nextLong(); d1 = sc.nextLong(); d2 = sc.nextLong(); if (n%3 != 0 || d1 > n/3 || d2 > n/3) { System.out.println("no"); continue; } else { if (solve(n,k,d1,d2) || solve(n,k,-d1,d2) || solve(n,k,d1,-d2) || solve(n,k,-d1,-d2)) { System.out.println("yes"); } else { System.out.println("no"); } } } } public static boolean solve(long n, long k, long d1, long d2) { long n1, n2, n3; n2 = (k - d1 + d2)/3; if ((k - d1 + d2)%3 != 0) { return false; } n1 = n2+ d1; n3 = n2 - d2; if (n1<0 || n2<0 || n3<0) { return false; } if(n1>n/3 || n2>n/3 || n3>n/3) { return false; } return true; } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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"); } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import static java.lang.Math.*; import static java.util.Arrays.*; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); private void run() throws IOException { if (new File("input.txt").exists()) in = new BufferedReader(new FileReader("input.txt")); else in = new BufferedReader(new InputStreamReader(System.in)); if (new File("output.txt").exists()) out = new PrintWriter("output.txt"); else out = new PrintWriter(System.out); solve(); in.close(); out.close(); } boolean getAns(long n, long k, long d1, long d2) { long b = k - d1 + d2; if (b % 3 != 0) return false; b /= 3; long a = d1 + b; long c = b - d2; return a <= n / 3 && b <= n / 3 && c <= n / 3 && a >= 0 && b >= 0 && c >= 0; } void solve() throws IOException { int countTest = nextInt(); for (int test = 0; test < countTest; test++) { long n = nextLong(); long k = nextLong(); long d1 = nextLong(); long d2 = nextLong(); boolean ans = false; if (n % 3 == 0) { ans = getAns(n, k, d1, d2) || getAns(n, k, -d1, d2) || getAns(n, k, d1, -d2) || getAns(n, k, -d1, -d2); } out.println(ans ? "yes" : "no"); } } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } String nextToken() throws IOException { while (!st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String str = in.readLine(); if (str == null) return true; st = new StringTokenizer(str); } return false; } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
import java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Mahmoud Aladdin <aladdin3> */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, InputReader jin, OutputWriter jout) { int t = jin.int32(); while(t-- > 0) { long n = jin.int64(); long k = jin.int64(); long d1 = jin.int64(); long d2 = jin.int64(); long[] _12 = new long[]{-d1, d1}; long[] _23 = new long[]{-d2, d2}; boolean valid = n % 3 == 0; if(valid) { valid = false; for(long _d1: _12) { for(long _d2: _23) { boolean tValid = true; if(tValid) { long x2 = (k - _d1 - _d2) / 3; long x1 = x2 + _d2; long x3 = x2 + _d1; long req = n / 3; tValid &= x1 + x2 + x3 == k; tValid &= x1 >= 0 && x1 <= req; tValid &= x2 >= 0 && x2 <= req; tValid &= x3 >= 0 && x3 <= req; } valid |= tValid; } } } jout.println(valid? "yes" : "no"); } } } class InputReader { private static final int bufferMaxLength = 1024; private InputStream in; private byte[] buffer; private int currentBufferSize; private int currentBufferTop; private static final String tokenizers = " \t\r\f\n"; public InputReader(InputStream stream) { this.in = stream; buffer = new byte[bufferMaxLength]; currentBufferSize = 0; currentBufferTop = 0; } private boolean refill() { try { this.currentBufferSize = this.in.read(this.buffer); this.currentBufferTop = 0; } catch(Exception e) {} return this.currentBufferSize > 0; } private Byte readChar() { if(currentBufferTop < currentBufferSize) { return this.buffer[this.currentBufferTop++]; } else { if(!this.refill()) { return null; } else { return readChar(); } } } public String token() { StringBuffer tok = new StringBuffer(); Byte first; while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) != -1)); if(first == null) return null; tok.append((char)first.byteValue()); while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) == -1)) { tok.append((char)first.byteValue()); } return tok.toString(); } public Integer int32() throws NumberFormatException { String tok = token(); return tok == null? null : Integer.parseInt(tok); } public Long int64() throws NumberFormatException { String tok = token(); return tok == null? null : Long.parseLong(tok); } } class OutputWriter { private final int bufferMaxOut = 1024; private PrintWriter out; private StringBuilder output; private boolean forceFlush = false; public OutputWriter(OutputStream outStream) { out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outStream))); output = new StringBuilder(2 * bufferMaxOut); } public OutputWriter(Writer writer) { forceFlush = true; out = new PrintWriter(writer); output = new StringBuilder(2 * bufferMaxOut); } private void autoFlush() { if(forceFlush || output.length() >= bufferMaxOut) { flush(); } } public void print(Object... tokens) { for(int i = 0; i < tokens.length; i++) { if(i != 0) output.append(' '); output.append(tokens[i]); } autoFlush(); } public void println(Object... tokens) { print(tokens); output.append('\n'); autoFlush(); } public void flush() { out.print(output); output.setLength(0); } public void close() { flush(); out.close(); } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
t = int(input()) for l in range(t): n, k, d1, d2 = map(int, input().split()) if n % 3 != 0: print("no") continue n = n // 3 ok = False for i in [-1, 1]: for j in [-1, 1]: tmp = k; tmp -= d1 * i tmp -= d1 * i tmp -= d2 * j if tmp % 3 != 0: continue if tmp < 0: continue tmp = tmp // 3 x1 = tmp x2 = x1 + d1 * i x3 = x2 + d2 * j if x1 < 0 or x2 < 0 or x3 < 0: continue if x1 <= n and x2 <= n and x3 <= n: ok = True break if ok: print("yes") else: print("no")
PYTHON3
451_C. Predict Outcome of the Game
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).
2
9
import java.util.*; import java.io.*; public class C { static void solve() { int T = in.nextInt(); outer : for (int i = 0; i < T; i++) { long n, k, d1, d2; n = in.nextLong(); k = in.nextLong(); d1 = in.nextLong(); d2 = in.nextLong(); for (long s2 : new long[] {d2, -d2}) { for (long s1 : new long[] {s2 + d1, s2 - d1}) { if (f(s1, s2, k)) { if (s1 < s2) { long tmp = s1; s1 = s2; s2 = tmp; } if (f(s1 - s2, s1, n - k)) { out.println("yes"); continue outer; } } } } out.println("no"); } } static boolean f(long s1, long s2, long k) { boolean b1 = (k - s1 - s2) % 3 == 0; long x = (k - s1 - s2) / 3; return b1 && x >= 0 && (x + s1) >= 0 && (x + s2) >= 0; } static PrintWriter out = new PrintWriter(System.out); static InputReader in = new InputReader(System.in); static void d(Object o) { out.println(o); out.flush(); } public static void main(String args[]) { solve(); out.close(); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
def get(k, d1, d2): x = (k + (2 * d1) + d2) if x < 0 or x % 3 != 0: return -1, -1, -1 x /= 3 y = (k + d1 + d2) - (2 * x) if y < 0: return -1, -1, -1 z = (x - d1 - d2) if z < 0: return -1, -1, -1 return x, y, z def solve(n, k, d1, d2): signs = [ (1, 1), (1, -1), (-1, 1), (-1, -1) ] for (s1, s2) in signs: x, y, z = get(k, s1 * d1, s2 * d2) if x != -1: remaining = (n - k) remaining += (x + y + z) if remaining % 3 == 0: remaining /= 3 if remaining >= 0 and remaining >= x and remaining >= y and remaining >= z: return True return False [t] = map(int, raw_input('').split(' ')) for i in xrange(t): nums = map(int, raw_input('').split(' ')) if solve(*nums): print 'yes' else: print 'no'
PYTHON
451_C. Predict Outcome of the Game
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).
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Collection; import java.util.Locale; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { new Thread(null, new Runnable() { public void run() { try { long prevTime = System.currentTimeMillis(); new Main().run(); System.err.println("Total time: " + (System.currentTimeMillis() - prevTime) + " ms"); System.err.println("Memory status: " + memoryStatus()); } catch (IOException e) { e.printStackTrace(); } } }, "1", 1L << 24).start(); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); Object o = solve(); if (o != null) out.println(o); out.close(); in.close(); } private Object solve() throws IOException { int t = ni(); for (int i = 0; i < t; i++) solve2(); return null; } private void solve2() throws IOException { long n = nl(); long k = nl(); long dif = n-k; long[] arr = new long[3]; for (int i = 1; i < 3; i++) arr[i] = nl(); long[] check = new long[3]; long[] ds = new long[3]; for (int d1 = -1; d1 <= 1; d1 += 2) { ds[1] = d1; for (int d2 = -1; d2 <= 1; d2 += 2) { ds[2] = d2; for (int i = 1; i < 3; i++) check[i] = check[i - 1] + arr[i] * ds[i]; long[] process = process(check); if (valid(k,process) && ok(dif, process)) { pln("yes"); return; } } } pln("no"); } private long[] process(long[] check) { long min=Arrays.stream(check).min().getAsLong(); return Arrays.stream(check).map(a -> a-min).toArray(); } private boolean valid(long k, long[] process) { long sum = Arrays.stream(process).sum(); return (sum<=k && (k-sum)%3L==0L); } private boolean ok(long dif, long[] check) { PriorityQueue<Long> pq = new PriorityQueue<Long>(); for (long a : check) pq.add(a); pq.poll(); long mid = pq.poll(); long max = pq.poll(); dif -= (2L*max - mid); if (dif >= 0L && dif % 3L == 0L) return true; return false; } BufferedReader in; PrintWriter out; StringTokenizer strTok = new StringTokenizer(""); String nextToken() throws IOException { while (!strTok.hasMoreTokens()) strTok = new StringTokenizer(in.readLine()); return strTok.nextToken(); } int ni() throws IOException { return Integer.parseInt(nextToken()); } long nl() throws IOException { return Long.parseLong(nextToken()); } double nd() throws IOException { return Double.parseDouble(nextToken()); } int[] nia(int size) throws IOException { int[] ret = new int[size]; for (int i = 0; i < size; i++) ret[i] = ni(); return ret; } long[] nla(int size) throws IOException { long[] ret = new long[size]; for (int i = 0; i < size; i++) ret[i] = nl(); return ret; } double[] nda(int size) throws IOException { double[] ret = new double[size]; for (int i = 0; i < size; i++) ret[i] = nd(); return ret; } String nextLine() throws IOException { strTok = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!strTok.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; strTok = new StringTokenizer(s); } return false; } void printRepeat(String s, int count) { for (int i = 0; i < count; i++) out.print(s); } void printArray(int[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(long[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array, String spec) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.printf(Locale.US, spec, array[i]); } out.println(); } void printArray(Object[] array) { boolean blank = false; for (Object x : array) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } @SuppressWarnings("rawtypes") void printCollection(Collection collection) { boolean blank = false; for (Object x : collection) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } static String memoryStatus() { return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() >> 20) + "/" + (Runtime.getRuntime().totalMemory() >> 20) + " MB"; } public void pln() { out.println(); } public void pln(int arg) { out.println(arg); } public void pln(long arg) { out.println(arg); } public void pln(double arg) { out.println(arg); } public void pln(String arg) { out.println(arg); } public void pln(boolean arg) { out.println(arg); } public void pln(char arg) { out.println(arg); } public void pln(float arg) { out.println(arg); } public void pln(Object arg) { out.println(arg); } public void p(int arg) { out.print(arg); } public void p(long arg) { out.print(arg); } public void p(double arg) { out.print(arg); } public void p(String arg) { out.print(arg); } public void p(boolean arg) { out.print(arg); } public void p(char arg) { out.print(arg); } public void p(float arg) { out.print(arg); } public void p(Object arg) { out.print(arg); } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Alex */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, InputReader in, OutputWriter out){ int tests = in.ri(); for(int test = 0; test < tests; test++) { long ngames = in.readLong(), kmissed = in.readLong(), d1 = in.readLong(), d2 = in.readLong(), gamesalready = ngames - kmissed; if (ngames % 3 != 0){out.printLine("no"); continue;} if (works(ngames, kmissed, d1, d2, gamesalready)) {out.printLine("yes"); continue;} if (works(ngames, kmissed, d2, d1, gamesalready)) {out.printLine("yes"); continue;} out.printLine("no"); } } boolean works(long ngames, long kmissed, long d1, long d2, long gamesalready){ //check in case of 1 long toplay = d1 + 2 * d2; long alreadyplayed = 2 * d1 + d2; if (toplay <= kmissed && (kmissed - toplay) % 3 == 0 && alreadyplayed <= gamesalready) return true; //check in case of 2 if (d2 >= d1){ toplay = d1 + d2; alreadyplayed = d2 + (d2 - d1); if(toplay <= kmissed && (kmissed - toplay) % 3 == 0 && alreadyplayed <= gamesalready) return true; } //case 3 if (d2 >= d1){ long max = Math.min(d1, d2); long min = Math.max(d1 - max, d2 - max); toplay = max + 2*min; alreadyplayed = d1 + d2; if(toplay <= kmissed && (kmissed - toplay) % 3 == 0 && alreadyplayed <= gamesalready) return true; } return false; } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int ri(){ return readInt(); } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.io.*; import java.util.*; public final class predict_outcome { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static FastScanner sc=new FastScanner(br); static PrintWriter out=new PrintWriter(System.out); static boolean check(long curr1,long curr2,long curr3,long k) { if(curr1>=0 && curr2>=0 && curr3>=0 && curr1<=k && curr2<=k && curr3<=k && curr1+curr2+curr3==k) { return true; } return false; } public static void main(String args[]) throws Exception { int t=sc.nextInt(); while(t>0) { long n=sc.nextLong(),k=sc.nextLong(),d1=sc.nextLong(),d2=sc.nextLong(); boolean ans=false; long val1=k-d1-d2; if(val1%3==0 && val1>=0) { long curr1=val1/3,curr2=curr1+d1,curr3=curr1+d2; if(check(curr1,curr2,curr3,k)) { long diff=n-k,max=Math.max(curr1,Math.max(curr2,curr3)); long need1=max-curr1,need2=max-curr2,need3=max-curr3; long now=need1+need2+need3; if(now<=diff) { if((diff-now)%3==0) { ans=true; } } } } long val2=k+d1-d2; if(val2%3==0 && val2>=0) { long curr1=val2/3,curr2=curr1-d1,curr3=curr1+d2; if(check(curr1,curr2,curr3,k)) { long diff=n-k,max=Math.max(curr1,Math.max(curr2,curr3)); long need1=max-curr1,need2=max-curr2,need3=max-curr3; long now=need1+need2+need3; if(now<=diff) { if((diff-now)%3==0) { ans=true; } } } } long val3=k+d1+d2; if(val3>=0 && val3%3==0) { long curr1=val3/3,curr2=curr1-d1,curr3=curr1-d2; if(check(curr1,curr2,curr3,k)) { long diff=n-k,max=Math.max(curr1,Math.max(curr2,curr3)); long need1=max-curr1,need2=max-curr2,need3=max-curr3; long now=need1+need2+need3; if(now<=diff) { if((diff-now)%3==0) { ans=true; } } } } long val4=k-d1+d2; if(val4>=0 && val4%3==0) { long curr1=val4/3,curr2=curr1+d1,curr3=curr1-d2; if(check(curr1,curr2,curr3,k)) { long diff=n-k,max=Math.max(curr1,Math.max(curr2,curr3)); long need1=max-curr1,need2=max-curr2,need3=max-curr3; long now=need1+need2+need3; if(now<=diff) { if((diff-now)%3==0) { ans=true; } } } } out.println(ans?"yes":"no"); t--; } out.close(); } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public String next() throws Exception { return nextToken().toString(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
import java.util.Scanner; public class PredictOutcomeOfTheGame { static boolean solve(long n, long k, long d1, long d2) { if (n % 3 == 0) { long d3 = 0; long min = Math.min(Math.min(d1, d2), 0); if (min < 0) { d1 += -min; d2 += -min; d3 += -min; } long end = d1 + d2 + d3; if (k - end >= 0 && (k - end) % 3 == 0) { long max = Math.max(d1, Math.max(d2, d3)); long need = max - d1 + max - d2 + max - d3; long X = n - k - need; if (X >= 0 && X % 3 == 0) { return true; } } } return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int t = sc.nextInt(); t > 0; t--) { long n = sc.nextLong(); long k = sc.nextLong(); long d1 = sc.nextLong(); long d2 = sc.nextLong(); if (solve(n, k, d1, d2) || solve(n, k, -d1, d2) || solve(n, k, d1, -d2) || solve(n, k, -d1, -d2)) System.out.println("yes"); else System.out.println("no"); } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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"); } } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
t=int(input()) while(t>0): n,k,d1,d2=map(int,raw_input().split()) w1=k+2*d1+d2 f=0 p=0 if(n%3!=0): print "no" t-=1 continue if(w1%3==0 and w1>=0): w1=w1/3 w2=w1-d1 w3=w2-d2 diff=n-k ch=max(w1,w2,w3) diff-=ch-w1 diff-=ch-w2 diff-=ch-w3 # p=w1-w2 # diff-=p # p=w1-w3 # diff-=p # if(diff>=0 and diff%3==0): if(w1<=n/3 and w2<=n/3 and w3<=n/3 and w1>=0 and w2>=0 and w3>=0): f=1 p=1 print "yes" if(f==0): w1=k-2*d1-d2 if(w1%3==0 and w1>=0): w1/=3 w2=w1+d1 w3=w2+d2 ch=max(w1,w2,w3) diff=n-k diff-=ch-w1 diff-=ch-w2 diff-=ch-w3 # if(diff>=0 and diff%3==0): if(w1<=n/3 and w2<=n/3 and w3<=n/3 and w1>=0 and w2>=0 and w3>=0): f=1 p=2 print "yes" if(f==0): w1=k-2*d1+d2 if(w1%3==0 and w1>=0): w1/=3 w2=w1+d1 w3=w2-d2 ch=max(w1,w2,w3) diff=n-k diff-=ch-w1 diff-=ch-w2 diff-=ch-w3 # if(diff>=0 and diff%3==0): if(w1<=n/3 and w2<=n/3 and w3<=n/3 and w1>=0 and w2>=0 and w3>=0): f=1 p=3 print "yes" if(f==0): w1=k+2*d1-d2 if(w1%3==0 and w1>=0): w1/=3 w2=w1-d1 w3=w2+d2 ch=max(w1,w2,w3) diff=n-k diff-=ch-w1 diff-=ch-w2 diff-=ch-w3 # if(diff>=0 and diff%3==0): if(w1<=n/3 and w2<=n/3 and w3<=n/3 and w1>=0 and w2>=0 and w3>=0): f=1 p=4 print "yes" if(f==0): print "no" # print p t-=1
PYTHON
451_C. Predict Outcome of the Game
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).
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class PredictOutcomeOfTheGame { static long[] arr = new long[3]; public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(bf.readLine()); String[] l; long n, k, a, b; StringBuffer ans = new StringBuffer(); for (int i = 0; i < t; i++) { l = bf.readLine().split(" "); n = Long.parseLong(l[0]); k = Long.parseLong(l[1]); a = Long.parseLong(l[2]); b = Long.parseLong(l[3]); if (can(n - k, a, b, k) || can2(n - k, a, b, k) || can3(n - k, a, b, k) || can4(n - k, a, b, k)) { ans.append("yes\n"); } else { ans.append("no\n"); } } System.out.print(ans); } private static boolean can(long diff, long a, long b, long k) { arr[0] = (k - a - b) / 3; arr[1] = arr[0] + a; arr[2] = arr[0] + b; return test(diff, k); } private static boolean can4(long diff, long a, long b, long k) { arr[0] = (k - a + b) / 3; arr[1] = arr[0] + a; arr[2] = arr[0] - b; return test(diff, k); } private static boolean can2(long diff, long a, long b, long k) { arr[0] = (k + a - b) / 3; arr[1] = arr[0] - a; arr[2] = arr[0] + b; return test(diff, k); } private static boolean can3(long diff, long a, long b, long k) { arr[0] = (k + a + b) / 3; arr[1] = arr[0] - a; arr[2] = arr[0] - b; return test(diff, k); } private static boolean test(long diff, long k) { if (arr[0] < 0 || arr[1] < 0 || arr[2] < 0) return false; if (arr[0] + arr[1] + arr[2] != k) return false; Arrays.sort(arr); long need = arr[2] - arr[0] + arr[2] - arr[1]; if (diff < need) return false; diff -= need; return diff % 3 == 0; } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.StringTokenizer; public class PredictOutcomeOfTheGame { public static void main(String[] args) { MyScanner sc = new MyScanner(); int T = sc.nextInt(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < T; i++) { long N = sc.nextLong(); long K = sc.nextLong(); long d1 = sc.nextLong(); long d2 = sc.nextLong(); Result r1 = case1(K, d1, d2); Result r2 = case2(K, d1, d2); Result r3 = case3(K, d1, d2); Result r4 = case4(K, d1, d2); // System.out.println("i = " + i); // printResult(r1); // printResult(r2); // printResult(r3); // printResult(r4); if (works(N, r1) || works(N, r2) || works(N, r3) || works(N, r4)) { sb.append("yes\n"); } else { sb.append("no\n"); } } System.out.print(sb.toString()); } public static void printResult(Result r) { if (r != null) { System.out.format("w1 = %d, w2 = %d, w3 = %d\n", r.w1, r.w2, r.w3); } else { System.out.println("NULL"); } } public static boolean works(long N, Result r) { if (r == null || N % 3 != 0) { return false; } long avg = N / 3; return (r.w1 <= avg && r.w2 <= avg && r.w3 <= avg); } public static Result case1(long K, long d1, long d2) { long num = K + d1 + d2; if (num % 3 != 0) { return null; } long w2 = num / 3; long w1 = w2 - d1; long w3 = w2 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) { return null; } return new Result(w1, w2, w3); } public static Result case2(long K, long d1, long d2) { long num = K - d1 - d2; if (num % 3 != 0) { return null; } long w2 = num / 3; long w1 = w2 + d1; long w3 = w2 + d2; if (w1 < 0 || w2 < 0 || w3 < 0) { return null; } return new Result(w1, w2, w3); } public static Result case3(long K, long d1, long d2) { long num = K - d1 + d2; if (num % 3 != 0) { return null; } long w2 = num / 3; long w1 = w2 + d1; long w3 = w2 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) { return null; } return new Result(w1, w2, w3); } public static Result case4(long K, long d1, long d2) { long num = K + d1 - d2; if (num % 3 != 0) { return null; } long w2 = num / 3; long w1 = w2 - d1; long w3 = w2 + d2; if (w1 < 0 || w2 < 0 || w3 < 0) { return null; } return new Result(w1, w2, w3); } public static class Result { public long w1, w2, w3; public Result(long x1, long x2, long x3) { this.w1 = x1; this.w2 = x2; this.w3 = x3; } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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"); } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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"; } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import sys stdin = sys.stdin t = int(stdin.readline()) for icase in range(t): n, k, d1, d2 = map(int, stdin.readline().split()) found = False for dir1, dir2 in [[1, 1], [-1, -1], [-1, 1], [1, -1]]: delta1 = d1*dir1 delta2 = d2*dir2 scores = [0, delta1, delta1+delta2] offset = abs(min(scores)) scores = [s + offset for s in scores] so_far_at_least_games = sum(scores) if so_far_at_least_games > k or (so_far_at_least_games - k) % 3 != 0: continue extra_per_team = (so_far_at_least_games - k) / 3 scores = [s + extra_per_team for s in scores] best_team = max(scores) need_matches = sum([best_team - s for s in scores]) if need_matches > n-k or (need_matches - (n-k)) % 3 != 0: continue found = True break print ["no", "yes"][found]
PYTHON
451_C. Predict Outcome of the Game
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).
2
9
for i in range(int(input())): n, u, a, b = map(int, input().split()) if n % 3: print('no') else: if a > b: a, b = b, a p, q = a + b, 2 * b - a s, r, v = p + a, p + b, n - u t = [(p, q), (q, p), (s, r), (r, s)] print('no' if all(x > u or (x - u) % 3 or y > v or (y - v) % 3 for x, y in t) else 'yes') # Made By Mostafa_Khaled
PYTHON3
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush from math import * from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect from time import perf_counter from fractions import Fraction import copy import time starttime = time.time() mod = int(pow(10, 9) + 7) mod2 = 998244353 # from sys import stdin # input = stdin.readline def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end) def L(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] try: # sys.setrecursionlimit(int(pow(10,6))) sys.stdin = open("input.txt", "r") # sys.stdout = open("../output.txt", "w") except: pass for _ in range(L()[0]): n,k,d1,d2=L() if n%3!=0: print('no') continue #C1 a=(2*d1+d2+k) a=(a*(-1 if a%3 else 1))//3 b=a-d1 c=b-d2 if 0<=a<=n//3 and 0<=b<=n//3 and 0<=c<=n//3: # print(1,a,b,c) print("yes") continue #C2 b=(d1+d2+k) b=(b*(-1 if b%3 else 1))//3 a=b-d1 c=b-d2 if 0<=a<=n//3 and 0<=b<=n//3 and 0<=c<=n//3: # print(2,a,b,c) print("yes") continue #C3 b=(k-d1-d2) if b<0: b=n**2 b=(b*(-1 if b%3 else 1))//3 a=b+d1 c=b+d2 if 0<=a<=n//3 and 0<=b<=n//3 and 0<=c<=n//3: # print(3,a,b,c) print("yes") continue #C1 b=(+d1-d2+k) if b<0: b=n**2 b=(b*(-1 if b%3 else 1))//3 a=b-d1 c=b+d2 if 0<=a<=n//3 and 0<=b<=n//3 and 0<=c<=n//3: # print(4,a,b,c) print("yes") continue print("no") endtime = time.time() # print(f"Runtime of the program is {endtime - starttime}")
PYTHON3
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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"; } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
from sys import * t=int(stdin.readline()) mm=0 for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) mm=2*max(d1,d2)-min(d1,d2) if (k-2*d1-d2>=0 and (k-2*d1-d2)%3==0 and n-2*d2-d1-k>=0 and (n-2*d2-d1-k)%3==0) or (k-2*d2-d1>=0 and (k-2*d2-d1)%3==0 and n-2*d1-d2-k>=0 and (n-2*d1-d2-k)%3==0) or (k-d1-d2>=0 and (k-d1-d2)%3==0 and n-mm-k>=0 and (n-mm-k)%3==0) or (k-mm>=0 and (k-mm)%3==0 and n-d1-d2-k>=0 and (n-d1-d2-k)%3==0): print("yes") else: print("no")
PYTHON3
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.io.*; import java.math.*; import java.util.*; public class Main{ FastScanner in; PrintWriter out; public void solve() { int t = in.nextInt(); while (t --> 0) { long n = in.nextLong(); long k = in.nextLong(); long d1 = in.nextLong(); long d2 = in.nextLong(); boolean f = false; for (int i1 = -1; !f && i1 < 2; i1++) { if (i1 == 0) continue; for (int i2 = -1; !f && i2 < 2; i2++) { if (i2 == 0) continue; long x1, x2, x3, tmp; x1 = 0; x2 = x1 + i1 * d1; x3 = x2 + i2 * d2; if (x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; } if (x2 > x3) { tmp = x2; x2 = x3; x3 = tmp; } if (x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; } x2 += -x1; x3 += -x1; x1 = 0; if (((k - (x1 + x2 + x3)) % 3) != 0 || k - (x1 + x2 + x3) < 0) continue; long r = n - k; r -= x3 - x1; r -= x3 - x2; if (r < 0) continue; if (r % 3 == 0) f = true; } } if (!f) out.println("no"); else out.println("yes"); } } public void run() { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) { new Main().run(); } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
def f1(d1, d2, n, k): a1 = 2 * d1 + d2 + k a2 = -d1 + d2 + k a3 = -d1 - (2 * d2) + k if (a1 < 0 or a2 < 0 or a3 < 0 or a1 % 3 or a2 % 3 or a2 % 3): return False else: a1 //= 3 a2 //= 3 a3 //= 3 a1, a2, a3 = tuple(sorted([a1, a2, a3])[::-1]) if (a2 - a3 + 2 * (a1 - a2)) > n - k: return False else: return True def f2(d1, d2, n, k): a1 = -2 * d1 + d2 + k a2 = d1 + d2 + k a3 = d1 - (2 * d2) + k if (a1 < 0 or a2 < 0 or a3 < 0 or a1 % 3 or a2 % 3 or a2 % 3): return False else: a1 //= 3 a2 //= 3 a3 //= 3 a1, a2, a3 = tuple(sorted([a1, a2, a3])[::-1]) if (a2 - a3 + 2 * (a1 - a2)) > n - k: return False else: return True def f3(d1, d2, n, k): a1 = 2 * d1 - d2 + k a2 = -d1 - d2 + k a3 = -d1 + (2 * d2) + k if (a1 < 0 or a2 < 0 or a3 < 0 or a1 % 3 or a2 % 3 or a2 % 3): return False else: a1 //= 3 a2 //= 3 a3 //= 3 a1, a2, a3 = tuple(sorted([a1, a2, a3])[::-1]) if (a2 - a3 + 2 * (a1 - a2)) > n - k: return False else: return True def f4(d1, d2, n, k): a1 = -2 * d1 - d2 + k a2 = d1 - d2 + k a3 = d1 + (2 * d2) + k if (a1 < 0 or a2 < 0 or a3 < 0 or a1 % 3 or a2 % 3 or a2 % 3): return False else: a1 //= 3 a2 //= 3 a3 //= 3 a1, a2, a3 = tuple(sorted([a1, a2, a3])[::-1]) if (a2 - a3 + 2 * (a1 - a2)) > n - k: return False else: return True a = [] z = int(input()) for i in range(z): n, k, d1, d2 = map(int, input().split()) v1 = f1(d1, d2, n, k) v2 = f2(d1, d2, n, k) v3 = f3(d1, d2, n, k) v4 = f4(d1, d2, n, k) if (v1 or v2 or v3 or v4) and n % 3 == 0: a.append('yes') else: a.append('no') print(*a, sep = '\n')
PYTHON3
451_C. Predict Outcome of the Game
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).
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CR258_predict_outcome_of_the_game { public static void main(String... args) throws IOException { MyScanner sc = new MyScanner(); int t = sc.nextInt(); _nextTestCase: for (int tt = 0; tt < t; tt++) { long n = sc.nextLong(); long pc = sc.nextLong(); long w1 = sc.nextLong(); long w2 = sc.nextLong(); long r = n - pc; long[] k1Tab = {w1, -w1}; long[] k2Tab = {w2, -w2}; for (long k1 : k1Tab) { for (long k2 : k2Tab) { long b = r - k1 + k2; if (b % 3 != 0) continue; b /= 3; long a = k1 + b; long c = b - k2; if (a >= 0 && b >= 0 && c >= 0) { long t2 = pc + a - 2 * b + c; if (t2 % 3 != 0) continue; t2 /= 3; long t1 = -a + b + t2; long t3 = t2 + b - c; if (t1 >=0 && t2 >= 0 && t3 >= 0){ System.out.println("yes"); continue _nextTestCase; } } } } System.out.println("no"); } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { if (st == null || !st.hasMoreTokens()) { String line = br.readLine(); st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() throws IOException { String next = next(); return Integer.parseInt(next); } private long nextLong() throws IOException { String next = next(); return Long.parseLong(next); } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
for i in range(int(input())): n, k, d1, d2 = map(int, input().split()) flag = True if flag: c = (k - d1 - 2*d2)//3 b = c + d2 a = b + d1 if a >= 0 and b >= 0 and c >= 0 and k == a + b + c: v = [a, b, c] v.sort(reverse=True) if n-k >= v[0]-v[1]+v[0]-v[2] and (n-k-(v[0]-v[1]+v[0]-v[2]))%3 == 0: # print(v) print("yes") flag = False if flag: c = (k - d1 + 2*d2)//3 b = c - d2 a = b + d1 if a >= 0 and b >= 0 and c >= 0 and k == a + b + c: v = [a, b, c] v.sort(reverse=True) if n-k >= v[0]-v[1]+v[0]-v[2] and (n-k-(v[0]-v[1]+v[0]-v[2]))%3 == 0: # print(v) print("yes") flag = False if flag: c = (k + d1 - 2*d2)//3 b = c + d2 a = b - d1 if a >= 0 and b >= 0 and c >= 0 and k == a + b + c: v = [a, b, c] v.sort(reverse=True) if n-k >= v[0]-v[1]+v[0]-v[2] and (n-k-(v[0]-v[1]+v[0]-v[2]))%3 == 0: # print(v) print("yes") flag = False if flag: c = (k + d1 + 2*d2)//3 b = c - d2 a = b - d1 if a >= 0 and b >= 0 and c >= 0 and k == a + b + c: v = [a, b, c] v.sort(reverse=True) if n-k >= v[0]-v[1]+v[0]-v[2] and (n-k-(v[0]-v[1]+v[0]-v[2]))%3 == 0: # print(v) print("yes") flag = False if flag: print("no")
PYTHON3
451_C. Predict Outcome of the Game
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).
2
9
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class C { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; static long n, k; static int test; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); test = nextInt(); while (test-->0) { n = nextLong(); k = nextLong(); long d1 = nextLong(); long d2 = nextLong(); if (can(0, d1, d1+d2) || can(0, d1, d1-d2) || can(0, -d1,-d1+d2) || can(0, -d1, -d1-d2)) pw.println("yes"); else pw.println("no"); } pw.close(); } private static boolean can(int d1, long d2, long d3) { long[]d = new long[3]; d[0] = d1; d[1] = d2; d[2] = d3; Arrays.sort(d); d[1] -= d[0]; d[2] -= d[0]; d[0] = 0; if (k < d[1]+d[2]) return false; if ((k-(d[1]+d[2])) % 3 != 0) return false; if (n-k >= d[2]-d[0]+d[2]-d[1]) { long s = n-k - (d[2]-d[0]+d[2]-d[1]); return s % 3==0; } return false; } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
from sys import stdin rints = lambda: [int(x) for x in stdin.readline().split()] out = [] for _ in range(int(input())): n, k, d1, d2 = rints() cases, ans = [[d1, 0, d2], [0, d1, d1 + d2], [d2 + d1, d2, 0]], 'no' if d1 >= d2: cases.append([0, d1, d1 - d2]) if d2 >= d1: cases.append([d2 - d1, d2, 0]) for i in cases: if sum(i) > k or sum(i) < k and (k - sum(i)) % 3: continue i.sort() ext = 2 * i[-1] - (sum(i[:2])) if ext <= n - k and (n - k - ext) % 3 == 0: ans = 'yes' break out.append(ans) print('\n'.join(map(str, out)))
PYTHON
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.io.*; import java.util.*; import java.math.*; //import java.util.logging.Level; //import java.util.logging.Logger; public class Main { BufferedReader read; BufferedWriter write; public static void main(String args[]) throws Exception { new Main().init("1"); } void init(String name) throws Exception { read=ri();// rf(name+".txt"); int t=i(read.readLine()); while(t-->0) { long a[]=la(read.readLine()); if(a[0]%3==0&&check(a[0],a[1],a[2],a[3])) { System.out.println("yes"); continue; } System.out.println("no"); } } boolean check(long n,long k,long d1,long d2) { n/=3; long p=k-(2*d1+d2); if(p%3==0&&p>=0) { p/=3; if(p+d1+d2<=n)return true; } p=k-(2*d2+d1); if(p%3==0&&p>=0) { p/=3; if(p+d1+d2<=n)return true; } // 3. p=k-(2*d2-d1); if(d2>=d1) if(p%3==0&&p>=0) { p/=3; if(p+d2<=n)return true; } //4. p=k-(-d2+2*d1); if(d1>=d2) if(p%3==0&&p>=0) { p/=3; if(p+d1<=n)return true; } //5. p=k-(d2+d1); if(p%3==0&&p>=0) { p/=3; if(p+Math.max(d1,d2)<=n)return true; } return false; } int i(String s){return Integer.parseInt(s.trim());} long l(String s){return Long.parseLong(s.trim());} int[] ia(String s1){String s[]=s1.trim().split(" ");int p[]=new int[s.length];for(int i=0;i<s.length;i++)p[i]=Integer.parseInt(s[i]);return p;} long[] la(String s) { String s1[]=s.split(" "); long la[]=new long[s1.length]; for(int i=0;i<s1.length;i++)la[i]=l(s1[i]); return la; } static BufferedWriter wf(String s) throws Exception{return new BufferedWriter(new FileWriter(new File(s)));} static BufferedReader rf(String s) throws Exception{return new BufferedReader(new FileReader(new File(s)));} static BufferedReader ri() throws Exception{return new BufferedReader(new InputStreamReader(System.in));} }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.util.*; import java.io.*; public class Task3 { private FastScanner in; private PrintWriter out; public Task3(InputStream in, OutputStream out) { this.in = new FastScanner(in); this.out = new PrintWriter(out); } public boolean canBe(long n, long k, long d1, long d2) { // x, x - d1, x - d1 - d2 // 3x - 2 * d1 - d2 == k boolean result = false; if (k + 2 * d1 + d2 >= 0 && (k + 2 * d1 + d2) % 3 == 0) { long x = (k + 2 * d1 + d2) / 3; result |= restCanBe(n - k, k, x, x - d1, x - d1 - d2); } // x, x + d1, x + d1 - d2 // 3x + 2 * d1 - d2 == k if (k - 2 * d1 + d2 >= 0 && (k - 2 * d1 + d2) % 3 == 0) { long x = (k - 2 * d1 + d2) / 3; result |= restCanBe(n - k, k, x, x + d1, x + d1 - d2); } // x, x - d1, x - d1 + d2 // 3x - 2d1 + d2 = k if (k + 2 * d1 - d2 >= 0 && (k + 2 * d1 - d2) % 3 == 0) { long x = (k + 2 * d1 - d2) / 3; result |= restCanBe(n - k, k, x, x - d1, x - d1 + d2); } // x, x + d1, x + d1 + d2 // 3x + 2d1 + d2 = k if (k - 2 * d1 - d2 >= 0 && (k - 2 * d1 - d2) % 3 == 0) { long x = (k - 2 * d1 - d2) / 3; result |= restCanBe(n - k, k, x, x + d1, x + d1 + d2); } return result; } private boolean restCanBe(long rest, long k, long x, long y, long z) { if (x < 0 || x > k) { return false; } if (y < 0 || y > k) { return false; } if (z < 0 || z > k) { return false; } long max = Math.max(Math.max(x, y), z); long diff = 3 * max - x - y - z; if (diff > rest) { return false; } rest -= diff; return rest % 3 == 0; } public void solve() throws IOException { int testsCount = in.nextInt(); for (int i = 0; i < testsCount; ++i) { if (canBe(in.nextLong(), in.nextLong(), in.nextLong(), in.nextLong())) { out.println("yes"); } else { out.println("no"); } } } public void run() { try { solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] arg) { new Task3(System.in, System.out).run(); } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
t = int(input()) for _ in range(t): n, k, d1, d2 = map(int, input().split()) if n % 3 != 0: print("no") continue n = n // 3 ok = False for i in [-1, 1]: for j in [-1, 1]: tmp = k; tmp -= d1 * i tmp -= d1 * i tmp -= d2 * j if tmp % 3 != 0: continue if tmp < 0: continue tmp = tmp // 3 x1 = tmp x2 = x1 + d1 * i x3 = x2 + d2 * j if x1 < 0 or x2 < 0 or x3 < 0: continue if x1 <= n and x2 <= n and x3 <= n: ok = True break if ok: print("yes") else: print("no")
PYTHON3
451_C. Predict Outcome of the Game
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).
2
9
import static java.util.Arrays.deepToString; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.StringTokenizer; public class C { private static final boolean isDebug = false; void solve() throws Throwable { int T = readInt(); for (int i = 0; i < T; i++) { long N = readLong(); long K = readLong(); long D1 = readLong(); long D2 = readLong(); if(N % 3 != 0) { pw.println("no"); continue; } boolean isPos = check(N, K, D1, D2) || check(N, K, -D1, D2) || check(N, K, -D1, -D2) || check(N, K, D1, -D2); pw.println(isPos ? "yes" : "no"); } } boolean check(long n, long k, long d1, long d2) { long tmp = k - d2 - d1 -d1; if (tmp % 3 != 0 || tmp < 0) { return false; } tmp /= 3; long[] vals = new long[] {tmp, tmp + d1, tmp + d1 + d2}; for (long l : vals) { if (l < 0) { return false; } } long max = Math.max(vals[0], Math.max(vals[1], vals[2])); n -= k; for (int i = 0; i < 3; i++) { n -= max - vals[i]; vals[i] = max; } if ( n < 0 || n % 3 != 0) { return false; } return true; } final void printMatrix(double[][] p) { for (double[] i : p) printArray(i); } final void printArray(double[] p) { for (double i : p) System.out.print(i + " "); System.out.println(); } private static long gcd(long n1, long n2) { return (n2 == 0)?n1:gcd(n2, n1%n2); } private static int gcd(int n1, int n2) { return (n2 == 0)?n1:gcd(n2, n1%n2); } static long startTime; public static void main(String[] args) { C app = new C(); try { app.br = new BufferedReader(new InputStreamReader(System.in)); app.solve(); } catch(Throwable e) { e.printStackTrace(); } finally { try { app.br.close();} catch (Exception igunore) {} } d(System.currentTimeMillis() - startTime + " ms"); pw.flush(); pw.close(); } static final void d(Object ... o) { if (isDebug) pw.println(deepToString(o)); } static final void d(int[][] oA) { for (int[] o : oA) { d(o); } } static final void d(boolean[][] oA) { for (boolean[] o : oA) { d(o); } } void permutationAll(int[] p) { permutation(p, 0, p.length - 1); } void permutationRange(int from, int to) { int cnt = to - from + 1; int[] elements = new int[cnt]; for (int i = 0 ; i < cnt; i++) elements[i] = from++; permutation(elements, 0, cnt - 1); } void permutation(int[] elements, int nowCnt, int totalCnt) { if (nowCnt == totalCnt) { // TODO insertCode } else { for (int i = nowCnt; i <= totalCnt; i++) { int tmp = elements[nowCnt]; elements[nowCnt] = elements[i]; elements[i] = tmp; permutation(elements, nowCnt+1, totalCnt); tmp = elements[nowCnt]; elements[nowCnt] = elements[i]; elements[i] = tmp; } } } static PrintWriter pw = new PrintWriter(System.out); private BufferedReader br = null; private StringTokenizer st = null; private String delimiter = null; public String next() { while (st == null || !st.hasMoreTokens()) { if(delimiter != null) { st = new StringTokenizer(readLine(), delimiter); } else { st = new StringTokenizer(readLine()); } } return st.nextToken(); } public void close() { try { this.br.close(); } catch(IOException ioe) { ioe.printStackTrace(); } } public final String readString() { return next(); } public final int readInt() { return Integer.parseInt(next()); } public final long readLong() { return Long.parseLong(next()); } public final double readDouble() { return Double.parseDouble(next()); } public final int[] readIntArray() { String[] s = readStrArray(); int cnt = s.length; int[] out = new int[cnt]; for (int i = 0; i < cnt; i++) out[i] = Integer.parseInt(s[i]); return out; } public final long[] readLongArray() { String[] s = readStrArray(); int cnt = s.length; long[] out = new long[cnt]; for (int i = 0; i < cnt; i++) out[i] = Long.parseLong(s[i]); return out; } public final int[] readIntColumnArray(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = readInt(); } return res; } public final int[][] readIntMatrix(int N) { int[][] res = new int[N][]; for (int i = 0; i < N; i++) { res[i] = readIntArray(); } return res; } public final char[][] readCharMatrix(int N) { char[][] res = new char[N][]; for (int i = 0; i < N; i++) { res[i] = readCharArray(); } return res; } public final char[] readCharArray() { String[] s = readStrArray(); int cnt = s.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < cnt; i++) sb.append(s[i]); return sb.toString().toCharArray(); } public final String[] readStrArray() { List<String> res = new ArrayList<String>(); StringTokenizer st = new StringTokenizer(readLine(), " "); while (st.hasMoreTokens()) { res.add(st.nextToken()); } return res.toArray(new String[0]); } public void setDelimiter(String delim) { this.delimiter = delim; } public String getDelimiter() { return this.delimiter; } private String readLine() { try { return br.readLine(); } catch(IOException ioe) { ioe.printStackTrace(); throw new RuntimeException(ioe); } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
import java.util.InputMismatchException; import java.math.BigInteger; import java.io.*; /** * Generated by Contest helper plug-in * Actual solution is at the bottom */ public class Main { public static void main(String[] args) { InputReader in = new StreamInputReader(System.in); PrintWriter out = new PrintWriter(System.out); run(in, out); } public static void run(InputReader in, PrintWriter out) { Solver solver = new Task(); solver.solve(1, in, out); Exit.exit(in, out); } } abstract class InputReader { private boolean finished = false; public abstract int read(); public long readLong() { return new BigInteger(readString()).longValue(); } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public void setFinished(boolean finished) { this.finished = finished; } public abstract void close(); } class StreamInputReader extends InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public StreamInputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public void close() { try { stream.close(); } catch (IOException ignored) { } } } class Exit { private Exit() { } public static void exit(InputReader in, PrintWriter out) { in.setFinished(true); in.close(); out.close(); } } interface Solver { public void solve(int testNumber, InputReader in, PrintWriter out); } class Task implements Solver { public void solve(int testNumber, InputReader in, PrintWriter out) { int num = in.readInt(); long[][] nums = new long[num][4]; for (int i = 0; i<num; i++) { nums[i][0] = in.readLong(); nums[i][1] = in.readLong(); nums[i][2] = in.readLong(); nums[i][3] = in.readLong(); } for (int i = 0; i<num; i++) { long n = nums[i][0]; if (n%3!=0) out.println("no"); else { long k = nums[i][1]; long d1 = nums[i][2]; long d2 = nums[i][3]; long max = Math.max(d1, d2); long min = Math.min(d1, d2); if (test(n,k,max+min,min) || test(n,k,max+min,max) || test(n,k,max,min) || test(n,k,max,max-min)) out.println("yes"); else out.println("no"); } } } private boolean test(long n, long k, long max, long mid) { long req = max+mid; if (k<req) return false; long modk = k-max-mid; if (modk%3!=0) return false; if (max+modk/3>n/3) return false; return true; } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
import java.util.*; public class GameOutcome { private static class Case { long n, k, d1, d2; public Case(long n, long k, long d1, long d2) { this.n = n; this.k = k; this.d1 = d1; this.d2 = d2; } } public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); Case[] tests = new Case[t]; for (int i=0; i<t; i++) tests[i] = new Case(s.nextLong(), s.nextLong(), s.nextLong(), s.nextLong()); for (int i=0; i<t; i++) { long n = tests[i].n, k = tests[i].k, d1 = tests[i].d1, d2 = tests[i].d2; if (n % 3 != 0) System.out.println("no"); else { boolean good = false; if ((k - d1 - d2) % 3 == 0) { long a2 = (k-d1-d2)/3, a1 = a2 + d1, a3 = a2 + d2; if (!good && 0 <= a1 && a1 <= n/3 && 0 <= a2 && a2 <= n/3 && 0 <= a3 && a3 <= n/3) { good = true; System.out.println("yes"); } } if ((k - d1 + d2) % 3 == 0) { long a2 = (k-d1+d2)/3, a1 = a2 + d1, a3 = a2 - d2; if (!good && 0 <= a1 && a1 <= n/3 && 0 <= a2 && a2 <= n/3 && 0 <= a3 && a3 <= n/3) { good = true; System.out.println("yes"); } } if ((k + d1 + d2) % 3 == 0) { long a2 = (k+d1+d2)/3, a1 = a2 - d1, a3 = a2 - d2; if (!good && 0 <= a1 && a1 <= n/3 && 0 <= a2 && a2 <= n/3 && 0 <= a3 && a3 <= n/3) { good = true; System.out.println("yes"); } } if ((k + d1 - d2) % 3 == 0) { long a2 = (k+d1-d2)/3, a1 = a2 - d1, a3 = a2 + d2; if (!good && 0 <= a1 && a1 <= n/3 && 0 <= a2 && a2 <= n/3 && 0 <= a3 && a3 <= n/3) { good = true; System.out.println("yes"); } } if (!good) System.out.println("no"); } } s.close(); } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.InputMismatchException; /** * * @author jigsaw */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { InputStream inputstream = System.in; OutputStream outputstream = System.out; InputReader in = new InputReader(inputstream); OutputWriter outt = new OutputWriter(outputstream); int t = in.readInt(); while(t-->0){ String s = in.readString(); long n = Long.parseLong(s); s = in.readString(); long m = Long.parseLong(s); s = in.readString(); long d1 = Long.parseLong(s); s = in.readString(); long d2 = Long.parseLong(s); long x,y,z; if(n%3!=0) outt.printLine("no"); else { y = -1 ; x = -1; z = -1; if((m-d1-d2)>=0&&(m-d1-d2)%3==0){ y = (m-d1-d2)/3; if(y<=n/3){ x = d1+y; if(x>=0&&x<=n/3){ z = d2+y; if(z<0||z>n/3) z = -1; } else x = -1; } else y = -1; } if(x!=-1&&y!=-1&&z!=-1) outt.printLine("yes"); else{ y = -1 ; x = -1; z = -1; if((m-d1+d2)>=0&&(m-d1+d2)%3==0){ y = (m-d1+d2)/3; if(y<=n/3){ x = d1+y; if(x>=0&&x<=n/3){ z = y-d2; if(z<0||z>n/3) z = -1; } else x = -1; } else y = -1; } if(x!=-1&&y!=-1&&z!=-1) outt.printLine("yes"); else{ y = -1 ; x = -1; z = -1; if((m+d1-d2)>=0&&(m+d1-d2)%3==0){ y = (m+d1-d2)/3; if(y<=n/3){ x = y-d1; if(x>=0&&x<=n/3){ z = d2+y; if(z<0||z>n/3) z = -1; } else x = -1; } else y = -1; } if(x!=-1&&y!=-1&&z!=-1) outt.printLine("yes"); else{ y = -1 ; x = -1; z = -1; if((m+d1+d2)>=0&&(m+d1+d2)%3==0){ y = (m+d1+d2)/3; if(y<=n/3){ x = y-d1; if(x>=0&&x<=n/3){ z = y-d2; if(z<0||z>n/3) z = -1; } else x = -1; } else y = -1; } if(x!=-1&&y!=-1&&z!=-1) outt.printLine("yes"); else outt.printLine("no"); } } } } } outt.close(); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); }}
JAVA
451_C. Predict Outcome of the Game
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).
2
9
import java.io.*; import java.math.*; import java.util.*; public class A { final static int MOD = 1000000007; public static void main(String[] args) throws Exception { FastReader in = new FastReader(System.in); int t = in.nextInt(); while (t-- > 0) { long n = in.nextLong(); long k = in.nextLong(); long d1 = in.nextLong(); long d2 = in.nextLong(); if (check(n,k,d1,d2)) { System.out.println("yes"); } else { System.out.println("no"); } } } static boolean check(long n, long k, long d1, long d2) { if (n % 3 != 0) return false; n /= 3; Roots Roots1 = solve(d1, d2, k); Roots Roots2 = solve(-d1, d2, k); Roots Roots3 = solve(d1, -d2, k); Roots Roots4 = solve(-d1, -d2, k); if (Roots1 == null && Roots2 == null && Roots3 == null && Roots4 == null) return false; if (Roots1 != null) { if (Roots1.x <= n && Roots1.y <= n && Roots1.z <= n) return true; } if (Roots2 != null) { if (Roots2.x <= n && Roots2.y <= n && Roots2.z <= n) return true; } if (Roots3 != null) { if (Roots3.x <= n && Roots3.y <= n && Roots3.z <= n) return true; } if (Roots4 != null) { if (Roots4.x <= n && Roots4.y <= n && Roots4.z <= n) return true; } return false; } static Roots solve(long x, long y, long k) { long temp = k - x + y; if (temp % 3 != 0) return null; long w2 = temp / 3; long w1 = w2 + x; long w3 = w2 - y; if (w3 < 0 || w1 < 0 || w2 < 0) return null; return new Roots(w1, w2, w3); } static class Roots { long x, y, z; Roots(long a, long b, long c) { x = a; y = b; z = c; } } static class FastReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c == ',') { c = read(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String nextLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String nextLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return nextLine(); } else { return readLine0(); } } public BigInteger nextBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author cunbidun */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CPredictOutcomeOfTheGame solver = new CPredictOutcomeOfTheGame(); solver.solve(1, in, out); out.close(); } static class CPredictOutcomeOfTheGame { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { long n = in.nextLong(); long k = in.nextLong(); long d1 = in.nextLong(); long d2 = in.nextLong(); long x, y, z; if (n % 3 != 0) { out.println("no"); continue; } z = (k - d1 - d2 - d2) / 3; y = z + d2; x = y + d1; if (x + y + z == k && x >= 0 && y >= 0 && z >= 0) { if (x <= n / 3 && y <= n / 3 && z <= n / 3) { out.println("yes"); continue; } } y = (k - d1 - d2) / 3; z = y + d2; x = y + d1; if (x + y + z == k && x >= 0 && y >= 0 && z >= 0) { if (x <= n / 3 && y <= n / 3 && z <= n / 3) { out.println("yes"); continue; } } x = (k - d1 - d1 - d2) / 3; y = x + d1; z = y + d2; if (x + y + z == k && x >= 0 && y >= 0 && z >= 0) { if (x <= n / 3 && y <= n / 3 && z <= n / 3) { out.println("yes"); continue; } } y = (k + d1 + d2) / 3; z = y - d2; x = y - d1; if (x + y + z == k && x >= 0 && y >= 0 && z >= 0) { if (x <= n / 3 && y <= n / 3 && z <= n / 3) { out.println("yes"); continue; } } out.println("no"); } } } static class InputReader extends InputStream { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
rIn = lambda: raw_input().strip() def verdict(k, d1, d2, n): a = k+2*d1+d2 if a % 3: return False a /= 3 b = a-d1 c = b-d2 if a < 0 or b < 0 or c < 0: return False avg = n/3 return True if a <= avg and b <= avg and c <= avg else False T = int(rIn()) for t in xrange(T): n, k, d1, d2 = map(int, rIn().split()) if n % 3: print 'no' continue res = verdict(k, d1, d2, n) or verdict(k, -d1, d2, n) \ or verdict(k, d1, -d2, n) or verdict(k, -d1, -d2, n) print 'yes' if res else 'no'
PYTHON
451_C. Predict Outcome of the Game
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).
2
9
import itertools import sys ''' w1 - w2 = d1 w2 - w3 = d2 w1 + w2 + w3 = k w1 = w2 + d1 w3 = w2 - d2 w2 + d1 + w2 + w2 - d2 = k w2 = (k - d1 + d2) / 3 w1 = w2 + d1 w3 = w2 - d2 ''' for _ in range(int(input())): n, k, d1, d2 = map(int, str.split(sys.stdin.readline())) for s1, s2 in itertools.product((1, -1), repeat=2): cd1, cd2 = d1 * s1, d2 * s2 w2 = k - cd1 + cd2 if w2 % 3 != 0: continue w2 //= 3 w1 = w2 + cd1 w3 = w2 - cd2 if w1 >= 0 and w2 >= 0 and w3 >= 0: d = n - k mw = max((w1, w2, w3)) nw = 3 * mw - w1 - w2 - w3 if d >= nw and (d - nw) % 3 == 0: print("yes") break else: print("no")
PYTHON3
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.io.*; import java.util.*; public class A implements Runnable { private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private BufferedReader in; private PrintWriter out; private StringTokenizer tok = new StringTokenizer(""); private void init() throws FileNotFoundException { Locale.setDefault(Locale.US); String fileName = ""; if (ONLINE_JUDGE && fileName.isEmpty()) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { if (fileName.isEmpty()) { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } else { in = new BufferedReader(new FileReader(fileName + ".in")); out = new PrintWriter(fileName + ".out"); } } } String readString() { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() { return Integer.parseInt(readString()); } long readLong() { return Long.parseLong(readString()); } double readDouble() { return Double.parseDouble(readString()); } int[] readIntArray(int size) { int[] a = new int[size]; for (int i = 0; i < size; i++) { a[i] = readInt(); } return a; } public static void main(String[] args) { //new Thread(null, new A(), "", 128 * (1L << 20)).start(); new A().run(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } @Override public void run() { try { timeBegin = System.currentTimeMillis(); init(); solve(); out.close(); time(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } class Point { int x, y; int id; public Point(int x, int y) { this.x = x; this.y = y; } Point rotate90(Point center) { Point res = new Point(x, y); res.x -= center.x; res.y -= center.y; int swap = res.x; res.x = -res.y; res.y = swap; res.x += center.x; res.y += center.y; return res; } boolean equal(Point o) { return o.x == x && o.y == y; } } long getDistSqr(Point p1, Point p2) { long dx = p1.x - p2.x; long dy = p1.y - p2.y; return dx * dx + dy * dy; } long getLong(int x1, int x2) { return ((1l * x1) << 32) + x2; } class Pair implements Comparable<Pair> { int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if (x == o.x) return Integer.compare(y, o.y); return Integer.compare(x, o.x); } } int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } private void solve() throws IOException { int t = readInt(); while (t-- > 0) { long n = readLong(); long k = readLong(); long d1 = readLong(); long d2 = readLong(); long restMatches = n - k; boolean yes = false; /** 1 **/ long needMatches = 2 * d1 + d2; if (restMatches >= needMatches && k >= d1 + 2 * d2) { if ((restMatches - needMatches) % 3 == 0 && (k - 2 * d2 - d1) % 3 == 0) { yes = true; } } /** 2 **/ needMatches = Math.abs(d1 - d2) + Math.max(d1, d2); if (restMatches >= needMatches && k >= d1 + d2) { if ((restMatches - needMatches) % 3 == 0 && (k - d1 - d2) % 3 == 0) { yes = true; } } /** 3 **/ needMatches = d1 + d2; if (restMatches >= needMatches && k >= Math.max(d1, d2) * 2 - Math.min(d1, d2)) { if ((restMatches - needMatches) % 3 == 0 && (k - Math.max(d1, d2) * 2 + Math.min(d1, d2)) % 3 == 0) { yes = true; } } /** 4 **/ needMatches = 2 * d2 + d1; if (restMatches >= needMatches && k >= 2 * d1 + d2) { if ((restMatches - needMatches) % 3 == 0 && (k - 2 * d1 - d2) % 3 == 0) { yes = true; } } if (yes) { out.println("yes"); } else { out.println("no"); } } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
import java.io.BufferedInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Deque; import java.util.HashSet; import java.util.InputMismatchException; import java.util.Set; public class Main { public boolean isLegal(long x, long y, long z, long n, long k) { return x >= 0 && x <= k && x <= n / 3 && y >= 0 && y <= k && y <= n / 3 && z >= 0 && z <= k && z <= n / 3; } public void foo() { MyScanner scan = new MyScanner(); int t = scan.nextInt(); while(t-- > 0) { long n = scan.nextLong(); long k = scan.nextLong(); long d1 = scan.nextLong(); long d2 = scan.nextLong(); if(n % 3 != 0) { System.out.println("no"); } else { if(0 == (k - d1 - d2) % 3) { if(isLegal((k - d1 - d2) / 3 + d1, (k - d1 - d2) / 3, (k - d1 - d2) / 3 + d2, n, k)) { System.out.println("yes"); continue; } } if(0 == (k - d1 + d2) % 3) { if(isLegal((k - d1 + d2) / 3 + d1, (k - d1 + d2) / 3, (k - d1 + d2) / 3 - d2, n, k)) { System.out.println("yes"); continue; } } if(0 == (k + d1 - d2) % 3) { if(isLegal((k + d1 - d2) / 3 - d1, (k + d1 - d2) / 3, (k + d1 - d2) / 3 + d2, n, k)) { System.out.println("yes"); continue; } } if(0 == (k + d1 + d2) % 3) { if(isLegal((k + d1 + d2) / 3 - d1, (k + d1 + d2) / 3, (k + d1 + d2) / 3 - d2, n, k)) { System.out.println("yes"); continue; } } System.out.println("no"); } } } public static void main(String[] args) { new Main().foo(); } class MyScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; BufferedInputStream bis = new BufferedInputStream(System.in); public int read() { if (-1 == numChars) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = bis.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c; } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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"); } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
def checksituation(x1, x2, n, k, d1, d2): q = (k + x1*d1 + x2*d2) if q % 3 != 0: return False t2 = q / 3 t1 = t2 - x1*d1 t3 = t2 - x2*d2 if t1 >= 0 and t2 >= 0 and t3 >= 0 and t1 <= k and t2 <= k and t3 <= k and (t1 + t2 + t3 == k): needed = n / 3 if t1 <= needed and t2 <= needed and t3 <= needed: return True return False def solve(n, k, d1, d2): if n % 3 != 0: return "no" for i in [-1, 1]: for j in [-1, 1]: if checksituation(i, j, n, k, d1, d2): return "yes" return "no" t = int(raw_input().strip()) for i in range(t): n, k, d1, d2 = [int(x) for x in raw_input().strip().split()] print(solve(n, k, d1, d2))
PYTHON
451_C. Predict Outcome of the Game
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).
2
9
t = int(raw_input()) def check(a, b, c, n, k): # print a, b, c val = min(a, b, c) if val < 0: a += -val b += -val c += -val # print a, b, c s = a + b + c if k < s: return False if (k-s) % 3 != 0: return False games = n - k if (games+s) % 3 == 0: m = max(a, b, c) if games >= m*3 - s: return True else: return False else: return False for _t in xrange(t): valid = False n, k, d1, d2 = [int(x) for x in raw_input().split()] valid = valid or check(+d2+d1, +d2, 0, n, k) valid = valid or check(-d2+d1, -d2, 0, n, k) valid = valid or check(+d2-d1, +d2, 0, n, k) valid = valid or check(-d2-d1, -d2, 0, n, k) if valid: print 'yes' else: print 'no'
PYTHON
451_C. Predict Outcome of the Game
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).
2
9
#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"); } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
from sys import stdin rints = lambda: [int(x) for x in stdin.readline().split()] out, n = [], int(input()) inputs = [rints() for _ in range(n)] for n, k, d1, d2 in inputs: cases, ans = [[d1, 0, d2], [0, d1, d1 + d2], [d2 + d1, d2, 0]], 'no' if d1 >= d2: cases.append([0, d1, d1 - d2]) if d2 >= d1: cases.append([d2 - d1, d2, 0]) for i in cases: if sum(i) > k or sum(i) < k and (k - sum(i)) % 3: continue i.sort() ext = 2 * i[-1] - (sum(i[:2])) if ext <= n - k and (n - k - ext) % 3 == 0: ans = 'yes' break out.append(ans) print('\n'.join(map(str, out)))
PYTHON
451_C. Predict Outcome of the Game
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).
2
9
#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"); } } } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
from sys import stdin def getint(): return int(stdin.readline()) def getints(): return tuple([int(z) for z in stdin.readline().split()]) def f((n,k,d1,d2)): p = [[0,z,z+t] for z in [d1,-d1] for t in [d2,-d2]] p = [[i-min(v) for i in v] for v in p] p = [v for v in p if sum(v) <= k and ((k % 3) == (sum(v) % 3))] p = [[i + (k - sum(v)) / 3 for i in v] for v in p] p = [v for v in p if ((n % 3) == 0) and (max(v) <= (n / 3))] return "yes" if len(p) else "no" for i in xrange(getint()): print f(getints())
PYTHON
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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"); } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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"); } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.io.*; import java.math.*; import java.util.*; public class Main { static long mod=((long)1e9)+7;//toString public static int gcd(int a,int b){if(b==0)return a;else return gcd(b,a%b);} public static long pow_mod(long x,long y){long res=1;x=x%mod;while(y > 0){if((y & 1)==1)res=(res * x)%mod;y=y>>1;x =(x * x)%mod;}return res;} public static int lower_bound(int[]arr,int val){int lo=0;int hi=arr.length-1;while(lo<hi){int mid=lo+((hi-lo+1)/2);if(arr[mid]==val){return mid;}else if(arr[mid]>val){hi=mid-1;}else lo=mid;}if(arr[lo]<=val)return lo;else return -1;} public static int upper_bound(int[]arr,int val){int lo=0;int hi=arr.length-1;while(lo<hi){int mid=lo+((hi-lo)/2);if(arr[mid]==val){return mid;}else if(arr[mid]>val){hi=mid;;}else lo=mid+1;}if(arr[lo]>=val)return lo;else return -1;} public static long first( long d1, long d2, long a, long b ){ long ans = 0;long x = -1; if( ( a - ( ( 2 * d1 ) + d2 ) ) > -1 ){ if( ( a - ( ( 2 * d1 ) + d2 ) ) % 3 == 0 ){ x = ( a - ( ( 2 * d1 ) + d2 ) ) /3; } } if( x == -1 ) return x; if( x < 0 || ( x + d1 ) < 0 || ( x + d1 + d2 ) < 0 ) return -1; if( ( b - x ) >= 0 ){ ans += ( b - x ); } else{ return -1; } if( b - ( x + d1 ) >= 0 ){ ans += ( b - ( x + d1 ) ); } else{ return -1; } if( b - ( x + d1 + d2 ) >= 0 ){ ans += ( b - ( x + d1 + d2 ) ); } else return -1; return ans; } public static long second( long d1, long d2, long a, long b ){ long ans = 0;long x = -1; if( ( a - ( ( 2 * d1 ) - d2 ) ) > -1 ){ if( ( a - ( ( 2 * d1 ) - d2 ) ) % 3 == 0 ){ x = ( a - ( ( 2 * d1 ) - d2 ) ) /3; } } if( x == -1 ) return x; if( x < 0 || ( x + d1 ) < 0 || ( x + d1 - d2 ) < 0 ) return -1; if( ( b - x ) >= 0 ){ ans += ( b - x ); } else{ return -1; } if( b - ( x + d1 ) >= 0 ){ ans += ( b - ( x + d1 ) ); } else{ return -1; } if( b - ( x + d1 - d2 ) >= 0 ){ ans += ( b - ( x + d1 - d2 ) ); } else return -1; return ans; } public static long third( long d1, long d2, long a, long b ){ long ans = 0;long x = -1; if( ( a - ( ( -2 * d1 ) + d2 ) ) > -1 ){ if( ( a - ( ( -2 * d1 ) + d2 ) ) % 3 == 0 ){ x = ( a - ( ( -2 * d1 ) + d2 ) ) /3; } } if( x == -1 ) return x; if( x < 0 || ( x - d1 ) < 0 || ( x - d1 + d2 ) < 0 ) return -1; if( ( b - x ) >= 0 ){ ans += ( b - x ); } else{ return -1; } if( b - ( x - d1 ) >= 0 ){ ans += ( b - ( x - d1 ) ); } else{ return -1; } if( b - ( x - d1 + d2 ) >= 0 ){ ans += ( b - ( x - d1 + d2 ) ); } else return -1; return ans; } public static long fourth( long d1, long d2, long a, long b ){ long ans = 0;long x = -1; if( ( a - ( ( -2 * d1 ) - d2 ) ) > -1 ){ if( ( a - ( ( -2 * d1 ) - d2 ) ) % 3 == 0 ){ x = ( a - ( ( -2 * d1 ) - d2 ) ) /3; } } if( x == -1 ) return x; if( x < 0 || ( x - d1 ) < 0 || ( x - d1 - d2 ) < 0 ) return -1; if( ( b - x ) >= 0 ){ ans += ( b - x ); } else{ return -1; } if( b - ( x - d1 ) >= 0 ){ ans += ( b - ( x - d1 ) ); } else{ return -1; } if( b - ( x - d1 - d2 ) >= 0 ){ ans += ( b - ( x - d1 - d2 ) ); } else return -1; return ans; } public static void main (String[] args) throws java.lang.Exception { Reader sn = new Reader(); Print p = new Print(); int t = sn.nextInt(); while( t -- > 0 ){ int ans = 0; long n = sn.nextLong(); long k = sn.nextLong(); long d1 = sn.nextLong(); long d2 = sn.nextLong(); if( ( n % 3 ) == 0){ if( first( d1, d2, k, n/3 ) != -1 ){ long a = first( d1, d2, k, n/3 ); if( ( ( n - k ) - a ) > -1 ){ ans = 1; } } if( second( d1, d2, k, n/3 ) != -1 ){ long a = second( d1, d2, k, n/3 ); if( ( ( n - k ) - a ) > -1 ){ ans = 1; } } if( third( d1, d2, k, n/3 ) != -1 ){ long a = third( d1, d2, k, n/3 ); if( ( ( n - k ) - a ) > -1 ){ ans = 1; } } if( fourth( d1, d2, k, n/3 ) != -1 ){ long a = fourth( d1, d2, k, n/3 ); if( ( ( n - k ) - a ) > -1 ){ ans = 1; }} if( ans == 1 ) p.printLine("yes"); else p.printLine("no"); } else{ p.printLine("no"); } } p.close(); } } class Pair implements Comparable<Pair> { int val; int in; Pair(int a, int b){ val=a; in=b; } @Override public int compareTo(Pair o) { if(val==o.val) return Integer.compare(in,o.in); else return Integer.compare(val,o.val); }} class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public String readWord()throws IOException { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); }while (!isSpaceChar(c)); return res.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } class Print { private final BufferedWriter bw; public Print() { bw=new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(String str)throws IOException { bw.append(str); } public void printLine(String str)throws IOException { print(str); bw.append("\n"); } public void close()throws IOException { bw.close(); }}
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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"); } }
CPP
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP
451_C. Predict Outcome of the Game
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).
2
9
import java.util.Scanner; public class T451C { private boolean getres(long n, long k, long d1, long d2) { if (n % 3 != 0) return false; long div = n / 3; long x1, x2, x3; if ((2 * d1 + k + d2) % 3 != 0) return false; x1 = (2 * d1 + k + d2) / 3; x2 = x1 - d1; x3 = x2 - d2; if (x1 < 0 || x2 < 0 || x3 < 0) return false; if (x1 > k || x2 > k || x3 > k) return false; if (x1 > div || x2 > div || x3 > div) return false; else return true; } public void solve(long n, long k, long d1, long d2) { boolean f = getres(n, k, d1, d2) || getres(n, k, -d1, d2) || getres(n, k, d1, -d2) || getres(n, k, -d1, -d2); if (f) System.out.println("yes"); else System.out.println("no"); } public static void main(String[] args) { T451C c = new T451C(); /* long n0 = 3, k0 = 0, d10 = 0, d20 = 0; long n1 = 3, k1 = 3, d11 = 0, d21 = 0; long n2 = 6, k2 = 4, d12 = 1, d22 = 0; long n3 = 6, k3 = 3, d13 = 3, d23 = 0; long n4 = 3, k4 = 3, d14 = 3, d24 = 2; c.solve(n0, k0, d10, d20); c.solve(n1, k1, d11, d21); c.solve(n2, k2, d12, d22); c.solve(n3, k3, d13, d23); c.solve(n4, k4, d14, d24); */ Scanner in = new Scanner(System.in); long t = in.nextLong(); for (long i = 0; i < t; i ++) { c.solve(in.nextLong(), in.nextLong(), in.nextLong(), in.nextLong()); } } }
JAVA
451_C. Predict Outcome of the Game
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).
2
9
#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; }
CPP