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
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 t in xrange(input()): n, k, d1, d2 = map(int, raw_input().split()) def p(n,k,d1,d2): if n % 3 == 0: x2 = (k-d1+d2)/3 if (k-d1+d2) % 3 == 0 and x2 >= 0 and x2 <= k: x1, x3 = d1+x2, x2-d2 if x1 >= 0 and x1 <= k and x3 >= 0 and x3 <= k: if x1 <= n/3 and x2 <= n/3 and x3 <= n/3: return 1 return 0 print "yes" if any([p(n,k,d1,d2),p(n,k,d1,-d2),p(n,k,-d1,d2),p(n,k,-d1,-d2)]) 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
#include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2; bool _(long long a, long long b) { return (a / 3 >= b && a / 3 * 3 == a && a <= n); } bool x() { if (n % 3) return false; if (_(n - k + d1 + d2 * 2, d1 + d2)) return true; if (_(n - k + d2 + d1 * 2, d1 + d2)) return true; if (_(n - k + d1 + d2, max(d1, d2))) return true; if (_(n - k - min(d1, d2) + max(d1, d2) * 2, max(d1, d2))) return true; return false; } int main() { scanf("%I64d", &t); for (long long i = 0; i < t; i++) { cin >> n >> k >> d1 >> d2; printf("%s\n", x() ? "yes" : "no"); } return 0; }
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.*; public class Main { FastScanner in; PrintWriter out; static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = null; } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { if (st == null || !st.hasMoreTokens()) return br.readLine(); StringBuilder result = new StringBuilder(st.nextToken()); while (st.hasMoreTokens()) { result.append(" "); result.append(st.nextToken()); } return result.toString(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } void run() throws IOException { in = new FastScanner(System.in); out = new PrintWriter(System.out, false); solve(); out.close(); } public static void main(String[] args) throws IOException{ new Main().run(); } public void printArr(int[] arr){ for(int i = 0; i < arr.length; i++){ out.print(arr[i] + " "); } out.println(); } public long gcd(long a, long b){ if(a == 0) return b; return gcd(b % a, a); } public void solve() throws IOException{ int t = in.nextInt(); for(int i = 0; i < t; i++){ long n = in.nextLong(), k = in.nextLong(), d1 = in.nextLong(), d2 = in.nextLong(); long r = n - k; //out.println("n: " + n + " k: " + k + " d1: " + d1 + " d2: " + d2); long n1 = 0, n2 = 0, n3 = 0; long s = k + d1 + d2; // 4 cases if((k + 2 * d1 + d2) % 3 == 0){ n1 = (k + 2 * d1 + d2) / 3; n2 = n1 - d1; n3 = n1 - d1 - d2; if(n1 >= 0 && n2 >= 0 && n3 >= 0 && r >= (2 * d1 + d2) && (r - 2 * d1 - d2) % 3 == 0 ){ out.println("yes"); continue; } } if((k + d1 + d2) % 3 == 0){ //out.println("c1"); n2 = s / 3; n1 = n2 - d1; n3 = n2 - d2; if(n1 >= 0 && n2 >= 0 && n3 >= 0 && r >= (d1 + d2) && (r - d1 - d2) % 3 == 0){ out.println("yes"); continue; } } long m = k + 2 * d2 - d1; if(m % 3 == 0){ //out.println("c2"); n3 = m / 3; n2 = n3 - d2; n1 = d1 + n2; //out.println(n1 + " " + n2 + " " + n3); if(n1 >= 0 && n2 >= 0 && n3 >= 0 && (r >= Math.max(d1, d2) + Math.abs(d1 - d2)) && (r - Math.max(d1, d2) - Math.abs(d1 - d2)) % 3 == 0){ out.println("yes"); continue; } } long l = k + d1 - d2; if(l % 3 == 0){ //out.println("c3"); n2 = l / 3; n3 = n2 + d2; n1 = n2 - d1; //out.println(n1 + " " + n2 + " " + n3); if(n1 >= 0 && n2 >= 0 && n3 >= 0 && (r >= d1 + 2 * d2) && (r - d1 - 2 * d2) % 3 == 0){ out.println("yes"); continue; } } out.println("no"); } return; } }
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 test(a, k, n): mn = abs(min(a)) a = [x + mn for x in a] s = sum(a) if k - s < 0 or (k - s) % 3 != 0: return False mx = max(a) s = sum([mx - x for x in a]) return n - s >= 0 and (n - s) % 3 == 0; t = int(raw_input()) for i in xrange(t): n, k, d0, d1 = map(int, raw_input().split()) ok = False w = [0, 0, 0] for i in xrange(-1, 2, 2): w[1] = w[0] + i * d0 for j in xrange(-1, 2, 2): w[2] = w[1] + j * d1 if test(w[:], k, n - k): ok = True if ok: 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.util.ArrayList; import java.util.List; import java.util.Scanner; public class SortingArr { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i = 0; i < t; ++i) { long m = in.nextLong(); long k = in.nextLong(); long d1 = in.nextLong(); long d2 = in.nextLong(); List<Long> x = new ArrayList<Long>(); if((k-(2*d2+d1))%3==0 && (k-(2*d2+d1))>=0) x.add(2*d1+d2); if((k-(d1+d2))%3==0 && d1>=d2 && (k-(d1+d2))>=0) x.add(2*d1-d2); if((k-(2*d2-d1))%3==0 && d2>=d1 && (k-(2*d2-d1))>=0) x.add(d1+d2); if((k-(2*d1-d2))%3==0 && d1>=d2 && (k-(2*d1-d2))>=0) x.add(d1+d2); if((k-(d1+d2))%3==0 && d2>=d1 && (k-(d1+d2))>=0) x.add(2*d2-d1); if((k-(2*d1+d2))%3==0 && (k-(2*d1+d2))>=0) x.add(2*d2+d1); boolean yes = false; for(int j=0; j<x.size(); ++j) { long diff = (m-k-x.get(j)); if(diff >= 0 && diff%3 == 0) { yes = true; break; } } if(yes) { 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; long long n, k, d1, d2; bool check(long long ofsa, long long ofsb) { long long c_3 = (k - ofsa - ofsb); if (c_3 % 3 != 0) return 0; long long c = c_3 / 3, a = c + ofsa, b = c + ofsb; if (a < 0 || b < 0 || c < 0) return 0; if (a > n / 3 || b > n / 3 || c > n / 3) return 0; return 1; } bool check() { if (n % 3 != 0) return 0; if (check(d2 + d1, d2)) return 1; if (check(d2 - d1, d2)) return 1; if (check(-d2 - d1, -d2)) return 1; if (check(-d2 + d1, -d2)) return 1; return 0; } void solve() { cin >> n >> k >> d1 >> d2; ; ; cout << (check() ? "yes" : "no") << endl; } void contest_main() { int t; cin >> t; while (t--) solve(); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); contest_main(); return 0; }
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 tc, n, k, d1, d2; cin >> tc; while (tc--) { cin >> n >> k >> d1 >> d2; if (false) { printf("no\n"); } else { if (false) { printf("no\n"); } else { const long long r = n - k; long long x; bool yes = false; long long a, b, c, sum; a = d1 + d2, b = d2, c = 0; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; x = r - ((a - c) + (a - b)); if (x >= 0 && x % 3 == 0) yes = true; } if (!yes) { a = 0, b = d1, c = d1 + d2; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; x = r - ((c - a) + (c - b)); if (x >= 0 && x % 3 == 0) yes = true; } } if (!yes) { a = (d1 < d2) ? d2 - d1 : 0, b = max(d1, d2), c = (d1 < d2) ? 0 : d1 - d2; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; x = r - ((b - a) + (b - c)); if (x >= 0 && x % 3 == 0) yes = true; } } if (!yes) { a = d1, b = 0, c = d2; sum = a + b + c; if (k >= sum && (k - sum) % 3 == 0) { x = (k - sum) / 3; a += x, b += x, c += x; if (a > c) x = r - ((a - b) + (a - c)); else x = r - ((c - b) + (c - a)); if (x >= 0 && x % 3 == 0) yes = true; } } if (yes) printf("yes\n"); else printf("no\n"); } } } return 0; }
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 Max(long long a, long long b) { return a > b ? a : b; } int main() { long long t, n, k, d1, d2, m; int flag; cin >> t; while (t--) { flag = 1; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } if (k < (d1 + d2 + d2) || (k - d2 - d1 - d2) % 3 != 0) { flag = 0; } if (flag) { m = (k - d2 - d1 - d2) / 3; if (n % 3 != 0 || (n / 3) < (d1 + d2 + m)) flag = 0; } if (flag) cout << "yes\n"; else { flag = 1; if (k < (d1 + d2) || (k - d1 - d2) % 3 != 0) flag = 0; if (flag) { m = (k - d1 - d2) / 3; if (n % 3 != 0 || (n / 3) < Max(d1 + m, d2 + m)) flag = 0; } if (flag) cout << "yes\n"; else { flag = 1; if (k < (d1 + d1 + d2) || (k - d1 - d1 - d2) % 3 != 0) flag = 0; if (flag) { m = (k - d1 - d1 - d2) / 3; if (n % 3 != 0 || (n / 3) < (d1 + d2 + m)) flag = 0; } if (flag) cout << "yes\n"; else { if (d1 < d2) { m = d1; d1 = d2; d2 = m; } flag = 1; if (k < (2 * d1 - d2) || (k - d1 + d2 - d1) % 3 != 0) { flag = 0; } if (flag) { m = (k - d1 + d2 - d1) / 3; if (n % 3 != 0 || (n / 3) < (d1 + m)) flag = 0; } if (flag) cout << "yes\n"; else cout << "no\n"; } } } } return 0; }
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, t, a, b, c; void solve() { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3) { printf("no\n"); return; } t = n / 3; bool ok = false; if ((d1 * 2 + d2 + k) % 3 == 0) { a = (k + d1 * 2 + d2) / 3; b = a - d1; c = b - d2; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } if ((k - d1 - d2) % 3 == 0) { b = (k - d1 - d2) / 3; a = b + d1; c = b + d2; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } if ((k + d1 + d2) % 3 == 0) { b = (k + d1 + d2) / 3; a = b - d1; c = b - d2; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } if ((k + d2 * 2 + d1) % 3 == 0) { c = (k + d2 * 2 + d1) / 3; b = c - d2; a = b - d1; if (a <= t && b <= t && c <= t && a >= 0 && b >= 0 && c >= 0) { printf("yes\n"); return; } } printf("no\n"); } int main() { int q; scanf("%d", &q); while (q--) solve(); return 0; }
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
q = int(input()) while q > 0: n, k, d1, d2 = map(int, input().split()) if d1 > d2: d1, d2 = d2, d1 if k - 2 * d1 - d2 >= 0 and (k - 2 * d1 - d2) % 3 == 0 and \ (n - k) - d1 - 2 * d2 >= 0 and ((n - k) - d1 - 2 * d2) % 3 == 0: print('yes') elif k - 2 * d2 - d1 >= 0 and (k - 2 * d2 - d1) % 3 == 0 and \ (n - k) - d2 - 2 * d1 >= 0 and ((n - k) - d2 - 2 * d1) % 3 == 0: print('yes') elif k - 2 * d2 + d1 >= 0 and (k - 2 * d2 + d1) % 3 == 0 and \ (n - k) - d2 - d1 >= 0 and ((n - k) - d2 - d1) % 3 == 0: print('yes') elif k - d1 - d2 >= 0 and (k - d1 - d2) % 3 == 0 and \ (n - k) - 2 * d2 + d1 >= 0 and ((n - k) - 2 * d2 + d1) % 3 == 0: print('yes') else: print('no') q -= 1
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 sys lines = sys.stdin.readlines() ''' (n, p) = map(int, lines[0].strip().split(" ")) ranges = [] for i in range(1, n+1): (l, r) = map(int, lines[i].strip().split(" ")) ranges.append((l,r)) probs = [] for lr in ranges: poss = lr[1]//p - (lr[0]-1)//p probs.append(poss/(lr[1]-lr[0]+1)) res = 0 for i in range(n): res += probs[i] + probs[i-1] - probs[i] * probs[i-1] print(res * 2000) ''' N = int(lines[0].strip()) for i in range(1, 1+N): (n, k, d1, d2) = map(int, lines[i].strip().split(" ")) if n % 3 != 0: print("no"); continue def solve(da, db): tmp = k - da - db if tmp % 3 != 0: return True b = tmp // 3 a = b + da c = b + db if min(a,b,c) < 0: return True if n//3 >= max(a,b,c): return False else: return True cannot = True if cannot: cannot = solve(d1, d2) if cannot: cannot = solve(d1, -d2) if cannot: cannot = solve(-d1, d2) if cannot: cannot = solve(-d1, -d2) if cannot: print("no") else: print("yes")
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 main() { int T, i, j, ok; long long n, k, d1, d2, r, a, b, c; for (scanf("%d", &T); T--;) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3) { puts("no"); continue; } ok = 0; for (i = -1; i <= 1; i += 2) for (j = -1; j <= 2; j += 2) { r = k - i * d1 - j * d2 - i * d1; if (r % 3) continue; a = r / 3; b = a + i * d1; c = b + j * d2; if (a < 0 || b < 0 || c < 0) continue; if (a > n / 3) continue; if (b > n / 3) continue; if (c > n / 3) continue; ok = 1; } if (ok) puts("yes"); else puts("no"); } return 0; }
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 int max(long long int a, long long int b) { if (a > b) return a; else return b; } long long int min(long long int a, long long int b) { if (a < b) return a; else return b; } const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1}; long long int func(long long int x) { long long int ans = 0; for (long long int i = 2; i * i * i <= x; i++) { ans += (x / (i * i * i)); } return ans; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int k, i, j, m, n, x, y, t, d1, d2, a, b, c; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; long long int x, y, z = 0; z = k - (d1 + d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1; b = x; c = x + d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (d1 - d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1; b = x; c = x - d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d1 - d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1; b = x; c = x - d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d1 + d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1; b = x; c = x + d2; long long int count = 0; long long int maxi = max(a, max(b, c)); count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (d2 + d1 + d2); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1 + d2; b = x + d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (d2 + d2 - d1); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1 + d2; b = x + d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d2 - d2 + d1); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x + d1 - d2; b = x - d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } z = k - (-d2 - d2 - d1); if (z >= 0 && z % 3 == 0) { long long int left = n - k; x = z / 3; a = x - d1 - d2; b = x - d2; c = x; long long int maxi = max(a, max(b, c)); long long int count = abs(maxi - a); count += abs(maxi - b); count += abs(maxi - c); if (left >= count && a >= 0 && b >= 0 && c >= 0) { left -= count; if (left % 3 == 0) { cout << "yes" << "\n"; continue; } } } cout << "no" << "\n"; } }
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; inline bool Solve(const long long d1, const long long d2) { long long p = k + 2 * d1 + d2; if (p % 3) return 0; long long x1 = p / 3; if (x1 < 0 || x1 > n / 3) return 0; long long x2 = x1 - d1; if (x2 < 0 || x2 > n / 3) return 0; long long x3 = x2 - d2; if (x3 < 0 || x3 > n / 3) return 0; return 1; } int main() { int t; cin.sync_with_stdio(false); cin >> t; long long d1, d2; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no\n"; continue; } bool ok = 0; ok |= Solve(d1, d2); ok |= Solve(-d1, d2); ok |= Solve(-d1, -d2); ok |= Solve(d1, -d2); if (ok) cout << "yes\n"; else cout << "no\n"; } return 0; }
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.InputStreamReader; import java.io.IOException; import java.util.InputMismatchException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Niyaz Nigmatullin */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, FastScanner in, FastPrinter out) { int t = in.nextInt(); for (int i = 0; i < t; i++) { long n = in.nextLong(); long k = in.nextLong(); long d2 = in.nextLong(); long d1 = in.nextLong(); boolean can = false; for (int f = -1; f <= 1; f += 2) { for (int g = -1; g <= 1; g += 2) { long q = (k - f * d1 - g * d2); if (q < 0 || q % 3 != 0) continue; long x = q / 3; long y = x + f * d1; long z = x + g * d2; if (y < 0 || z < 0) continue; if (n % 3 != 0 || n / 3 < Math.max(x, Math.max(y, z))) { continue; } can = true; } } out.println(can ? "yes" : "no"); } } } class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public long nextLong() { return Long.parseLong(next()); } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } } class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } }
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; void calc() { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no" << endl; return; } long long a[3]; for (int i = 0; i < 2; i++) for (int j = 1; j < 3; j++) { a[0] = a[1] = a[2] = 0; if (i == j) { a[0] = -d1; a[2] = -d2; } else if (i == 0 && j == 1) { a[0] = d1; a[2] = -d2; } else { a[i] += d1; a[2] = a[1]; a[j] += d2; } long long mi = min(a[0], min(a[1], a[2])); a[0] -= mi; a[1] -= mi; a[2] -= mi; long long ma = max(a[0], max(a[1], a[2])); if ((a[0] + a[1] + a[2]) % 3 == k % 3 && a[0] + a[1] + a[2] <= k && 3 * ma + k - a[0] - a[1] - a[2] <= n) { cerr << a[0] << ' ' << a[1] << ' ' << a[2] << endl; cout << "yes" << endl; return; } } cout << "no" << endl; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) calc(); }
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, a, b, ans; void work() { scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2), ans = 0; a = k + 2 * d1 + d2, b = k - d1 - 2 * d2; if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; a = k + 2 * max(d1, d2) - min(d1, d2), b = k - d1 - d2; if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; a = k + d1 + d2, b = k - 2 * max(d1, d2) + min(d1, d2); if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; a = k + d1 + 2 * d2, b = k - 2 * d1 - d2; if ((n >= a) && ((n - a) % 3 == 0) && (b >= 0) && (b % 3 == 0)) ans++; if (ans) printf("yes\n"); else printf("no\n"); } int main() { int t; scanf("%d", &t); while (t--) work(); return 0; }
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 check(x,a,b,c): #print(x,a,b,c) n1=n//3 if n%3!=0 or (abs(n1-a)+abs(n1-b)+abs(n1-c))!=(n-k) or a+b+c!=k or a>n1 or b>n1 or c>n1 or a<0 or b<0 or c<0: return(False) #print(x,a,b,c) return(True) def fun(d1,d2,k): return((k+(2*d1)+d2)//3) for t in range(int(input())): n,k,d1,d2=map(int,input().split()) #d1,d2=max(d11,d22),min(d11,d22) flag=False x=fun(d1,d2,k) a1,b1,c1=x,x-d1,x-d1-d2 flag=flag or check(x,a1,b1,c1) x=fun(d1,-d2,k) a2,b2,c2=x,x-d1,x-d1+d2 flag=flag or check(x,a2,b2,c2) x=fun(-d1,d2,k) a3,b3,c3=x,x+d1,x+d1-d2 flag=flag or check(x,a3,b3,c3) x=fun(-d1,-d2,k) a4,b4,c4=x,x+d1,x+d1+d2 flag=flag or check(x,a4,b4,c4) if flag: 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
t = int( raw_input() ) for i in range( t ): n, k, d1, d2 = map( int, raw_input().split() ) if ( n % 3 == 0 ): r = n - k xs = [ ( 2 * d2 + d1, 2 * d1 + d2 ), ( max( d1, d2 ) + abs( d1 - d2 ), d1 + d2 ), ( d1 + d2, max( d1, d2 ) + abs( d1 - d2 ) ), ( 2 * d1 + d2, 2 * d2 + d1 ) ] ok = False for x in xs: if ( r >= x[0] ) and ( ( r - x[0] ) % 3 == 0 ) and ( k >= x[1] ): print 'yes' ok = True break if not ok: print 'no' 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; int main() { long long int t, n, k, d1, d2, i, j; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { long long int cd1 = d1 * (i == 0 ? 1 : -1); long long int cd2 = d2 * (j == 0 ? 1 : -1); long long int y = (k - cd2 + cd1) / 3; long long int x = y - cd1; long long int z = k - x - y; if (llabs(y - x) == d1 && llabs(y - z) == d2 && x >= 0 && y >= 0 && z >= 0) { if (x <= n / 3 && y <= n / 3 && z <= n / 3 && n % 3 == 0) { printf("yes\n"); i = j = 100; break; } } } } if (i < 10) { printf("no\n"); } } return 0; }
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.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Temp { FastScanner in; PrintWriter out; BufferedReader bf; class FastScanner { public boolean can(long a, long b, long c, long n, long k) { long rem = n - k; if (a + b + c > k) return false; if ((k - a - b - c) % 3 != 0) { return false; } if (a < 0 || b < 0 || c < 0) return false; long tot = a + b + c + rem; if (tot % 3 == 0) { long val = tot / 3; if (a <= val && b <= val && c <= val) return true; } return false; } public void Solve() { int N = ni(); for (int i = 0; i < N; i++) { long n = nl(); long k = nl(); long d1 = nl(); long d2 = nl(); long rem = n - k; boolean can = false; if (can(0, d1, d1 + d2, n, k) || can(0, d1, d1 - d2, n, k) || can(d1, 0, d2, n, k) || can(d2 - d1, d2, 0, n, k) || can(d2 + d1, d2, 0, n, k)) can = true; if (can) out.println("yes"); else out.println("no"); } } BufferedReader br; StringTokenizer st; public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String n() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } String nextL() { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int ni() { return Integer.parseInt(n()); } double nd() { return Double.parseDouble(n()); } long nl() { return Long.parseLong(n()); } } public void run() { in = new FastScanner(System.in); out = new PrintWriter(System.out); in.Solve(); out.close(); } public static void main(String[] args) { new Temp().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
import java.util.*; public class predict_outcome_of_the_game { static int t; static long n, k, d1, d2, a, b, c; static boolean w = false; public static void main( String[] args ) { Scanner in = new Scanner( System.in ); t = in.nextInt(); for ( int i = 0; i < t; i++ ) { n = in.nextLong(); k = in.nextLong(); d1 = in.nextLong(); d2 = in.nextLong(); w = false; for ( int j = -1; j < 2; j += 2 ) { for ( int k = -1; k < 2; k += 2 ) { a = 0; b = a + j * d1; c = b + k * d2; c(); } } if ( w && n % 3 == 0 ) System.out.println( "yes" ); else System.out.println( "no" ); } in.close(); System.exit( 0 ); } public static void c() { n(); if ( ( k - a - b - c ) % 3 != 0 ) return; long add = ( k - a - b - c ) / 3; if ( add < 0 ) return; a += add; b += add; c += add; if ( a <= n / 3 && b <= n / 3 && c <= n / 3 ) w = true; } public static void n() { if ( a < 0 ) { b -= a; c -= a; a = 0; } if ( b < 0 ) { a -= b; c -= b; b = 0; } if ( c < 0 ) { a -= c; b -= c; c = 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.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.InputMismatchException; import java.util.StringTokenizer; public class CodeG { static long n, k, d1, d2; static boolean caseOne() { long a = d1 + d2; long b = d2; long c = 0; long remaining = k - a - b - c; if (remaining < 0) { return false; } if(remaining % 3 != 0) { return false; } a += remaining / 3; b += remaining / 3; c += remaining / 3; remaining = n - k; long max = Math.max(a, b); max = Math.max(max, c); remaining -= max - a; remaining -= max - b; remaining -= max - c; if (Math.abs(a - b) != d1) { return false; } if (Math.abs(b - c) != d2) { return false; } a = max + remaining / 3; b = max + remaining / 3; c = max + remaining / 3; return remaining >= 0 && ((remaining % 3) == 0) && (a + b + c) == n; } static boolean caseTwo() { long a = 0; long b = d1; long c = d1 - d2; if (c < 0) { a -= c; b -= c; c = 0; } long remaining = k - a - b - c; if (remaining < 0) { return false; } if(remaining % 3 != 0) { return false; } a += remaining / 3; b += remaining / 3; c += remaining / 3; remaining = n - k; long max = Math.max(a, b); max = Math.max(max, c); remaining -= max - a; remaining -= max - b; remaining -= max - c; if (Math.abs(a - b) != d1) { return false; } if (Math.abs(b - c) != d2) { return false; } a = max + remaining / 3; b = max + remaining / 3; c = max + remaining / 3; return remaining >= 0 && ((remaining % 3) == 0) && (a + b + c) == n; } static boolean caseThree() { long a = 0; long b = d1; long c = d1 + d2; long remaining = k - a - b - c; if (remaining < 0) { return false; } if(remaining % 3 != 0) { return false; } a += remaining / 3; b += remaining / 3; c += remaining / 3; remaining = n - k; long max = Math.max(a, b); max = Math.max(max, c); remaining -= max - a; remaining -= max - b; remaining -= max - c; if (Math.abs(a - b) != d1) { return false; } if (Math.abs(b - c) != d2) { return false; } a = max + remaining / 3; b = max + remaining / 3; c = max + remaining / 3; return remaining >= 0 && ((remaining % 3) == 0) && (a + b + c) == n; } static boolean caseFour() { long a = d1; long b = 0; long c = d2; long remaining = k - a - b - c; if (remaining < 0) { return false; } if(remaining % 3 != 0) { return false; } a += remaining / 3; b += remaining / 3; c += remaining / 3; remaining = n - k; long max = Math.max(a, b); max = Math.max(max, c); remaining -= max - a; remaining -= max - b; remaining -= max - c; if (Math.abs(a - b) != d1) { return false; } if (Math.abs(b - c) != d2) { return false; } a = max + remaining / 3; b = max + remaining / 3; c = max + remaining / 3; return remaining >= 0 && ((remaining % 3) == 0) && (a + b + c) == n; } public static void main(String[] args) { Scanner sc = new SuperScanner(); int x = sc.nextInt(); for (int i = 0; i < x; i++) { n = sc.nextLong(); k = sc.nextLong(); d1 = sc.nextLong(); d2 = sc.nextLong(); System.out.println(caseOne() || caseTwo() || caseThree() || caseFour() ? "yes" : "no"); } } static class SuperScanner extends Scanner { private InputStream stream; private byte[] buf = new byte[8096]; private int curChar; private int numChars; public SuperScanner() { this.stream = System.in; } 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 static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private final StringBuilder sb = new StringBuilder(); @Override public String next() { int c = read(); while (isWhitespace(c)) { if (c == -1) { return null; } c = read(); } sb.setLength(0); do { sb.append((char) c); c = read(); } while (!isWhitespace(c)); return sb.toString(); } @Override public int nextInt() { int c = read(); while (isWhitespace(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 = (res << 3) + (res << 1); res += c - '0'; c = read(); } while (!isWhitespace(c)); return res * sgn; } @Override public long nextLong() { int c = read(); while (isWhitespace(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 = (res << 3) + (res << 1); res += c - '0'; c = read(); } while (!isWhitespace(c)); return res * sgn; } } static class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String nextLine() { try { return br.readLine(); } catch (Exception e) { throw (new RuntimeException()); } } public int nextIntOrQuit() { Integer n = nextInteger(); if (n == null) { System.exit(0); } return n.intValue(); } public String next() { while (!st.hasMoreTokens()) { String l = nextLine(); if (l == null) return null; st = new StringTokenizer(l); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < res.length; i++) res[i] = nextInt(); return res; } public long[] nextLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < res.length; i++) res[i] = nextLong(); return res; } public double[] nextDoubleArray(int n) { double[] res = new double[n]; for (int i = 0; i < res.length; i++) res[i] = nextDouble(); return res; } public void sortIntArray(int[] array) { Integer[] vals = new Integer[array.length]; for (int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for (int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortLongArray(long[] array) { Long[] vals = new Long[array.length]; for (int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for (int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortDoubleArray(double[] array) { Double[] vals = new Double[array.length]; for (int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for (int i = 0; i < array.length; i++) array[i] = vals[i]; } public String[] nextStringArray(int n) { String[] vals = new String[n]; for (int i = 0; i < n; i++) vals[i] = next(); return vals; } public Integer nextInteger() { String s = next(); if (s == null) return null; return Integer.parseInt(s); } public int[][] nextIntMatrix(int n, int m) { int[][] ans = new int[n][]; for (int i = 0; i < n; i++) ans[i] = nextIntArray(m); return ans; } public char[][] nextGrid(int r) { char[][] grid = new char[r][]; for (int i = 0; i < r; i++) grid[i] = next().toCharArray(); return grid; } public static <T> T fill(T arreglo, int val) { if (arreglo instanceof Object[]) { Object[] a = (Object[]) arreglo; for (Object x : a) fill(x, val); } else if (arreglo instanceof int[]) Arrays.fill((int[]) arreglo, val); else if (arreglo instanceof double[]) Arrays.fill((double[]) arreglo, val); else if (arreglo instanceof long[]) Arrays.fill((long[]) arreglo, val); return arreglo; } public <T> T[] nextObjectArray(Class<T> clazz, int size) { @SuppressWarnings("unchecked") T[] result = (T[]) java.lang.reflect.Array.newInstance(clazz, size); for (int c = 0; c < 3; c++) { Constructor<T> constructor; try { if (c == 0) constructor = clazz.getDeclaredConstructor(Scanner.class, Integer.TYPE); else if (c == 1) constructor = clazz.getDeclaredConstructor(Scanner.class); else constructor = clazz.getDeclaredConstructor(); } catch (Exception e) { continue; } try { for (int i = 0; i < result.length; i++) { if (c == 0) result[i] = constructor.newInstance(this, i); else if (c == 1) result[i] = constructor.newInstance(this); else result[i] = constructor.newInstance(); } } catch (Exception e) { throw new RuntimeException(e); } return result; } throw new RuntimeException("Constructor not found"); } public void printLine(int... vals) { if (vals.length == 0) System.out.println(); else { System.out.print(vals[0]); for (int i = 1; i < vals.length; i++) System.out.print(" ".concat(String.valueOf(vals[i]))); System.out.println(); } } public void printLine(long... vals) { if (vals.length == 0) System.out.println(); else { System.out.print(vals[0]); for (int i = 1; i < vals.length; i++) System.out.print(" ".concat(String.valueOf(vals[i]))); System.out.println(); } } public void printLine(double... vals) { if (vals.length == 0) System.out.println(); else { System.out.print(vals[0]); for (int i = 1; i < vals.length; i++) System.out.print(" ".concat(String.valueOf(vals[i]))); System.out.println(); } } public void printLine(int prec, double... vals) { if (vals.length == 0) System.out.println(); else { System.out.printf("%." + prec + "f", vals[0]); for (int i = 1; i < vals.length; i++) System.out.printf(" %." + prec + "f", vals[i]); System.out.println(); } } public Collection<Integer> wrap(int[] as) { ArrayList<Integer> ans = new ArrayList<Integer>(); for (int i : as) ans.add(i); return ans; } public int[] unwrap(Collection<Integer> collection) { int[] vals = new int[collection.size()]; int index = 0; for (int i : collection) vals[index++] = i; return vals; } } }
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; bool solve(long long k, long long d1, long long d2, long long m) { long long t2 = (k + d2 - d1) / 3; if ((k + d2 - d1) % 3 != 0) return false; long long t1 = d1 + t2; long long t3 = t2 - d2; long long maxV = max(t1, max(t2, t3)); long long need = 0; if (maxV == t1) { need = (maxV - t2) + (maxV - t3); } else if (maxV == t2) { need = (maxV - t1) + (maxV - t3); } else if (maxV == t3) { need = (maxV - t1) + (maxV - t2); } if (t1 < 0 || t2 < 0 || t3 < 0) return false; m -= k; m -= need; if (m < 0 || m % 3 != 0) { return false; } return true; } int main() { long long t; cin >> t; for (long long i = 0; i < t; i++) { long long k, d1, d2; cin >> n >> k >> d1 >> d2; if (solve(k, d1, d2, n)) cout << "yes\n"; else if (solve(k, -1 * d1, d2, n)) cout << "yes\n"; else if (solve(k, -1 * d1, -1 * d2, n)) cout << "yes\n"; else if (solve(k, d1, -1 * d2, n)) cout << "yes\n"; else { cout << "no\n"; } } return 0; }
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 d1, d2, tests, n, k, d, ans; long long tob; long long v1, v2, v3, has, need; long long up, dwn; void check() { long long temp = k - has; if (temp < 0 || temp % 3 > 0) return; long long rem = n - k; rem -= need; if (rem < 0) return; if (rem % 3) return; ans = 1; } int main() { ios_base::sync_with_stdio(0); cin >> tests; for (; tests; --tests) { cin >> n >> k >> d1 >> d2; ans = 0; for (int mask = 0; mask < 4; mask++) { v1 = 0; v2 = v1; if (mask & 1) v2 -= d1; else v2 += d1; v3 = v2; if (mask & 2) v3 -= d2; else v3 += d2; up = max(max(v1, v2), v3); dwn = min(min(v1, v2), v3); has = v1 + v2 + v3 - dwn * 3; need = up * 3 - v1 - v2 - v3; check(); } if (ans) cout << "yes" << endl; else cout << "no" << endl; } cin.get(); cin.get(); return 0; }
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.lang.*; import java.io.*; import java.awt.Point; // SHIVAM GUPTA : //NSIT //decoder_1671 // STOP NOT TILL IT IS DONE OR U DIE . // U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U HERE................ // ASCII = 48 + i ;// 2^28 = 268,435,456 > 2* 10^8 // log 10 base 2 = 3.3219 // odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3// even:: (x^2/4)+1 ,(x^2/4)-1 x >=4 // FOR ANY ODD NO N : N,N-1,N-2 //ALL ARE PAIRWISE COPRIME //THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS // two consecutive odds are always coprime to each other // two consecutive even have always gcd = 2 ; public class Main { // static int[] arr = new int[100002] ; // static int[] dp = new int[100002] ; static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } String next(){ while(st==null || !st.hasMoreElements()){ 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()); } String nextLine(){ String str = ""; try{ str=br.readLine(); } catch(IOException e){ e.printStackTrace(); } return str; } } //////////////////////////////////////////////////////////////////////////////////// public static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } ///////////////////////////////////////////////////////////////////////////////////////// public static int sumOfDigits(long n) { if( n< 0)return -1 ; int sum = 0; while( n > 0) { sum = sum + (int)( n %10) ; n /= 10 ; } return sum ; } ////////////////////////////////////////////////////////////////////////////////////////////////// public static long arraySum(int[] arr , int start , int end) { long ans = 0 ; for(int i = start ; i <= end ; i++)ans += arr[i] ; return ans ; } ///////////////////////////////////////////////////////////////////////////////// public static int mod(int x) { if(x <0)return -1*x ; else return x ; } public static long mod(long x) { if(x <0)return -1*x ; else return x ; } //////////////////////////////////////////////////////////////////////////////// public static void swapArray(int[] arr , int start , int end) { while(start < end) { int temp = arr[start] ; arr[start] = arr[end]; arr[end] = temp; start++ ;end-- ; } } ////////////////////////////////////////////////////////////////////////////////// public static int[][] rotate(int[][] input){ int n =input.length; int m = input[0].length ; int[][] output = new int [m][n]; for (int i=0; i<n; i++) for (int j=0;j<m; j++) output [j][n-1-i] = input[i][j]; return output; } /////////////////////////////////////////////////////////////////////////////////////////////////////// public static int countBits(long n) { int count = 0; while (n != 0) { count++; n = (n) >> (1L) ; } return count; } /////////////////////////////////////////// //////////////////////////////////////////////// public static boolean isPowerOfTwo(long n) { if(n==0) return false; if(((n ) & (n-1)) == 0 ) return true ; else return false ; } ///////////////////////////////////////////////////////////////////////////////////// public static int min(int a ,int b , int c, int d) { int[] arr = new int[4] ; arr[0] = a;arr[1] = b ;arr[2] = c;arr[3] = d;Arrays.sort(arr) ; return arr[0]; } ///////////////////////////////////////////////////////////////////////////// public static int max(int a ,int b , int c, int d) { int[] arr = new int[4] ; arr[0] = a;arr[1] = b ;arr[2] = c;arr[3] = d;Arrays.sort(arr) ; return arr[3]; } /////////////////////////////////////////////////////////////////////////////////// public static String reverse(String input) { StringBuilder str = new StringBuilder("") ; for(int i =input.length()-1 ; i >= 0 ; i-- ) { str.append(input.charAt(i)); } return str.toString() ; } /////////////////////////////////////////////////////////////////////////////////////////// public static boolean sameParity(long a ,long b ) { long x = a% 2L; long y = b%2L ; if(x==y)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static boolean isPossibleTriangle(int a ,int b , int c) { if( a + b > c && c+b > a && a +c > b)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////// static long xnor(long num1, long num2) { if (num1 < num2) { long temp = num1; num1 = num2; num2 = temp; } num1 = togglebit(num1); return num1 ^ num2; } static long togglebit(long n) { if (n == 0) return 1; long i = n; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return i ^ n; } /////////////////////////////////////////////////////////////////////////////////////////////// public static int xorOfFirstN(int n) { if( n % 4 ==0)return n ; else if( n % 4 == 1)return 1 ; else if( n % 4 == 2)return n+1 ; else return 0 ; } ////////////////////////////////////////////////////////////////////////////////////////////// public static int gcd(int a, int b ) { if(b==0)return a ; else return gcd(b,a%b) ; } public static long gcd(long a, long b ) { if(b==0)return a ; else return gcd(b,a%b) ; } //////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c , int d ) { int temp = lcm(a,b , c) ; int ans = lcm(temp ,d ) ; return ans ; } /////////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c ) { int temp = lcm(a,b) ; int ans = lcm(temp ,c) ; return ans ; } //////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a , int b ) { int gc = gcd(a,b); return (a*b)/gc ; } public static long lcm(long a , long b ) { long gc = gcd(a,b); return (a*b)/gc ; } /////////////////////////////////////////////////////////////////////////////////////////// static boolean isPrime(long n) { if(n==1) { return false ; } boolean ans = true ; for(long i = 2L; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } /////////////////////////////////////////////////////////////////////////// static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; public static void sieveOfEratosthenes() { // FALSE == prime // TRUE == COMPOSITE // FALSE== 1 // time complexity = 0(NlogLogN)== o(N) // gives prime nos bw 1 to N for(int i = 4; i<= sieve ; i++) { prime[i] = true ; i++ ; } for(int p = 3; p*p <= sieve; p++) { if(prime[p] == false) { for(int i = p*p; i <= sieve; i += p) prime[i] = true; } p++ ; } } /////////////////////////////////////////////////////////////////////////////////// public static void sortD(int[] arr , int s , int e) { sort(arr ,s , e) ; int i =s ; int j = e ; while( i < j) { int temp = arr[i] ; arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ; } return ; } public static void sortD(long[] arr , int s , int e) { sort(arr ,s , e) ; int i =s ; int j = e ; while( i < j) { long temp = arr[i] ; arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ; } return ; } ///////////////////////////////////////////////////////////////////////////////////////// public static long countSubarraysSumToK(long[] arr ,long sum ) { HashMap<Long,Long> map = new HashMap<>() ; int n = arr.length ; long prefixsum = 0 ; long count = 0L ; for(int i = 0; i < n ; i++) { prefixsum = prefixsum + arr[i] ; if(sum == prefixsum)count = count+1 ; if(map.containsKey(prefixsum -sum)) { count = count + map.get(prefixsum -sum) ; } if(map.containsKey(prefixsum )) { map.put(prefixsum , map.get(prefixsum) +1 ); } else{ map.put(prefixsum , 1L ); } } return count ; } /////////////////////////////////////////////////////////////////////////////////////////////// // KMP ALGORITHM : TIME COMPL:O(N+M) // FINDS THE OCCURENCES OF PATTERN AS A SUBSTRING IN STRING //RETURN THE ARRAYLIST OF INDEXES // IF SIZE OF LIST IS ZERO MEANS PATTERN IS NOT PRESENT IN STRING public static ArrayList<Integer> kmpAlgorithm(String str , String pat) { ArrayList<Integer> list =new ArrayList<>(); int n = str.length() ; int m = pat.length() ; String q = pat + "#" + str ; int[] lps =new int[n+m+1] ; longestPefixSuffix(lps, q,(n+m+1)) ; for(int i =m+1 ; i < (n+m+1) ; i++ ) { if(lps[i] == m) { list.add(i-2*m) ; } } return list ; } public static void longestPefixSuffix(int[] lps ,String str , int n) { lps[0] = 0 ; for(int i = 1 ; i<= n-1; i++) { int l = lps[i-1] ; while( l > 0 && str.charAt(i) != str.charAt(l)) { l = lps[l-1] ; } if(str.charAt(i) == str.charAt(l)) { l++ ; } lps[i] = l ; } } /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// // CALCULATE TOTIENT Fn FOR ALL VALUES FROM 1 TO n // TOTIENT(N) = count of nos less than ornequal to n and greater than 1 whose gcd with n is 1 // or n and the no will be coprime in nature //time : O(n*(log(logn))) public static void eulerTotientFunction(int[] arr ,int n ) { for(int i = 1; i <= n ;i++)arr[i] =i ; for(int i= 2 ; i<= n ;i++) { if(arr[i] == i) { arr[i] =i-1 ; for(int j =2*i ; j<= n ; j+= i ) { arr[j] = (arr[j]*(i-1))/i ; } } } return ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; int j=1; for(;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } /////////////////////////////////////////////////////////////////////////////////////////// public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } public static ArrayList<Long> allFactors(long n) { ArrayList<Long> list = new ArrayList<>() ; for(long i = 1L; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } //////////////////////////////////////////////////////////////////////////////////////////////////// static final int MAXN = 10000001; static int spf[] = new int[MAXN]; static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) spf[i] = i; for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } // The above code works well for n upto the order of 10^7. // Beyond this we will face memory issues. // Time Complexity: The precomputation for smallest prime factor is done in O(n log log n) // using sieve. // Where as in the calculation step we are dividing the number every time by // the smallest prime number till it becomes 1. // So, let’s consider a worst case in which every time the SPF is 2 . // Therefore will have log n division steps. // Hence, We can say that our Time Complexity will be O(log n) in worst case. static Vector<Integer> getFactorization(int x) { Vector<Integer> ret = new Vector<>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } ///////////////////////////////////////////////////////////////////////////////////////// public static long knapsack(int[] weight,long value[],int maxWeight){ int n= value.length ; //dp[i] stores the profit with KnapSack capacity "i" long []dp = new long[maxWeight+1]; //initially profit with 0 to W KnapSack capacity is 0 Arrays.fill(dp, 0); // iterate through all items for(int i=0; i < n; i++) //traverse dp array from right to left for(int j = maxWeight; j >= weight[i]; j--) dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]); /*above line finds out maximum of dp[j](excluding ith element value) and val[i] + dp[j-wt[i]] (including ith element value and the profit with "KnapSack capacity - ith element weight") */ return dp[maxWeight]; } /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// // to return max sum of any subarray in given array public static long kadanesAlgorithm(long[] arr) { long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long kadanesAlgorithm(int[] arr) { long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } /////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// public static long binarySerachGreater(int[] arr , int start , int end , int val) { // fing total no of elements strictly grater than val in sorted array arr if(start > end)return 0 ; //Base case int mid = (start + end)/2 ; if(arr[mid] <=val) { return binarySerachGreater(arr,mid+1, end ,val) ; } else{ return binarySerachGreater(arr,start , mid -1,val) + end-mid+1 ; } } ////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //TO GENERATE ALL(DUPLICATE ALSO EXIST) PERMUTATIONS OF A STRING // JUST CALL generatePermutation( str, start, end) start :inclusive ,end : exclusive //Function for swapping the characters at position I with character at position j public static String swapString(String a, int i, int j) { char[] b =a.toCharArray(); char ch; ch = b[i]; b[i] = b[j]; b[j] = ch; return String.valueOf(b); } //Function for generating different permutations of the string public static void generatePermutation(String str, int start, int end) { //Prints the permutations if (start == end-1) System.out.println(str); else { for (int i = start; i < end; i++) { //Swapping the string by fixing a character str = swapString(str,start,i); //Recursively calling function generatePermutation() for rest of the characters generatePermutation(str,start+1,end); //Backtracking and swapping the characters again. str = swapString(str,start,i); } } } //////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public static long factMod(long n, long mod) { if (n <= 1) return 1; long ans = 1; for (int i = 1; i <= n; i++) { ans = (ans * i) % mod; } return ans; } ///////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static long power(long x ,long n) { //time comp : o(logn) if(n==0)return 1L ; if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1) { ans = ans *x ; } n /= 2 ; x = x*x ; } return ans ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static long powerMod(long x, long n, long mod) { //time comp : o(logn) if(n==0)return 1L ; if(n==1)return x; long ans = 1; while (n > 0) { if (n % 2 == 1) ans = (ans * x) % mod; x = (x * x) % mod; n /= 2; } return ans; } ////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// /* lowerBound - finds largest element equal or less than value paased upperBound - finds smallest element equal or more than value passed if not present return -1; */ public static long lowerBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static int lowerBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static long upperBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } public static int upperBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } ////////////////////////////////////////////////////////////////////////////////////////// public static void printArray(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printArrayln(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printLArray(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printLArrayln(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printtwodArray(int[][] ans) { for(int i = 0; i< ans.length ; i++) { for(int j = 0 ; j < ans[0].length ; j++)out.print(ans[i][j] +" "); out.println() ; } out.println() ; } static long modPow(long a, long x, long p) { //calculates a^x mod p in logarithmic time. long res = 1; while(x > 0) { if( x % 2 != 0) { res = (res * a) % p; } a = (a * a) % p; x /= 2; } return res; } static long modInverse(long a, long p) { //calculates the modular multiplicative of a mod m. //(assuming p is prime). return modPow(a, p-2, p); } static long modBinomial(long n, long k, long p) { // calculates C(n,k) mod p (assuming p is prime). long numerator = 1; // n * (n-1) * ... * (n-k+1) for (int i=0; i<k; i++) { numerator = (numerator * (n-i) ) % p; } long denominator = 1; // k! for (int i=1; i<=k; i++) { denominator = (denominator * i) % p; } // numerator / denominator mod p. return ( numerator* modInverse(denominator,p) ) % p; } ///////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// static ArrayList<Integer>[] tree ; // static long[] child; // static int mod= 1000000007 ; // static int[][] pre = new int[3001][3001]; // static int[][] suf = new int[3001][3001] ; //program to calculate noof nodes in subtree for every vertex including itself public static void countNoOfNodesInsubtree(int child ,int par , int[] dp) { int count = 1 ; for(int x : tree[child]) { if(x== par)continue ; countNoOfNodesInsubtree(x,child,dp) ; count= count + dp[x] ; } dp[child] = count ; } public static void depth(int child ,int par , int[] dp , int d ) { dp[child] =d ; for(int x : tree[child]) { if(x== par)continue ; depth(x,child,dp,d+1) ; } } public static void dfs(int sv , boolean[] vis) { vis[sv] = true ; for(int x : tree[sv]) { if( !vis[x]) { dfs(x ,vis) ; } } } ////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// public static void solve() { FastReader scn = new FastReader() ; //Scanner scn = new Scanner(System.in); //int[] store = {2 ,3, 5 , 7 ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ; // product of first 11 prime nos is greater than 10 ^ 12; //ArrayList<Integer> list = new ArrayList<>() ; ArrayList<Long> list = new ArrayList<>() ; ArrayList<Long> listl = new ArrayList<>() ; ArrayList<Integer> lista = new ArrayList<>() ; ArrayList<Integer> listb = new ArrayList<>() ; //ArrayList<String> lists = new ArrayList<>() ; HashMap<Integer,Integer> map = new HashMap<>() ; //HashMap<Character,Integer> map = new HashMap<>() ; //HashMap<Long,Long> map = new HashMap<>() ; HashMap<Integer,Integer> map1 = new HashMap<>() ; HashMap<Integer,Integer> map2 = new HashMap<>() ; //HashMap<String,Integer> maps = new HashMap<>() ; //HashMap<Integer,Boolean> mapb = new HashMap<>() ; //HashMap<Point,Integer> point = new HashMap<>() ; // Set<Long> set = new HashSet<>() ; Set<Integer> set = new HashSet<>() ; //Set<Character> set = new HashSet<>() ; Set<Integer> setx = new HashSet<>() ; Set<Integer> sety = new HashSet<>() ; StringBuilder sb =new StringBuilder("") ; //Collections.sort(list); //if(map.containsKey(arr[i]))map.put(arr[i] , map.get(arr[i]) +1 ) ; //else map.put(arr[i],1) ; // if(map.containsKey(temp))map.put(temp , map.get(temp) +1 ) ; // else map.put(temp,1) ; //int bit =Integer.bitCount(n); // gives total no of set bits in n; // Arrays.sort(arr, new Comparator<Pair>() { // @Override // public int compare(Pair a, Pair b) { // if (a.first != b.first) { // return a.first - b.first; // for increasing order of first // } // return a.second - b.second ; //if first is same then sort on second basis // } // }); int testcases = 1; testcases = scn.nextInt() ; for(int testcase =1 ; testcase <= testcases ;testcase++) { //if(map.containsKey(arr[i]))map.put(arr[i],map.get(arr[i])+1) ;else map.put(arr[i],1) ; //if(map.containsKey(temp))map.put(temp,map.get(temp)+1) ;else map.put(temp,1) ; // int n = scn.nextInt() ;int m = scn.nextInt() ; // tree = new ArrayList[n] ; // for(int i = 0; i< n; i++) // { // tree[i] = new ArrayList<Integer>(); // } // for(int i = 0 ; i <= m-1 ; i++) // { // int fv = scn.nextInt()-1 ; int sv = scn.nextInt()-1 ; // tree[fv].add(sv) ; // tree[sv].add(fv) ; // } // boolean[] visited = new boolean[n] ; // int ans = 0 ; // for(int i = 0 ; i < n ; i++) // { // if(!visited[i]) // { // dfs(i , visited ) ; // ans++ ; // } // } long n= scn.nextLong() ; long k = scn.nextLong() ; long d1 = scn.nextLong() ; long d2 = scn.nextLong() ; if(n %3 !=0)out.println("no") ; else{ int[] arr1 ={1,-1} ; int[] arr2 = {1,-1} ; boolean ans = false ; for(int x : arr1) { for(int y : arr2) { long x1 = k+ (2*x*d1)+(y*d2) ; long x2 = k-(x*d1)+(y*d2) ; long x3 = k-(x*d1)-(2*y*d2) ; if(x1 % 3 != 0 || x2 % 3 != 0 || x3 % 3 != 0 )continue ; x1/=3;x2/=3 ;x3/=3 ; if(x1 <0 || x2 <0 ||x3 <0 || x1 >k ||x2 >k || x3 >k)continue ; if(x1 <=n/3 && x2 <=n/3 && x3 <=n/3) { ans = true ; } } } if(ans)out.println("yes") ;else out.println("no") ; } sb.delete(0 , sb.length()) ; list.clear() ;listb.clear() ; map.clear() ; map1.clear() ; map2.clear() ; set.clear() ;sety.clear() ; } // test case end loop out.flush() ; } // solve fn ends public static void main (String[] args) throws java.lang.Exception { solve() ; } } class Pair { int first ; int second ; @Override public String toString() { String ans = "" ; ans += this.first ; ans += " "; ans += this.second ; return ans ; } }
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 c { public static boolean solve(long total, long already, long firstD, long secondD) { if(total%3!=0) return false; //we know that a is (b plus or minus firstD) //and that c is (b plus or minus secondD) //this gives four cases //only test for success long a, b, c, res, f, s, max; for(long i = -1; i <= 1; i++) { for(long j = -1; j <=1; j++) { if(i == 0 || j == 0) continue; f = firstD*i; s = secondD*j; res = 0; b = ((already +f) +s)/3; if( ((already +f) +s)%3 != 0 || b > already) continue; a = b-f; c = b-s; if(a < 0 || b < 0 || c < 0) continue; if(a <= already && c <= already) if(a <= total/3 && b <= total/3 && c <= total/3) { if(Math.abs(a-b) == firstD && Math.abs(b-c) == secondD) return true; } } } return false; } public static void main(String[] args) { Scanner in = new Scanner(System.in); long test = in.nextLong(); long n, k, firstD, secondD; boolean res; for(long i = 0; i < test; i++) { n = in.nextLong(); k = in.nextLong(); firstD = in.nextLong(); secondD = in.nextLong(); res = solve(n, k, firstD, secondD); if(res) 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; long long n, k, d1, d2; bool check(long long a, long long b, long long c) { if (a > b) swap(a, b); if (b > c) swap(b, c); if (a > b) swap(a, b); if (a < 0) { b -= a, c -= a; a = 0; } if (a + b + c > k) return 0; if ((k - (a + b + c)) % 3) return 0; long long t = n - k - (2 * c - b - a); if (n - k - (2 * c - b - a) < 0) return 0; if (t % 3 == 0) return 1; return 0; } int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); bool flag = 0; if (check(0, d1, d1 + d2) || check(0, d1, d1 - d2) || check(0, -d1, -d1 + d2) || check(0, -d1, -d1 - d2)) flag = 1; if (flag) puts("yes"); else puts("no"); } return 0; }
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 GCC optimize("O2") using namespace std; const int MAX = 2e5 + 5; const long long MAX2 = 11; const int MOD = 1000000000 + 7; const long long INF = 20000; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; const double pi = acos(-1); long long tc, a, b, c, d, x[3]; bool ans; inline bool cek() { sort(x, x + 3); if (x[0] < 0 || 2 * x[2] - x[0] - x[1] > a || (a - 2 * x[2] + x[0] + x[1]) % 3 || x[0] + x[1] + x[2] > b || (x[0] + x[1] + x[2]) % 3 != b % 3) return 0; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> tc; while (tc--) { cin >> a >> b >> c >> d; if (c > d) c ^= d ^= c ^= d; a -= b; ans = 0; x[0] = 0, x[1] = c, x[2] = d; ans |= cek(); x[0] = 0, x[1] = c, x[2] = c + d; ans |= cek(); x[0] = 0, x[1] = d, x[2] = c + d; ans |= cek(); x[0] = 0, x[1] = c - d, x[2] = c; ans |= cek(); x[0] = 0, x[1] = d - c, x[2] = d; ans |= cek(); if (ans) cout << "yes\n"; else cout << "no\n"; } return 0; }
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 getAns(n, k, x, y): if n % 3 != 0: return 'no' if y < x: x, y = y, x # now y > x smallest = float('inf') temp = None for i in [-1, 1]: for j in [-1, 1]: if i == -1 and j == -1: if 2*y - x <= k and (((2*y - x) % 3) == (k % 3)): temp = x + y elif i == -1 and j == 1: if 2*x + y <= k and (((2*x + y) % 3) == (k % 3)): temp = 2*y + x elif i == 1 and j == -1: if 2*y + x <= k and (((2*y + x) % 3) == (k % 3)): temp = 2*x + y else: if x + y <= k and (((x + y) % 3) == (k % 3)): temp = 2*y - x if temp != None and temp < smallest: smallest = temp if n - k >= smallest: return 'yes' return 'no' T = input() for _ in xrange(T): N, K, D1, D2 = map(int, raw_input().split()) print getAns(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
#include <bits/stdc++.h> using namespace std; long long n, k; bool yes(long long a, long long b) { long long x; x = k - 2 * a - b; if (x < 0 || x % 3) return false; x /= 3; if (x < 0 || x + a < 0 || x + a + b < 0) return false; if (n % 3) return false; if (x > n / 3 || x + a > n / 3 || x + a + b > n / 3) return false; return true; } int main() { int cn; cin >> cn; while (cn--) { long long a, b; cin >> n >> k >> a >> b; if (yes(a, b) || yes(-a, b) || yes(a, -b) || yes(-a, -b)) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
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 t, n, k, d1, d2; int solve(long long a, long long b, long long c) { long long m = min(min(a, b), c); a -= m; b -= m; c -= m; if (a + b + c > k || (a + b + c - k) % 3) return 0; m = max(max(a, b), c); long long x = m - a + m - b + m - c; if (x <= n - k && (n - k - x) % 3 == 0) return 1; return 0; } int main() { ios::sync_with_stdio(false); cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; int ok = 0; ok |= solve(0, d1, d1 + d2); ok |= solve(-d1, 0, -d2); ok |= solve(0, -d1, -d1 - d2); ok |= solve(d1, 0, d2); cout << (ok ? "yes" : "no") << endl; } return 0; }
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 long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long n, m, d1, d2; long long a[5]; inline bool jud() { if ((m - a[1] - a[2]) % 3 || (m - a[1] - a[2]) < 0) return 0; long long t = (m - a[1] - a[2]) / 3; a[1] += t; a[2] += t; a[3] += t; t = n / 3; long long tot = n - m; for (int i = 1; i <= 3; i++) if (a[i] > t) return 0; else tot -= t - a[i]; if (tot != 0) return 0; return 1; } inline bool solve() { if (d1 < d2) swap(d1, d2); a[1] = d1; a[2] = d2; a[3] = 0; if (jud()) return 1; a[1] = d1; a[2] = d1 - d2; a[3] = 0; if (jud()) return 1; a[1] = d1 + d2; a[2] = d1; a[3] = 0; if (jud()) return 1; a[1] = d1 + d2; a[2] = d2; a[3] = 0; if (jud()) return 1; return 0; } int main() { int T = read(); while (T--) { n = read(); m = read(); d1 = read(); d2 = read(); if (n % 3 || !solve()) puts("no"); else puts("yes"); } return 0; }
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:102400000,102400000") using namespace std; bool judge(long long k, long long x) { if (k >= x && (k - x) % 3 == 0) return true; return false; } int main() { int T; scanf("%d", &T); while (T--) { long long n, k, d1, d2, a, b, c, flag = 0; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n - k >= (2 * d1 + d2) && (n - k - 2 * d1 - d2) % 3 == 0 && 3 * (d1 + d2) <= n && judge(k, 2 * d2 + d1)) flag = 1; if (d1 >= d2) { if (n - k >= (2 * d1 - d2) && (n - k - 2 * d1 + d2) % 3 == 0 && 3 * d1 <= n && judge(k, d1 + d2)) flag = 1; } else { if (n - k >= (2 * d2 - d1) && (n - k - 2 * d2 + d1) % 3 == 0 && 3 * d2 <= n && judge(k, d1 + d2)) flag = 1; } if (n - k >= (d1 + d2) && (n - k - d1 - d2) % 3 == 0 && d1 >= d2 && 3 * d1 <= n && judge(k, 2 * d1 - d2)) flag = 1; if (n - k >= (d1 + d2) && (n - k - d1 - d2) % 3 == 0 && d1 < d2 && 3 * d2 <= n && judge(k, 2 * d2 - d1)) flag = 1; if (n - k >= (2 * d2 + d1) && (n - k - 2 * d2 - d1) % 3 == 0 && 3 * (d1 + d2) <= n && judge(k, 2 * d1 + d2)) flag = 1; if (flag) puts("yes"); else puts("no"); } return 0; }
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 eps(1e-8); int t; long long n, k, d1, d2, x1, x2, x3; bool check(long long x) { if (0 <= x && x <= n / 3) { return true; } return false; } int main() { cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no" << endl; continue; } if ((k + d2 - d1) % 3 == 0 && (k + d2 + 2 * d1) % 3 == 0 && (k - 2 * d2 - d1) % 3 == 0) { x1 = (k + d2 + 2 * d1) / 3; x2 = (k + d2 - d1) / 3; x3 = (k - 2 * d2 - d1) / 3; if (x1 >= x2 && x2 >= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } if ((k - d1 - d2) % 3 == 0 && (k + 2 * d1 - d2) % 3 == 0 && (k - d1 + 2 * d2) % 3 == 0) { x1 = (k + 2 * d1 - d2) / 3; x2 = (k - d1 - d2) / 3; x3 = (k - d1 + 2 * d2) / 3; if (x1 >= x2 && x2 <= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } if ((k + d2 + d1) % 3 == 0 && (k + d2 - 2 * d1) % 3 == 0 && (k - 2 * d2 + d1) % 3 == 0) { x1 = (k + d2 - 2 * d1) / 3; x2 = (k + d2 + d1) / 3; x3 = (k - 2 * d2 + d1) / 3; if (x1 <= x2 && x2 >= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } if ((k + d1 - d2) % 3 == 0 && (k - 2 * d1 - d2) % 3 == 0 && (k + d1 + 2 * d2) % 3 == 0) { x1 = (k - 2 * d1 - d2) / 3; x2 = (k + d1 - d2) / 3; x3 = (k + d1 + 2 * d2) / 3; if (x1 <= x2 && x2 <= x3 && check(x1) && check(x2) && check(x3)) { cout << "yes" << endl; continue; } } cout << "no" << endl; } return 0; }
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.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Main { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Scanner sc = new Scanner(new BufferedReader(new InputStreamReader( System.in))); int N = sc.nextInt(); for (int i = 0; i < N; i++) { long n = sc.nextLong(); long k = sc.nextLong(); long d1 = sc.nextLong(); long d2 = sc.nextLong(); // a-b=d1 b-c=d2 boolean result = false; long x3 = k + (2 * d1) + (d2); if (x3 % 3 == 0 && n % 3 == 0) { x3 = x3 / 3; long part = n / 3; long a = x3; long b = x3 - d1; long c = x3 - d1 - d2; if (a >= 0 && b >= 0 && c >= 0) { a = part - a; b = part - b; c = part - c; if (a >= 0 && b >= 0 && c >= 0 && a + b + c == n - k) { result = true; } } } // a-b=d1 c-b=d2 x3 = k + (2 * d1) - (d2); if (x3 % 3 == 0 && n % 3 == 0) { x3 = x3 / 3; long part = n / 3; long a = x3; long b = x3 - d1; long c = x3 - d1 + d2; if (a >= 0 && b >= 0 && c >= 0) { a = part - a; b = part - b; c = part - c; if (a >= 0 && b >= 0 && c >= 0 && a + b + c == n - k) { result = true; } } } // b-a=d1 b-c=d2 x3 = k + d1 + (d2); if (x3 % 3 == 0 && n % 3 == 0) { x3 = x3 / 3; long part = n / 3; long a = x3 - d1; long b = x3; long c = x3 - d2; if (a >= 0 && b >= 0 && c >= 0) { a = part - a; b = part - b; c = part - c; if (a >= 0 && b >= 0 && c >= 0 && a + b + c == n - k) { result = true; } } } // b-a=d1 c-b=d2 x3 = k + d1 - d2; if (x3 % 3 == 0 && n % 3 == 0) { x3 = x3 / 3; long part = n / 3; long a = x3 - d1; long b = x3; long c = x3 + d2; if (a >= 0 && b >= 0 && c >= 0) { a = part - a; b = part - b; c = part - c; if (a >= 0 && b >= 0 && c >= 0 && a + b + c == n - k) { result = true; } } } if (result) { 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
t=int(input()) for j in range(t): inp=[int(n) for n in input().split()] n=inp[0] k=inp[1] d1=inp[2] d2=inp[3] if d2<d1: s=d1 d1=d2 d2=s if ((k>=2*d1+d2) and ((k-2*d1-d2)%3==0) and (n-k>=d1+2*d2) and ((n-k-d1-2*d2)%3==0)): print('yes') elif ((k>=2*d2+d1) and ((k-2*d2-d1)%3==0) and (n-k>=d2+2*d1) and ((n-k-d2-2*d1)%3==0)): print('yes') elif ((k>=d1+d2) and ((k-d1-d2)%3==0) and (n-k>=2*d2-d1) and ((n-k-2*d2+d1)%3==0)): print('yes') elif ((k>=2*d2-d1) and ((k-2*d2+d1)%3==0) and (n-k>=d1+d2) and ((n-k-d1-d2)%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
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.math.BigInteger; 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 shu_mj @ http://shu-mj.com */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { Scanner in; PrintWriter out; public void solve(int testNumber, Scanner in, PrintWriter out) { this.in = in; this.out = out; run(); } void run() { int t = in.nextInt(); while (t-- != 0) solve(); } private void solve() { long n = in.nextLong(); long k = in.nextLong(); long d1 = in.nextLong(); long d2 = in.nextLong(); if (n % 3 != 0) { out.println("no"); return ; } for (int i = -1; i <= 1; i += 2) { for (int j = -1; j <= 1; j += 2) { if (fit(n, k, i * d1, j * d2)) { // Algo.debug(n, k, d1, d2, i * d1, j * d2); out.println("yes"); return ; } } } out.println("no"); } private boolean fit(long n, long k, long d1, long d2) { if (k < d1 + d2) return false; if ((k - d1 - d2) % 3 != 0) return false; long x = (k - d1 - d2) / 3; long a = x + d1; long b = x; long c = x + d2; x = n / 3; if (a < 0 || b < 0 || c < 0 || a > x || b > x || c > x) return false; return true; } } class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } private void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return st.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
#include <bits/stdc++.h> using namespace std; void _fill_int(int* p, int val, int rep) { int i; for (i = 0; i < rep; i++) p[i] = val; } int T; signed long long N, K, D1, D2; void solve() { int f, i, j, k, l, x, y; cin >> T; for (i = 0; i < T; i++) { cin >> N >> K >> D1 >> D2; int ok = 0; for (j = 0; j < 4; j++) { x = (j % 2) ? 1 : -1; y = (j / 2) ? 1 : -1; signed long long t = K + x * D1 + y * D2; signed long long b = t / 3; signed long long a = b - x * D1; signed long long c = b - y * D2; if (t % 3 == 0 && a <= N / 3 && a >= 0 && b <= N / 3 && b >= 0 && c <= N / 3 && c >= 0) ok++; } if (N % 3 == 0 && ok) (void)printf("yes\n"); else (void)printf("no\n"); } } int main(int argc, char** argv) { string s; if (argc == 1) ios::sync_with_stdio(false); for (int i = 1; i < argc; i++) s += argv[i], s += '\n'; for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin); solve(); return 0; }
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.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.StringTokenizer; /** * 10 * * Y4 B1 R3 G5 R5 W3 W5 W2 R1 Y1 * * * @author pttrung */ public class A { public static int Mod; public static int min; /* ybshzefoxkqdigcjafs nffvaxdmditsolfxbyquira */ public static void main(String[] args) throws FileNotFoundException, IOException { Scanner in = new Scanner(); // PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt"))); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int z = 0; z < t; z++) { long n = in.nextLong(); long k = in.nextLong(); long d1 = in.nextLong(); long d2 = in.nextLong(); boolean ok = cal(d1, d2, n, k) || cal(d1, -d2, n, k) || cal(-d1, d2, n, k) || cal(-d1, -d2, n, k); if (ok) { out.println("yes"); } else { out.println("no"); } } out.close(); } public static boolean cal(long d1, long d2, long n, long k) { if (n % 3 != 0) { return false; } long need = n / 3; long val = k + d2 - d1; if (val % 3 == 0 && val >= 0) { long y = val / 3; long x = y + d1; long z = y - d2; if (need < y || need < x || need < z || x < 0 || y < 0 || z < 0) { return false; } } else { return false; } return true; } public static int[][] powSquareMatrix(int[][] A, long p) { int[][] unit = new int[A.length][A.length]; for (int i = 0; i < unit.length; i++) { unit[i][i] = 1; } if (p == 0) { return unit; } int[][] val = powSquareMatrix(A, p / 2); if (p % 2 == 0) { return mulMatrix(val, val); } else { return mulMatrix(A, mulMatrix(val, val)); } } public static int[][] mulMatrix(int[][] A, int[][] B) { int[][] result = new int[A.length][B[0].length]; for (int i = 0; i < result.length; i++) { for (int j = 0; j < result[0].length; j++) { long temp = 0; for (int k = 0; k < A[0].length; k++) { temp += ((long) A[i][k] * B[k][j] % Mod); temp %= Mod; } temp %= Mod; result[i][j] = (int) temp; } } return result; } static double dist(Point a, Point b) { long total = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); return Math.sqrt(total); } static class Point implements Comparable<Point> { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Point o) { if (x != o.x) { return x - o.x; } return y - o.y; } } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } static class FT { int[] data; FT(int n) { data = new int[n]; } void update(int index, int val) { // System.out.println("UPDATE INDEX " + index); while (index < data.length) { data[index] += val; index += index & (-index); // System.out.println("NEXT " +index); } } int get(int index) { // System.out.println("GET INDEX " + index); int result = 0; while (index > 0) { result += data[index]; index -= index & (-index); // System.out.println("BACK " + index); } return result; } } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int pow(int a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } int val = pow(a, b / 2); if (b % 2 == 0) { return val * val; } else { return val * val * a; } } // static Point intersect(Point a, Point b, Point c) { // double D = cross(a, b); // if (D != 0) { // return new Point(cross(c, b) / D, cross(a, c) / D); // } // return null; // } // // static Point convert(Point a, double angle) { // double x = a.x * cos(angle) - a.y * sin(angle); // double y = a.x * sin(angle) + a.y * cos(angle); // return new Point(x, y); // } // static Point minus(Point a, Point b) { // return new Point(a.x - b.x, a.y - b.y); // } // // static Point add(Point a, Point b) { // return new Point(a.x + b.x, a.y + b.y); // } // // static double cross(Point a, Point b) { // return a.x * b.y - a.y * b.x; // // // } // // static class Point { // // int x, y; // // Point(int x, int y) { // this.x = x; // this.y = y; // } // // @Override // public String toString() { // return "Point: " + x + " " + y; // } // } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { //System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new FileReader(new File("A-large-practice.in"))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
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 Bat-Orgil */ 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 T= in.nextInt(); FOR: for(int count=0; count<T; count++){ long n= in.nextLong(); long k =in.nextLong(); long d1= in.nextLong(); long d2= in.nextLong(); long s1; long s2; long s3; long[] D1= {d1,-d1}; long[] D2= {d2,-d2}; for(long dx : D1){ for(long dy:D2){ if( k >= (dx+dy) && (k-dx-dy)%3==0 && ((k-dx-dy)/3 >= -dx) && ((k-dx-dy)/3 >=-dy)){ s1= (k-dx-dy)/3; s2= s1 + dx; s3= s1+dy; long max= Math.max(Math.max(s1,s2),s3); long needed= (max-s1) + (max-s2) + (max-s3); if( (n-k) >= needed && (n-k-needed)%3==0){ out.println("yes"); continue FOR; } } } } out.println("no"); } } } 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 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.*; import java.util.*; public class CF_451C { public static void main(String[] args) throws IOException { new CF_451C().solve(); } boolean gameExist(long n, long k, long d1, long d2){ ArrayList<Long> al=new ArrayList<Long>(); long min=Math.min(d1, d2), max=Math.max(d1,d2); if (k>=max+(max-min) && (k-(2*max-min))%3==0) al.add(d1+d2); if (k>=2*d1+d2 && (k-(2*d1+d2))%3==0 ) al.add(d1+2*d2);//first negative if (k>=d1+2*d2 && (k-(d1+2*d2))%3==0) al.add(2*d1+d2); if (k>=d1+d2 && (k-(d1+d2))%3==0) al.add(2*max-min); // both up // System.out.println(al); for (Long games:al) if (n-k>=games && (n-k-games)%3==0){ // System.out.println(games); return true; } return false; } private void solve() throws IOException{ InputStream in = System.in; PrintStream out = System.out; // in = new FileInputStream("in.txt"); // out = new PrintStream("out.txt"); long mod=1_000_000_007; Scanner sc=new Scanner(in); int t=sc.nextInt(); for (int i=0;i<t;i++){ long n=sc.nextLong(),k=sc.nextLong(),d1=sc.nextLong(), d2=sc.nextLong(); if (gameExist(n,k,d1,d2)) 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.*; import java.util.*; public class Main { static FastScanner in; public static void main(String[] args) throws IOException { // System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.out")), true)); in = new FastScanner(System.in); // in = new FastScanner("input.txt"); int t = in.nextInt(); while (t-- > 0) { long n = in.nextLong(); long k = in.nextLong(); long d1 = in.nextLong(); long d2 = in.nextLong(); if (n % 3 != 0) { System.out.println("no"); } else { String ans = "no"; long cur = k - d1 + d2; long rem = n - k; long mm = n / 3; if (cur >= 0 && cur % 3 == 0) { long x2 = cur / 3; long x1 = d1 + x2; long x3 = x2 - d2; if (x1 >= 0 && x3 >= 0 && x1 <= mm && x2 <= mm && x3 <= mm) { ans = "yes"; } } if (ans.equals("yes")) { System.out.println(ans); continue; } cur = k - d1 - d2; if (cur >= 0 && cur % 3 == 0) { long x2 = cur / 3; long x1 = d1 + x2; long x3 = x2 + d2; if (x1 >= 0 && x3 >= 0 && x1 <= mm && x2 <= mm && x3 <= mm) { ans = "yes"; } } if (ans.equals("yes")) { System.out.println(ans); continue; } cur = k + d1 + d2; if (cur >= 0 && cur % 3 == 0) { long x2 = cur / 3; long x1 = x2 - d1; long x3 = x2 - d2; if (x1 >= 0 && x3 >= 0 && x1 <= mm && x2 <= mm && x3 <= mm) { ans = "yes"; } } if (ans.equals("yes")) { System.out.println(ans); continue; } cur = k + d1 - d2; if (cur >= 0 && cur % 3 == 0) { long x2 = cur / 3; long x1 = x2 - d1; long x3 = x2 + d2; if (x1 >= 0 && x3 >= 0 && x1 <= mm && x2 <= mm && x3 <= mm) { ans = "yes"; } } System.out.println(ans); } } } } class FastScanner { BufferedReader br; StringTokenizer tokenizer; FastScanner(String fileName) throws FileNotFoundException { this(new FileInputStream(new File(fileName))); } FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String nextLine() throws IOException { tokenizer = null; return br.readLine(); } String next() throws IOException { if (tokenizer == null || !tokenizer.hasMoreTokens()) { String line = br.readLine(); if (line == null) { return null; } tokenizer = new StringTokenizer(line); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } char nextChar() throws IOException { return next().charAt(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 static java.lang.System.in; import java.io.IOException; public class C258 { static byte[] buffer = new byte[8192]; static int offset = 0; static int bufferSize = 0; public static void main(String args[])throws IOException { long t= readLong(); while(t-->0) { long n = readLong(); long k = readLong(); long d1 = readLong(); long d2 = readLong(); if (n%3!=0 ) { System.out.println("no"); continue; } long sum = k+2*d1+d2; long val = sum/3; long win = n/3; long a = win -val; long b = a +d1; long c = a + d1+d2; long y = val - d1; long z = val -d1-d2; if (sum%3==0 && a>=0 && b >=0 && c>=0 && y>=0 && z>=0 && val>=0 ) { //System.out.println("1"); System.out.println("yes"); continue; } sum = k + 2*d1 -d2; val = sum/3; a = win -val; b = a +d1; c = a + d1-d2; y = val - d1; z = val -d1+d2; if (sum%3==0 && a>=0 && b >=0 && c>=0 && y>=0 && z>=0 && val>=0 ) { //System.out.println("2"); System.out.println("yes"); continue; } sum = k + d1 +d2; val = sum/3; a = win -val; b = a +d1; c = a +d2; y = val - d1; z = val -d2; if (sum%3==0 && a>=0 && b >=0 && c>=0 && y>=0 && z>=0 && val>=0 ) { //System.out.println("3"); System.out.println("yes"); continue; } sum = k + 2*d2 +d1; val = sum/3; a = win -val; b = a +d2; c = a +d2+d1; y = val - d2; z = val -d1-d2; if (sum%3==0 && a>=0 && b >=0 && c>=0 && y>=0 && z>=0 && val>=0 ) { //System.out.println("4"); System.out.println("yes"); continue; } System.out.println("no"); } } static long readLong() throws IOException{ long number = 0; long s=1; if(offset==bufferSize){ offset = 0; bufferSize = in.read(buffer); } for(;buffer[offset]<0x30; ++offset) { if (buffer[offset]=='-') s=-1; if(offset==bufferSize-1){ offset=-1; bufferSize = in.read(buffer); } } for(;offset<bufferSize && buffer[offset]>0x2f;++offset){ number = number*0x0a+buffer[offset]-0x30; if(offset==bufferSize-1){ offset = -1; bufferSize = in.read(buffer); } } ++offset; return number*s; } }
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; long long n, k, d1, d2; for (scanf("%d", &T); T--;) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } bool flag = false; if ((k - (d1 + d2)) % 3 == 0) { long long base = (k - d1 - d2) / 3; if (k - d1 - d2 >= 0) if (base + max(d1, d2) <= n / 3) flag = true; } if ((k - (2 * d1 + d2)) % 3 == 0) { long long base = (k - 2 * d1 - d2) / 3; if (k - 2 * d1 - d2 >= 0) if (base + d1 + d2 <= n / 3) flag = true; } if ((k - (d1 + 2 * d2)) % 3 == 0) { long long base = (k - d1 - 2 * d2) / 3; if (k - d1 - 2 * d2 >= 0) if (base + d1 + d2 <= n / 3) flag = true; } if ((k - (2 * d1 - d2)) % 3 == 0 && d1 >= d2) { long long base = (k - 2 * d1 + d2) / 3; if (k - 2 * d1 + d2 >= 0) if (base + d1 <= n / 3) flag = true; } if ((k - (2 * d2 - d1)) % 3 == 0 && d1 <= d2) { long long base = (k + d1 - 2 * d2) / 3; if (k + d1 - 2 * d2 >= 0) if (base + d2 <= n / 3) flag = true; } printf(flag ? "yes\n" : "no\n"); } return 0; }
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 i, j, _; long long n, k, d1, d2; long long win[3]; scanf("%d", &_); while (_--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } int flag = 0; for (i = 0; i < 4; i++) { if (i == 0) { win[0] = d1 + d2; win[1] = d2; win[2] = 0; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((d1 + d2 + (k - tmp) / 3) * 3 <= n) flag = 1; } if (i == 1) { win[0] = d1; win[1] = 0; win[2] = d2; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((max(d1, d2) + (k - tmp) / 3) * 3 <= n) flag = 1; } if (i == 2) { win[1] = max(d1, d2); win[0] = win[1] - d1; win[2] = win[1] - d2; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((max(d1, d2) + (k - tmp) / 3) * 3 <= n) flag = 1; } if (i == 3) { win[0] = 0; win[1] = d1; win[2] = d1 + d2; long long tmp = win[0] + win[1] + win[2]; if (tmp > k) continue; if ((k - tmp) % 3 != 0) continue; if ((d1 + d2 + (k - tmp) / 3) * 3 <= n) flag = 1; } } if (flag) printf("yes\n"); else printf("no\n"); } return 0; }
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; bool f(long long a, long long b) { long long p[3] = {}; long long r = k - b - a - b; if (r % 3 != 0) return false; p[2] = r / 3; p[1] = p[2] + b; p[0] = p[1] + a; if (p[1] < 0 || p[2] < 0 || p[0] < 0) return false; if (n % 3 != 0) return false; long long m = n / 3; return m >= p[0] && m >= p[1] && m >= p[2]; } int main() { long long t, a, b; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> k >> a >> b; if (f(a, b) || f(a, -b) || f(-a, b) || f(-a, -b)) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
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 dx[] = {1, 1, -1, -1}; int dy[] = {1, -1, -1, 1}; long long int n, k, d1, d2, rem; bool _solve(long long int w1, long long int w2, long long int w3) { long long int tar = n / 3; if (w1 < 0 || w2 < 0 || w3 < 0) return false; if (tar >= w1 && tar >= w2 && tar >= w3 && abs(w1 - w2) == d1 && abs(w2 - w3) == d2) return true; return false; } bool solve() { long long int _d1, w1, w2, w3, _d2; for (int i = 0; i < 4; i++) { _d1 = d1 * dx[i]; _d2 = d2 * dy[i]; w1 = (k + 2 * _d1 + _d2) / 3; w2 = w1 - _d1; w3 = w2 - _d2; if (w1 + w2 + w3 != k) continue; if (_solve(w1, w2, w3)) return true; } return false; } int main() { ios_base ::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 == 0) { if (solve()) cout << "yes" << endl; else cout << "no" << endl; } else cout << "no" << endl; } return 0; }
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, i, j, n, k, d1, d2; while (cin >> t) { while (t--) { scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2); if (n % 3) { puts("no"); continue; } int f = 0; if (d1 + 2 * d2 <= k) if ((k - (d1 + 2 * d2)) % 3 == 0) if ((n - k) >= (d2 + 2 * d1)) if ((n - k - (d2 + 2 * d1)) % 3 == 0) f = 1; if (d1 + d2 <= k) if ((k - (d1 + d2)) % 3 == 0) { if ((n - k) >= (max(d1, d2) + abs(d1 - d2))) { if ((n - k - (max(d1, d2) + abs(d1 - d2))) % 3 == 0) { f = 2; } } } long long tmp = max(d1, d2); long long x = (d1 > d2 ? d1 - d2 : d2 - d1); if (tmp + x <= k) { if ((k - tmp - x) % 3 == 0) { if (tmp - x + tmp <= n - k) { if ((n - k - (tmp - x + tmp)) % 3 == 0) f = 1; } } } if (2 * d1 + d2 <= k) if ((k - (2 * d1 + d2)) % 3 == 0) if ((n - k) >= (2 * d2 + d1)) if ((n - k - (2 * d2 + d1)) % 3 == 0) f = 4; if (f) puts("yes"); else puts("no"); } } }
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 check(n, k, d1, d2): if (k - d1 - d2) % 3 != 0: return False y = (k - d1 - d2) // 3 return min(y+d1, y, y+d2) >= 0 and max(y+d1, y, y+d2) <= n//3 t = int(input()) all_res = [] for _ in range(t): n, k, d1, d2 = map(int, input().split()) if n % 3 != 0: all_res += ['no'] elif check(n, k, d1, d2) or check(n, k, -d1, d2) or \ check(n, k, d1, -d2) or check(n, k, -d1, -d2): all_res += ['yes'] else: all_res += ['no'] print('\n'.join(all_res))
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
t = int(raw_input()) for i in range(t): n, k, d1, d2 = raw_input().split() n, k, d1, d2 = int(n), int(k), int(d1), int(d2) if n % 3 == 0: r = n - k vs = [ (2*d2 + d1, 2*d1 + d2), (max(d1, d2) + abs(d1 - d2), d1 + d2), (d1 + d2, max(d1, d2) + abs(d1 - d2)), (2*d1 + d2, 2*d2 + d1) ] y = False for v in vs: if (r - v[0] >= 0) and ((r - v[0]) % 3 == 0) and (k >= v[1]): print 'yes' y = True break if not y: print 'no' 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 solve(long long n, long long k, long long d1, long long d2) { n -= k; long long tmp = k - d1 - d1 - d2; if (tmp % 3 || tmp < 0) return false; long long a[3] = {tmp / 3, tmp / 3 + d1, tmp / 3 + d1 + d2}; sort(a, a + 3); if (a[0] < 0) return false; long long eq = a[2] - a[0] + a[2] - a[1]; if (eq <= n && ((n - eq) % 3) == 0) return true; else return false; } int main() { int t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (solve(n, k, d1, d2) || solve(n, k, -d1, d2) || solve(n, k, d1, -d2) || solve(n, k, -d1, -d2)) cout << "yes\n"; else cout << "no\n"; } return 0; }
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; vector<long long int> bin_s(long long int k, long long int d1, long long int d2, long long int a, long long int b) { long long int lo = 0, hi = k + 1; while (lo < hi) { long long int mid = (lo + hi) / 2; long long int x = mid + (a * d1); long long int y = mid; long long int z = mid + (b * d2); if (x >= 0 && y >= 0 && z >= 0 && (x + y + z) == k) return vector<long long int>({x, y, z}); else if (x < 0 || y < 0 || z < 0 || (x + y + z) < k) lo = mid + 1; else hi = mid; } return vector<long long int>({-1000}); } bool check(vector<long long int> A, long long int n) { sort(A.begin(), A.end()); n -= (A[2] - A[0]); n -= (A[2] - A[1]); return n >= 0 && (n % 3 == 0); } int main() { long long int t; cin >> t; while (t--) { long long int n, k, d1, d2; cin >> n >> k >> d1 >> d2; vector<long long int> A = bin_s(k, d1, d2, 1, 1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } A = bin_s(k, d1, d2, -1, 1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } A = bin_s(k, d1, d2, 1, -1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } A = bin_s(k, d1, d2, -1, -1); if ((A.size() == 3) && check(A, n - k)) { cout << "yes" << endl; continue; } cout << "no" << endl; } }
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.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class C { private void solve() throws IOException { int T = ni(); for (int t = 0; t < T; ++t) { long n = nl(); long k = nl(); long d1 = nl(); long d2 = nl(); long c = k - (d1 + d2 * 2); long c2 = n - k - (d2 + d1 * 2); if (c >= 0 && c % 3 == 0 && c2 >= 0 && c2 % 3 == 0) { prln("yes"); continue; } if (d2 <= d1) { c = k - (d1 + d2); c2 = n - k - (2 * d1 - d2); if (c >= 0 && c % 3 == 0 && c2 >= 0 && c2 % 3 == 0) { prln("yes"); continue; } } if (d2 >= d1) { c = k - (2 * d2 - d1); c2 = n - k - (d2 + d1); if (c >= 0 && c % 3 == 0 && c2 >= 0 && c2 % 3 == 0) { prln("yes"); continue; } } if (d1 >= d2) { c = k - (2 * d1 - d2); c2 = n - k - (d2 + d1); if (c >= 0 && c % 3 == 0 && c2 >= 0 && c2 % 3 == 0) { prln("yes"); continue; } } if (d2 >= d1) { c = k - (d1 + d2); c2 = n - k - (d2 * 2 - d1); if (c >= 0 && c % 3 == 0 && c2 >= 0 && c2 % 3 == 0) { prln("yes"); continue; } } c = k - (d2 + d1 * 2); c2 = n - k - (d2 * 2 + d1); if (c >= 0 && c % 3 == 0 && c2 >= 0 && c2 % 3 == 0) { prln("yes"); continue; } prln("no"); } } public static void main(String ... args) throws IOException { new C().run(); } private PrintWriter pw; private BufferedReader br; private StringTokenizer st; private void run() throws IOException { pw = new PrintWriter(System.out); br = new BufferedReader(new InputStreamReader(System.in)); solve(); br.close(); pw.close(); } private void pr(Object o) { pw.print(o); } private void prln(Object o) {pw.println(o);} private void prln() {pw.println();} private long nl() throws IOException { return Long.parseLong(nt()); } private double nd() throws IOException { return Double.parseDouble(nt()); } private int ni()throws IOException { return Integer.parseInt(nt()); } private String nt() throws IOException { if (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; bool isPossible(long long n, long long k, long long d1, long long d2) { int i, j; if (n % 3 != 0) return false; for (i = -1; i <= 1; i += 2) for (j = -1; j <= 1; j += 2) { long long D1 = d1 * i; long long D2 = d2 * j; if ((k - 2 * D2 - D1) % 3 != 0) continue; long long x[3]; x[2] = (k - 2 * D2 - D1) / 3; x[1] = x[2] + D2; x[0] = x[1] + D1; if ((x[2] >= 0 && x[2] <= k) && (x[1] >= 0 && x[1] <= k) && (x[0] >= 0 && x[0] <= k)) if (x[2] <= n / 3 && x[1] <= n / 3 && x[0] <= n / 3) return true; } return false; } int main(void) { int T; scanf("%i", &T); while (T--) { long long n, k, d1, d2; scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2); if (isPossible(n, k, d1, d2)) printf("yes\n"); else printf("no\n"); } return 0; }
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 tt = 0; tt < (T); ++tt) { long long n, k, d1, d2; cin >> n, cin >> k, cin >> d1, cin >> d2; long long cur; bool ok = false; cur = 2 * d1 + d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a + d1; long long c = a + d1 + d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } cur = 2 * d1 - d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a + d1; long long c = a + d1 - d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } cur = -2 * d1 + d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a - d1; long long c = a - d1 + d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } cur = -2 * d1 - d2; if (k - cur >= 0 and (k - cur) % 3 == 0) { long long a = (k - cur) / 3; long long b = a - d1; long long c = a - d1 - d2; if (a >= 0 and b >= 0 and c >= 0) { long long maxx = max(a, max(b, c)); long long req = (maxx - a) + (maxx - b) + (maxx - c); if ((n - k) >= req and (n - k - req) % 3 == 0) { ok = true; } } } puts(ok ? "yes" : "no"); } return 0; }
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 i in range(t): n, k, a, b = map(int, input().split()) if n % 3 != 0: print("no") else: for i in range(2): for j in range(2): flagf = False if i == 0: a1 = a else: a1 = -a if j == 0: b1 = b else: b1 = -b t2 = (k - a1 + b1)/3 t1 = a1 + t2 t3 = t2 - b1 # print(t1, t2, t3) flag1 = False flag2 = False # valores vΓ‘lidos if (k-a1+b1) % 3 == 0 and t1 >= 0 and t2 >= 0 and t3 >= 0: if t1 <= n/3 and t2 <= n/3 and t3 <= n/3: if i == 0 and t1 >= t2: flag1 = True elif i == 1 and t1 < t2: flag1 = True if j == 0 and t2 >= t3: flag2 = True elif j == 1 and t2 < t3: flag2 = True if flag1 and flag2: if t1 == t2 and t2 == t3 and (n-k) % 3 == 0: flagf = True break else: v = [t1, t2, t3] v = sorted(v) faltam = n-k faltam -= v[2] - v[1] faltam -= v[2] - v[0] if faltam < 0: break elif faltam == 0 or faltam %3 == 0: flagf = True break if flagf: print("yes") break else: print("no") # 3 1 1 0
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; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; int main() { int t; cin >> t; for (int i = 0, _n = (t); i < _n; i++) { long long n, k, d1, d2; cin >> n >> k; cin >> d1 >> d2; long long t1, t2, t3; t2 = k - d1 + d2; if (t2 >= 0 && t2 % 3 == 0) { t2 /= 3; t3 = t2 - d2; long long m = n - k - (d1 + d2 + d1); if (t3 >= 0 && m >= 0 && m % 3 == 0) { puts("yes"); continue; } } t2 = k - d1 - d2; if (t2 >= 0 && t2 % 3 == 0) { long long m = n - k - (max(d1, d2) + abs(d1 - d2)); if (m >= 0 && m % 3 == 0) { puts("yes"); continue; } } t2 = k + d1 + d2; if (t2 >= 0 && t2 % 3 == 0) { t2 /= 3; long long m = n - k - (d1 + d2); t1 = t2 - d1; t3 = t2 - d2; if (t1 >= 0 && t3 >= 0 && m >= 0 && m % 3 == 0) { puts("yes"); continue; } } t2 = k + d1 - d2; if (t2 >= 0 && t2 % 3 == 0) { t2 /= 3; long long m = n - k - (d1 + d2 + d2); t1 = t2 - d1; if (t1 >= 0 && m >= 0 && m % 3 == 0) { puts("yes"); continue; } } puts("no"); } return 0; }
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 _ in xrange(int(raw_input())): n, k, d1, d2 = map(int, raw_input().split()) if n%3 != 0: print "no" continue s = [[1, 1], [1, -1], [-1, 1], [-1, -1]] done = False for i in s: D1 = d1*i[0] D2 = d2*i[1] if (k+2*D1+D2)%3 != 0: continue a = (k+2*D1+D2)/3 c = a-D1-D2 b = c+D2 nreach = n/3 if (a>=0 and a<=k and b>=0 and b<= k and c>=0 and c<=k) : if a>nreach or b>nreach or c>nreach: continue if (nreach*3 - (a+b+c) == n-k): done = True break print "yes" if done 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
#include <bits/stdc++.h> using namespace std; int ans; long long n, k, d1, d2, a, b, c; void gettin(long long x, long long y) { if (n % 3) return; long long aa, bb, cc; if ((k - x - y) < 0) return; if ((k - x - y) % 3) return; aa = (k - x - y) / 3; bb = aa + x; cc = aa + y; if (aa > n / 3) return; if (aa < 0) return; if (bb > n / 3) return; if (cc > n / 3) return; if (bb < 0) return; if (cc < 0) return; ans = 1; } int main() { int t; scanf("%d", &t); while (t--) { ans = 0; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); gettin(d1, d1 + d2); gettin(d1, d1 - d2); gettin(-d1, -d1 + d2); gettin(-d1, -d1 - d2); if (ans) puts("yes"); else puts("no"); } }
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; void solve() { scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); if (d1 < d2) swap(d1, d2); long long mi[] = {d1 + d2, d1 + 2 * d2, d1 * 2 - d2, d2 + 2 * d1}; long long ned[] = {d1 * 2 - d2, d1 * 2 + d2, d1 + d2, d2 * 2 + d1}; long long has = n - k; for (int i = (0); i < (4); ++i) if (k >= mi[i] && (k - mi[i]) % 3 == 0 && has >= ned[i] && (has - ned[i]) % 3 == 0) { puts("yes"); return; } puts("no"); } int main() { int t; scanf("%d", &t); for (int i = (0); i < (t); ++i) solve(); return 0; }
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.IOException; import java.io.InputStreamReader; import java.util.PriorityQueue; import java.util.StringTokenizer; public class c11 { public static void main(String[] args)throws Exception { StringBuilder b=new StringBuilder(); int n=in(); while (n-->0){ b.append(solve(ll(),ll(),ll(),ll())+"\n"); } System.out.println(b); } private static String solve(long n, long k, long d1, long d2) { boolean x=can(n,k,d1,d2) || can(n,k,d1,-d2) || can(n,k,-d1,d2) || can(n,k,-d1,-d2); return x?"yes":"no"; } private static boolean can(long n, long k, long d1, long d2) { if (n%3!=0)return false; if ((k+2*d1-d2)%3!=0)return false; long x=(k+2*d1-d2)/3; long y=x-d1; long z=x-(d1-d2); long x1=(n/3)-x; long y1=(n/3)-y; long z1=(n/3)-z; return x>=0 && y>=0 && z>=0 && x1>=0 && y1>=0 && z1>=0; } static BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static int in() throws IOException{ if (st==null || !st.hasMoreTokens()){ st=new StringTokenizer(buf.readLine()); } return Integer.parseInt(st.nextToken()); } static long ll() throws IOException{ if (st==null || !st.hasMoreTokens()){ st=new StringTokenizer(buf.readLine()); } return Long.parseLong(st.nextToken()); } static String str() throws IOException{ if (st==null || !st.hasMoreTokens()){ st=new StringTokenizer(buf.readLine()); } return (st.nextToken()); } static double dub() throws IOException{ if (st==null || !st.hasMoreTokens()){ st=new StringTokenizer(buf.readLine()); } return Double.parseDouble(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; long long total, n, k, d1, d2; int main() { cin >> total; while (total--) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } long long tar = n / 3; if ((d2 + k - d1) % 3 == 0) { long long x1 = (d2 + k - d1) / 3; long long x2 = d1 + x1; long long x3 = x1 - d2; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } if ((k - d1 - d2) % 3 == 0) { long long x2 = (k - d1 - d2) / 3; long long x1 = d1 + x2; long long x3 = d2 + x2; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } if ((k + d1 - d2) % 3 == 0) { long long x1 = (d1 + k - d2) / 3; long long x2 = d2 + x1; long long x3 = x1 - d1; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } if ((k + d1 + d2) % 3 == 0) { long long x2 = (k + d1 + d2) / 3; long long x1 = x2 - d1; long long x3 = x2 - d2; if (x1 >= 0 && x1 <= tar && x2 >= 0 && x2 <= tar && x3 >= 0 && x3 <= tar) { cout << "yes\n"; continue; } } cout << "no\n"; } return 0; }
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 t, n, k, d1, d2; int main() { cin >> t; for (__typeof(t) i = 0; i < (t); i++) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } bool flag = false; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 || j == 0) continue; long long td1 = d1 * i, td2 = d2 * j; long long num = k - td1 + td2; if (num % 3 != 0) continue; long long n2 = num / 3; if (0 <= n2 && n2 <= k) { long long n1 = td1 + n2, n3 = n2 - td2; if (n1 >= 0 && n1 <= k && n3 >= 0 && n3 <= k && n1 <= n / 3 && n2 <= n / 3 && n3 <= n / 3) { flag = 1; break; } } } if (flag) break; } if (flag) { cout << "yes\n"; } else cout << "no\n"; } }
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 doall(): t = int(input()) def solve(n, k, d1, d2): if n % 3 == 0: r = n - k a = [[0, d1, d1 + d2], [0, d1, d1 - d2], [0, -d1, -d1 + d2], [0, -d1, -d1 - d2]] for now in a: mn = min(now) sumn = sum(now) sumb = sumn - 3 * min(now) if k < sumb or (k - sumb) % 3 != 0: continue w = max(now) tmp = 3 * w - sumn if tmp <= r and (r - tmp) % 3 == 0: return True return False ans = [] for i in range(t): n, k, d1, d2 = list(map(int, input().split())) if solve(n, k, d1, d2): ans.append('yes') else: ans.append('no') print('\n'.join(ans)) doall()
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; const long long N = 10; bool valid(long long x, long long y, long long z) { if (x < 0) return false; if (y < 0) return false; if (z < 0) return false; return true; } int main() { long long t; cin >> t; while (t--) { long long n, k, d1, d2, diff, sum, mx; long long i, w1, w2, w3; bool flag = false; cin >> n >> k >> d1 >> d2; diff = n - k; if (n % 3) { cout << "no\n"; continue; } for (i = 1; i <= 4; i++) { if (i == 1) { w2 = (k - d1 - d2) / 3; w1 = w2 + d1; w3 = w2 + d2; } if (i == 2) { w2 = (k + d1 - d2) / 3; w1 = w2 - d1; w3 = w2 + d2; } if (i == 3) { w2 = (k - d1 + d2) / 3; w1 = w2 + d1; w3 = w2 - d2; } if (i == 4) { w2 = (k + d1 + d2) / 3; w1 = w2 - d1; w3 = w2 - d2; } mx = max({w1, w2, w3}); if (valid(w1, w2, w3)) { sum = mx * 3 - w1 - w2 - w3; flag |= (sum <= diff && !((diff - sum) % 3)); } } cout << (flag ? ("yes\n") : ("no\n")); } return 0; }
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; class C_ {}; template <typename T> C_& operator<<(C_& __m, const T& __s) { if (!1) cerr << "\E[91m" << __s << "\E[0m"; return __m; } C_ merr; struct __s { __s() { if (1) { ios_base::Init i; cin.sync_with_stdio(0); cin.tie(0); } } ~__s() { merr << "Execution time: " << fixed << setprecision(3) << (double)clock() / CLOCKS_PER_SEC << " s.\n"; } } __S; int main(void) { int t; cin >> t; for (int tt = 0; tt < t; tt++) { long long n, k, d1, d2; long long left; long long d3; long long a, b, c; cin >> n >> k >> d1 >> d2; if (n % 3) { } d3 = (d1 - d2 >= 0 ? d1 - d2 : d2 - d1); left = n - k; a = max(d1, d2); b = a - d1, c = a - d2; if (k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d1 + d2 <= left && (left - (d1 + d2)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = d1 + d2; b = 0, c = d2; if (k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d1 + d1 + d2 <= left && (left - (d1 + d1 + d2)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = max(d1, d3); b = a - d1, c = a - d3; if (abs(b - c) == d2 && (b <= min(a, c) || b >= max(a, c)) && k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d1 + d3 <= left && (left - (d1 + d3)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = d1 + d2; b = 0, c = d1; if (k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d2 + d1 + d2 <= left && (left - (d2 + d1 + d2)) % 3 == 0) { cout << "yes" << '\n'; continue; } a = max(d2, d3); b = a - d2, c = a - d3; if (abs(b - c) == d1 && (b <= min(a, c) || b >= max(a, c)) && k >= a + b + c && (k - (a + b + c)) % 3 == 0 && d2 + d3 <= left && (left - (d2 + d3)) % 3 == 0) { cout << "yes" << '\n'; continue; } cout << "no" << '\n'; } return 0; }
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 = input() def solve(a, b, c, left): if min([a, b, c]) < 0: return False M = max([a, b, c]) left -= (M-a) + (M-b) + (M-c) if left < 0: return False if left == 0: return True if left % 3 != 0: return False return True for test in range(0, t): n, k, d1, d2 = map(int, raw_input().split()) left = n-k a = [(k - 2*d1 - d2) / 3.0, (k - 2*d1 + d2) / 3.0, (k + 2*d1 - d2) / 3.0, (k + 2*d1 + d2) / 3.0] possible = False if a[0] == int(a[0]) and a[0] >= 0: possible |= solve(a[0], a[0]+d1, a[0]+d1+d2, left) if a[1] == int(a[1]) and a[1] >= 0: possible |= solve(a[1], a[1]+d1, a[1]+d1-d2, left) if a[2] == int(a[2]) and a[2] >= 0: possible |= solve(a[2], a[2]-d1, a[2]-d1+d2, left) if a[3] == int(a[3]) and a[3] >= 0: possible |= solve(a[3], a[3]-d1, a[3]-d1-d2, left) print "yes" if possible 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
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (d1 < d2) swap(d1, d2); if (k >= d1 + d1 + d2 && (k - (d1 + d1 + d2)) % 3 == 0 && n - k >= d2 + d1 + d2 && (n - k - (d2 + d1 + d2)) % 3 == 0) { cout << "yes" << endl; } else if (k >= d1 + d1 - d2 && (k - (d1 + d1 - d2)) % 3 == 0 && n - k >= d1 + d2 && (n - k - (d1 + d2)) % 3 == 0) { cout << "yes" << endl; } else if (k >= d1 + d2 + d2 && (k - (d1 + d2 + d2)) % 3 == 0 && n - k >= d2 + d1 + d1 && (n - k - (d2 + d1 + d1)) % 3 == 0) { cout << "yes" << endl; } else if (k >= d1 + d2 && (k - (d1 + d2)) % 3 == 0 && n - k >= d1 + d1 - d2 && (n - k - (d1 + d1 - d2)) % 3 == 0) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
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; template <class T> inline T gcd(T a, T b) { return (b) == 0 ? (a) : gcd((b), ((a) % (b))); } template <class T> inline T lcm(T a, T b) { return ((a) / gcd((a), (b)) * (b)); } template <class T> inline T BigMod(T Base, T power, T M = 1000000007) { if (power == 0) return 1; if (power & 1) return ((Base % M) * (BigMod(Base, power - 1, M) % M)) % M; else { T y = BigMod(Base, power / 2, M) % M; return (y * y) % M; } } template <class T> inline T ModInv(T A, T M = 1000000007) { return BigMod(A, M - 2, M); } int fx[] = {-1, +0, +1, +0, +1, +1, -1, -1, +0}; int fy[] = {+0, -1, +0, +1, +1, -1, +1, -1, +0}; int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k, d1, d2; int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; long long x = max(d1, d2); if (x > n / 3 || n % 3 != 0) { cout << "no" << endl; continue; } x += abs(d1 - d2); if (n - x - k >= 0 && (n - x - k) % 3 == 0 && max(d1, d2) + (n - x - k) / 3 <= n / 3) { cout << "yes" << endl; continue; } x = d1 + d2 + d1; if (n - x - k >= 0 && (n - x - k) % 3 == 0 && (d1 + d2 + (n - x - k) / 3) <= n / 3) { cout << "yes" << endl; continue; } x = d1 + d2; if (n - x - k >= 0 && (n - x - k) % 3 == 0 && max(d1, d2) + (n - x - k) / 3 <= n / 3) { cout << "yes" << endl; continue; } x = d1 + d2 + d2; if (n - x - k >= 0 && (n - x - k) % 3 == 0 && (d1 + d2 + (n - x - k) / 3) <= n / 3) { cout << "yes" << endl; continue; } cout << "no" << endl; } return 0; }
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.Arrays; import java.util.Scanner; public class Main { private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int t = scanner.nextInt(); for(int i = 0; i < t; i++){ Long n, k, d1, d2; n = scanner.nextLong(); k = scanner.nextLong(); d1 = scanner.nextLong(); d2 = scanner.nextLong(); Long remGames = n - k; if(remGames == 0 && (d1 != 0 || d2 != 0)){ System.out.println("no"); continue; } int f = 0; Long fop = d1 + d2; // r - x r r - x if((remGames - fop) >= 0 && (remGames - fop) % 3 == 0){ Long begin = Math.max(d1, d2) + Math.max(d1, d2) - d1 + Math.max(d1, d2) - d2; if(begin <= k){ Long diff = k - begin; if(diff % 3 == 0){ f = 1; } } } Long sop = d1 + 2 * d2; // r - x r r + x if((remGames - sop) >= 0 && (remGames - sop) % 3 == 0){ Long begin = d1 + d1 + d2; if(begin <= k) { Long diff = k - begin; if (diff % 3 == 0) { f = 1; } } } Long top = d2 + 2 * d1; // r + x r r - x if ((remGames - top) >= 0 && (remGames - top) % 3 == 0) { Long begin = d2 + d2 + d1; if(begin <= k) { Long diff = k - begin; if (diff % 3 == 0) { f = 1; } } } Long foop = Math.max(d1, d2) + (Math.max(d1, d2) - d1) + (Math.max(d1, d2) - d2); // r + x r r + x if((remGames - foop) >= 0 && (remGames - foop) % 3 == 0){ Long begin = d1 + d2; if(begin <= k){ Long diff = k - begin; if(diff % 3 == 0){ f = 1; } } } if(f == 0){ System.out.println("no"); } else { System.out.println("yes"); } } } public static void print(int[] arr){ System.out.println(Arrays.toString(arr)); } public static void print(int item){ System.out.println(item); } public static void print(String s){ System.out.println(s); } }
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; const int inf = 2000000000; static inline int Rint() { struct X { int dig[256]; X() { for (int i = '0'; i <= '9'; ++i) dig[i] = 1; dig['-'] = 1; } }; static X fuck; int s = 1, v = 0, c; for (; !fuck.dig[c = getchar()];) ; if (c == '-') s = 0; else if (fuck.dig[c]) v = c ^ 48; for (; fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48)) ; return s ? v : -v; } template <typename T> static inline void cmax(T& a, const T& b) { if (b > a) a = b; } template <typename T> static inline void cmin(T& a, const T& b) { if (b < a) a = b; } const int maxn = 100005; long long check0(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = 2 * d1 + d2 + k; if (temp % 3 != 0) return 0; long long w1 = temp / 3; long long w2 = w1 - d1; long long w3 = w2 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } long long check1(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = d1 + d2 + k; if (temp % 3 != 0) return 0; long long w1 = temp / 3; long long w2 = w1 - d1; long long w3 = w1 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } long long check2(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = k - d1 - d2; if (temp % 3 != 0) return 0; long long w2 = temp / 3; long long w1 = w2 + d1; long long w3 = w2 + d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } long long check3(long long n, long long k, long long d1, long long d2) { long long dest = n / 3; long long temp = d1 + 2 * d2 + k; if (temp % 3 != 0) return 0; long long w1 = temp / 3; long long w2 = w1 - d1; long long w3 = w2 - d2; if (w1 < 0 || w2 < 0 || w3 < 0) return 0; if (w1 > dest || w2 > dest || w3 > dest) return 0; return 1; } int main() { int q = Rint(); while (q--) { long long n, k, d1, d2; scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { puts("no"); continue; } if (check0(n, k, d1, d2) || check1(n, k, d1, d2) || check2(n, k, d1, d2) || check3(n, k, d1, d2)) puts("yes"); else puts("no"); } return 0; }
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 t, ti, d1, d2, n, k, p, ok = 0, maxim, v; int check(long long s1, long long s2, long long s3) { if (s1 + s2 + s3 > k) return 0; if ((k - s1 - s2 - s3) % 3 != 0) return 0; maxim = s1; if (s2 > maxim) maxim = s2; if (s3 > maxim) maxim = s3; v = 3 * maxim - s1 - s2 - s3; if (v > n - k) return 0; if ((n - k - v) % 3 != 0) return 0; ok = 1; return 1; } int main() { cin >> t; for (ti = 1; ti <= t; ti++) { cin >> n >> k >> d1 >> d2; ok = 0; check(d1, 0, d2); check(d1 + d2, d2, 0); check(0, d1, d1 + d2); p = d1; if (d2 > d1) p = d2; check(-d1 + p, p, -d2 + p); if (ok) cout << "yes\n"; else cout << "no\n"; } return 0; }
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 t, i; long long n, k, d1, d2; bool solve(long long _d1, long long _d2) { long long y = k - _d1 - _d2; if (y % 3) return false; long long x = y; if (x < 0 || x > n) return false; x = 3 * _d1 + y; if (x < 0 || x > n) return false; x = 3 * _d2 + y; if (x < 0 || x > n) return false; return true; } int main() { cin >> t; for (i = 0; i < t; ++i) { cin >> n >> k >> d1 >> d2; if (n % 3 || (!solve(d1, d2) && !solve(d1, -d2) && !solve(-d1, d2) && !solve(-d1, -d2))) printf("no\n"); else printf("yes\n"); } return 0; }
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; int T; bool check(long long a, long long b, long long c) { if (a + b + c > k) return 0; if ((k - a - b - c) % 3) return 0; if (a < b) swap(a, b); if (a < c) swap(a, c); long long nd = a - b + a - c; if (nd > n - k) return 0; if ((n - k - nd) % 3) return 0; return 1; } int main() { scanf("%d", &T); while (T--) { scanf("%I64d", &n), scanf("%I64d", &k), scanf("%I64d", &d1), scanf("%I64d", &d2); bool ok = 0; if (check(d1 + d2, d2, 0)) ok = 1; if (check(d1, 0, d2)) ok = 1; if (d1 > d2 && check(0, d1, d1 - d2)) ok = 1; if (d2 >= d1 && check(d2 - d1, d2, 0)) ok = 1; if (check(0, d1, d1 + d2)) ok = 1; if (ok) printf("yes\n"); else printf("no\n"); } return 0; }
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; class Codeforce { public: bool task(long long n, long long k, long long d1, long long d2) { if (n % 3 != 0) return false; long long t = n / 3; if (d1 > t || d2 > t) return false; if (((k - d1 + d2) % 3 == 0 && (k - d1 + d2) / 3 >= 0 && (k - d1 + d2) / 3 <= t) && ((k - d1 - 2 * d2) % 3 == 0 && (k - d1 - 2 * d2) / 3 >= 0 && (k - d1 - 2 * d2) / 3 <= t)) { if ((k + 2 * d1 + d2) % 3 == 0 && (k + 2 * d1 + d2) / 3 >= 0 && (k + 2 * d1 + d2) / 3 <= t) return true; } if (((k - d1 - d2) % 3 == 0 && (k - d1 - d2) / 3 >= 0 && (k - d1 - d2) / 3 <= t) && ((k - d1 + 2 * d2) % 3 == 0 && (k - d1 + 2 * d2) / 3 >= 0 && (k - d1 + 2 * d2) / 3 <= t)) { if ((k + 2 * d1 - d2) % 3 == 0 && (k + 2 * d1 - d2) / 3 >= 0 && (k + 2 * d1 - d2) / 3 <= t) return true; } if (((k + d1 + d2) % 3 == 0 && (k + d1 + d2) / 3 >= 0 && (k + d1 + d2) / 3 <= t) && ((k + d1 - 2 * d2) % 3 == 0 && (k + d1 - 2 * d2) / 3 >= 0 && (k + d1 - 2 * d2) / 3 <= t)) { if ((k - 2 * d1 + d2) % 3 == 0 && (k - 2 * d1 + d2) / 3 >= 0 && (k - 2 * d1 + d2) / 3 <= t) return true; } if (((k + d1 - d2) % 3 == 0 && (k + d1 - d2) / 3 >= 0 && (k + d1 - d2) / 3 <= t) && ((k + d1 + 2 * d2) % 3 == 0 && (k + d1 + 2 * d2) / 3 >= 0 && (k + d1 + 2 * d2) / 3 <= t)) { if ((k - 2 * d1 - d2) % 3 == 0 && (k - 2 * d1 - d2) / 3 >= 0 && (k - 2 * d1 - d2) / 3 <= t) return true; } return false; } }; int main() { Codeforce cf = Codeforce(); int t; long long n, k, d1, d2; scanf("%d", &t); while (t--) { cin >> n >> k >> d1 >> d2; if (cf.task(n, k, d1, d2)) printf("yes\n"); else printf("no\n"); } return 0; }
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 warning(disable : 4996) using namespace std; const int IT_MAX = 131072; const long long MOD = 1000000007; const int INF = 2034567891; const long long LL_INF = 1234567890123456789ll; int main() { int T; scanf("%d", &T); for (int tc = 1; tc <= T; tc++) { long long N, K, d1, d2, i, j; scanf("%I64d %I64d %I64d %I64d", &N, &K, &d1, &d2); if (N % 3 != 0) { printf("no\n"); continue; } for (i = -1; i <= 1; i += 2) { for (j = -1; j <= 1; j += 2) { long long t1 = d1 * i, t2 = d2 * j; long long x1, x2, x3; if ((K - t1 - 2 * t2) % 3 != 0 || (K - t1 - 2 * t2) < 0) continue; x3 = (K - t1 - 2 * t2) / 3; x2 = x3 + t2; x1 = x2 + t1; if (max(max(x1, x2), x3) <= N / 3 && min(min(x1, x2), x3) >= 0) break; } if (j <= 1) break; } if (i <= 1) printf("yes\n"); else printf("no\n"); } return 0; }
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.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { static class Scanner{ BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tk; public Scanner(){ rd = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException{ while(tk == null || !tk.hasMoreTokens()) tk = new StringTokenizer(rd.readLine()); return tk.nextToken(); } int nextInt() throws NumberFormatException, IOException{ return Integer.valueOf(next()); } long nextLong() throws NumberFormatException, IOException{ return Long.valueOf(next()); } double nextDouble() throws NumberFormatException, IOException{ return Double.valueOf(next()); } } static int T; static long N, K, D1, D2; static boolean compute(long a, long b, long c, long queda){ long min = Math.min(a, Math.min(b, c)); if (min < 0){ a += -min; b += -min; c += -min; } long viejos = K - a - b - c; if (viejos < 0 || (viejos % 3 != 0)) return false; long max = Math.max(Math.max(a, b), c); long diff = 3*max - a - b - c; if (diff > queda) return false; queda -= diff; return ((queda % 3) == 0); } public static void main(String args[]) throws NumberFormatException, IOException{ Scanner sc = new Scanner(); T = sc.nextInt(); for(int i = 0; i < T; i++){ N = sc.nextLong(); K = sc.nextLong(); D1 = sc.nextLong(); D2 = sc.nextLong(); long queda = N - K; boolean t1 = compute( D1, 0, D2, queda); boolean t2 = compute( D1, 0, -D2, queda); boolean t3 = compute(-D1, 0, D2, queda); boolean t4 = compute( -D1, 0, -D2, queda); if (t1 || t2 || t3 || t4) 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; bool Check(long long n, long long k, long long d1, long long d2) { if ((k + d1 + d2) % 3) return false; long long w2 = (k + d1 + d2) / 3, w1 = w2 - d1, w3 = w2 - d2; return w1 >= 0 && w1 <= n && w2 >= 0 && w2 <= n && w3 >= 0 && w3 <= n; } int main() { int cas; cin >> cas; while (cas--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (n % 3) { puts("no"); continue; } n /= 3; bool ok = false; for (int i = -1; i <= 1; ++i) { if (i == 0) continue; for (int j = -1; j <= 1; ++j) { if (j == 0) continue; if (Check(n, k, d1 * i, d2 * j)) { ok = true; break; } } if (ok) break; } puts(ok ? "yes" : "no"); } return 0; }
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 f[4][2] = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; int main() { int t; scanf("%d", &t); while (t--) { long long n, k, _d1, _d2; scanf("%I64d%I64d%I64d%I64d", &n, &k, &_d1, &_d2); if (n % 3 != 0) { printf("no\n"); continue; } bool flag = false; for (int i = 0; i < 4; i++) { long long d1 = f[i][0] * _d1; long long d2 = f[i][1] * _d2; long long a = (k + d1 * 2 + d2) / 3; long long b = a - d1; long long c = b - d2; if (a >= 0 && a <= k && b >= 0 && b <= k && c >= 0 && c <= k && a + b + c == k) if (a <= n / 3 && b <= n / 3 && c <= n / 3) { printf("yes\n"); flag = true; break; } } if (!flag) printf("no\n"); } return 0; }
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 C implements Runnable { @Override public void run() { try { int[] x = new int[]{1, 1, -1, -1}; int[] y = new int[]{1, -1, 1, -1}; Scanner in = new Scanner(System.in); int testNum = in.nextInt(); for (int test = 0; test < testNum; test++) { long n = in.nextLong(); long k = in.nextLong(); long[] d = new long[2]; d[0] = in.nextLong(); d[1] = in.nextLong(); if (n % 3 != 0) { System.out.println("no"); continue; } long m = n / 3; boolean ok = false; for (int i = 0; i < x.length; i++) { long l = k - x[i] * d[0] - y[i] * d[1]; if (l < 0 || l % 3 != 0) continue; l = l / 3; long l1 = l + x[i] * d[0]; long l2 = l + y[i] * d[1]; if (l1 < 0 || l2 < 0) continue; ok |= l <= m && l1 <= m && l2 <= m; } if (ok) { System.out.println("yes"); } else { System.out.println("no"); } } } catch (Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) { new Thread(new C()).start(); } }
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.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.InputMismatchException; import java.util.*; import java.io.*; public class Main{ public static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 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 String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } 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 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 String readString() { 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 c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter w = new PrintWriter(outputStream); int t=sc.nextInt(); while(t-->0) { long n=sc.nextLong(); long k=sc.nextLong(); long d1=sc.nextLong(); long d2=sc.nextLong(); long have=n-k; long dx[]={-1, +1}; long check=0; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { long a=0; long b=dx[i]*d1; long c=b+dx[j]*d2; long reqd=3*Math.max(a,Math.max(b,c)) - a - b - c; long played=a + b + c - 3*Math.min(a,Math.min(b,c)); if(have>=reqd && (have-reqd)%3==0 && k>=played && (k-played)%3==0) { check=1; } } } if(check==1) w.println("yes"); else { w.println("no"); } } w.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; template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } long long N, K, D1, D2; bool check(long long a, long long b, long long c) { if (N % 3) return false; long long temp = a + b + c; temp = K - temp; if (temp % 3) return false; temp /= 3; long long cur_a = temp + a; long long cur_b = temp + b; long long cur_c = temp + c; ; ; ; if (cur_a < 0 || cur_a > N / 3) return false; if (cur_b < 0 || cur_b > N / 3) return false; if (cur_c < 0 || cur_c > N / 3) return false; return true; } void solve() { scanf("%I64d %I64d %I64d %I64d", &N, &K, &D1, &D2); if (check(0, D1, D1 + D2) || check(0, D1, D1 - D2) || check(0, -D1, -D1 + D2) || check(0, -D1, -D1 - D2)) { printf("yes\n"); } else { printf("no\n"); } } int main() { int test; scanf("%d", &test); for (int tt = 0; tt < test; tt++) { solve(); } return 0; }
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.*; /** * @author master_j * @version 0.4 * @since May 3, 2014 */ public class Main { long n, k, n3; private void solve() throws IOException { int t = io.nI(); for(int i = 0; i < t; i++){ n = io.nL(); k = io.nL(); n3 = n / 3; io.wln(solve(io.nL(), io.nL()) ? "yes" : "no"); } }//2.2250738585072012e-308 private boolean solve(long d10, long d20){ if(n % 3 != 0) return false; if(d10 != 0 && d20 != 0) { for (long d1 = -d10; d1 <= d10; d1 += 2 * d10) for (long d2 = -d20; d2 <= d20; d2 += 2 * d20) if (solve0(d1, d2)) return true; return false; } if(d10 == 0 && d20 != 0) { for (long d2 = -d20; d2 <= d20; d2 += 2 * d20) if (solve0(0, d2)) return true; return false; } if(d10 != 0 && d20 == 0) { for (long d1 = -d10; d1 <= d10; d1 += 2 * d10) if (solve0(d1, 0)) return true; return false; } return solve0(0, 0); } private boolean solve0(long d1, long d2){ long x1 = (k - (2*d1 + d2)); if(x1 % 3 != 0) return false; x1 /= 3; long x2 = x1 + d1; long x3 = x2 + d2; if(x1 < 0 || x2 < 0 || x3 < 0) return false; if(x1 + x2 + x3 != k) return false; long k1 = n / 3 - x1; long k2 = n / 3 - x2; long k3 = n / 3 - x3; if(k1 < 0 || k2 < 0 || k3 < 0) return false; if(k1 + k2 + k3 != n - k) return false; return true; } public static void main(String[] args) throws IOException { IO.launchSolution(args); } Main(IO io) throws IOException { this.io = io; solve(); } private final IO io; } class IO { static final String _localArg = "master_j"; private static final String _problemName = ""; private static final IO.Mode _inMode = Mode.STD_; private static final IO.Mode _outMode = Mode.STD_; private static final boolean _autoFlush = false; static enum Mode {STD_, _PUT_TXT, PROBNAME_} private final StreamTokenizer st; private final BufferedReader br; private final Reader reader; private final PrintWriter pw; private final Writer writer; static void launchSolution(String[] args) throws IOException { boolean local = (args.length == 1 && args[0].equals(IO._localArg)); IO io = new IO(local); long nanoTime = 0; if (local) { nanoTime -= System.nanoTime(); io.wln("<output>"); } io.flush(); new Main(io); io.flush(); if(local){ io.wln("</output>"); nanoTime += System.nanoTime(); final long D9 = 1000000000, D6 = 1000000, D3 = 1000; if(nanoTime >= D9) io.wf("%d.%d seconds\n", nanoTime/D9, nanoTime%D9); else if(nanoTime >= D6) io.wf("%d.%d millis\n", nanoTime/D6, nanoTime%D6); else if(nanoTime >= D3) io.wf("%d.%d micros\n", nanoTime/D3, nanoTime%D3); else io.wf("%d nanos\n", nanoTime); } io.close(); } IO(boolean local) throws IOException { if(_inMode == Mode.PROBNAME_ || _outMode == Mode.PROBNAME_) if(_problemName.length() == 0) throw new IllegalStateException("You imbecile. Where's my <_problemName>?"); if(_problemName.length() > 0) if(_inMode != Mode.PROBNAME_ && _outMode != Mode.PROBNAME_) throw new IllegalStateException("You imbecile. What's the <_problemName> for?"); Locale.setDefault(Locale.US); if (local) { reader = new FileReader("input.txt"); writer = new OutputStreamWriter(System.out); } else { switch (_inMode) { case STD_: reader = new InputStreamReader(System.in); break; case PROBNAME_: reader = new FileReader(_problemName + ".in"); break; case _PUT_TXT: reader = new FileReader("input.txt"); break; default: throw new NullPointerException("You imbecile. Gimme _inMode."); } switch (_outMode) { case STD_: writer = new OutputStreamWriter(System.out); break; case PROBNAME_: writer = new FileWriter(_problemName + ".out"); break; case _PUT_TXT: writer = new FileWriter("output.txt"); break; default: throw new NullPointerException("You imbecile. Gimme _outMode."); } } br = new BufferedReader(reader); st = new StreamTokenizer(br); pw = new PrintWriter(writer, _autoFlush); if(local && _autoFlush) wln("Note: auto-flush is on."); } void wln() {pw.println(); } void wln(boolean x) {pw.println(x);} void wln(char x) {pw.println(x);} void wln(char x[]) {pw.println(x);} void wln(double x) {pw.println(x);} void wln(float x) {pw.println(x);} void wln(int x) {pw.println(x);} void wln(long x) {pw.println(x);} void wln(Object x) {pw.println(x);} void wln(String x) {pw.println(x);} void wf(String f, Object...o){pw.printf(f, o);} void w(boolean x) {pw.print(x);} void w(char x) {pw.print(x);} void w(char x[]) {pw.print(x);} void w(double x) {pw.print(x);} void w(float x) {pw.print(x);} void w(int x) {pw.print(x);} void w(long x) {pw.print(x);} void w(Object x) {pw.print(x);} void w(String x) {pw.print(x);} int nI() throws IOException {st.nextToken(); return (int)st.nval;} double nD() throws IOException {st.nextToken(); return st.nval;} float nF() throws IOException {st.nextToken(); return (float)st.nval;} long nL() throws IOException {st.nextToken(); return (long)st.nval;} String nS() throws IOException {st.nextToken(); return st.sval;} void wc(String x){ wc(x.toCharArray()); } void wc(char c1, char c2){for(char c = c1; c<=c2; c++)wc(c);} void wc(char x[]){ for(char c : x) wc(c); } void wc(char x){st.ordinaryChar(x); st.wordChars(x, x);} public boolean eof() {return st.ttype == StreamTokenizer.TT_EOF;} public boolean eol() {return st.ttype == StreamTokenizer.TT_EOL;} void flush(){pw.flush();} void close() throws IOException{reader.close(); br.close(); flush(); pw.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; int main() { int T; long long n, k, d1, d2, a, b, c, fuck; scanf("%d", &T); while (T--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } long long ave = n / 3; a = d1; b = 0; c = d2; fuck = (k - a - c) / 3; if (a + b + c <= k && (k - a - c) % 3 == 0 && a + fuck >= 0 && c + fuck >= 0 && fuck >= 0 && fuck <= ave && a <= ave - fuck && c <= ave - fuck) { printf("yes\n"); continue; } a = d1 + d2; b = d2; c = 0; fuck = (k - a - b) / 3; if (a + b + c <= k && (k - a - b) % 3 == 0 && a + fuck >= 0 && b + fuck >= 0 && fuck <= ave && fuck >= 0 && a <= ave - fuck && b <= ave - fuck) { printf("yes\n"); continue; } a = 0; b = d1; c = d1 + d2; fuck = (k - b - c) / 3; if (a + b + c <= k && (k - b - c) % 3 == 0 && fuck <= ave && fuck >= 0 && b + fuck >= 0 && c + fuck >= 0 && c <= ave - fuck && b <= ave - fuck) { printf("yes\n"); continue; } a = 0; b = d1; c = d1 - d2; fuck = (k - c - b) / 3; if (a + b + c <= k && (k - c - b) % 3 == 0 && fuck <= ave && fuck >= 0 && b + fuck <= ave && b + fuck >= 0 && c + fuck <= ave && c + fuck >= 0) { printf("yes\n"); continue; } c = 0; b = d2; a = d2 - d1; fuck = (k - b - a) / 3; if (a + b + c <= k && (k - b - a) % 3 == 0 && fuck <= ave && fuck >= 0 && b + fuck <= ave && b + fuck >= 0 && a + fuck <= ave && a + fuck >= 0) { printf("yes\n"); continue; } printf("no\n"); } return 0; }
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.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { int t = nextInt(); for (int i = 0; i < t; i++) { long n = nextLong(); long k = nextLong(); long d1 = nextLong(); long d2 = nextLong(); boolean can = solve(d1, d2, k, n); can = can || solve(-d1, d2, k, n); can = can || solve(d1, -d2, k, n); can = can || solve(-d1, -d2, k, n); if(can) System.out.println("yes"); else System.out.println("no"); } } private static boolean solve(long xy, long yz, long k, long n) { long y3 = k - xy + yz; if (y3 % 3 != 0) return false; long y = y3 / 3; long x = y + xy, z = y - yz; if (x < 0 || y < 0 || z < 0) return false; long max = Math.max(Math.max(x, y),z); long rem = n-k-(max-x)-(max-y)-(max-z); if(rem < 0 || rem%3 != 0) return false; return true; } static BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); static StringTokenizer tokenizer = null; static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.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 main() { int t; scanf("%d", &t); while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; long long p = n - k; if (p >= 2 * d1 + d2 && k >= d1 + 2 * d2) { if ((p - (2 * d1 + d2)) % 3 == 0 && (k - d1 - 2 * d2) % 3 == 0) { printf("yes\n"); continue; } } if (p >= d1 + d2 && k >= (max(d1, d2) + abs(d1 - d2))) { if ((p - d1 - d2) % 3 == 0 && (k - (max(d1, d2) + abs(d1 - d2))) % 3 == 0) { printf("yes\n"); continue; } } if (p >= (max(d1, d2) + abs(d1 - d2)) && k >= d1 + d2) { if ((p - (max(d1, d2) + abs(d1 - d2))) % 3 == 0 && (k - (d1 + d2)) % 3 == 0) { printf("yes\n"); continue; } } if (p >= (d1 + 2 * d2) && k >= 2 * d1 + d2) { if ((p - (d1 + 2 * d2)) % 3 == 0 && (k - 2 * d1 - d2) % 3 == 0) { printf("yes\n"); continue; } } printf("no\n"); } return 0; }
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, x, y, z, tmp, r; void doit() { scanf("%I64d%I64d%I64d%I64d", &n, &K, &d1, &d2); if (n % 3) { printf("no\n"); return; } for (int key1 = -1; key1 <= 1; key1 += 2) for (int key2 = -1; key2 <= 1; key2 += 2) { tmp = K - key1 * d1 - key2 * d2; if (tmp % 3 || tmp < 0) continue; x = tmp / 3; y = x + key1 * d1; z = x + key2 * d2; if (y < 0 || z < 0) continue; if (x + y + z > K) continue; r = max(max(x, y), z); tmp = K; K += 3 * r - x - y - z; if (K <= n && (n - K) % 3 == 0) { printf("yes\n", x, y, z); return; } K = tmp; } printf("no\n"); } int main() { int T; scanf("%d", &T); while (T--) { doit(); } }
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> const int mod = 1000000007; using namespace std; long long x, y, z, t, n, k, d1, d2; ; bool check() { if (x >= 0 && y >= 0 && z >= 0 && x % 3 == 0 && y % 3 == 0 && z % 3 == 0) { x /= 3; y /= 3; z /= 3; long long mx = max(max(x, y), z); if ((n - (mx - x) - (mx - y) - (mx - z)) >= 0 && (n - (mx - x) - (mx - y) - (mx - z)) % 3 == 0) { cout << "yes" << endl; return 1; } } return 0; } int main() { cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; n -= k; x = 2 * d1 + d2 + k; y = k - d1 + d2; z = k - d1 - 2 * d2; if (check()) continue; x = d2 - 2 * d1 + k; y = k + d1 + d2; z = k + d1 - 2 * d2; if (check()) continue; x = 2 * d1 - d2 + k; y = k - d1 - d2; z = k + 2 * d2 - d1; if (check()) continue; x = k - 2 * d1 - d2; y = k - d2 + d1; z = k + 2 * d2 + d1; if (check()) continue; cout << "no" << endl; } return 0; }
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 nxt() { int res; scanf("%d", &res); return res; } long long n, k; bool ok(long long s1, long long s2, long long s3) { if (k - s1 - s2 - s3 < 0 || (k - s1 - s2 - s3) % 3 != 0) { return false; } long long m = max(s1, max(s2, s3)); long long first = n - (k + 3 * m - s1 - s2 - s3); if (first < 0 || first % 3 != 0) { return false; } return true; } inline void solve() { long long d1, d2; cin >> n >> k >> d1 >> d2; long long d = max(d1, d2); if (ok(0, d1, d1 + d2) || ok(d1 + d2, d2, 0) || ok(d1, 0, d2) || ok(d - d1, d, d - d2)) { puts("yes"); } else { puts("no"); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; for (int i = 0; i < t; ++i) { 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 main(): t = int(input()) for z in range(t): n, k, d1, d2 = map(int, input().split()) if n % 3 != 0: print('no') continue f = 0 for i in [-1, +1]: for j in [-1, +1]: w = (k - i * d1 - j * d2) if f == 0 and (w % 3 == 0) and (n//3)>=(w//3)>=0 and (n//3)>=(w//3 + i * d1)>=0 and (n//3)>=(w//3 + j * d2)>=0: print('yes') f = 1 if f == 0: print('no') main()
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 n, k, d1, d2; bool fun(long long t1, long long t2, long long t3) { long long mi = (min(min(t1, t2), t3)); t1 -= mi; t2 -= mi; t3 -= mi; long long ma = (max(max(t1, t2), t3)); if ((t1 + t2 + t3) <= k && (k - (t1 + t2 + t3)) % 3 == 0) { long long kal = (ma - t1) + (ma - t2) + (ma - t3); if (kal + k <= n && (n - (kal + k)) % 3 == 0 && ma <= n) { return 1; } } return 0; } void fun() { if (d1 < d2) swap(d1, d2); long long t1 = 0, t2 = 0, t3 = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 || j == 0) continue; if (i == -1) { t1 = d1; t2 = 0; } else { t2 = d1; t1 = 0; } if (j == -1) t3 = d2 + t2; else t3 = t2 - d2; if (fun(t1, t2, t3) == 1) { cout << "yes\n"; return; } } } cout << "no\n"; return; } int main() { int t = 0; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; fun(); } 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 n, k, d1, d2; bool DW(long long num, int d) { long long a, b, c; if (num % 3) return false; b = num / 3; if (d == 1) { a = b + d1; c = b - d2; } else if (d == 2) { a = b + d1; c = b + d2; } else if (d == 3) { a = b - d1; c = b - d2; } else { a = b - d1; c = b + d2; } return a <= n / 3 && b <= n / 3 && c <= n / 3 && a >= 0 && b >= 0 && c >= 0; } int main() { int t; scanf("%d", &t); while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3) printf("no\n"); else { if (DW(k - d1 + d2, 1)) printf("yes\n"); else if (DW(k - d1 - d2, 2)) printf("yes\n"); else if (DW(k + d1 + d2, 3)) printf("yes\n"); else if (DW(k + d1 - d2, 4)) 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 math t = int(raw_input()) for i in range(t): found = False tn = raw_input().split() if int(tn[0]) % 3 != 0: print "no" continue d1 = int(tn[2]) d2 = int(tn[3]) for i in range(-1,2,2): for j in range(-1,2,2): D1 = d1 * i D2 = d2 * j x2 = (int(tn[1]) - D1 + D2) / 3 if (int(tn[1]) - D1 + D2) % 3 != 0: continue if x2 >= 0 and x2 <= int(tn[1]): x1 = D1 + x2 x3 = x2 - D2 if x1 >= 0 and x3 >= 0 and x1 <= int(tn[1]) and x3 <= int(tn[1]): if x1 <= int(tn[0]) / 3 and x3 <= int(tn[0]) / 3 and x2 <= int(tn[0]) / 3: found = True if found and abs(x1 - x2) == d1 and abs(x2 - x3) == d2: break if not found: print "no" continue print "yes"
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 first(long long n, long long k, long long d1, long long d2) { if ((k + 2 * d1 + d2) % 3 != 0) return false; long long alpha = (k + 2 * d1 + d2) / 3; if (alpha < d1 || alpha < d1 + d2) return false; if (alpha > n / 3) return false; return true; } bool second(long long n, long long k, long long d1, long long d2) { if ((k + 2 * d1 - d2) % 3 != 0) return false; long long alpha = (k + 2 * d1 - d2) / 3; if (alpha < 0 || alpha - d1 < 0 || alpha - d1 + d2 < 0 || alpha > n / 3 || alpha - d1 > n / 3 || alpha - d1 + d2 > n / 3) return false; return true; } bool third(long long n, long long k, long long d1, long long d2) { if ((k - 2 * d1 + d2) % 3 != 0) return false; long long alpha = (k - 2 * d1 + d2) / 3; if (alpha < 0 || alpha + d1 < 0 || alpha + d1 - d2 < 0 || alpha > n / 3 || alpha + d1 > n / 3 || alpha + d1 - d2 > n / 3) return false; return true; } bool fourth(long long n, long long k, long long d1, long long d2) { if ((k - 2 * d1 - d2) % 3 != 0) return false; long long alpha = (k - 2 * d1 - d2) / 3; if (alpha < 0 || alpha + d1 + d2 > n / 3) return false; return true; } int main() { int t; cin >> t; long long n, k, d1, d2; for (int i = 0; i < t; i++) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no" << "\n"; continue; } if (first(n, k, d1, d2) || second(n, k, d1, d2) || third(n, k, d1, d2) || fourth(n, k, 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 java.util.*; public class Invader { public static void main(String [] args){ Scanner in=new Scanner(System.in); int t=in.nextInt(); for(int i=0;i<t;i++){ long n=in.nextLong(); long k=in.nextLong(); long d1=in.nextLong(); long d2=in.nextLong(); boolean flag=false; if(n%3!=0) System.out.println("no"); else{ double x1=(double)(k+2*d1+d2)/3; double x2=(double)(k-d1+d2)/3; double x3=(double)(k-d1-2*d2)/3; if(x1>=0 && x2>=0 && x3>=0 && x1<=n/3 && x2<=n/3 && x3<=n/3 && x1==(long)x1 && x2==(long)x2 && x3==(long)x3) flag=true; //System.out.println(x1+" "+x2+" "+x3); //System.out.println((long)x1+" "+(long)x2+" "+(long)x3); x1=(double)(k+2*d1-d2)/3; x2=(double)(k-d1-d2)/3; x3=(double)(k-d1+2*d2)/3; if(x1>=0 && x2>=0 && x3>=0 && x1<=n/3 && x2<=n/3 && x3<=n/3 && x1==(long)x1 && x2==(long)x2 && x3==(long)x3) flag=true; //System.out.println(x1+" "+x2+" "+x3); //System.out.println((long)x1+" "+(long)x2+" "+(long)x3); x1=(double)(k-2*d1+d2)/3; x2=(double)(k+d1+d2)/3; x3=(double)(k+d1-2*d2)/3; if(x1>=0 && x2>=0 && x3>=0 && x1<=n/3 && x2<=n/3 && x3<=n/3 && x1==(long)x1 && x2==(long)x2 && x3==(long)x3) flag=true; //System.out.println(x1+" "+x2+" "+x3); //System.out.println((long)x1+" "+(long)x2+" "+(long)x3); x1=(double)(k-2*d1-d2)/3; x2=(double)(k+d1-d2)/3; x3=(double)(k+d1+2*d2)/3; if(x1>=0 && x2>=0 && x3>=0 && x1<=n/3 && x2<=n/3 && x3<=n/3 && x1==(long)x1 && x2==(long)x2 && x3==(long)x3) flag=true; //System.out.println(x1+" "+x2+" "+x3); //System.out.println((long)x1+" "+(long)x2+" "+(long)x3); if(flag) 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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class DZYLovesHash { 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(in, out); out.close(); } } class TaskC { public void solve(InputReader in, PrintWriter out) { long tcs = in.nextInt(); long n, k, d1, d2; long remainMatches; long x1, x2, x3; long max; long min; long totalWin; long temp; for (long i = 0; i < tcs; i++) { n = in.nextLong(); k = in.nextLong(); d1 = in.nextLong(); d2 = in.nextLong(); remainMatches = n - k; if (n % 3 != 0) { out.println("no"); continue; } // 1 if ((2 * d1 + d2 + k) % 3 == 0 || (d2 - d1 + k) % 3 == 0 || (k - d1 - 2 * d2) % 3 == 0) { x1 = (2 * d1 + d2 + k) / 3; x2 = (d2 - d1 + k) / 3; x3 = (k - d1 - 2 * d2) / 3; max = Math.max(x1, x2); max = Math.max(max, x3); min = Math.min(x1, x2); min = Math.min(min, x3); totalWin = Math.abs(x1 - min) + Math.abs(x2 - min) + Math.abs(x3 - min); if (totalWin <= k) { temp = Math.abs(max - x1) + Math.abs(max - x2) + Math.abs(max - x3); if (temp == remainMatches) { out.println("yes"); continue; } if (temp < remainMatches) { temp -= remainMatches; if (temp % 3 == 0) { out.println("yes"); continue; } } } } // 2 d1 *= -1; // if ((2 * d1 + d2 + k) % 3 == 0 || (d2 - d1 + k) % 3 == 0 || (k - d1 - 2 * d2) % 3 == 0) { x1 = (2 * d1 + d2 + k) / 3; x2 = (d2 - d1 + k) / 3; x3 = (k - d1 - 2 * d2) / 3; max = Math.max(x1, x2); max = Math.max(max, x3); min = Math.min(x1, x2); min = Math.min(min, x3); totalWin = Math.abs(x1 - min) + Math.abs(x2 - min) + Math.abs(x3 - min); if (totalWin <= k) { temp = Math.abs(max - x1) + Math.abs(max - x2) + Math.abs(max - x3); if (temp == remainMatches) { out.println("yes"); continue; } if (temp < remainMatches) { temp -= remainMatches; if (temp % 3 == 0) { out.println("yes"); continue; } } } } d1*=-1; // 3 d2 *= -1; // if ((2 * d1 + d2 + k) % 3 == 0 || (d2 - d1 + k) % 3 == 0 || (k - d1 - 2 * d2) % 3 == 0) { x1 = (2 * d1 + d2 + k) / 3; x2 = (d2 - d1 + k) / 3; x3 = (k - d1 - 2 * d2) / 3; max = Math.max(x1, x2); max = Math.max(max, x3); min = Math.min(x1, x2); min = Math.min(min, x3); totalWin = Math.abs(x1 - min) + Math.abs(x2 - min) + Math.abs(x3 - min); if (totalWin <= k) { temp = Math.abs(max - x1) + Math.abs(max - x2) + Math.abs(max - x3); if (temp == remainMatches) { out.println("yes"); continue; } if (temp < remainMatches) { temp -= remainMatches; if (temp % 3 == 0) { out.println("yes"); continue; } } } } // d2*=-1; // 4 d1 *= -1; d2 *= -1; // if ((2 * d1 + d2 + k) % 3 == 0 || (d2 - d1 + k) % 3 == 0 || (k - d1 - 2 * d2) % 3 == 0) { x1 = (2 * d1 + d2 + k) / 3; x2 = (d2 - d1 + k) / 3; x3 = (k - d1 - 2 * d2) / 3; max = Math.max(x1, x2); max = Math.max(max, x3); min = Math.min(x1, x2); min = Math.min(min, x3); totalWin = Math.abs(x1 - min) + Math.abs(x2 - min) + Math.abs(x3 - min); if (totalWin <= k) { temp = Math.abs(max - x1) + Math.abs(max - x2) + Math.abs(max - x3); if (temp == remainMatches) { out.println("yes"); continue; } if (temp < remainMatches) { temp -= remainMatches; if (temp % 3 == 0) { out.println("yes"); continue; } } } } out.println("no"); } } } 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 int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public 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
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; bool solve(long long cc, long long ss, long long gg) { long long b2ee = n - k, sum = 0, mksb = n / 3; if (cc < mksb) sum += mksb - cc; if (ss < mksb) sum += mksb - ss; if (gg < mksb) sum += mksb - gg; return (cc > mksb || cc < 0 || ss > mksb || ss < 0 || gg > mksb || gg < 0 || sum > b2ee); } int main() { ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; int t; cin >> t; while (t--) { bool z = true; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continue; } else { long long c = (k + (2 * d1) + d2) / 3; long long s = c - d1; long long g = s - d2; z &= solve(c, s, g); c = (k + (2 * d2) + d1) / 3; s = c - d2; g = s - d1; z &= solve(c, s, g); c = (k + (d2) + d1) / 3; s = c - d1; g = c - d2; z &= solve(c, s, g); c = (k + (2 * d1) - d2) / 3; s = c - d1; g = s + d2; z &= solve(c, s, g); } (z) ? cout << "no\n" : cout << "yes\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; long long _, n, k, d1, d2, t, x, y; bool V(long long i, long long j, long long k, long long n, long long m) { long long u = m - i - j - k; if (u % 3) { return 0; } long long g = u / 3; i += g; j += g; k += g; if (i < 0 || j < 0 || k < 0) return 0; x = i > j ? k > i ? k : i : k > j ? k : j; y = (n - m) - x * 3 + i + j + k; return y >= 0; } int main() { for (cin >> _; _--;) { cin >> n >> k >> d1 >> d2; puts((!(n % 3) && (V(d1, 0, -d2, n, k) || V(-d1, 0, -d2, n, k) || V(d1, 0, d2, n, k) || V(-d1, 0, d2, n, k))) ? "yes" : "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
num = raw_input() num=int(num) while num>0: s=raw_input() inp=map(int,s.split()) sign = (-1,1) done = False if inp[0]%3!=0: print "no" else: for i in sign: for j in sign: d1=i*inp[2] d2=j*inp[3] k=inp[1] n=inp[0] x2=(k+d2-d1)/3 if (k+d2-d1)%3!=0: continue if x2>=0 and x2<=k: x1=x2+d1 x3=x2-d2 if x1>=0 and x1<=k and x3>=0 and x3<=k: if x1<=n/3 and x2<=n/3 and x3<=n/3: done=True break if done==True: break if done: print "yes" else: print "no" num=num-1
PYTHON