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
import java.util.Scanner; public class C { static boolean solve(long n, long k, long d1, long d2) { if(n%3 == 0) { long d3 = 0; long min = Math.min(Math.min(d1, d2), 0); if(min < 0) { d1 += -min; d2 += -min; d3 += -min; } long end = d1 + d2 + d3; if(k - end >= 0 && (k - end)%3 == 0) { long max = Math.max(d1, Math.max(d2, d3)); long need = max - d1 + max - d2 + max - d3; long X = n - k - need; if(X >= 0 && X%3 == 0) { return true; } } } return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); for(int T = sc.nextInt(); T-->0;) { long n = sc.nextLong(); long k = sc.nextLong(); long d1 = sc.nextLong(); long d2 = sc.nextLong(); if(solve(n, k, d1, d2) || solve(n, k, -d1, d2) || solve(n, k, d1, -d2) || solve(n, k, -d1, -d2)) { System.out.println("yes"); } else { System.out.println("no"); } } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
from sys import * t=int(stdin.readline()) for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) vars=((2*d1+d2,2*d2+d1),(2*d2+d1,2*d1+d2),(2*max(d1,d2)-min(d1,d2),d1+d2), (d1+d2,2*max(d1,d2)-min(d1,d2))) y=False for i in vars: if i[0]<=k and i[0]%3==k%3 and n-k-i[1]>=0 and (n-i[1]-k)%3==0: print("yes") y=True break if not y: print("no")
PYTHON3
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
input=__import__('sys').stdin.readline for _ in range(int(input())): n,k,d1,d2 = map(int,input().split()) lis=[[2*d1+d2 , 2*d2+d1] , [2*d2+d1 , 2*d1+d2] , [2*max(d1,d2)-min(d1,d2) , d1+d2] , [d1+d2 , 2*max(d1,d2) - min(d1,d2)]] flag=1 for i in lis: if i[0]<=k and i[0]%3==k%3 and n-k-i[1]>=0 and (n-k-i[1])%3==0: print("yes") flag=0 break if flag: print("no")
PYTHON3
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.util.Scanner; public class C { public static void main(String[] args) { Scanner sc=new Scanner(System.in); L:for(int t = (int)sc.nextLong();t>0;t--){ long n = sc.nextLong(); long k = sc.nextLong(); long d1 = sc.nextLong(); long d2 = sc.nextLong(); if(n%3!=0){ System.out.println("no"); continue; } if(k==0){ System.out.println("yes"); continue; } long high=k,low=0; // A wins, Bwins for(int i=0;i<4;i++){ // Awin Bwin if(i==0){ } // Awin Cwin else if(i==1){ d2*=-1; } // Bwin Cwin else if(i==2){ d1*=-1; } // Bwin Bwin else if(i==3){ d2*=-1; } high=k; low=0; while(low<=high){ long mid = (high+low)/2; long awin = mid; long bwin = awin-d1; if(bwin<0){ low=mid+1; continue; } else if(bwin>k){ high=mid-1; continue; } if(awin+bwin>k){ high=mid-1; continue; } long cwin = bwin-d2; if(cwin<0){ low=mid+1; continue; } else if(cwin>k){ high=mid-1; continue; } if(awin+bwin+cwin == k){ long max=Math.max(awin, Math.max(bwin, cwin)); long sum = max-awin + max-bwin + max-cwin; if(((n-k)-sum)>=0&&((n-k)-sum)%3==0){ System.out.println("yes"); continue L; } else { break; } } else if(awin+bwin+cwin > k){ high=mid-1; } else { low=mid+1; } } } 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
from sys import stdin rints = lambda: [int(x) for x in stdin.readline().split()] for _ in range(int(input())): n, k, d1, d2 = rints() cases, ans = [[d1, 0, d2], [0, d1, d1 + d2], [d2 + d1, d2, 0]], 'no' if d1 >= d2: cases.append([0, d1, d1 - d2]) if d2 >= d1: cases.append([d2 - d1, d2, 0]) for i in cases: if sum(i) > k or sum(i) < k and (k - sum(i)) % 3: continue i.sort() ext = 2 * i[-1] - (sum(i[:2])) if ext <= n - k and (n - k - ext) % 3 == 0: ans = 'yes' break print(ans)
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 static java.lang.Math.*; import static java.lang.System.currentTimeMillis; import static java.lang.System.exit; import static java.lang.System.arraycopy; import static java.util.Arrays.sort; import static java.util.Arrays.binarySearch; import static java.util.Arrays.fill; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { try { if (new File("input.txt").exists()) System.setIn(new FileInputStream("input.txt")); } catch (SecurityException e) { } new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); private void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } boolean check(long n, long k, long d1, long d2) { if(n % 3 != 0) return false; for(long signD1 = -1; signD1 <= 1; signD1 += 2) for(long signD2 = -1; signD2 <= 1; signD2 += 2) { long D1 = signD1 * d1; long D2 = signD2 * d2; if( (2 * D1 + D2 + k) % 3 == 0) { long x1 = (2 * D1 + D2 + k) / 3; long x3 = k + D1 - 2 * x1; long x2 = k - x1 - x3; if(x1 >= 0 && x1 <= k && x2 >= 0 && x2 <= k && x3 >= 0 && x3 <= k && x1 <= n/3 && x2 <= n/3 && x3 <= n/3) { return true; } } } return false; } private void solve() throws IOException { int t = nextInt(); for (int test = 0; test < t; test++) { long n = nextLong(), k = nextLong(), d1 = nextLong(), d2 = nextLong(); if(check(n,k,d1,d2)) { out.println("yes"); } else { out.println("no"); } } } String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } return false; } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; long long N, k, d1, d2; bool c1() { if (k - d1 - d2 < 0) return false; if ((k - d1 - d2) % 3) return false; long long b = (k - d1 - d2) / 3; long long a = b + d1; long long c = b + d2; if (a < 0 || b < 0 || c < 0 || a + b + c != k) return false; if (a < 0 || b < 0 || c < 0 || a + b + c != k) return false; if (a > (N + k) / 3 || b > (N + k) / 3 || c > (N + k) / 3) return false; if ((N - 2 * abs(d1 - d2) - min(d1, d2)) < 0) return false; if ((N - 2 * abs(d1 - d2) - min(d1, d2)) % 3) return false; return true; } bool c2() { if ((k + d1 + d2) % 3) return false; if (N - d1 - d2 < 0) return false; long long b = (k + d1 + d2) / 3; long long a = b - d1; long long c = b - d2; if (a < 0 || b < 0 || c < 0 || a + b + c != k) return false; if (a > (N + k) / 3 || b > (N + k) / 3 || c > (N + k) / 3) return false; if ((N - d1 - d2) % 3) return false; return true; } bool c3() { if (k + d1 - d2 < 0) return false; if ((k + d1 - d2) % 3) return false; long long b = (k + d1 - d2) / 3; long long a = b - d1; long long c = b + d2; if (a < 0 || b < 0 || c < 0 || a + b + c != k) return false; if (a > (N + k) / 3 || b > (N + k) / 3 || c > (N + k) / 3) return false; if (N - 2 * d2 - d1 < 0) return false; if ((N - 2 * d2 - d1) % 3) return false; return true; } bool c4() { if (k - d1 + d2 < 0) return false; if ((k - d1 + d2) % 3) return false; long long b = (k - d1 + d2) / 3; long long a = b + d1; long long c = b - d2; if (a < 0 || b < 0 || c < 0 || a + b + c != k) return false; if (a > (N + k) / 3 || b > (N + k) / 3 || c > (N + k) / 3) return false; if (a > (N + k) / 3 || b > (N + k) / 3 || c > (N + k) / 3) return false; if (N - 2 * d1 - d2 < 0) return false; if ((N - 2 * d1 - d2) % 3) return false; return true; } int main() { long long T; cin >> T; while (T--) { cin >> N >> k >> d1 >> d2; N -= k; if (c1() || c2() || c3() || c4()) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ProblemC2 { private ProblemC2() throws IOException { BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); String h = rd.readLine(); int t = Integer.parseInt(h); for(int i=0;i<t;i++) { h = rd.readLine(); String[] q = h.split("\\s+"); long n = Long.parseLong(q[0]); long k = Long.parseLong(q[1]); long d1 = Long.parseLong(q[2]); long d2 = Long.parseLong(q[3]); out(compute(n,k,d1,d2)); } } private String compute(long n, long k, long d1, long d2) { boolean res = n % 3 == 0; if(res) { res = false; long third = n / 3; for(int i=-1;i<3;i+=2) { for(int j=-1;j<3;j+=2) { long[] w1w3 = solve(new long[][] { { 2*i, 1*i, d1 + k*i }, { -1*j, -2*j, d2-k*j } }); long w1 = w1w3[0]; long w3 = w1w3[1]; if(w1 >= 0 && w3 >= 0) { long w2 = k - w1 - w3; if(w2 >= 0 && w1 <= third && w2 <= third && w3 <= third) { res = true; } } } } } return res?"yes":"no"; } private long[] solve(long[][] m) { long a = m[0][0] * m[1][1] - m[0][1] * m[1][0]; long b = m[0][2] * m[1][1] - m[1][2] * m[0][1]; long c = m[0][0] * m[1][2] - m[1][0] * m[0][2]; if((b%a==0) && (c%a==0)) { return new long[] { b/a, c/a }; } return new long[] { -1, -1 }; } private static void out(Object x) { System.out.println(x); } public static void main(String[] args) throws IOException { new ProblemC2(); } }
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 i in range(t): n,k,d1,d2=map(int,input().split()) m=d1+d2 mm=2*max(d1,d2)-min(d1,d2) mmm=2*d1+d2 mmmm=2*d2+d1 if (k-mmm>=0 and (k-mmm)%3==0 and n-mmmm-k>=0 and (n-mmmm-k)%3==0) or (k-mmmm>=0 and (k-mmmm)%3==0 and n-mmm-k>=0 and (n-mmm-k)%3==0) or (k-m>=0 and (k-m)%3==0 and n-mm-k>=0 and (n-mm-k)%3==0) or (k-mm>=0 and (k-mm)%3==0 and n-m-k>=0 and (n-m-k)%3==0): print("yes") else: print("no")
PYTHON3
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; long long t, n, k, d1, d2, m, a, b, c, x, y; bool flag; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> t; for (int i = 0; i < t; ++i) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) cout << "no\n"; else { m = n / 3; for (int j = 0; j < 4; ++j) { flag = true; if (j % 2) x = d1; else x = -d1; if (j & 2) y = d2; else y = -d2; if ((k - x - y) % 3 != 0) flag = false; b = (k - x - y) / 3; a = b + x; c = b + y; if (a < 0 || a > m || b < 0 || b > m || c < 0 || c > m) flag = false; if (flag == true) { cout << "yes\n"; break; } } if (flag == false) 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.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastReader in, PrintWriter out) { long n = in.nextLong(), k = in.nextLong(), d1 = in.nextLong(), d2 = in.nextLong(); out.println(solveFaster(n, k, d1, d2) ? "yes" : "no"); } private boolean solveFaster(long n, long k, long d1, long d2) { if (n % 3 != 0) return false; for (int sgn2 = -1; sgn2 <= 1; ++sgn2) { for (int sgn1 = -1; sgn1 <= 1; ++sgn1) { if (sgn1 == 0 || sgn2 == 0) continue; long D1 = d1 * sgn1; long D2 = d2 * sgn2; long x2 = (k - D1 + D2) / 3; if ((k - D1 + D2) % 3 != 0) continue; if (x2 >= 0 && x2 <= k) { long x1 = D1 + x2; long x3 = x2 - D2; if (x1 >= 0 && x1 <= k && x3 >= 0 && x3 <= k) { if (x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) { return true; } } } } } return false; } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public String next() { return nextString(); } public long nextLong() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = pread(); while (isSpaceChar(c)) c = pread(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = pread(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; const int MnoD = 1000000007; const int INF = int(1e9); bool solve(long long n, long long k, long long d1, long long d2) { if (n % 3 != 0) { return false; } for (int sign1 = -1; sign1 <= 1; sign1 += 2) { for (int sign2 = -1; sign2 <= 1; sign2 += 2) { long long D1 = d1 * sign1; long long D2 = d2 * sign2; long long x2 = (k - D1 + D2) / 3; if ((k - D1 + D2) % 3 != 0) { continue; } if (x2 >= 0 && x2 <= k) { long long x1 = x2 + D1; long long x3 = x2 - D2; if (x1 >= 0 && x1 <= k && x3 >= 0 && x3 <= k) { if (x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) { return true; } } } } } return false; } int main() { ios_base::sync_with_stdio(false); int testCases; cin >> testCases; while (testCases--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; cout << (solve(n, k, d1, d2) ? "yes" : "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> template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break; } if (ch >= '0' && ch <= '9') break; } while (1) { n = n * 10 + (ch - '0'); ch = getchar(); if (ch < '0' || ch > '9') break; } return (ng ? -n : n); } template <typename T> inline T ABS(T a) { if (a < 0) return -a; else return a; } template <typename T> inline T Dis(T x1, T y1, T x2, T y2) { return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2)); } template <typename T> inline T gcd(T a, T b) { if (a < 0) return gcd(-a, b); if (b < 0) return gcd(a, -b); return (b == 0) ? a : gcd(b, a % b); } template <typename T> inline T lcm(T a, T b) { if (a < 0) return lcm(-a, b); if (b < 0) return lcm(a, -b); return a * (b / gcd(a, b)); } bool isVowel(char ch) { ch = toupper(ch); if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E') return true; return false; } template <typename T> T POW(T b, T p) { if (p == 0) return 1; if (p % 2 == 0) { T s = POW(b, p / 2); return s * s; } return b * POW(b, p - 1); } using namespace std; long long int n, k, d1, d2, x1, x2, x3; void chk() { x1 = (2 * d1 + d2 + k) / 3; x2 = x1 - d1; x3 = x2 - d2; return; } void chk1() { x1 = (2 * d1 - d2 + k) / 3; x2 = x1 - d1; x3 = x2 + d2; return; } void chk2() { x1 = ((2 * d1 * -1) + d2 + k) / 3; x2 = x1 + d1; x3 = x2 - d2; return; } void chk3() { x1 = ((2 * d1 * -1) - d2 + k) / 3; x2 = x1 + d1; x3 = x2 + d2; return; } int fnlchk() { long long int rem = n - k, dv = n / 3; if (x1 > dv || x2 > dv || x3 > dv || (x1 + x2 + x3) > k || x1 < 0 || x2 < 0 || x3 < 0) return 0; long long int tp = (dv - x1) + (dv - x2) + (dv - x3); if (tp == rem) return 1; return 0; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> k >> d1 >> d2; if (n % 3) { printf("no\n"); continue; } int f = 0; chk(); f += fnlchk(); chk1(); f += fnlchk(); chk2(); f += fnlchk(); chk3(); f += fnlchk(); if (!f) 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; template <typename T> T sqr(T x) { return x * x; } template <typename T> T abs(T x) { return x < 0 ? -x : x; } template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } int t; inline bool f(long long x) { return x >= 0 && x % 3 == 0; } int main() { ios_base ::sync_with_stdio(false); cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (f(k + d2 - d1 * 2) && f(k + d1 - d2 * 2) && f(n - k - d1 - d2)) { cout << "yes\n"; continue; } if (f(k - d1 - d2) && ((d1 >= d2 && f(n - k - d1 * 2 + d2)) || (d1 < d2 && f(n - k - d2 * 2 + d1)))) { cout << "yes\n"; continue; } if (f(k - d1 - d2 * 2) && f(n - k - d1 * 2 - d2)) { cout << "yes\n"; continue; } if (f(k - d2 - d1 * 2) && f(n - k - d1 - d2 * 2)) { 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
//package com.example.hackerranksolutions; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; public class CodeforcesProblems { static void buildSegmentTree(int[] tree, int[] arr, int left, int right, int pos, int n, int level) { if(left == right) { tree[pos] = arr[left]; return; } int mid = (left+right)/2; buildSegmentTree(tree, arr, left, mid, pos*2+1, n, level+1); buildSegmentTree(tree, arr, mid+1, right, pos*2+2, n, level+1); if(n%2 == 0) { if(level % 2 == 0) { tree[pos] = tree[pos*2+1] ^ tree[pos*2+2]; } else { tree[pos] = tree[pos*2+1] | tree[pos*2+2]; } } else { if(level % 2 == 0) { tree[pos] = tree[pos*2+1] | tree[pos*2+2]; } else { tree[pos] = tree[pos*2+1] ^ tree[pos*2+2]; } } } static void updateTree(int[] tree, int[] arr, int index, int val, int left, int right, int pos, int n, int level) { if(left>index) return; if(right<index) return; if(index == left && index == right) { tree[pos] = val; return; } int mid = (left+right)/2; updateTree(tree, arr, index, val, left, mid, pos*2+1, n, level+1); updateTree(tree, arr, index, val, mid+1, right, pos*2+2, n, level+1); if(n%2 == 0) { if(level % 2 == 0) { tree[pos] = tree[pos*2+1] ^ tree[pos*2+2]; } else { tree[pos] = tree[pos*2+1] | tree[pos*2+2]; } } else { if(level % 2 == 0) { tree[pos] = tree[pos*2+1] | tree[pos*2+2]; } else { tree[pos] = tree[pos*2+1] ^ tree[pos*2+2]; } } } //++451B static void solve451B() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int len = Integer.parseInt(br.readLine().trim()); String numbers[] = br.readLine().split(" "); int arr[] = new int[len]; for(int i = 0; i<len; i++) { arr[i] = Integer.parseInt(numbers[i]); } int i = 0; while(i<len-1 && arr[i]<=arr[i+1]) { i++; } if(i == len-1) { System.out.println("yes\n1 1"); return; } int first = i; while(i<len-1 && arr[i]>=arr[i+1]) { i++; } int last = i; for(int j = first, k = last; j<k; j++,k--) { int temp = arr[j]; arr[j] = arr[k]; arr[k] = temp; } first++; last++; boolean isSorted = true; for(int k = 0; k<len-1; k++) { if(arr[k]>arr[k+1]) { isSorted = false; break; } } if(isSorted) { System.out.println("yes\n"+first+" "+last); } else { System.out.println("no"); } } //--451B //++451C static boolean isPossible(long[] vals, long remain) { if(remain == 0) { return vals[0] == vals[1] && vals[1] == vals[2]; } Arrays.sort(vals); if(vals[0]<0) return false; long need = vals[2] - vals[0] + vals[2] - vals[1]; if(need>remain) return false; return (remain-need) % 3L == 0; } static void solve451C() throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine().trim()); for(int t = 0; t<T; t++) { String[] strArr = br.readLine().split(" "); long n = Long.parseLong(strArr[0]); long k = Long.parseLong(strArr[1]); long d1 = Long.parseLong(strArr[2]); long d2 = Long.parseLong(strArr[3]); if(n%3L != 0) { System.out.println("no"); continue; } long[] vals = new long[3]; long calc = k-d1-d2; if(calc%3L == 0) { vals[1] = calc/3L; vals[0] = vals[1]+d1; vals[2] = vals[1]+d2; if(isPossible(vals, n-k)) { System.out.println("yes"); continue; } } calc = k-d1+d2; if(calc%3L == 0) { vals[1] = calc/3L; vals[0] = vals[1]+d1; vals[2] = vals[1]-d2; if(isPossible(vals, n-k)) { System.out.println("yes"); continue; } } calc = k+d1-d2; if(calc%3L == 0) { vals[1] = calc/3L; vals[0] = vals[1]-d1; vals[2] = vals[1]+d2; if(isPossible(vals, n-k)) { System.out.println("yes"); continue; } } calc = k+d1+d2; if(calc%3L == 0) { vals[1] = calc/3L; vals[0] = vals[1]-d1; vals[2] = vals[1]-d2; if(isPossible(vals, n-k)) { System.out.println("yes"); continue; } } System.out.println("no"); } } //--451C public static void main(String[] args) throws Exception { /*BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String nq[] = br.readLine().split(" "); int n = Integer.parseInt(nq[0]); int m = Integer.parseInt(nq[1]); int q = Integer.parseInt(nq[2]); String numbers[] = br.readLine().split(" "); int len = numbers.length; int arr[] = new int[len]; for(int i = 0; i<len; i++) arr[i] = Integer.parseInt(numbers[i]); int tree[] = new int[len*2+1]; buildSegmentTree(tree, arr, 0, len-1, 0, n, 0); StringBuilder sb = new StringBuilder(); for(int i = 0; i<q; i++) { numbers = br.readLine().split(" "); int index = Integer.parseInt(numbers[0]) - 1; int val = Integer.parseInt(numbers[1]); updateTree(tree, arr, index, val, 0, len-1, 0, n, 0); sb.append(tree[0]).append('\n'); } System.out.print(sb); br.close();*/ //solve451B(); solve451C(); } }
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 func(long long a, long long b, long long c, long long d) { if (a >= b && a >= c) { d = d - (a - b); d = d - (a - c); if (d < 0 || d % 3 != 0) return false; else return true; } else if (b >= a && b >= c) { d = d - (b - a); d = d - (b - c); if (d < 0 || d % 3 != 0) return false; else return true; } else if (c >= a && c >= b) { d = d - (c - a); d = d - (c - b); if (d < 0 || d % 3 != 0) return false; else return true; } else return false; } int main() { long long t; cin >> t; while (t--) { long long n, c, a, b; bool p; cin >> n >> c >> a >> b; long long x = -1, y = -1, z = -1; if (n % 3 != 0) { cout << "no" << endl; continue; } if ((2 * a + b + c) % 3 == 0) { x = (2 * a + b + c) / 3; y = x - a; z = y - b; if (!(x > c || y < 0 || z < 0)) { p = func(x, y, z, n - c); if (p == true) { cout << "yes" << endl; continue; } } } if ((a + b + c) % 3 == 0) { y = (a + b + c) / 3; x = y - a; z = y - b; if (!(y > c || x < 0 || z < 0)) { p = func(x, y, z, n - c); if (p == true) { cout << "yes" << endl; continue; } } } if ((2 * b + c - a) % 3 == 0) { z = (2 * b + c - a) / 3; y = z - b; x = y + a; if (!(z > c || y < 0 || x < 0 || x > c || z < 0)) { p = func(x, y, z, n - c); if (p == true) { cout << "yes" << endl; continue; } } } if ((c - b - 2 * a) % 3 == 0) { x = (c - b - 2 * a) / 3; y = x + a; z = y + b; if (!(x < 0 || y > c || z > c)) { p = func(x, y, z, n - c); if (p == true) { 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.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; public class Main { public static void main(String[] args) { TaskC mSol = new TaskC(); mSol.solve(); mSol.flush(); mSol.close(); } } class TaskC { public FasterScanner mFScanner; public PrintWriter mOut; public long mT1, mT2, mT3; public TaskC() { mFScanner = new FasterScanner(); mOut = new PrintWriter(System.out); } public void solve() { int T; long N, K, d1, d2; boolean status; T = mFScanner.nextInt(); while (T-- > 0) { N = mFScanner.nextLong(); K = mFScanner.nextLong(); d1 = mFScanner.nextLong(); d2 = mFScanner.nextLong(); status = possible(d1, d2, N, K); status |= possible(d1, -1 * d2, N, K); status |= possible(-1 * d1, d2, N, K); status |= possible(-1 * d1, -1 * d2, N, K); if (status) mOut.println("yes"); else mOut.println("no"); } } public boolean check(long rem) { long max, min, mid; max = Math.max(mT1, mT2); max = Math.max(max, mT3); min = Math.min(mT1, mT2); min = Math.min(min, mT3); mid = mT1 + mT2 + mT3 - max - min; long p = 2 * max - min - mid; rem -= p; if (rem < 0 || rem % 3 != 0) return false; return true; } public boolean possible(long d1, long d2, long N, long K) { long t1, t2, t3; t2 = K - d1 - d2; if (t2 < 0 || t2 % 3 != 0) return false; t2 /= 3; t1 = d1 + t2; t3 = d2 + t2; if (t1 < 0 || t3 < 0) return false; mT1 = t1; mT2 = t2; mT3 = t3; return check(N - K); } public void flush() { mOut.flush(); } public void close() { mOut.close(); } } class FasterScanner { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FasterScanner() { this(System.in); } public FasterScanner(InputStream is) { mIs = is; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; static const double PI = 4.0 * atan(1.0); bool ISINT(double x) { return fabs(x - (int)round(x)) < EPS; } bool ISEQ(double x, double y) { return fabs(x - y) < EPS; } string itos(long long x) { stringstream ss; ss << x; return ss.str(); } long long n, k, d[2]; bool check(long long bit) { long long t[3] = {0}; long long minVal = 0; for (long long i = 0; i < 2; i++) { if (bit & (1LL << i)) { t[i + 1] = t[i] + d[i]; } else { t[i + 1] = t[i] - d[i]; } minVal = min(minVal, t[i + 1]); } if (minVal < 0) { for (long long i = 0; i < 3; i++) { t[i] += -minVal; } } long long sum = 0; for (long long i = 0; i < 3; i++) { sum += t[i]; } if (sum > k) return false; if ((k - sum) % 3LL != 0) return false; for (long long i = 0; i < 3; i++) { t[i] += (k - sum) / 3LL; } for (long long i = 0; i < 3; i++) { if (t[i] > n / 3LL) { return false; } } return true; } int main(void) { long long T; cin >> T; while (T--) { cin >> n >> k >> d[0] >> d[1]; bool flg = false; if (n % 3LL != 0LL) { cout << "no" << endl; continue; } for (long long i = 0; i < (1LL << 2LL); i++) { if (check(i)) { flg = true; break; } } cout << (flg ? "yes" : "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
from sys import * t=int(stdin.readline()) mm,mmm,mmmm,m=0,0,0,0 for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) m=d1+d2 mm=2*max(d1,d2)-min(d1,d2) mmm=2*d1+d2 mmmm=2*d2+d1 if (k-mmm>=0 and (k-mmm)%3==0 and n-mmmm-k>=0 and (n-mmmm-k)%3==0) or (k-mmmm>=0 and (k-mmmm)%3==0 and n-mmm-k>=0 and (n-mmm-k)%3==0) or (k-m>=0 and (k-m)%3==0 and n-mm-k>=0 and (n-mm-k)%3==0) or (k-mm>=0 and (k-mm)%3==0 and n-m-k>=0 and (n-m-k)%3==0): print("yes") else: print("no")
PYTHON3
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int cnt = (0); cnt < (t); ++cnt) { int64_t n, k, d1, d2; cin >> n >> k >> d1 >> d2; int s[] = {1, -1}; bool ok = false; for (int i = (0); i < (2); ++i) for (int j = (0); j < (2); ++j) { int64_t sum = s[i] * d1 + s[j] * d2; int64_t dgames = k - sum; if (dgames % 3 != 0) continue; int64_t w[3]; w[0] = dgames / 3 + s[i] * d1; w[1] = dgames / 3; w[2] = dgames / 3 + s[j] * d2; sort(begin(w), begin(w) + 3); if (w[0] < 0) continue; int64_t nd = w[2] * 2 - w[0] - w[1]; if (nd <= n - k && (n - k - nd) % 3 == 0) { ok = true; break; } } if (ok) 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; bool solve(long long n, long long k, long long d1, long long d2) { if ((n % 3) != 0) { return false; } for (int s1 = -1; s1 <= 1; s1 += 2) { for (int s2 = -1; s2 <= 1; s2 += 2) { long long dd1 = s1 * d1; long long dd2 = s2 * d2; if (((k - dd1 + dd2) % 3) != 0) { continue; } long long b = (k - dd1 + dd2) / 3; long long a = b + dd1; long long c = b - dd2; if (((n - k + dd1 - dd2) % 3) != 0) { continue; } long long bp = (n - k + dd1 - dd2) / 3; long long ap = bp - dd1; long long cp = bp + dd2; if (a >= 0 && b >= 0 && c >= 0 && ap >= 0 && bp >= 0 && cp >= 0 && a <= k && b <= k && c <= k && ap <= n - k && bp <= n - k && cp <= n - k && a + ap == n / 3 && b + bp == n / 3 && c + cp == n / 3) { return true; } } } return false; } int main() { long long m, n, k, d1, d2; cin >> m; for (int i = 0; i < m; i++) { cin >> n >> k >> d1 >> d2; if (solve(n, k, d1, d2)) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Scanner; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top * * @author mthai */ 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); CF_451C solver = new CF_451C(); solver.solve(1, in, out); out.close(); } static class CF_451C { long max(long... v) { long m = Long.MIN_VALUE; for (long i : v) if (i > m) m = i; return m; } public void solve(int testNumber, Scanner input, PrintWriter out) { ShortScanner in = new ShortScanner(input); int t = in.i(); while (t-- > 0) { long n = in.l(), k = in.l(), d1 = in.l(), d2 = in.l(); List<Long> rs = cal(k, d1, d2); String ans = "no"; for (long l : rs) { if (n - k - l >= 0 && (n - k - l) % 3 == 0 && n % 3 == 0) ans = "yes"; } out.println(ans); } } List<Long> cal(long k, long x, long y) { List<Long> rs = new ArrayList<>(); long b1 = (k - x - y) / 3; long b2 = (k - x + y) / 3; long b3 = (k + x - y) / 3; long b4 = (k + x + y) / 3; if (b1 >= 0 && b1 * 3 + x + y == k) { long mx = max(b1 + x, b1 + y); rs.add(mx - b1 - x + mx - b1 + mx - b1 - y); } if (b2 >= 0 && b2 * 3 + x - y == k && b2 - y >= 0) { long mx = b2 + x; rs.add(mx - b2 - x + mx - b2 + mx - b2 + y); } if (b3 >= 0 && b3 * 3 - x + y == k && b3 - x >= 0) { long mx = b3 + y; rs.add(mx - b3 + x + mx - b3 + mx - b3 - y); } if (b4 >= 0 && b4 * 3 - x - y == k && b4 - x >= 0 && b4 - y >= 0) { long mx = b4; rs.add(mx - b4 + x + mx - b4 + mx - b4 + y); } return rs; } class ShortScanner { Scanner in; ShortScanner(Scanner in) { this.in = in; } int i() { return in.nextInt(); } long l() { return in.nextLong(); } } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; const int N = 111111; const int INF = 1000000000, mod = 1000000007; const long long LLINF = 1000000000000000000ll; long long n, k, d1, d2; bool try_solve(long long d1, long long d2) { if (n % 3) return 0; if ((k - d1 + d2) % 3) return 0; long long x2 = (k - d1 + d2) / 3; if (x2 >= 0 && x2 <= k) { long long x1 = d1 + x2; long long x3 = x2 - d2; if (x1 >= 0 && x1 <= k && x3 >= 0 && x3 <= k && x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) { return 1; } } return 0; } int main() { int t; scanf("%d", &t); while (t-- > 0) { scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2); bool can = 0; for (int i = -1; i <= 1; ++i) { for (int j = -1; j <= 1; ++j) { if (i == 0 || j == 0) continue; can |= try_solve(d1 * i, d2 * j); } } puts(can ? "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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main1 { static boolean isValid(long n, long k, long d1, long d2) { if (n % 3 != 0 || (k + 2 * d1 + d2) % 3 != 0) return false; long d = n / 3; long x1 = (k + 2 * d1 + d2) / 3; long x2 = x1 - d1; long x3 = x2 - d2; return x1 >= 0 && x2 >= 0 && x3 >= 0 && x1 <= d && x2 <= d && x3 <= d; } public static void main(String[] args) throws Throwable { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { long n = sc.nextLong(), k = sc.nextLong(), d1 = sc.nextLong(), d2 = sc.nextLong(); boolean ans = isValid(n, k, d1, d2) || isValid(n, k, -d1, d2) || isValid(n, k, d1, -d2) || isValid(n, k, -d1, -d2); sb.append(ans ? "yes\n" : "no\n"); } System.out.print(sb); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.io.*; import java.math.*; import java.util.*; import java.lang.*; public class Main { public static InputStream inputStream = System.in; public static OutputStream outputStream = System.out; public static FastReader in = new FastReader(inputStream); public static PrintWriter out = new PrintWriter(outputStream); static BigInteger five = BigInteger.valueOf(5); static BigInteger two = BigInteger.valueOf(2); public static void main(String[] args) { new Main().run(); } void run(){ int t = in.nextInt(); for(int i=0; i<t; i++){ long games = in.nextLong(); long k = in.nextLong(); long d1 = in.nextLong(); long d2 = in.nextLong(); if(done(games, k, d1, d2)){ System.out.println("yes"); }else if(done(games, k, -d1, d2)){ System.out.println("yes"); }else if(done(games, k, d1, -d2)){ System.out.println("yes"); }else if(done(games, k, -d1, -d2)){ System.out.println("yes"); }else{ System.out.println("no"); } } } boolean done(long games, long k, long d1, long d2){ long n1 = (k+d2+2*d1)/3; long n2 = (k+d2-d1)/3; long n3 = (k-d1-2*d2)/3; if(n1-n2 != d1 || n2-n3!=d2 || n1+n2+n3!=k || n1<0 || n2<0 || n3<0){ return false; } //System.out.println(n1 + " " + n2 + " " + n3); long n = games-k; long wins1 = (2*n+2*n2-4*n1+2*n3)/6; long wins3 = (n+n2-2*n3+n1)/3; long wins2 = (n3-2*n2+n+n1)/3; //System.out.println(wins1 + " " + wins2 + " " + wins3); if((2*n+2*n2-4*n1+2*n3)%6 != 0 || (n+n2-2*n3+n1)%3 != 0 || (n3-2*n2+n+n1)%3 != 0){ return false; } if(wins1>=0 && wins2>=0 && wins3>=0 && wins1+wins2+wins3==n && n1+wins1==n2+wins2 && n2+wins2 == n3+wins3){ return true; }else{ return false; } } boolean inc(int[] arr){ for(int i=1; i<arr.length; i++){ if(arr[i] <= arr[i-1]){ return false; } } return true; } void exch(int[] arr, int a, int b){ for(int i=a, j=b; i<=j; i++, j--){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int[][] arr = new int[101][101]; boolean win(int n, int m){ if(arr[n][m] != 0){ if(arr[n][m]==1){ return true; } return false; } if(n==0 || m==0){ return false; } if(!win(n-1, m-1)){ arr[n][m] = 1; //System.out.println("arr[" + n + "] [" + m + "] = " + arr[n][m]); return true; } //System.out.println("arr[" + n + "] [" + m + "] = " + arr[n][m]); arr[n][m] = 2; return false; } } class Obj{ char val; int ht; HashMap<Character, Obj> kids; public Obj(char c){ val = c; ht = -1; kids = new HashMap<Character, Obj>(); } } class FastReader{ private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream){ this.stream = stream; } public int read(){ if (numChars == -1){ throw new InputMismatchException (); } if (curChar >= numChars){ curChar = 0; try{ numChars = stream.read (buf); } catch (IOException e){ throw new InputMismatchException (); } if (numChars <= 0){ return -1; } } return buf[curChar++]; } public int peek(){ if (numChars == -1){ return -1; } if (curChar >= numChars){ curChar = 0; try{ numChars = stream.read (buf); } catch (IOException e){ return -1; } if (numChars <= 0){ return -1; } } return buf[curChar]; } public int nextInt(){ int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-'){ sgn = -1; c = read (); } int res = 0; do{ if(c==','){ c = read(); } if (c < '0' || c > '9'){ throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public long nextLong(){ int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-'){ sgn = -1; c = read (); } long res = 0; do{ if (c < '0' || c > '9'){ throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public String nextString(){ int c = read (); while (isSpaceChar (c)) c = read (); StringBuilder res = new StringBuilder (); do{ res.appendCodePoint (c); c = read (); } while (!isSpaceChar (c)); return res.toString (); } public boolean isSpaceChar(int c){ if (filter != null){ return filter.isSpaceChar (c); } return isWhitespace (c); } public static boolean isWhitespace(int c){ return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0(){ StringBuilder buf = new StringBuilder (); int c = read (); while (c != '\n' && c != -1){ if (c != '\r'){ buf.appendCodePoint (c); } c = read (); } return buf.toString (); } public String nextLine(){ String s = readLine0 (); while (s.trim ().length () == 0) s = readLine0 (); return s; } public String nextLine(boolean ignoreEmptyLines){ if (ignoreEmptyLines){ return nextLine (); }else{ return readLine0 (); } } public BigInteger nextBigInteger(){ try{ return new BigInteger (nextString ()); } catch (NumberFormatException e){ throw new InputMismatchException (); } } public char nextCharacter(){ int c = read (); while (isSpaceChar (c)) c = read (); return (char) c; } public double nextDouble(){ int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-'){ sgn = -1; c = read (); } double res = 0; while (!isSpaceChar (c) && c != '.'){ if (c == 'e' || c == 'E'){ return res * Math.pow (10, nextInt ()); } if (c < '0' || c > '9'){ throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } if (c == '.'){ c = read (); double m = 1; while (!isSpaceChar (c)){ if (c == 'e' || c == 'E'){ return res * Math.pow (10, nextInt ()); } if (c < '0' || c > '9'){ throw new InputMismatchException (); } m /= 10; res += (c - '0') * m; c = read (); } } return res * sgn; } public boolean isExhausted(){ int value; while (isSpaceChar (value = peek ()) && value != -1) read (); return value == -1; } public String next(){ return nextString (); } public SpaceCharFilter getFilter(){ return filter; } public void setFilter(SpaceCharFilter filter){ this.filter = filter; } public interface SpaceCharFilter{ public boolean isSpaceChar(int ch); } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.math.BigInteger; import java.nio.ByteBuffer; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Stack; //: God does not play dice with the world :) public class Main{ public static void main(String[] args) throws IOException { // String IN = "C:\\Users\\ugochukwu.okeke\\Desktop\\in.file"; // String OUT = "C:\\Users\\ugochukwu.okeke\\Desktop\\out.file"; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); //System.out.println((-50+50*15)%15); int T = Integer.parseInt(br.readLine()); String[] in; while(T-->0) { in = br.readLine().split(" "); long n = Long.parseLong(in[0]); long k = Long.parseLong(in[1]); long d1 = Long.parseLong(in[2]); long d2 = Long.parseLong(in[3]); boolean seen=false; if(n%3!=0) { bw.append("no\n");continue; } if(k-d1+2l*d2>=0 && (k-d1+2l*d2)%3==0 ) { long x = (k-d1+2l*d2)/3; if(x <= n/3 && x>=x-d2 && x>=x-d2+d1 && x-d2>=0 && x-d2+d1>=0&& 3l*x-2l*d2+d1==k) { //System.out.println("got 1"); seen=true; } } if(!seen && k+d1+2l*d2>=0 && (k+d1+2l*d2)%3==0) { long x = (k+d1+2l*d2)/3; if(x <= n/3 && x-d1-d2>=0 && x-d2>=0 && x>=x-d1-d2 && x>=x-d2 && 3l*x-2l*d2-d1==k) { //System.out.println("got 2"); seen=true; } } if(!seen && k+d1+d2>=0 && (k+d1+d2)%3==0) { long x = (k+d1+d2)/3; if(x <= n/3 && x>=x-d1 && x>=x-d2 && x-d1>=0 && x-d2>=0 && 3l*x-d1-d2==k) { //System.out.println("got 3"); seen=true; } } if(!seen && k+d2+2l*d1>=0 && (k+d2+2l*d1)%3==0) { long x = (k+d2+2l*d1)/3; if(x <= n/3 && x-d1>=0 && x-d1-d2>=0 && x>=x-d1 && x>=x-d1-d2 && 3l*x-2l*d1-d2==k) { //System.out.println("got 4"); seen=true; } } if(!seen && k-d2+2l*d1>=0 && (k-d2+2l*d1)%3==0 && d1>=d2) { long x = (k-d2+2l*d1)/3; if(x <= n/3 && x-d1>=0 && x-d1+d2>=0 && x>=x-d1 && x>=x-d1+d2 && 3l*x+d2-2l*d1==k) { //System.out.println("got 5"); seen=true; } } if(seen) { bw.append("yes\n"); } else{ bw.append("no\n"); } } bw.close(); } //999999999980 258442058745 258442058715 258442058715 }
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
# CodeForces Problem 451C # Predict Outcome of the Game # Brian Zhang, bcubed346 # # First line contains the number of test cases (1 <= t < 10^5) # Remaining t lines contain n, k, x, y def helper(m, k, x, y): a = k + 2*x + y b = k - x + y c = k - x - 2*y if (a % 3 != 0) or (b % 3 != 0) or (c % 3 != 0): return False a = a / 3 b = b / 3 c = c / 3 if (a < 0) or (a > m): return False if (b < 0) or (b > m): return False if (c < 0) or (c > m): return False return True def compute(n, k, x, y): if (n % 3 != 0): print "no" return m = n / 3 # number of games each team should win if helper(m, k, x, y): print "yes" return if helper(m, k, x, -y): print "yes" return if helper(m, k, -x, y): print "yes" return if helper(m, k, -x, -y): print "yes" return print "no" def main(): lst = map(int, raw_input().split()) t = lst[0] inputs = [[0 for j in range(4)] for i in range(t)] for i in range(t): inputs[i] = map(int, raw_input().split()) for i in range(t): n = inputs[i][0] k = inputs[i][1] x = inputs[i][2] y = inputs[i][3] compute(n, k, x, y) if __name__ == '__main__': main()
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 sys def check(a, b, c, n, k): need = n // 3 return ((n - k) == (need - a) + (need - b) + (need - c) and a <= need and b <= need and c <= need and a >= 0 and b >= 0 and c >= 0) for tc in range(int(sys.stdin.readline())): n,k,d1,d2 = map(int, sys.stdin.readline().split()) if n % 3 != 0: print('no') continue ans = False #case++ a = k - 2 * d1 - d2 if a % 3 == 0: a //= 3 ans |= check(a, a + d1, a + d1 + d2, n ,k) #case+- a = k + d2 - 2 * d1 if a % 3 == 0: a //= 3 ans |= check(a, a + d1, a + d1 - d2, n, k) #case-- a = k + 2 * d1 + d2 if a % 3 == 0: a //= 3 ans |= check(a, a - d1, a - d1 - d2, n, k) #case-+ a = k - d2 + 2 * d1 if a % 3 == 0: a //= 3 ans |= check(a, a - d1, a - d1 + d2, n, k) print('yes' if ans else 'no')
PYTHON3
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; const int INF = 2e9 + 9; int main() { int T; cin >> T; long long N, K, D1, D2, a, b, c; while (T--) { cin >> N >> K >> D1 >> D2; long long med = N; if (N % 3 != 0) cout << "no" << endl; else { bool f = 0; a = 2 * D1 + D2 + K; b = -1 * D1 + D2 + K; c = -1 * D1 - 2 * D2 + K; if (a >= 0 and b >= 0 and c >= 0 and a % 3 == 0 and b % 3 == 0 and c % 3 == 0 and a <= med and b <= med and c <= med) f = 1; a = -2 * D1 + D2 + K; b = D1 + D2 + K; c = D1 - 2 * D2 + K; if (a >= 0 and b >= 0 and c >= 0 and a % 3 == 0 and b % 3 == 0 and c % 3 == 0 and a <= med and b <= med and c <= med) f = 1; a = 2 * D1 - 1 * D2 + K; b = -1 * D1 - 1 * D2 + K; c = -1 * D1 + 2 * D2 + K; if (a >= 0 and b >= 0 and c >= 0 and a % 3 == 0 and b % 3 == 0 and c % 3 == 0 and a <= med and b <= med and c <= med) f = 1; a = -2 * D1 - 1 * D2 + K; b = D1 - 1 * D2 + K; c = D1 + 2 * D2 + K; if (a >= 0 and b >= 0 and c >= 0 and a % 3 == 0 and b % 3 == 0 and c % 3 == 0 and a <= med and b <= med and c <= med) f = 1; if (f) cout << "yes" << endl; else 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 com.sun.org.apache.xpath.internal.operations.String; import java.io.*; import java.util.*; public class scratch_25 { // int count=0; //static long count=0; static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } public static class Graph{ public static class Vertex{ HashMap<Integer,Integer> nb= new HashMap<>(); // for neighbours of each vertex } public static HashMap<Integer,Vertex> vt; // for vertices(all) public Graph(){ vt= new HashMap<>(); } public static int numVer(){ return vt.size(); } public static boolean contVer(int ver){ return vt.containsKey(ver); } public static void addVer(int ver){ Vertex v= new Vertex(); vt.put(ver,v); } public static int numEdg(){ int count=0; ArrayList<Integer> vrtc= new ArrayList<>(vt.keySet()); for (int i = 0; i <vrtc.size() ; i++) { count+=(vt.get(vrtc.get(i))).nb.size(); } return count/2; } public static boolean contEdg(int ver1, int ver2){ if(vt.get(ver1)==null || vt.get(ver2)==null){ return false; } Vertex v= vt.get(ver1); if(v.nb.containsKey(ver2)){ return true; } return false; } public static void addEdge(int ver1, int ver2, int weight){ if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){ return; } Vertex v1= vt.get(ver1); Vertex v2= vt.get(ver2); v1.nb.put(ver2,weight); // if previously there is an edge, then this replaces that edge v2.nb.put(ver1,weight); } public static void delEdge(int ver1, int ver2){ if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){ return; } vt.get(ver1).nb.remove(ver2); vt.get(ver2).nb.remove(ver1); } public static void delVer(int ver){ if(!vt.containsKey(ver)){ return; } Vertex v1= vt.get(ver); ArrayList<Integer> arr= new ArrayList<>(v1.nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int s= arr.get(i); vt.get(s).nb.remove(ver); } vt.remove(ver); } public class Pair{ int vname; ArrayList<Integer> psf= new ArrayList<>(); // path so far int dis; int col; } public HashMap<Integer,Integer> bfs(int src){ HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye HashMap<Integer, Integer> ans= new HashMap<>(); LinkedList<Pair> queue= new LinkedList<>(); // for bfs queue Pair strtp= new Pair(); strtp.vname= src; ArrayList<Integer> ar= new ArrayList<>(); ar.add(src); strtp.psf= ar; strtp.dis=0; queue.addLast(strtp); while(!queue.isEmpty()){ Pair rp = queue.removeFirst(); if(prcd.containsKey(rp.vname)){ continue; } prcd.put(rp.vname,true); ans.put(rp.vname,rp.dis); // if(contEdg(rp.vname,dst)){ // return true; // } Vertex a= vt.get(rp.vname); ArrayList<Integer> arr= new ArrayList<>(a.nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int s= arr.get(i); if(!prcd.containsKey(s)){ Pair np = new Pair(); np.vname= s; np.dis+=rp.dis+a.nb.get(s); np.psf.addAll(rp.psf); np.psf.add(s); // np.psf.add(s); // np.psf= rp.psf+" "+s; queue.addLast(np); } } } return ans; // return false; } public HashMap<Integer,Integer> dfs(int src){ HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye HashMap<Integer, Integer> ans= new HashMap<>(); LinkedList<Pair> stack= new LinkedList<>(); // for bfs queue Pair strtp= new Pair(); strtp.vname= src; ArrayList<Integer> ar= new ArrayList<>(); ar.add(src); strtp.psf= ar; strtp.dis=0; stack.addFirst(strtp); while(!stack.isEmpty()){ Pair rp = stack.removeFirst(); if(prcd.containsKey(rp.vname)){ continue; } prcd.put(rp.vname,true); ans.put(rp.vname,rp.dis); // if(contEdg(rp.vname,dst)){ // return true; // } Vertex a= vt.get(rp.vname); ArrayList<Integer> arr= new ArrayList<>(a.nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int s= arr.get(i); if(!prcd.containsKey(s)){ Pair np = new Pair(); np.vname= s; np.dis+=rp.dis+a.nb.get(s); np.psf.addAll(rp.psf); np.psf.add(s); // np.psf.add(s); // np.psf= rp.psf+" "+s; stack.addFirst(np); } } } return ans; // return false; } public boolean isCycle(){ HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye // HashMap<Integer, Integer> ans= new HashMap<>(); LinkedList<Pair> queue= new LinkedList<>(); // for bfs queue ArrayList<Integer> keys = new ArrayList<>(vt.keySet()); for (int i = 0; i <keys.size(); i++) { int cur= keys.get(i); if(prcd.containsKey(cur)){ continue; } Pair sp = new Pair(); sp.vname= cur; ArrayList<Integer> as= new ArrayList<>(); as.add(cur); sp.psf= as; queue.addLast(sp); while(!queue.isEmpty()){ Pair rp= queue.removeFirst(); if(prcd.containsKey(rp.vname)){ return true; } prcd.put(rp.vname,true); Vertex v1= vt.get(rp.vname); ArrayList<Integer> nbrs= new ArrayList<>(v1.nb.keySet()); for (int j = 0; j <nbrs.size() ; j++) { int u= nbrs.get(j); Pair np= new Pair(); np.vname= u; queue.addLast(np); } } } return false; } public ArrayList<ArrayList<Integer>> genConnctdComp(){ HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye ArrayList<ArrayList<Integer>> ans= new ArrayList<>(); // HashMap<Integer, Integer> ans= new HashMap<>(); LinkedList<Pair> queue= new LinkedList<>(); // for bfs queue // int con=-1; ArrayList<Integer> keys = new ArrayList<>(vt.keySet()); for (int i = 0; i <keys.size(); i++) { int cur= keys.get(i); if(prcd.containsKey(cur)){ //return true; continue; } int count=0; ArrayList<Integer> fu= new ArrayList<>(); fu.add(cur); Pair sp = new Pair(); sp.vname= cur; ArrayList<Integer> as= new ArrayList<>(); as.add(cur); sp.psf= as; queue.addLast(sp); while(!queue.isEmpty()){ Pair rp= queue.removeFirst(); if(prcd.containsKey(rp.vname)){ //return true; continue; } prcd.put(rp.vname,true); fu.add(rp.vname); Vertex v1= vt.get(rp.vname); ArrayList<Integer> nbrs= new ArrayList<>(v1.nb.keySet()); for (int j = 0; j <nbrs.size() ; j++) { int u= nbrs.get(j); if(!prcd.containsKey(u)){ count++; Pair np= new Pair(); np.vname= u; queue.addLast(np); } }} fu.add(count); ans.add(fu); } //return false; return ans; } public boolean isBip(int src){ // only for connected graph HashMap<Integer,Integer> clr= new HashMap<>(); // colors are 1 and -1 LinkedList<Integer>q = new LinkedList<Integer>(); clr.put(src,1); q.add(src); while(!q.isEmpty()){ int u = q.getFirst(); q.pop(); ArrayList<Integer> arr= new ArrayList<>(vt.keySet()); for (int i = 0; i <arr.size() ; ++i) { int x= arr.get(i); if(vt.get(u).nb.containsKey(x) && !clr.containsKey(x)){ if(clr.get(u)==1){ clr.put(x,-1); } else{ clr.put(x,1); } q.push(x); } else if(vt.get(u).nb.containsKey(x) && (clr.get(x)==clr.get(u))){ return false; } } } return true; } public static void printGr() { ArrayList<Integer> arr= new ArrayList<>(vt.keySet()); for (int i = 0; i <arr.size() ; i++) { int ver= arr.get(i); Vertex v1= vt.get(ver); ArrayList<Integer> arr1= new ArrayList<>(v1.nb.keySet()); for (int j = 0; j <arr1.size() ; j++) { System.out.println(ver+"-"+arr1.get(j)+":"+v1.nb.get(arr1.get(j))); } } } } static class Cus implements Comparable<Cus>{ int size; int mon; int num; public Cus(int size,int mon,int num){ this.size=size; this.mon=mon; this.num=num; } @Override public int compareTo(Cus o){ if(this.mon!=o.mon){ return this.mon-o.mon;} else{ return o.size-this.size; } } } static class Table implements Comparable<Table>{ int go; int cum; int cost; public Table(int go,int cum,int cost){ this.go=go; this.cum=cum; this.cost=cost; } @Override public int compareTo(Table o){ if(this.go==o.go){ return this.cum-o.cum; } return this.go-o.go; } } static class Table1 implements Comparable<Table1>{ int go; int cum; int cost; public Table1(int go,int cum,int cost){ this.go=go; this.cum=cum; this.cost=cost; } @Override public int compareTo(Table1 o){ return this.cost-o.cost; }} public static class DisjointSet{ HashMap<Integer,Node> mp= new HashMap<>(); public static class Node{ int data; Node parent; int rank; } public void create(int val){ Node nn= new Node(); nn.data=val; nn.parent=nn; nn.rank=0; mp.put(val,nn); } public int find(int val){ return findn(mp.get(val)).data; } public Node findn(Node n){ if(n==n.parent){ return n; } Node rr= findn(n.parent); n.parent=rr; return rr; } public void union(int val1, int val2){ // can also be used to check cycles Node n1= findn(mp.get(val1)); Node n2= findn(mp.get(val2)); if(n1.data==n2.data) { return; } if(n1.rank<n2.rank){ n1.parent=n2; } else if(n2.rank<n1.rank){ n2.parent=n1; } else { n2.parent=n1; n1.rank++; } } } public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t= Reader.nextInt(); for (int i = 0; i <t ; i++) { long n= Reader.nextLong(); long x= Reader.nextLong(); long d1= Reader.nextLong(); long d2= Reader.nextLong(); boolean b1=false; long b; long a; long c; long rem= n-x; if((x-d1-d2)%3==0 && (x+2*d1-d2)%3==0 && (x+2*d2-d1)%3==0){ // System.out.println(1); b= (x-(d1+d2))/3; a=(x+2*d1-d2)/3; c= (x+2*d2-d1)/3; if((n)%3==0 && (n/3)>=Math.max(a,Math.max(b,c)) && Math.min(a,Math.min(b,c))>=0) { // System.out.println(1); b1=true; System.out.println("yes"); } } if(!b1 && (x+d1+d2)%3==0 && (x-2*d1+d2)%3==0 && (x-2*d2+d1)%3==0 && (n)%3==0 && (n/3)>=Math.max((x+d1+d2)/3,Math.max((x-2*d1+d2)/3, (x-2*d2+d1)/3)) && Math.min((x+d1+d2)/3,Math.min((x-2*d1+d2)/3, (x-2*d2+d1)/3))>=0){ b1=true; // System.out.println(2); System.out.println("yes"); } if(!b1 && (x-d1+d2)%3==0 && (x+2*d1+d2)%3==0 && (x-d1-2*d2)%3==0 && (n)%3==0 && (n/3)>=Math.max((x-d1+d2)/3,Math.max( (x+2*d1+d2)/3,(x-d1-2*d2)/3)) && Math.min((x-d1+d2)/3,Math.min( (x+2*d1+d2)/3,(x-d1-2*d2)/3))>=0) { b1=true; // System.out.println(3); System.out.println("yes"); } long q= d2; d2=d1; d1=q; if(!b1 && (x-d1+d2)%3==0 && (x+2*d1+d2)%3==0 && (x-d1-2*d2)%3==0 && (n)%3==0 && (n/3)>=Math.max((x-d1+d2)/3, Math.max((x+2*d1+d2)/3,(x-d1-2*d2)/3 )) && Math.min((x-d1+d2)/3, Math.min((x+2*d1+d2)/3,(x-d1-2*d2)/3 ))>=0){ b1=true; // System.out.println(4); System.out.println("yes"); } if(!b1){ System.out.println("no"); } } out.flush(); out.close(); } static int countDivisibles(int A, int B, int M) { // Add 1 explicitly as A is divisible by M if (A % M == 0) return (B / M) - (A / M) + 1; // A is not divisible by M return (B / M) - (A / M); } public static void df(int x, int y,int n, int m, char[][]ar, int l){ boolean done[][]= new boolean[n][m]; int count=0; LinkedList<int[]> stk= new LinkedList<>(); int a[]= {x,y}; stk.addFirst(a); while(!stk.isEmpty() && count<l){ // count++; int b[]=stk.removeFirst(); if( done[b[0]][b[1]]){ continue; } count++; done[b[0]][b[1]]=true; ar[b[0]][b[1]]='@'; int arr[][]={{1,0},{-1,0},{0,1},{0,-1}}; for (int i = 0; i <arr.length ; i++) { if(!(b[0]+arr[i][0]>=n || b[1]+arr[i][1]>=m || b[0]+arr[i][0]<0 || b[1]+arr[i][1]<0 || ar[b[0]+arr[i][0]][b[1]+arr[i][1]]=='#')){ int r[]= {b[0]+arr[i][0], b[1]+arr[i][1]}; stk.addFirst(r); // continue; } }} for (int i = 0; i <n ; i++) { for (int j = 0; j <m ; j++) { if(ar[i][j]=='.'){ System.out.print('X'); } else if(ar[i][j]=='@'){ System.out.print('.'); } else{ System.out.print(ar[i][j]); } } System.out.println(); } } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd1(long a, long b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater //count++; if (a > b){ return gcd(a-b, b); } return gcd(a, b-a); } // method to return LCM of two numbers static long lcm(long a, long b) { return (a*b)/gcd(a, b); } // Driver method static int partition(long arr[],long arr1[], int low, int high) { long pivot = arr[high]; int i = (low-1); // index of smaller element for (int j=low; j<high; j++) { // If current element is smaller than the pivot if (arr[j] < pivot) { i++; // swap arr[i] and arr[j] long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; long temp1=arr1[i]; arr1[i]=arr1[j]; arr1[j]=temp1; } } // swap arr[i+1] and arr[high] (or pivot) long temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; long temp1= arr1[i+1]; arr1[i+1]=arr1[high]; arr1[high]= temp1; return i+1; } /* The main function that implements QuickSort() arr[] --> Array to be sorted, low --> Starting index, high --> Ending index */ static void sort(long arr[],long arr1[], int low, int high) { if (low < high) { /* pi is partitioning index, arr[pi] is now at right place */ int pi = partition(arr,arr1, low, high); // Recursively sort elements before // partition and after partition sort(arr,arr1, low, pi-1); sort(arr,arr1, pi+1, high); } } public static ArrayList<Integer> Sieve(int n) { boolean arr[]= new boolean [n+1]; Arrays.fill(arr,true); arr[0]=false; arr[1]=false; for (int i = 2; i*i <=n ; i++) { if(arr[i]){ for (int j = 2; j <=n/i ; j++) { int u= i*j; arr[u]=false; }} } ArrayList<Integer> ans= new ArrayList<>(); for (int i = 0; i <n+1 ; i++) { if(arr[i]){ ans.add(i); } } 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
def cal(x,y): return x*y def cal_equation(a,b,c,n): total = a + b + c if total >= 0 and total % 3 == 0 and total <= n: return 1 return 0 alist = [[1,0,1],[1,0,-1],[-1,0,1],[-1,0,-1]] n = int(raw_input()) for i in range(0,n): a = map(int, raw_input().split()) b = [a[2],0,a[3]] ok = False if a[0] % 3 != 0: print 'no' continue for j in range(0,4): tmp = map(cal,alist[j],b) x = cal_equation(a[1],2*tmp[0],tmp[2],a[0]) y = cal_equation(a[1],-1*tmp[0],tmp[2],a[0]) z = cal_equation(a[1],-1*tmp[0],-2*tmp[2],a[0]) #print x,y,z if x and y and z: 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
#include <bits/stdc++.h> long long val(long long *a, long long *b, long long *c, long long k, long long d1, long long d2) { *c = k - (d1 + 2 * d2); if (*c % 3 != 0) return 0; *c /= 3; *b = *c + d2; *a = *c + d1 + d2; } int main() { long long t, a, b, c, n, k, d1, d2, i, j; scanf("%lld", &t); for (i = 1; i <= t; i++) { scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); continue; } a = -1; b = -1; c = -1; val(&a, &b, &c, k, d1, d2); if (!(a < 0 || b < 0 || c < 0 || a > n / 3 || b > n / 3 || c > n / 3 || a + b + c != k)) { printf("yes\n"); continue; } val(&a, &b, &c, k, d1, -d2); if (!(a < 0 || b < 0 || c < 0 || a > n / 3 || b > n / 3 || c > n / 3 || a + b + c != k)) { printf("yes\n"); continue; } val(&a, &b, &c, k, -d1, d2); if (!(a < 0 || b < 0 || c < 0 || a > n / 3 || b > n / 3 || c > n / 3 || a + b + c != k)) { printf("yes\n"); continue; } val(&a, &b, &c, k, -d1, -d2); if (!(a < 0 || b < 0 || c < 0 || a > n / 3 || b > n / 3 || c > n / 3 || a + b + c != k)) { 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.util.Scanner; public class cf258c { private static boolean judge(long n,long k,long d1,long d2) { long[] p1={-d1,d1}; long[] p2={-d2,d2}; long[] a=new long[5]; long sum; for(int i=0;i<=1;i++) { for(int j=0;j<=1;j++) { a[0]=0; a[1]=p1[i]; a[2]=p1[i]+p2[j]; sum=a[0]+a[1]+a[2]; // System.out.println(k+" "+sum); if(k-sum<0||(k-sum)%3!=0) continue; long x=(k-sum)/3; long b[]={x,x+a[1],x+a[2]}; if(n%3!=0) continue; boolean flag=true; // System.out.println(b[0]+" "+b[1]+" "+b[2]); for(int r=0;r<=2;r++) { if(b[r]>n/3||b[r]<0) flag=false; } if(flag) return true; } } return false; } public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int t=scanner.nextInt(); while(t>0) { t--; long n=scanner.nextLong(); long k=scanner.nextLong(); long d1=scanner.nextLong(); long d2=scanner.nextLong(); boolean z=judge(n,k,d1,d2); if(z) 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 int gcd(long long int a, long long int b) { if (b < a) { long long int temp = a; a = b; b = temp; } if (a == 0) { return b; } return gcd(b % a, a); } long long int lcm(long long int a, long long int b) { return (a * b / gcd(a, b)); } long long int fact(long long int x) { long long int ans = 1; if (x == 0) { return 1; } while (x > 0) { ans = (ans * x) % 1000000007; x--; } return ans % 1000000007; } long long int power(long long int a, long long int b) { long long int ans = 1; while (b > 0) { if (b % 2 == 1) { ans *= a; ans = ans; } b /= 2; a *= a; a = a; } return ans; } vector<long long int> getbinary(long long int x) { vector<long long int> bin(20000, 0); long long int iter = 0; while (x > 0) { if (x % 2 == 1) { bin[iter] = 1; } else { bin[iter] = 0; } iter++; x /= 2; } return bin; } using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, k, d1, d2, i, j, x1, x2, x3, p1, p2, y = 0; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no" << endl; continue; } for (i = -1; i < 2; i++) { for (j = -1; j < 2; j++) { if (i == 0 || j == 0) continue; p1 = i * d1; p2 = j * d2; if ((k + p2 - p1) % 3 != 0) continue; x1 = (k + p2 - p1) / 3; if (x1 >= 0 && x1 <= k) { x2 = x1 + p1; x3 = x1 - p2; if (x2 >= 0 && x2 <= k && x3 >= 0 && x3 <= k) { if (x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) { y = 1; i = 5; j = 5; } } } } } if (y) cout << "yes" << endl; else 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.util.*; public class C{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int d[] = {1,-1}; while(N-->0){ long n = sc.nextLong(); long k = sc.nextLong(); long d1 = sc.nextLong(); long d2 = sc.nextLong(); boolean has = false; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++){ long rem = k - d1*d[i] - d2*d[j]; if(rem < 0) continue; if(rem % 3 != 0) continue; rem/=3; if(rem + d1*d[i] < 0) continue; if(rem + d2*d[j] < 0) continue; if(rem + d1*d[i] > n/3) continue; if(rem + d2*d[j] > n/3) continue; if(rem < 0) continue; if(rem > n/3) continue; has = true; } if(has && n%3 == 0) System.out.println("yes"); else System.out.println("no"); } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, k, d1, d2, rem; cin >> n >> k >> d1 >> d2; rem = n - k; if (n % 3 != 0) { cout << "no" << endl; continue; } if (d1 + d2 + d2 <= k && (d1 + d2 + d2) % 3 == k % 3) { long long needed = d1 + d1 + d2; if ((rem - needed) >= 0 && (rem - needed) % 3 == 0) { cout << "yes" << endl; continue; } } if ((d1 + d2) <= k && (d1 + d2) % 3 == k % 3) { long long needed = abs(d1 - d2); needed += max(d1, d2); if ((rem - needed) >= 0 && (rem - needed) % 3 == 0) { cout << "yes" << endl; continue; } } if ((max(d1, d2) + abs(d1 - d2)) <= k && (max(d1, d2) + abs(d1 - d2)) % 3 == k % 3) { long long needed = d1 + d2; if ((rem - needed) >= 0 && (rem - needed) % 3 == 0) { cout << "yes\n"; continue; } } if (d1 + d2 + d1 <= k && (d1 + d1 + d2) % 3 == k % 3) { long long needed = d2 + d1 + d2; if ((rem - needed) >= 0 && (rem - needed) % 3 == 0) { 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.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputstream = System.in; OutputStream outputstream = System.out; InputReader in = new InputReader(inputstream); OutputWriter out = new OutputWriter(outputstream); mysolver mysol = new mysolver(); mysol.solve(in, out); out.close(); } } class mysolver { public void solve(InputReader in,OutputWriter out) { PrintWriter pout = new PrintWriter(new BufferedOutputStream(System.out)); int t = in.readInt(); while(t--!=0) { long n = Long.parseLong(in.readString()); long k = Long.parseLong(in.readString()); long d1 = Long.parseLong(in.readString()); long d2 = Long.parseLong(in.readString()); if(n%3!=0) { System.out.println("no"); continue; } else { int count =0; long val = k+d2-d1; long A1 = val/3+d1; long A2 = val/3; long A3 = val/3-d2; if(val%3==0 && val >=0) { if(A1>=0 && A1<=n/3 && A2>=0 && A2<=n/3 && A3>=0 && A3<=n/3) count++; } val = k-d1-d2; A1 = d1+val/3; A2 = val/3; A3 = val/3+d2; if(val%3==0 && val >=0) { if(A1>=0 && A1<=n/3 && A2>=0 && A2<=n/3 && A3>=0 && A3<=n/3) count++; } val = k+d1+d2; A1 = -d1+val/3; A2 = val/3; A3 = val/3-d2; if(val%3==0 && val >=0) { if(A1>=0 && A1<=n/3 && A2>=0 && A2<=n/3 && A3>=0 && A3<=n/3) count++; } val = k+d1-d2; A1 = -d1+val/3; A2 = val/3; A3 = val/3+d2; if(val%3==0 && val>=0) { if(A1>=0 && A1<=n/3 && A2>=0 && A2<=n/3 && A3>=0 && A3<=n/3) count++; } if(count > 0) { System.out.println("yes"); } else { System.out.println("no"); } } } pout.close(); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(int i) { writer.println(i); } } class IOUtils { public static void readIntArrays(InputReader in, int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readInt(); } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, d1, d2, f1, f2, f3; int t; scanf("%d", &t); while (t--) { scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2); if (n % 3) { puts("no"); continue; } n /= 3; if ((k + 2 * d1 + d2) % 3 == 0) { f1 = (k + 2 * d1 + d2) / 3; f2 = f1 - d1; f3 = f2 - d2; if (f1 >= 0 && f1 <= n && f2 >= 0 && f2 <= n && f3 >= 0 && f3 <= n) { puts("yes"); continue; } } if ((k + 2 * d1 - d2) % 3 == 0) { f1 = (k + 2 * d1 - d2) / 3; f2 = f1 - d1; f3 = f2 + d2; if (f1 >= 0 && f1 <= n && f2 >= 0 && f2 <= n && f3 >= 0 && f3 <= n) { puts("yes"); continue; } } if ((k - 2 * d1 + d2) % 3 == 0) { f1 = (k - 2 * d1 + d2) / 3; f2 = f1 + d1; f3 = f2 - d2; if (f1 >= 0 && f1 <= n && f2 >= 0 && f2 <= n && f3 >= 0 && f3 <= n) { puts("yes"); continue; } } if ((k - 2 * d1 - d2) % 3 == 0) { f1 = (k - 2 * d1 - d2) / 3; f2 = f1 + d1; f3 = f2 + d2; if (f1 >= 0 && f1 <= n && f2 >= 0 && f2 <= n && f3 >= 0 && f3 <= n) { 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
import java.io.InputStreamReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Lokesh Khandelwal */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.nextInt(); while (t-- > 0) { long n = in.nextLong(), k = in.nextLong(), d1 = in.nextLong(), d2 = in.nextLong(); long a[][] = new long[4][3]; a[0][0] = d1;a[0][1] = 0;a[0][2] = -d2; a[1][0] = d1;a[1][1] = 0;a[1][2] = d2; a[2][0] = -d1;a[2][1] = 0;a[2][2] = -d2; a[3][0] = -d1;a[3][1] = 0;a[3][2] = d2; boolean good[] = new boolean[4]; Arrays.fill(good, true); for(int i=0;i<4;i++) { Arrays.sort(a[i]); if(a[i][0] < 0) { a[i][2] += -a[i][0]; a[i][1] += -a[i][0]; a[i][0] = 0; } long sum = a[i][0] + a[i][1] + a[i][2]; if(sum > k) { good[i] = false; } else if(sum < k) { long rem = k - sum; if(rem % 3 == 0) { rem /= 3; a[i][0] += rem; a[i][1] += rem; a[i][2] += rem; } else { good[i] = false; } } } boolean status = false; for(int i=0;i<4;i++) { if(!good[i]) continue; long rem = n - k; long sum = a[i][0] + a[i][1] + a[i][2]; rem -= ((a[i][2] - a[i][1]) + (a[i][2] - a[i][0])); if(sum == k && rem >= 0 && rem % 3 == 0) { status = true; //DebugUtils.print(a[i]); break; } } if(status) { out.printLine("yes"); } else out.printLine("no"); } } } class InputReader { BufferedReader in; StringTokenizer tokenizer=null; public InputReader(InputStream inputStream) { in=new BufferedReader(new InputStreamReader(inputStream)); } public String next() { try{ while (tokenizer==null||!tokenizer.hasMoreTokens()) { tokenizer=new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (IOException e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; long long n, k, d1, d2; int main() { int T; scanf("%d", &T); while (T--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { puts("no"); continue; } if (k >= max(d1, d2) + abs(d1 - d2) && n - k - d1 - d2 >= 0 && (n - k - d1 - d2) % 3 == 0) { puts("yes"); continue; } if (k >= d1 + d2 + d2 && n - k - d1 - d2 - d1 >= 0 && (n - k - d1 - d2 - d1) % 3 == 0) { puts("yes"); continue; } if (k >= d1 + d2 + d1 && n - k - d1 - d2 - d2 >= 0 && (n - k - d1 - d2 - d2) % 3 == 0) { puts("yes"); continue; } if (k >= d1 + d2 && n - k - abs(d1 - d2) - max(d1, d2) >= 0 && (n - k - abs(d1 - d2) - max(d1, d2)) % 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
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } const int N = 1e5 + 7; const int xinc[] = {0, 0, 1, -1}; const int yinc[] = {1, -1, 0, 0}; long long a, b, n, k, left, need, mk; bool check() { return (k >= mk && (k - mk) % 3 == 0 && mk >= 0 && need >= 0 && n - need - k >= 0 && (n - need - k) % 3 == 0); } void solve() { cin >> n >> k >> a >> b; bool valid = false; mk = a + 2 * b, need = 2 * a + b; if (check()) valid = true; if (a < b) { mk = a + b, need = 2 * b - a; if (check()) valid = true; mk = 2 * b - a, need = a + b; if (check()) valid = true; } else { mk = a + b, need = 2 * a - b; if (check()) valid = true; mk = 2 * a - b, need = a + b; if (check()) valid = true; } mk = 2 * a + b, need = 2 * b + a; if (check()) valid = true; cout << (valid ? "yes" : "no") << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) solve(); return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.io.*; import java.lang.*; import static java.lang.Math.*; import java.lang.reflect.*; import java.math.BigInteger; import static java.math.BigInteger.*; import java.security.CodeSource; import java.security.KeyStore; import java.util.*; import static java.util.Arrays.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.logging.Level; import java.util.logging.Logger; // https://netbeans.org/kb/73/java/editor-codereference_ru.html#display public class Main { private void run() throws Exception { err = System.err; boolean oj = true; try { oj = System.getProperty("MYLOCAL") == null; } catch (Exception e) { } if( oj ){ sc = new FastScanner(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); } else{ try { sc = new FastScanner(new FileReader("input.txt")); out = new PrintWriter(new FileWriter("output.txt")); } catch ( IOException e) { MLE(); } } solve(); out.flush(); } FastScanner sc; PrintWriter out; PrintStream err; void MLE(){ int[][] arr = new int[1024*1024][]; for (int i = 0; i < arr.length; i++) { arr[i] = new int[1024*1024]; } } private int getBit(int msk, int pos) { return (msk >> pos) & 1; } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStreamReader reader) { br = new BufferedReader(reader); st = new StringTokenizer(""); } String next() { while( !st.hasMoreElements() ) try { st = new StringTokenizer(br.readLine()); } catch (IOException ex) { MLE(); } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next());} long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) throws Exception { new Main().run(); } //-------------------------------------------------------------------// String solveTest(){ long n = sc.nextLong(); long k = sc.nextLong(); long d0 = sc.nextLong(); long d1 = sc.nextLong(); for (int i = -1; i <= 1; i+=2) { for (int j = -1; j <= 1; j+=2) { long dd0 = d0 * i; long dd1 = d1 * j + dd0; long a = (k - dd0 - dd1); if( a%3!=0 ) continue; a /= 3; long b = a + dd0; long c = a + dd1; if( 0 <= a && a <= k && 0 <= b && b <= k && 0 <= c && c <= k && a + b + c == k ){ long m = max(a,max(b,c)); long cnt = 3*m - a - b - c; long rem = n - k - cnt; if( 0 <= rem && rem%3==0 ){ return "yes"; } } } } return "no"; } void solve(){ int t = sc.nextInt(); for (int i = 0; i < t; i++) { out.println( solveTest() ); } } }
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 N = 2e5 + 5; long long mod = 1e9 + 7; int T; int main() { std::ios::sync_with_stdio(0); ; cin >> T; while (T--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; long long h = n - k; long long t = h; long long a, b, c; b = 0; a = d1; c = d2; long long g = max(a, c); long long res = g + g - min(a, c); h -= res; int flag = 0; if (h >= 0 && h % 3 == 0) { if (a + b + c <= k && (k - a - b - c) % 3 == 0) flag = 1; } a = d1 + d2; b = d2; c = 0; res = d1 + d2 + d1; h = t; h -= res; if (h >= 0 && h % 3 == 0) { if (a + b + c <= k && (k - a - b - c) % 3 == 0) flag = 1; } a = 0; b = d1; c = d1 + d2; res = d2 + d2 + d1; h = t; h -= res; if (h >= 0 && h % 3 == 0) { if (a + b + c <= k && (k - a - b - c) % 3 == 0) flag = 1; } a = 0; b = d1; c = d1 - d2; if (c >= 0) { res = d1 + d2; h = t; h -= res; if (h >= 0 && h % 3 == 0) { if (a + b + c <= k && (k - a - b - c) % 3 == 0) flag = 1; } } c = 0; b = d2; a = d2 - d1; if (a >= 0) { res = d1 + d2; h = t; h -= res; if (h >= 0 && h % 3 == 0) { if (a + b + c <= k && (k - a - b - c) % 3 == 0) flag = 1; } } if (n % 3) flag = 0; if (flag) cout << "yes" << endl; else 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.util.Scanner; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); for (long T = in.nextInt(), s, t; T > 0; T--) { long n = in.nextLong(), k = in.nextLong(), p = in.nextLong(), q = in.nextLong(); boolean yes = false; s = p + p + q; t = q + q + p; // a < b b < c yes |= good(n,k,s,t); // a > b b > c yes |= good(n,k,t,s); // a > b b < c // a < b b > c s = p + q; t = Math.max(p, q) + Math.abs(p - q); yes |= good(n,k,s,t) || good(n,k,t,s); System.out.println(yes ? "yes" : "no"); } } static boolean good(long n, long k, long s, long t) { return (s <= k && (k - s) % 3 == 0) && (n - k >= t && (n - k - t) % 3 == 0); } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> int main() { long long n; scanf("%lld", &n); while (n--) { long long n, k, d1, d2; scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); long long a = d1 > d2 ? d1 : d2; ; long long b = d1 < d2 ? d1 : d2; ; int flag = 0; if (n - k >= 2 * d1 + d2 && (n - k - 2 * d1 - d2) % 3 == 0 && k >= d1 + 2 * d2 && (k - d1 - 2 * d2) % 3 == 0) flag = 1; if (n - k >= d1 + d2 && (n - k - d1 - d2) % 3 == 0 && k >= 2 * a - b && (k - 2 * a + b) % 3 == 0) flag = 1; if (n - k >= d1 + d2 * 2 && (n - k - d1 - d2 * 2) % 3 == 0 && k >= 2 * d1 + d2 && (k - 2 * d1 - d2) % 3 == 0) flag = 1; if (n - k >= a * 2 - b && (n - k - 2 * a + b) % 3 == 0 && k >= d1 + d2 && (k - d1 - d2) % 3 == 0) 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 a, b, c; long long n, k, d1, d2; bool work(int i) { a = n; if (i == 1) { b = a - d1; c = b - d2; } else if (i == 2) { b = a - d1; c = b + d2; } else if (i == 3) { b = a + d1; c = b + d2; } else { b = a + d1; c = b - d2; } long long q = min(a, min(b, c)); a -= q; b -= q; c -= q; if ((k - (a + b + c)) % 3 == 0 && a + b + c <= k) return 1; return 0; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> k >> d1 >> d2; bool ch = 0; for (int j = 0; j < 4 && !ch; j++) { if (work(j)) { long long q = max(a, max(b, c)); if ((n - k - q - q - q + a + b + c) % 3 == 0 && (n - k - q - q - q + a + b + c) >= 0) { ch = 1; cout << "yes" << endl; } } } if (!ch) 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 n, k, d1, d2; long long a, b, c; bool check() { if (a >= 0 && a <= n / 3 && b >= 0 && b <= n / 3 && c >= 0 && c <= n / 3) return 1; return 0; } void doing() { cin >> n >> k >> d1 >> d2; double aa; if (n % 3 != 0) { puts("no"); return; } aa = 0.0 + ((k + d2) - 2.0 * d1) / 3.0; if (aa >= 0 && aa == (long long)(aa)) { a = aa; b = d1 + a; c = b - d2; if (check()) { puts("yes"); return; } } aa = 0.0 + ((k - d2) - 2.0 * d1) / 3.0; if (aa >= 0 && aa == (long long)(aa)) { a = aa; b = d1 + a; c = b + d2; if (check()) { puts("yes"); return; } } aa = 0.0 + ((k + d2) + 2.0 * d1) / 3.0; if (aa >= 0 && aa == (long long)(aa)) { a = aa; b = a - d1; c = b - d2; if (check()) { puts("yes"); return; } } aa = 0.0 + ((k - d2) + 2.0 * d1) / 3.0; if (aa >= 0 && aa == (long long)(aa)) { a = aa; b = a - d1; c = b + d2; if (check()) { puts("yes"); return; } } puts("no"); } int main() { int T; scanf("%d", &T); while (T--) doing(); 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.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { Main main = new Main(); main.read(); } /* IO */ private StringBuilder ans; private BufferedReader in; private StringTokenizer tok; /* fields */ private long n; private long k; private long d1; private long d2; private void read() throws IOException { // streams boolean file = false; if (file) in = new BufferedReader(new FileReader("input.txt")); else in = new BufferedReader(new InputStreamReader(System.in)); ans = new StringBuilder(); // read all StringBuilder strb = new StringBuilder(); String str = in.readLine(); while (str != null) { strb.append(str.trim()); strb.append(" "); str = in.readLine(); } tok = new StringTokenizer(strb.toString().trim()); // read int nCases = Integer.parseInt(tok.nextToken()); for (int i = 0; i < nCases; i++) { n = Long.parseLong(tok.nextToken()); k = Long.parseLong(tok.nextToken()); d1 = Long.parseLong(tok.nextToken()); d2 = Long.parseLong(tok.nextToken()); ans.append(solve() ? "yes" : "no"); ans.append("\n"); } // output System.out.print(ans.toString()); } private boolean solve() { if (n%3 != 0) return false; // try all signs and see if we can solve the 3 equations for (long sign1 = -1; sign1 <= 1; sign1 += 2) for (long sign2 = -1; sign2 <= 1; sign2 += 2) { long sum = sign1 * d1 * 2 + sign2 * d2 + k; if (sum % 3 != 0) continue; long x1 = sum / 3; long x2 = x1 - sign1 * d1; long x3 = x2 - sign2 * d2; if (x1 >= 0 && x1 <= n / 3 && x2 >= 0 && x2 <= n / 3 && x3 >= 0 && x3 <= n / 3) return true; } return false; } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class PredictOutcomeoftheGame { static long t; static long n; static long k; static long d1; static long d2; static StringBuilder res = new StringBuilder(); public static void main(String[] args) throws IOException { PredictOutcomeoftheGame main = new PredictOutcomeoftheGame(); main.input(); } void input() throws IOException { Init(System.in); t = NextInt(); for (int i = 0; i < t; i++) { n = NextLong(); k = NextLong(); d1 = NextLong(); d2 = NextLong(); solve(n, k, d1, d2); } System.out.println(res); } void solve(long n, long k, long d1, long d2) { if (n % 3 != 0) { res.append("no\n"); return; } if (k == 0) { res.append("yes\n"); return; } long t1 = 0, t2 = 0, t3 = 0; // 1 t1 = 0; t2 = d1; t3 = t2 + d2; if (check(t1, t2, t3)) { return; } t3 = t2 - d2; if (check(t1, t2, t3)) { return; } // 2 t1 = d1; t2 = 0; t3 = t2 + d2; if (check(t1, t2, t3)) { return; } t3 = t2 - d2; if (check(t1, t2, t3)) { return; } // 3 t2 = 0; t3 = d2; t1 = t2 + d1; if (check(t1, t2, t3)) { return; } t1 = t2 - d1; if (check(t1, t2, t3)) { return; } // 4 t2 = d2; t3 = 0; t1 = t2 + d1; if (check(t1, t2, t3)) { return; } t1 = t2 - d1; if (check(t1, t2, t3)) { return; } res.append("no\n"); } boolean check(long t1, long t2, long t3) { if (t1 >= 0 && t3 >= 0 && t2 >= 0) { if (t1 + t2 + t3 <= k) { long max = t1; long mod = 0; long div = 0; if (t2 > max) { max = t2; } if (t3 > max) { max = t3; } mod = ((n - k) + (t1 + t2 + t3)) % 3; div = ((n - k) + (t1 + t2 + t3)) / 3; if (mod == 0 && div >= max) { res.append("yes\n"); return true; } } } return false; } /***************************************************************** ******************** READER ******************************* *****************************************************************/ static BufferedReader reader; static StringTokenizer tokenizer; static void Init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String Next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int NextInt() throws IOException { return Integer.parseInt(Next()); } static long NextLong() throws IOException { return Long.parseLong(Next()); } static Double NextDouble() throws IOException { 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; bool deal(long long a, long long b, long long c, long long n, long long k) { long long Min = min(a, b), nn = n - k; Min = min(Min, c); if (Min < 0) { a -= Min; b -= Min; c -= Min; } long long sum = a + b + c; if (sum > k) return false; k -= sum; if (k % 3) return false; a += k / 3; b += k / 3; c += k / 3; long long Max = max(a, b); Max = max(Max, c); long long els = 3 * Max - a - b - c; if (els > nn) return false; nn -= els; if (nn % 3) return false; return true; } int main() { int t; cin >> t; while (t--) { long long n, k, x, y; cin >> n >> k >> x >> y; int res = 0; res += deal(0, x, x + y, n, k); res += deal(0, x, x - y, n, k); res += deal(0, -x, -x + y, n, k); res += deal(0, -x, -x - y, n, k); if (res) 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
//package codeforces; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class C { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer stringTokenizer; String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } void solve() throws IOException { for(int i = nextInt(); i >= 1; i--) { solve(nextLong(), nextLong(), nextLong(), nextLong()); } writer.close(); } void solve(long n, long k, long a, long b) { if(n % 3 != 0) { writer.println("no"); return; } for(int mask = 0; mask < 4; mask++) { long w1 = 0; long w2 = w1 + a * ((mask % 2) * 2 - 1); long w3 = w2 + b * ((mask / 2) * 2 - 1); long min = Math.min(w1, Math.min(w2, w3)); w1 -= min; w2 -= min; w3 -= min; long sum = w1 + w2 + w3; long rem = k - sum; if(rem < 0 || rem % 3 != 0) { continue; } w1 += rem / 3; w2 += rem / 3; w3 += rem / 3; if(w1 > n / 3 || w2 > n / 3 || w3 > n / 3) { continue; } writer.println("yes"); return; } writer.println("no"); } public static void main(String[] args) throws IOException { new C().solve(); } }
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
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author sousnake */ public class C { public static void main(String sasa[]) throws IOException{ BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb =new StringBuilder(); for(int tst=0;tst<t;tst++){ String s[] = br.readLine().split(" "); long n = Long.parseLong(s[0]); long k =Long.parseLong(s[1]); long d1 =Long.parseLong(s[2]); long d2 =Long.parseLong(s[3]); if(n%3!=0||d1>n/3||d2>n/3){ sb.append("no"+""+'\n'); } else{ long x1 = k+2*d2+d1; long x2 = k+2*d2-d1; long x3 = k-2*d2+d1; long x4 = k-2*d2-d1; boolean ans=false; if(x1%3==0&&x1>=0){ x1=x1/3; long lft = n-k; long t1 = x1-d2-d1; long t2 = x1-d2; long t3=x1; if(t1>=0&&t2>=0&&t3>=0){ lft = lft-((t3-t1)+(t3-t2)); if(lft%3==0&&lft>=0) ans=true; } } if(x2%3==0&&x2>=0){ x2=x2/3; long lft = n-k; long t1 = x2-d2+d1; long t2 = x2-d2; long t3=x2; if(t1>=0&t2>=0&&t3>=0){ if(t1>t3){ lft = lft-(t1-t3)-(t1-t2); if(lft%3==0&&lft>=0) ans=true; } else{ lft = lft-(t3-t1)-(t3-t2); if(lft%3==0&&lft>=0) ans=true; } } } if(x3%3==0&&x3>=0){ x3=x3/3; long lft=n-k; long t1 = x3+d2-d1; long t2 = x3+d2; long t3=x3; if(t1>=0&&t2>=0&&t3>=0){ lft = lft-(t2-t1)-(t2-t3); if(lft%3==0&&lft>=0) ans=true; } } if(x4%3==0&&x4>=0){ x4=x4/3; long lft=n-k; long t1 = x4+d2+d1; long t2 = x4+d2; long t3 =x4; if(t1>=0&&t2>=0&&t3>=0){ lft = lft- (t1-t2)-(t1-t3); if(lft%3==0&&lft>=0) ans=true; } } if(ans) sb.append("yes"+""+'\n'); else sb.append("no"+""+'\n'); } } System.out.print(sb.toString()); } }
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.*; import java.util.Map.Entry; public class C { boolean q(long n, long k, long x, long y) { long ccc = k + 2 * y + x; if (ccc < 0 || ccc % 3 != 0) { return false; } long c = ccc / 3; long b = c - y; long a = b - x; if (a < 0 || b < 0) { return false; } long m = Math.max(a, Math.max(b, c)); long d = m * 3 - a - b - c; long g = n - k - d; if (g < 0) { return false; } return g % 3 == 0; } boolean solove(long n, long k, long[] d) { if (n % 3 != 0) { return false; } long[][] ss = new long[][] { { -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 } }; for (long[] s : ss) { if (q(n, k, s[0] * d[0], s[1] * d[1])) { return true; } } return false; } void run() throws IOException { int t = ni(); while (--t >= 0) { pw.println(solove(nextLong(), nextLong(), new long[] { nextLong(), nextLong() }) ? "yes" : "no"); } } int gcd(int a, int b) { return (b == 0) ? (a) : (gcd(b, a % b)); } int[] na(int a_len) throws IOException { int[] _a = new int[a_len]; for (int i = 0; i < a_len; i++) _a[i] = ni(); return _a; } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nl() throws IOException { return br.readLine(); } void tr(String debug) { if (!OJ) pw.println(" " + debug); } static PrintWriter pw; static BufferedReader br; static StringTokenizer st; static boolean OJ; public static void main(String[] args) throws IOException { long timeout = System.currentTimeMillis(); OJ = System.getProperty("ONLINE_JUDGE") != null; pw = new PrintWriter(System.out); br = new BufferedReader(OJ ? new InputStreamReader(System.in) : new FileReader(new File("C.txt"))); while (br.ready()) new C().run(); if (!OJ) { pw.println("----------------"); pw.println(System.currentTimeMillis() - timeout); } br.close(); 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
import java.util.*; import java.io.*; public class PredictOutcomeOfTheGame { public static InputReader in; public static PrintWriter out; public static final int MOD = (int) (1e9 + 7); public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); int t = in.nextInt(); while(t-- > 0) { long n = in.nextLong(), k = in.nextLong(), d1 = in.nextLong(), d2 = in.nextLong(); boolean found = false; for(int x : new int[] {-1, 1}) { for(int y : new int[] {-1, 1}) { long a = (k - x*d1 + y*d2), b = (k + 2*x*d1 + y*d2), c = (k - x*d1 - 2*y*d2); if(a%3 != 0 || b%3 != 0 || c%3 != 0) { continue; } a /= 3; b /= 3; c /= 3; if(a < 0 || a > k || b < 0 || b > k || c < 0 || c > k) { continue; } found |= n%3 == 0 && a <= n/3 && b <= n/3 && c <= n/3; } } out.println(found ? "yes" : "no"); } out.close(); } static class Node implements Comparable<Node> { int next; int dist; public Node(int u, int v) { this.next = u; this.dist = v; } public void print() { out.println(next + " " + dist + " "); } public int compareTo(Node that) { return Integer.compare(this.next, that.next); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import fileinput import sys inp = [] for line in fileinput.input(): inp.append(line) #inp = raw_input().split("\n") inp = inp[1:] def solve(test): n = test[0] k = test[1] d1 = test[2] d2 = test[3] if not(n%3 == 0): return "no" each = n/3 for i in [-1,1]: for j in [-1,1]: if (k-j*d2-2*i*d1)%3 == 0: a = (k-j*d2-2*i*d1)/3 b = a + i*d1 c = b + j*d2 if a >= 0 and b >= 0 and c >= 0 and a <= each and b <= each and c <= each: return "yes" return "no" for test in inp: test = [int(x) for x in test.split()] print solve(test)
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 t, n, k, d1, d2, i; cin >> t; for (i = 0; i < t; i++) { cin >> n >> k >> d1 >> d2; if ((k - d1 - d2) >= 0 && (k - d1 - d2) % 3 == 0 && (3 * max(d1, d2) - d1 - d2) <= (n - k) && (n - k - (3 * max(d1, d2) - d1 - d2)) % 3 == 0) { cout << "yes" << endl; } else if ((k - d1 - 2 * d2) >= 0 && (k - d1 - 2 * d2) % 3 == 0 && (2 * d1 + d2) <= (n - k) && (n - k - (2 * d1 + d2)) % 3 == 0) { cout << "yes" << endl; } else if ((k - 3 * max(d1, d2) + d2 + d1) >= 0 && (k + d1 + d2 - 3 * max(d1, d2)) % 3 == 0 && (d1 + d2) <= (n - k) && (n - k - d1 - d2) % 3 == 0) { cout << "yes" << endl; } else if ((k - 2 * d1 - d2) >= 0 && (k - 2 * d1 - d2) % 3 == 0 && (d1 + 2 * d2) <= (n - k) && (n - k - d1 - 2 * d2) % 3 == 0) { cout << "yes" << endl; } else 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
#include <bits/stdc++.h> using namespace std; int is(long long n, long long k, long long d1, long long d2, long long d3) { if ((d1 + d2 + d3) > k) return 0; if ((k - (d1 + d2 + d3)) % 3) return 0; long long rest = 0; rest = (d3 - d2) + (d3 - d1); if ((k + rest) > (n)) return 0; rest = (n) - (k + rest); if ((rest % 3) == 0) return 1; else return 0; } int main() { int i, j, m, T; long long n, k, d1, d2; scanf("%d", &T); while (T--) { scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2); if (d1 > d2) swap(d1, d2); if (is(n, k, 0, d1, d1 + d2) || is(n, k, 0, d2, d1 + d2) || is(n, k, 0, d1, d2) || is(n, k, 0, d2 - 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 t; long long n, k, d1, d2; int main() { scanf("%d", &t); while (t--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); } else { bool flag = false; for (int i = 0; i < 4; ++i) { long long t1 = i & 1 ? -d1 : d1; long long t2 = i & 2 ? -d2 : d2; long long x2 = (k - t1 + t2); if (x2 % 3 != 0 || x2 < 0) { continue; } x2 /= 3; long long x1 = x2 + t1; long long x3 = x2 - t2; if (x1 < 0 || x2 < 0 || x3 < 0 || x1 > k || x2 > k || x3 > k) { continue; } if (max(x1, max(x2, x3)) <= n / 3) { flag = true; break; } } printf("%s\n", flag ? "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.util.Scanner; public class C { public static void main(String[] args) { L:for(int t = (int)NI();t>0;t--){ long n = NI(); long k = NI(); long d1 = NI(); long d2 = NI(); if(n%3!=0){ System.out.println("no"); continue; } if(k==0){ System.out.println("yes"); continue; } long high=k,low=0; // A wins, Bwins for(int i=0;i<4;i++){ // Awin Bwin if(i==0){ } // Awin Cwin else if(i==1){ d2*=-1; } // Bwin Cwin else if(i==2){ d1*=-1; } // Bwin Bwin else if(i==3){ d2*=-1; } high=k; low=0; while(low<=high){ long mid = (high+low)/2; long awin = mid; long bwin = awin-d1; if(bwin<0){ low=mid+1; continue; } else if(bwin>k){ high=mid-1; continue; } if(awin+bwin>k){ high=mid-1; continue; } long cwin = bwin-d2; if(cwin<0){ low=mid+1; continue; } else if(cwin>k){ high=mid-1; continue; } if(awin+bwin+cwin == k){ long max=Math.max(awin, Math.max(bwin, cwin)); long sum = max-awin + max-bwin + max-cwin; if(((n-k)-sum)>=0&&((n-k)-sum)%3==0){ System.out.println("yes"); continue L; } else { break; } } else if(awin+bwin+cwin > k){ high=mid-1; } else { low=mid+1; } } } System.out.println("no"); } } static long NI(){ try { long c=System.in.read(),r=0; for(;c!='-'&&(c<'0'||'9'<c);)c=System.in.read(); if(c=='-') return -NI(); for(;'0'<=c&&c<='9';c=System.in.read()) r = r * 10L + c - '0'; return r; } catch (Exception e) { return -1; } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); long long n, k, d1, d2, ans, x, y, z; int flag = 0; while (t--) { cin >> n >> k >> d1 >> d2; if ((n % 3) != 0) { cout << "no\n"; continue; } if (n == k) { if (d1 == d2 && d1 == 0) cout << "yes\n"; else cout << "no\n"; continue; } flag = 0; if (((k + d1 + d2) % 3 == 0) && ((k - 2 * d1 + d2) % 3 == 0) && ((k + d1 - 2 * d2) % 3 == 0)) { y = (k + d1 + d2) / 3; x = (k - 2 * d1 + d2) / 3; z = (k + d1 - 2 * d2) / 3; if (x >= 0 && y >= 0 && z >= 0) { if ((n - k - d1 - d2) % 3 == 0) { if ((n - k - d1 - d2) >= 0) flag = 1; } } } if (((k - d1 + d2) % 3 == 0) && ((k + 2 * d1 + d2) % 3 == 0) && ((k - d1 - 2 * d2) % 3 == 0)) { y = (k - d1 + d2) / 3; x = (k + 2 * d1 + d2) / 3; z = (k - d1 - 2 * d2) / 3; if (x >= 0 && y >= 0 && z >= 0) { if ((n - k - d1 - d2 - d1) % 3 == 0) { if ((n - k - d1 - d2 - d1) >= 0) flag = 1; } } } if (((k + d1 - d2) % 3 == 0) && ((k - 2 * d1 - d2) % 3 == 0) && ((k + d1 + 2 * d2) % 3 == 0)) { y = (k + d1 - d2) / 3; x = (k - 2 * d1 - d2) / 3; z = (k + d1 + 2 * d2) / 3; if (x >= 0 && y >= 0 && z >= 0) { if ((n - k - d1 - d2 - d2) % 3 == 0) { if ((n - k - d1 - d2 - d2) >= 0) flag = 1; } } } if (((k - d1 - d2) % 3 == 0) && ((k + 2 * d1 - d2) % 3 == 0) && ((k - d1 + 2 * d2) % 3 == 0)) { y = (k - d1 - d2) / 3; x = (k + 2 * d1 - d2) / 3; z = (k - d1 + 2 * d2) / 3; if (x >= 0 && y >= 0 && z >= 0) { if ((n - k - max(d1, d2) - abs(d1 - d2)) % 3 == 0) { if ((n - k - max(d1, d2) - abs(d1 - d2)) >= 0) flag = 1; } } } if (flag == 1) { 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
#include <bits/stdc++.h> using namespace std; int gcd(int p, int q) { return q == 0 ? p : gcd(q, p % q); } long long t, n, k, d1, d2; long long x1, x2, x3; bool temp; bool check() { if (x1 >= 0 && x2 >= 0 && x3 >= 0 && x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) { return true; } return false; } int main() { cin >> t; while (t--) { temp = 0; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { puts("no"); continue; } if ((k - d1 + d2) >= 0 && (k - d1 + d2) % 3 == 0) { x2 = (k - d1 + d2) / 3; x1 = d1 + x2; x3 = x2 - d2; if (check()) { temp = 1; } } if ((k - d1 - d2) >= 0 && (k - d1 - d2) % 3 == 0) { x2 = (k - d1 - d2) / 3; x1 = x2 + d1; x3 = x2 + d2; if (check()) { temp = 1; } } if ((k + d1 + d2) >= 0 && (k + d1 + d2) % 3 == 0) { x2 = (k + d2 + d1) / 3; x1 = x2 - d1; x3 = x2 - d2; if (check()) { temp = 1; } } if ((k + d1 - d2) >= 0 && (k + d1 - d2) % 3 == 0) { x2 = (k + d1 - d2) / 3; x1 = x2 - d1; x3 = d2 + x2; if (check()) { temp = 1; } } if (temp) { 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; bool check1(void); bool check2(void); bool check3(void); bool check4(void); long long test, n, k, d1, d2, p, A[4], remain; int main() { ios::sync_with_stdio(0); cin >> test; while (test--) { cin >> n >> k >> d1 >> d2; if (check1() || check2() || check3() || check4()) cout << "yes" << endl; else cout << "no" << endl; } return 0; } bool check1(void) { p = k - d1 - 2 * d2; if (p < 0) return false; else if (p % 3) return false; else { A[3] = p / 3; A[2] = d2 + A[3]; A[1] = d1 + A[2]; remain = n - k; sort(A + 1, A + 4); if (A[1] < 0) return false; p = A[3] - A[2] + A[3] - A[1]; if (remain < p) return false; else { remain -= p; if (remain % 3) return false; else return true; } } } bool check2(void) { p = k - 2 * d1 - d2; if (p < 0) return false; else if (p % 3) return false; else { A[1] = p / 3; A[2] = d1 + A[1]; A[3] = d2 + A[2]; remain = n - k; sort(A + 1, A + 4); if (A[1] < 0) return false; p = A[3] - A[2] + A[3] - A[1]; if (remain < p) return false; else { remain -= p; if (remain % 3) return false; else return true; } } } bool check3(void) { p = k + d1 + d2; if (p < 0) return false; else if (p % 3) return false; else { A[2] = p / 3; A[3] = A[2] - d2; A[1] = A[2] - d1; remain = n - k; sort(A + 1, A + 4); if (A[1] < 0) return false; p = A[3] - A[2] + A[3] - A[1]; if (remain < p) return false; else { remain -= p; if (remain % 3) return false; else return true; } } } bool check4(void) { p = k - d1 - d2; if (p < 0) return false; else if (p % 3) return false; else { A[2] = p / 3; A[3] = d2 + A[2]; A[1] = d1 + A[2]; remain = n - k; sort(A + 1, A + 4); if (A[1] < 0) return false; p = A[3] - A[2] + A[3] - A[1]; if (remain < p) return false; else { remain -= p; if (remain % 3) return false; else return true; } } }
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; #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") bool check(long long foo) { return (foo % 3 == 0 && foo >= 0); } bool isValid(long long n, long long k, long long d1, long long d2) { if (n % 3LL) return false; long long x, y, z; x = (k + 2 * d1 - d2) / 3; y = (k - d1 - d2) / 3; z = (k - d1 + 2 * d2) / 3; if (x <= n / 3 && y <= n / 3 && z <= n / 3 && check(k + 2 * d1 - d2) && check(k - d1 - d2) && check(k - d1 + 2 * d2)) return true; x = (k - 2 * d1 + d2) / 3; y = (k + d1 + d2) / 3; z = (k + d1 - 2 * d2) / 3; if (x <= n / 3 && y <= n / 3 && z <= n / 3 && check(k - 2 * d1 + d2) && check(k + d1 + d2) && check(k + d1 - 2 * d2)) return true; x = (k + 2 * d1 + d2) / 3; y = (k - d1 + d2) / 3; z = (k - d1 - 2 * d2) / 3; if (x <= n / 3 && y <= n / 3 && z <= n / 3 && check(k + 2 * d1 + d2) && check(k - d1 + d2) && check(k - d1 - 2 * d2)) return true; x = (k - 2 * d1 - d2) / 3; y = (k + d1 - d2) / 3; z = (k + d1 + 2 * d2) / 3; if (x <= n / 3 && y <= n / 3 && z <= n / 3 && check(k - 2 * d1 - d2) && check(k + d1 - d2) && check(k + d1 + 2 * d2)) return true; return false; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (isValid(n, k, d1, d2)) cout << "yes"; else cout << "no"; cout << '\n'; } return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
from sys import stdin def solve(n, k, d1, d2): if (d1 + 2 * d2 + k) % 3 != 0: return False c = (d1 + 2 * d2 + k) // 3 a = c - d1 - d2 b = k - a - c if a >= 0 and b >= 0 and c >= 0 and a + b + c == k: _max = max(a, b, c) diff = sum(_max - e for e in [a, b, c]) d = n - k if d >= diff and (d - diff) % 3 == 0: return True return False def main(): test = stdin.readlines() output = [] for i in range(1, int(test[0]) + 1): n, k, d1, d2 = map(int, test[i].split()) for i, j in [(-1, -1), (-1, 1), (1, -1), (1, 1)]: if solve(n, k, i * d1, j * d2): output.append('yes') break else: output.append('no') print('\n'.join(output)) if __name__ == "__main__": 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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.File; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author zodiacLeo */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastScanner in, PrintWriter out) { long q = in.nextLong(); StringBuilder sb = new StringBuilder(""); while (q-- > 0) { long n = in.nextLong(); long k = in.nextLong(); long d1 = in.nextLong(); long d2 = in.nextLong(); if (check(n, k, d1, d2)) { sb.append("yes\n"); } else { sb.append("no\n"); } } out.print(sb); } public boolean check(long n, long k, long d1, long d2) { long sum = k + d1 + d2; if (sum % 3 == 0) { long w2 = sum / 3; long w3 = w2 - d2; long w1 = w2 - d1; if (isValid(w1, w2, w3, k)) { if (canFit(n - k, w1, w2, w3)) { return true; } } } sum = k - d1 + d2; if (sum % 3 == 0) { long w2 = sum / 3; long w3 = w2 - d2; long w1 = w2 + d1; if (isValid(w1, w2, w3, k)) { if (canFit(n - k, w1, w2, w3)) { return true; } } } sum = k + d1 - d2; if (sum % 3 == 0) { long w2 = sum / 3; long w3 = d2 + w2; long w1 = w2 - d1; if (isValid(w1, w2, w3, k)) { if (canFit(n - k, w1, w2, w3)) { return true; } } } sum = k - d1 - d2; if (sum % 3 == 0) { long w2 = sum / 3; long w3 = d2 + w2; long w1 = w2 + d1; if (isValid(w1, w2, w3, k)) { if (canFit(n - k, w1, w2, w3)) { return true; } } } return false; } public boolean canFit(long left, long w1, long w2, long w3) { long max = Math.max(w1, Math.max(w2, w3)); long delta = max - w1; delta += max - w2; delta += max - w3; if (left < delta) { return false; } else if (left == delta) { return true; } else { left -= delta; return left % 3 == 0; } } public boolean isValid(long w1, long w2, long w3, long k) { return w1 >= 0 && w2 >= 0 && w3 >= 0 && (w1 + w2 + w3 == k); } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String next() { while (st == null || !st.hasMoreElements()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } 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; int main() { int t; scanf("%d", &t); for (int cc = 0; cc < t; cc++) { long long n, k, d1, d2; long long a, b, c; cin >> n >> k >> d1 >> d2; bool found = false; for (int x = 0; x < 2; x++) { for (int y = 0; y < 2; y++) { long long sub = 2 * d1 + d2; long long rem = k - sub; a = rem / 3; b = a + d1; c = b + d2; d1 *= -1; if (rem % 3 != 0) continue; if (n % 3 != 0) continue; if (a < 0 || b < 0 || c < 0) continue; rem = n - k; sub = n / 3; if (a > sub || b > sub || c > sub) continue; long long tot = (sub - a) + (sub - b) + (sub - c); if (tot != rem) continue; found = true; } d2 *= -1; } if (found) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.io.*; import java.util.*; public class Main { public static class pair implements Comparable<pair> { int a; int b; public pair(int pa, int pb) { a = pa; b= pb; } @Override public int compareTo(pair o) { if(this.a < o.a) return -1; if(this.a > o.a) return 1; return Integer.compare(o.b, this.b); } } //int n = Integer.parseInt(in.readLine()); //String[] spl = in.readLine().split(" "); public static void main (String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(in.readLine()); for (int cas = 0; cas < t; cas++) { String[] spl = in.readLine().split(" "); long n = Long.parseLong(spl[0]); long k = Long.parseLong(spl[1]); long d1 = Long.parseLong(spl[2]); long d2 = Long.parseLong(spl[3]); if(n%3 !=0) System.out.println("no"); else { boolean fin = false; for(long sd1 = -1; sd1<=1 && !fin; sd1+=2) for(long sd2 = -1; sd2<=1 && !fin; sd2+=2) { long den = k -1*sd1*d1 -1*sd2*d2; if(den%3!=0) continue; long w2 = den/3; long w1 = sd1*d1+w2; long w3 = sd2*d2+w2; if(w1 <0 || w2 < 0 || w3 < 0)continue; long z1 = n/3 - w1; long z2 = n/3 - w2; long z3 = n/3 - w3; if(z1 < 0 || z2 < 0 || z3 < 0) continue; fin = true; System.out.println("yes"); } if(!fin) System.out.println("no"); } } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no" << endl; continue; } long long w1, w2, w3; bool flag = true; if (k - 2 * d1 - d2 < 0 || (k - 2 * d1 - d2) % 3 != 0) flag = false; if (flag) { w1 = (k - 2 * d1 - d2) / 3; w2 = d1 + w1; w3 = d1 + d2 + w1; if (w1 > n / 3 || w2 > n / 3 || w3 > n / 3) flag = false; } if (!flag) { flag = true; if (k + 2 * d2 - d1 < 0 || (k + 2 * d2 - d1) % 3 != 0) flag = false; if (flag) { w3 = (k + 2 * d2 - d1) / 3; w1 = w3 + d1 - d2; w2 = w3 - d2; if (w1 > n / 3 || w2 > n / 3 || w3 > n / 3 || w1 < 0 || w2 < 0) flag = false; } } if (!flag) { flag = true; if (k + d2 - 2 * d1 < 0 || (k + d2 - 2 * d1) % 3 != 0) flag = false; if (flag) { w1 = (k + d2 - 2 * d1) / 3; w2 = d1 + w1; w3 = d1 - d2 + w1; if (w1 > n / 3 || w2 > n / 3 || w3 > n / 3 || w3 < 0) flag = false; } } if (!flag) { flag = true; if (k - 2 * d2 - d1 < 0 || (k - 2 * d2 - d1) % 3 != 0) flag = false; if (flag) { w3 = (k - 2 * d2 - d1) / 3; w1 = w3 + d1 + d2; w2 = d2 + w3; if (w3 > n / 3 || w2 > n / 3 || w1 > n / 3) flag = false; } } if (flag) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; long long t, n, m; bool ok1(long long n, long long k, long long d1, long long d2) { if ((k - d1 - 2 * d2) % 3 != 0) return false; if ((k - d1 - 2 * d2) / 3 < 0 || (k - d1 - 2 * d2) / 3 < 0) return false; n -= k; n -= d1 * 2; n -= d2; if (n >= 0 && (n) % 3 == 0) return true; return false; } bool ok2(long long n, long long k, long long d1, long long d2) { if ((k + d1 + d2) % 3 != 0) return false; if ((k + d1 + d2) / 3 - d1 < 0 || (k + d1 + d2) / 3 - d2 < 0) return false; n -= k; n -= d1; n -= d2; if (n >= 0 && (n) % 3 == 0) return true; return false; } bool ok3(long long n, long long k, long long d1, long long d2) { if ((k - 2 * d1 - d2) % 3 != 0) return false; if ((k - 2 * d1 - d2) / 3 < 0 || (k - 2 * d1 - d2) / 3 < 0) return false; n -= k; n -= d2 * 2; n -= d1; if (n >= 0 && (n) % 3 == 0) return true; return false; } bool ok4(long long n, long long k, long long d1, long long d2) { if ((k - d1 - d2) % 3 != 0) return false; if ((k - d1 - d2) / 3 < 0 || (k - d1 - d2) / 3 < 0) return false; n -= k; if (d2 > d1) n -= 2 * d2 - d1; else n -= 2 * d1 - d2; if (n >= 0 && (n) % 3 == 0) return true; return false; } int main() { cin >> t; while (t--) { long long k, d1, d2; scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (ok1(n, k, d1, d2) || ok2(n, k, d1, d2) || ok3(n, k, d1, d2) || ok4(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; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans % m; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t; cin >> t; long long n, k, d1, d2; long long a1, a2, a3; while (t--) { cin >> n >> k >> d1 >> d2; long long left = (n - k); long long fr = (k - d1 - d2); if (fr >= 0 && (fr % 3 == 0)) { a2 = (fr / 3); a1 = (a2 + d1); a3 = (a2 + d2); if (a1 >= 0 && a2 >= 0 && a3 >= 0) { long long match = max(a1, a3); long long req = 0; req += (match - a1); req += (match - a2); req += (match - a3); long long lleft = (left - req); if (lleft >= 0 && lleft % 3 == 0) { cout << "yes" << "\n"; continue; } } } fr = (k - d1 + d2); if (fr >= 0 && (fr % 3 == 0)) { a2 = (fr / 3); a1 = a2 + d1; a3 = a2 - d2; if (a1 >= 0 && a2 >= 0 && a3 >= 0) { long long match = a1; long long req = 0; req += (match - a1); req += (match - a2); req += (match - a3); long long lleft = (left - req); if (lleft >= 0 && lleft % 3 == 0) { cout << "yes" << "\n"; continue; } } } fr = (k + d1 - d2); if (fr >= 0 && (fr % 3 == 0)) { a2 = (fr / 3); a1 = a2 - d1; a3 = a2 + d2; if (a1 >= 0 && a2 >= 0 && a3 >= 0) { long long match = a3; long long req = 0; req += (match - a1); req += (match - a2); req += (match - a3); long long lleft = (left - req); if (lleft >= 0 && lleft % 3 == 0) { cout << "yes" << "\n"; continue; } } } fr = (k + d1 + d2); if (fr >= 0 && (fr % 3 == 0)) { a2 = (fr / 3); a1 = a2 - d1; a3 = a2 - d2; if (a1 >= 0 && a2 >= 0 && a3 >= 0) { long long match = a2; long long req = 0; req += (match - a1); req += (match - a2); req += (match - a3); long long lleft = (left - req); if (lleft >= 0 && lleft % 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; bool f(long long x, long long y, long long z) { if ((k - x - y - z) < 0 || (k - x - y - z) % 3 != 0) return false; long long m = max(x, max(y, z)), p = n - (k + 3 * m - (x + y + z)); if (p < 0 || p % 3 != 0) return false; return true; } int main() { int t; ios_base::sync_with_stdio(0); cin >> t; for (int i = 0; i < t; i++) { long long d1, d2; cin >> n >> k >> d1 >> d2; long long d = max(d1, d2); if (f(0ll, d1, d1 + d2) || f(d1 + d2, d2, 0ll) || f(d1, 0ll, d2) || f(d - d1, d, d - d2)) cout << "yes" << endl; else 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
#include <bits/stdc++.h> using namespace std; struct node { long long int f, g, h; }; long long int n, k, d1, d2; struct node fun1(long long int a, long long int b) { struct node s; s.g = k - a - b; s.g /= 3; s.f = a + s.g; s.h = b + s.g; return s; } struct node fun2(long long int a, long long int b) { struct node s; s.g = k + a + b; s.g /= 3; s.f = s.g - a; s.h = s.g - b; return s; } struct node fun3(long long int a, long long int b) { struct node s; s.g = k + a - b; s.g /= 3; s.f = s.g - a; s.h = s.g + b; return s; } struct node fun4(long long int a, long long int b) { struct node s; s.g = k - a + b; s.g /= 3; s.f = s.g + a; s.h = s.g - b; return s; } int chec(long long int arr[3]) { sort(arr, arr + 3); long long int r; r = n - k; r -= (arr[2] - arr[0]); r -= (arr[2] - arr[1]); if (r >= 0 && r % 3 == 0 && arr[0] + arr[1] + arr[2] == k && arr[0] >= 0) { return 1; } return 0; } int main() { int test; cin >> test; while (test--) { cin >> n >> k >> d1 >> d2; long long int arr[3]; struct node x; x = fun1(d1, d2); arr[0] = x.f; arr[1] = x.g; arr[2] = x.h; if (chec(arr)) { printf("yes\n"); continue; } x = fun2(d1, d2); arr[0] = x.f; arr[1] = x.g; arr[2] = x.h; if (chec(arr)) { printf("yes\n"); continue; } x = fun3(d1, d2); arr[0] = x.f; arr[1] = x.g; arr[2] = x.h; if (chec(arr)) { printf("yes\n"); continue; } x = fun4(d1, d2); arr[0] = x.f; arr[1] = x.g; arr[2] = x.h; if (chec(arr)) { 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.InputStreamReader; import java.util.StringTokenizer; public class PredictOutcomeOfTheGame { static int [] sign1=new int[]{1,1,-1,-1}; static int [] sign2=new int[]{1,-1,1,-1}; public static void main(String[] args) throws Exception { // TODO Auto-generated method stub BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); int T=Integer.parseInt(st.nextToken()); while(T>0){ st=new StringTokenizer(br.readLine()); long N=Long.parseLong(st.nextToken()); long K=Long.parseLong(st.nextToken()); long d1=Long.parseLong(st.nextToken()); long d2=Long.parseLong(st.nextToken()); boolean isOk=false; if(N%3==0){ long neededWins=N/3; for(int k=0;k<4;k++){ long D1= d1* sign1[k]; long D2= d2 *sign2[k]; long x1=(K+D2+2*D1)/3; if((K+D2 + 2*D1)%3 !=0){ continue; } long x2=x1-D1; long x3=x2-D2; if((x1>=0 && x2>=0 && x3>=0) &&(x1+x2+x3==K) &&(x1<=neededWins && x2<=neededWins && x3<=neededWins) ){ isOk=true; break; } } } if(isOk){ System.out.println("yes"); } else{ System.out.println("no"); } T--; } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; int t; long long n, k, d1, d2; int main() { scanf("%I64d", &t); for (int i = 0; i < t; i++) { scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2); if ((k >= 2 * d2 + d1 && (k - 2 * d2 - d1) % 3 == 0) && ((n - k >= 2 * d1 + d2) && ((n - k - 2 * d1 - d2) % 3 == 0))) printf("yes\n"); else if (((n - k >= d1 + d2) && ((n - k - d1 - d2) % 3 == 0)) && ((k + d1 + d2) % 3 == 0) && (k + d1 + d2) / 3 >= d1 && (k + d1 + d2) / 3 >= d2) printf("yes\n"); else if ((n - k >= 2 * d2 + d1) && ((n - k - 2 * d2 - d1) % 3 == 0) && (k >= 2 * d1 + d2) && (k - 2 * d1 - d2) % 3 == 0) printf("yes\n"); else if ((n - k >= 2 * max(d1, d2) - min(d1, d2)) && ((n - k - 2 * max(d1, d2) + min(d1, d2)) % 3 == 0) && k >= d1 + d2 && (k - d1 - d2) % 3 == 0) printf("yes\n"); else printf("no\n"); } return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class a { public static void main(String[] args) { Scanner s = new Scanner(System.in); long q = s.nextLong(); while(q>0){ long n=s.nextLong(); long k=s.nextLong(); long d1=s.nextLong(); long d2=s.nextLong(); long x1=0,x2=0,x3=0; long k1=n/3,flag=0; if((k-d1+d2)%3==0&&(k-d1-2*d2)>=0){ x1=(k-d1+d2)/3; x2=x1+d1; x3=x1-d2; long sum1=k1-x1; long sum2=k1-x2; long sum3=k1-x3; if(!(sum1<0||sum2<0||sum3<0||(sum1+sum2+sum3!=n-k))) flag=1; } if((k-d1-d2)%3==0&&(k-d1-d2)>=0){ x1=(k-d1-d2)/3; x2=x1+d1; x3=x1+d2; long sum1=k1-x1; long sum2=k1-x2; long sum3=k1-x3; if(!(sum1<0||sum2<0||sum3<0||(sum1+sum2+sum3!=n-k))) flag=1; } if((k-2*d1+d2)%3==0&&(k-2*d2+d1)>=0&&(k-2*d1+d2)>=0){ x1=(k-2*d1+d2)/3; x2=x1+d1; x3=x1-d2+d1; long sum1=k1-x1; long sum2=k1-x2; long sum3=k1-x3; if(!(sum1<0||sum2<0||sum3<0||(sum1+sum2+sum3!=n-k))) flag=1; } if((k-2*d1-d2)%3==0&&(k-2*d1-d2)>=0){ x1=(k-2*d1-d2)/3; x2=x1+d1; x3=x1+d2+d1; long sum1=k1-x1; long sum2=k1-x2; long sum3=k1-x3; if(!(sum1<0||sum2<0||sum3<0||(sum1+sum2+sum3!=n-k))) flag=1; } if(flag==0) System.out.println("no"); else System.out.println("yes"); q--; } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; int T; long long n, k, d1, d2; long long x1, x2, x3; bool good() { x1 = (k + d1 + d2) / 3; x2 = x1 - d1; x3 = x1 - d2; return (k + d1 + d2) % 3 == 0 && min(min(x1, x2), x3) >= 0 && max(max(x1, x2), x3) <= n / 3; } int main() { scanf("%d", &T); for (int t = 0; t < T; t++) { scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2); if (n % 3) { printf("no\n"); continue; } if (good()) { printf("yes\n"); continue; } d1 *= -1; if (good()) { printf("yes\n"); continue; } d2 *= -1; if (good()) { printf("yes\n"); continue; } d1 *= -1; if (good()) { printf("yes\n"); continue; } printf("no\n"); } }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class PredictOutcomeoftheGame { public static void main(String[] args) throws IOException { Init(System.in); int t = nextInt(); long n, k, d1, d2; for (int i = 0; i < t; ++i) { n = nextLong(); k = nextLong(); d1 = nextLong(); d2 = nextLong(); System.out.println(solve(n, k, d1, d2)); } } private static String solve(long n, long k, long d1, long d2) { if (n % 3 != 0) { return "no"; } if (k == 0) { return "yes"; } // win1>win2, win2 >win3 long sum = k + d1 + d2; long win1, win2, win3; if ((k - d1 + d2) % 3 == 0) { win2 = (k - d1 + d2) / 3; win1 = d1 + win2; win3 = win2 - d2; if (!(win1 < win2 || win2 < win3 || win1 < 0 || win2 < 0 || win3 < 0)) { if (n - k >= 2 * d1 + d2) { return "yes"; } } } if (sum % 3 == 0) { win2 = sum / 3; win1 = win2 - d1; win3 = win2 - d2; if (!(win1 > win2 || win3 > win2 || win1 < 0 || win2 < 0 || win3 < 0)) { if (n - k >= d1 + d2) { return "yes"; } } } if ((k - d1 - d2) % 3 == 0) { win2 = (k - d1 - d2) / 3; win1 = d1 + win2; win3 = d2 + win2; if (!(win2 > win1 || win2 > win3 || win1 < 0 || win2 < 0 || win3 < 0)) { if (n - k >= d2 * 2 - d1 && d1 <= d2) { return "yes"; } if (n - k >= d1 * 2 - d2 && d2 <= d1) { return "yes"; } } } if ((k - d2 + d1) % 3 == 0) { win2 = (k - d2 + d1) / 3; win3 = d2 + win2; win1 = win2 - d1; if (!(win2 < win1 || win3 < win2 || win1 < 0 || win2 < 0 || win3 < 0)) { if (n - k >= 2 * d2 + d1) { return "yes"; } } } return "no"; } static BufferedReader reader; static StringTokenizer tokenizer; static void Init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static long nextLong() throws IOException { return Long.parseLong(next()); } static int nextInt() throws IOException { return Integer.parseInt(next()); } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; long long mod = 1e9 + 7; double eps = 1e-9; double pi = acos(-1); long long fastpower(long long b, long long p) { double ans = 1; while (p) { if (p % 2) { ans = (ans * b); } b = b * b; p /= 2; } return ans; } bool valid(int x, int y, int n, int m) { return (x >= 0 && y < m && x < n && y >= 0); } using namespace std; bool solvef(long long n, long long k, long long d1, long long d2) { if (n % 3) { return 0; } long long t; t = -d1 - (2 * d2) + k; if (t % 3 || t < 0) { return 0; } t /= 3; long long s = d2 + t; long long f = d1 + s; if (f >= 0 && s >= 0 && t >= 0 && s <= (n / 3) && f <= (n / 3) && t <= (n / 3) && d1 == f - s && d2 == s - t && f + s + t == k) { return 1; } else { return 0; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; bool check = solvef(n, k, d1, d2) | solvef(n, k, -d1, -d2) | solvef(n, k, d1, -d2) | solvef(n, k, -d1, d2); if (check) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; long long n, k, d1, d2; cin >> t; while (t--) { scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2); if (n % 3) { cout << "no" << endl; continue; } int f = 0; long long tt = n / 3; long long x, y, z; if ((k + d1 + d2 + d1) % 3 == 0) { x = (k + d1 + d2 + d1) / 3; y = x - d1; z = y - d2; if (x >= 0 && x <= tt && y >= 0 && y <= tt && z >= 0 && z <= tt) { cout << "yes" << endl; f = 1; continue; } } if ((k - d1 - d1 + d2) % 3 == 0) { x = (k - d1 - d1 + d2) / 3; y = x + d1; z = y - d2; if (x >= 0 && x <= tt && y >= 0 && y <= tt && z >= 0 && z <= tt) { cout << "yes" << endl; f = 1; continue; } } if ((k + d1 + d1 - d2) % 3 == 0) { x = (k + d1 + d1 - d2) / 3; y = x - d1; z = y + d2; if (x >= 0 && x <= tt && y >= 0 && y <= tt && z >= 0 && z <= tt) { cout << "yes" << endl; f = 1; continue; } } if ((k - d1 - d1 - d2) % 3 == 0) { x = (k - d1 - d1 - d2) / 3; y = x + d1; z = d2 + y; if (x >= 0 && x <= tt && y >= 0 && y <= tt && z >= 0 && z <= tt) { cout << "yes" << endl; f = 1; continue; } } if (f == 0) { cout << "no" << endl; } } return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> int main() { int T; scanf("%d", &T); while (T--) { long long n, k, d1, d2; scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); if (n % 3) { printf("no\n"); continue; } n /= 3; if ((k - d1 - d2) % 3 == 0) { long long a = (k - d1 - d2) / 3; if (a <= n && a + d1 <= n && a + d2 <= n && a >= 0) { printf("yes\n"); continue; } } if ((k - d1 + d2) % 3 == 0) { long long a = (k - d1 + d2) / 3; if (a <= n && a + d1 <= n && a - d2 <= n && a - d2 >= 0 && a >= 0) { printf("yes\n"); continue; } } if ((k + d1 + d2) % 3 == 0) { long long a = (k + d1 + d2) / 3; if (a <= n && a - d1 <= n && a - d2 <= n && a - d2 >= 0 && a - d1 >= 0 && a >= 0) { printf("yes\n"); continue; } } if ((k + d1 - d2) % 3 == 0) { long long a = (k + d1 - d2) / 3; if (a <= n && a - d1 <= n && a + d2 <= n && a - d1 >= 0 && a >= 0 && a + d2 >= 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
from sys import stdin def solve(n, k, d1, d2): if (d1 + 2 * d2 + k) % 3 != 0: return False c = (d1 + 2 * d2 + k) // 3 a = c - d1 - d2 b = k - a - c if a >= 0 and b >= 0 and c >= 0: _max = max(a, b, c) diff = sum(_max - e for e in [a, b, c]) d = n - k if d >= diff and (d - diff) % 3 == 0: return True return False def main(): test = stdin.readlines() output = [] for i in range(1, int(test[0]) + 1): n, k, d1, d2 = map(int, test[i].split()) for i, j in [(-1, -1), (-1, 1), (1, -1), (1, 1)]: if solve(n, k, i * d1, j * d2): output.append('yes') break else: output.append('no') print('\n'.join(output)) if __name__ == "__main__": 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
import java.util.*; public class C451 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sca=new Scanner(System.in); int t=sca.nextInt(); for(int i=1;i<=t;i++){ long n=sca.nextLong(); long k=sca.nextLong(); long d1=sca.nextLong(); long d2=sca.nextLong(); if(play(n,k,d1,d2)||play(n,k,d1,-d2)||play(n,k,-d1,d2)||play(n,k,-d1,-d2)){ System.out.println("yes"); } else System.out.println("no"); } } static boolean play(long n,long k,long d1,long d2){ long a1=k-d2-2*d1; //System.out.println(a1); if(a1%3!=0)return false; a1=a1/3; if(a1<0)return false; long a2=a1+d1; long a3=a2+d2; //System.out.println(a1+" "+a2+" "+a3); if(a2<0||a3<0)return false; long max=a1; if(max<a2)max=a2; if(max<a3)max=a3; long now=n-k; if(a1<max)now=now-(max-a1); if(a2<max)now=now-(max-a2); if(a3<max)now=now-(max-a3); if(now<0||now%3!=0)return false; return true; } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; long long int n, k; bool check(vector<long long int> a) { sort(a.rbegin(), a.rend()); long long int tmp = min(a[1], a[2]); if (tmp < 0) { for (int i = 0; i < (int)(a.size()); i++) a[i] -= tmp; } long long int ret = 0; for (int i = 0; i < (int)(3); i++) ret += a[i]; tmp = k - ret; if (tmp < 0 || tmp % 3) return false; for (int i = 0; i < (int)(3); i++) a[i] += tmp / 3; tmp = max(a[0], max(a[1], a[2])); if (tmp > n / 3) return false; return true; } int main(void) { long long int t; cin >> t; while (t--) { long long int d1, d2; cin >> n >> k >> d1 >> d2; if (n % 3) { cout << "no" << endl; continue; } vector<long long int> a; int f = 0; a.push_back(0); a.push_back(a[0] + d1); a.push_back(a[1] + d2); if (check(a)) f = 1; a.clear(); a.push_back(0); a.push_back(a[0] - d1); a.push_back(a[1] + d2); if (check(a)) f = 1; a.clear(); a.push_back(0); a.push_back(a[0] + d1); a.push_back(a[1] - d2); if (check(a)) f = 1; a.clear(); a.push_back(0); a.push_back(a[0] - d1); a.push_back(a[1] - d2); if (check(a)) f = 1; if (f) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import sys import operator input = sys.stdin.readline def read_int(): return int(input().strip()) def read_ints(): return list(map(int, input().strip().split(' '))) def solve(): """ n, k, d1, d2 d1 = abs(w1-w2) d2 = abs(w2-w3) w1+w2+w3=k 1) w1 >= w2 and w2 >= w3 d1 = w1-w2 d2 = w2-w3 d1-d2 = w1+w3-2*w2 d1-d2-k = -3*w2 w1 = d1-(d1-d2-k)/3 w2 = -(d1-d2-k)/3 w3 = -(d1-d2-k)/3-d2 n/3-w1 >= 0 n/3-w2 >= 0 n/3-w3 >= 0 """ n, k, d1, d2 = read_ints() if n%3 != 0: return 'no' # w1 >= w2 and w2 >= w3 def check(n, k, d1, d2, op1, op2): if (d1-d2-k)%3 != 0: return False w1 = d1-(d1-d2-k)//3 w2 = -(d1-d2-k)//3 w3 = -(d1-d2-k)//3-d2 if w1 >= 0 and w2 >= 0 and w3 >= 0 and n//3-w1 >= 0 and n//3-w2 >= 0 and n//3-w3 >= 0 and op1(w1, w2) and op2(w2, w3): return True return False if check(n, k, d1, d2, operator.ge, operator.ge): return 'yes' if check(n, k, d1, -d2, operator.ge, operator.lt): return 'yes' if check(n, k, -d1, d2, operator.lt, operator.ge): return 'yes' if check(n, k, -d1, -d2, operator.lt, operator.lt): return 'yes' return 'no' if __name__ == '__main__': T = read_int() for _ in range(T): print(solve())
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 solveFaster(long long n, long long k, long long d1, long long d2) { if (n % 3 != 0) { return 0; } for (int sign1 = -1; sign1 <= 1; sign1++) { for (int sign2 = -1; sign2 <= 1; sign2++) { if (sign1 == 0 || sign2 == 0) { continue; } long long D1 = d1 * sign1; long long D2 = d2 * sign2; long long x2 = (k - D1 + D2) / 3; if ((k - D1 + D2) % 3 != 0) { continue; } if (x2 >= 0 && x2 <= k) { long long x1 = D1 + x2; long long x3 = x2 - D2; if (x1 >= 0 && x1 <= k && x3 >= 0 && x3 <= k) { if (x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) { assert(abs(x1 - x2) == d1); assert(abs(x2 - x3) == d2); return true; } } } } } return false; } int main() { int T; cin >> T; while (T--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; int ans = solveFaster(n, k, d1, d2); if (ans == 1) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
import java.io.*; import java.math.*; import java.util.*; public class Main { static final long MOD = 1000000007L; static final int INF = 50000000; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int Q = sc.ni(); for (int q = 0; q < Q; q++) { long N = sc.nl(); long K = sc.nl(); long D1 = sc.nl(); long D2 = sc.nl(); String ans = "no"; for (int i = -1; i <= 1; i += 2) { for (int j = -1; j <= 1; j += 2) { long A = i*D1; long B = j*D2; long g1 = 0; long g2 = g1-A; long g3 = g2-B; long min = Math.min(g1, Math.min(g2,g3)); g1 -= min; g2 -= min; g3 -= min; long minG = g1+g2+g3; long m = 0; if (A >= 0 && B >= 0) { m = 2*D1+D2; } else if (A >= 0 && B <= 0) { m = Math.max(D1,D2)+Math.abs(D1-D2); } else if (A <= 0 && B >= 0) { m = D1+D2; } else { m = D1+2*D2; } if (minG <= K && (K-minG)%3==0 && m <= N-K && ((N-K)-m)%3==0) { ans = "yes"; } } } pw.println(ans); } pw.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; const int N = 100050; int t; long long n, k, d1, d2; bool ok(long long x1, long long x2, long long x3) { long long val; if (abs(x1 - x2) != d1 || abs(x2 - x3) != d2) while (1) ; while (x1 < 0 || x2 < 0 || x3 < 0) { val = (x1 < 0 ? -x1 : (x2 < 0 ? -x2 : -x3)); x1 += val; x2 += val; x3 += val; } if (x1 + x2 + x3 > k) return false; val = max(x1, max(x2, x3)); if ((n - k - (3 * val - x1 - x2 - x3)) < 0) return false; return (n - k - (3 * val - x1 - x2 - x3)) % 3 == 0; } int main() { scanf("%d", &t); while (t--) { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 > 0) printf("no\n"); else if (ok(0, d1, d1 - d2) || ok(0, -d1, -d1 - d2) || ok(0, d1, d1 + d2) || ok(0, -d1, -d1 - d2)) printf("yes\n"); else if (ok(-d1, 0, -d2) || ok(d1, 0, -d2) || ok(-d1, 0, d2) || ok(d1, 0, 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
def test(n,k,d1,d2): f=min(0,d2,d1+d2) if k<2*d2+d1-3*f: return False if (k-d1-2*d2)%3!=0: return False r=n-k if (r+d1-d2)%3!=0: return False if min(r+d1-d2,r-d2-2*d1,r+d1+2*d2)<0: return False return True for _ in range(input()): n,k,d1,d2=map(int,raw_input().split()) if test(n,k,d1,d2): print 'yes' elif test(n,k,-d1,d2): print 'yes' elif test(n,k,-d1,-d2): print 'yes' elif test(n,k,d1,-d2): 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.*; import java.io.*; import java.math.*; import java.lang.reflect.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Task solver = new TaskC(); int t=in.readInt(); for (int i=1; i<=t; i++) solver.solve(i, in, out); out.close(); } } interface Task { public void solve(int testNumber, InputReader in, OutputWriter out); } class TaskA implements Task { public void solve(int testNumber, InputReader in, OutputWriter out) { int n=in.readInt(), m=in.readInt(); out.printLine(MiscUtils.min(n, m)%2==0?"Malvika":"Akshat"); } } class TaskB implements Task { int[] arr; public void solve(int testNumber, InputReader in, OutputWriter out) { int n=in.readInt(), x=-1, y=-1; arr=IOUtils.readIntArray(in, n); for (int i=1; i<n; i++) if (arr[i]<arr[i-1] && x==0) x=i-1; for (int i=n-2; i>=0; i--) if (arr[i]>arr[i+1] && y==-1) y=i+1; if (x==-1) { out.printLine("yes"); out.printLine(1, 1); return; } // if (x>y) { // for (y=) // } } } class TaskC implements Task { public void solve(int testNumber, InputReader in, OutputWriter out) { long n=in.readLong(), k=in.readLong(), d1=in.readLong(), d2=in.readLong(); boolean val=false; for (int i=0; i<2; i++) for (int j=0; j<2; j++) val|=f((i==0?-d1:d1), (j==0?-d2:d2), n, k); out.printLine(val?"yes":"no"); } boolean f(long d1, long d2, long n, long k) { long aux=k-2L*d1-d2; if (aux%3!=0) return false; long x=aux/3L, y=x+d1, z=y+d2; if (x<0 || y<0 || z<0) return false; n-=k; aux=3L*MiscUtils.max(MiscUtils.max(x, y), z)-x-y-z; if (aux>n) return false; aux-=n; return aux%3==0; } } class TaskD implements Task { public void solve(int testNumber, InputReader in, OutputWriter out) { int n=in.readInt(), m=in.readInt(); } } class TaskE implements Task { public void solve(int testNumber, InputReader in, OutputWriter out) { } } class TaskF implements Task { public void solve(int testNumber, InputReader in, OutputWriter out) { } } class TaskG implements Task { public void solve(int testNumber, InputReader in, OutputWriter out) { } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter( outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(char[] array) { writer.print(array); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) writer.print(' '); writer.print(array[i]); } } public void print(long[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) writer.print(' '); writer.print(array[i]); } } public void print(Collection<Integer> collection) { boolean first = true; for (int value : collection) { if (first) first = false; else writer.print(' '); writer.print(value); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(long[] array) { print(array); writer.println(); } public void printLine(Collection<Integer> collection) { print(collection); writer.println(); } public void printLine() { writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } public void print(char i) { writer.print(i); } public void printLine(char i) { writer.println(i); } public void printLine(char[] array) { writer.println(array); } public void printFormat(String format, Object... objects) { writer.printf(format, objects); } public void close() { writer.close(); } public void flush() { writer.flush(); } public void print(long i) { writer.print(i); } public void printLine(long i) { writer.println(i); } public void print(int i) { writer.print(i); } public void printLine(int i) { writer.println(i); } } class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { 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, readInt()); 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, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value == -1; } public String next() { return readString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class IOUtils { public static Pair<Integer, Integer> readIntPair(InputReader in) { int first = in.readInt(); int second = in.readInt(); return Pair.makePair(first, second); } public static Pair<Long, Long> readLongPair(InputReader in) { long first = in.readLong(); long second = in.readLong(); return Pair.makePair(first, second); } public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } public static long[] readLongArray(InputReader in, int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = in.readLong(); return array; } public static double[] readDoubleArray(InputReader in, int size) { double[] array = new double[size]; for (int i = 0; i < size; i++) array[i] = in.readDouble(); return array; } public static String[] readStringArray(InputReader in, int size) { String[] array = new String[size]; for (int i = 0; i < size; i++) array[i] = in.readString(); return array; } public static char[] readCharArray(InputReader in, int size) { char[] array = new char[size]; for (int i = 0; i < size; i++) array[i] = in.readCharacter(); return array; } public static Pair<Integer, Integer>[] readIntPairArray(InputReader in, int size) { @SuppressWarnings({ "unchecked" }) Pair<Integer, Integer>[] result = new Pair[size]; for (int i = 0; i < size; i++) result[i] = readIntPair(in); return result; } public static Pair<Long, Long>[] readLongPairArray(InputReader in, int size) { @SuppressWarnings({ "unchecked" }) Pair<Long, Long>[] result = new Pair[size]; for (int i = 0; i < size; i++) result[i] = readLongPair(in); return result; } public static void readIntArrays(InputReader in, int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readInt(); } } public static void readLongArrays(InputReader in, long[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readLong(); } } public static void readDoubleArrays(InputReader in, double[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readDouble(); } } public static char[][] readTable(InputReader in, int rowCount, int columnCount) { char[][] table = new char[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readCharArray(in, columnCount); return table; } public static int[][] readIntTable(InputReader in, int rowCount, int columnCount) { int[][] table = new int[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readIntArray(in, columnCount); return table; } public static double[][] readDoubleTable(InputReader in, int rowCount, int columnCount) { double[][] table = new double[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readDoubleArray(in, columnCount); return table; } public static long[][] readLongTable(InputReader in, int rowCount, int columnCount) { long[][] table = new long[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readLongArray(in, columnCount); return table; } public static String[][] readStringTable(InputReader in, int rowCount, int columnCount) { String[][] table = new String[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readStringArray(in, columnCount); return table; } public static String readText(InputReader in) { StringBuilder result = new StringBuilder(); while (true) { int character = in.read(); if (character == '\r') continue; if (character == -1) break; result.append((char) character); } return result.toString(); } public static void readStringArrays(InputReader in, String[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readString(); } } public static void printTable(OutputWriter out, char[][] table) { for (char[] row : table) out.printLine(new String(row)); } } class ArrayUtils { private static int[] tempInt = new int[0]; private static long[] tempLong = new long[0]; public static Integer[] generateOrder(int size) { Integer[] order = new Integer[size]; for (int i = 0; i < size; i++) order[i] = i; return order; } public static void fill(short[][] array, short value) { for (short[] row : array) Arrays.fill(row, value); } public static void fill(long[][] array, long value) { for (long[] row : array) Arrays.fill(row, value); } public static void fill(double[][] array, double value) { for (double[] row : array) Arrays.fill(row, value); } public static void fill(double[][][] array, double value) { for (double[][] row : array) fill(row, value); } public static void fill(double[][][][] array, double value) { for (double[][][] row : array) fill(row, value); } public static void fill(double[][][][][] array, double value) { for (double[][][][] row : array) fill(row, value); } public static void fill(long[][][] array, long value) { for (long[][] row : array) fill(row, value); } public static void fill(long[][][][] array, long value) { for (long[][][] row : array) fill(row, value); } public static void fill(long[][][][][] array, long value) { for (long[][][][] row : array) fill(row, value); } public static void fillColumn(long[][] array, int index, long value) { for (long[] row : array) row[index] = value; } public static void fillColumn(int[][] array, int index, int value) { for (int[] row : array) row[index] = value; } public static void fill(int[][] array, int value) { for (int[] row : array) Arrays.fill(row, value); } public static void fill(boolean[][] array, boolean value) { for (boolean[] row : array) Arrays.fill(row, value); } public static void fill(boolean[][][] array, boolean value) { for (boolean[][] row : array) fill(row, value); } public static long sumArray(int[] array) { long result = 0; for (int element : array) result += element; return result; } public static int[] range(int from, int to) { int[] result = new int[Math.max(from, to) - Math.min(from, to) + 1]; int index = 0; if (to > from) { for (int i = from; i <= to; i++) result[index++] = i; } else { for (int i = from; i >= to; i--) result[index++] = i; } return result; } public static void fill(int[][][] array, int value) { for (int[][] subArray : array) fill(subArray, value); } public static void fill(short[][][] array, short value) { for (short[][] subArray : array) fill(subArray, value); } public static void fill(int[][][][] array, int value) { for (int[][][] subArray : array) fill(subArray, value); } public static void fill(short[][][][] array, short value) { for (short[][][] subArray : array) fill(subArray, value); } public static void fill(int[][][][][] array, int value) { for (int[][][][] subArray : array) fill(subArray, value); } public static void fill(short[][][][][] array, short value) { for (short[][][][] subArray : array) fill(subArray, value); } public static void fill(int[][][][][][] array, int value) { for (int[][][][][] subArray : array) fill(subArray, value); } public static void fill(short[][][][][][] array, short value) { for (short[][][][][] subArray : array) fill(subArray, value); } public static void fill(int[][][][][][][] array, int value) { for (int[][][][][][] subArray : array) fill(subArray, value); } public static void fill(short[][][][][][][] array, short value) { for (short[][][][][][] subArray : array) fill(subArray, value); } public static Integer[] order(int size, Comparator<Integer> comparator) { Integer[] order = generateOrder(size); Arrays.sort(order, comparator); return order; } public static <T> void fill(T[][] array, T value) { for (T[] row : array) Arrays.fill(row, value); } public static void fill(char[][] array, char value) { for (char[] row : array) Arrays.fill(row, value); } public static void fill(byte[][] array, byte value) { for (byte[] row : array) Arrays.fill(row, value); } public static void fill(byte[][][] array, byte value) { for (byte[][] row : array) fill(row, value); } public static void fill(byte[][][][] array, byte value) { for (byte[][][] row : array) fill(row, value); } public static long multiply(int[] first, int[] second) { long result = 0; for (int i = 0; i < first.length; i++) result += (long) first[i] * second[i]; return result; } public static int[] createOrder(int size) { int[] order = new int[size]; for (int i = 0; i < size; i++) order[i] = i; return order; } public static int[] sort(int[] array, IntComparator comparator) { return sort(array, 0, array.length, comparator); } public static int[] sort(int[] array, int from, int to, IntComparator comparator) { Integer[] intArray = new Integer[to - from]; for (int i = from; i < to; i++) intArray[i - from] = array[i]; Arrays.sort(intArray, comparator); for (int i=from; i<to; i++) array[i]=intArray[i-from]; return array; } private static void ensureCapacityInt(int size) { if (tempInt.length >= size) return; size = Math.max(size, tempInt.length << 1); tempInt = new int[size]; } private static void ensureCapacityLong(int size) { if (tempLong.length >= size) return; size = Math.max(size, tempLong.length << 1); tempLong = new long[size]; } private static void sortImpl(int[] array, int from, int to, int[] temp, int fromTemp, int toTemp, IntComparator comparator) { if (to - from <= 1) return; int middle = (to - from) >> 1; int tempMiddle = fromTemp + middle; sortImpl(temp, fromTemp, tempMiddle, array, from, from + middle, comparator); sortImpl(temp, tempMiddle, toTemp, array, from + middle, to, comparator); int index = from; int index1 = fromTemp; int index2 = tempMiddle; while (index1 < tempMiddle && index2 < toTemp) { if (comparator.compare(temp[index1], temp[index2]) <= 0) array[index++] = temp[index1++]; else array[index++] = temp[index2++]; } if (index1 != tempMiddle) System.arraycopy(temp, index1, array, index, tempMiddle - index1); if (index2 != toTemp) System.arraycopy(temp, index2, array, index, toTemp - index2); } public static int[] order(final int[] array) { return sort(createOrder(array.length), new IntComparator() { public int compare(Integer first, Integer second) { if (array[first] < array[second]) return -1; if (array[first] > array[second]) return 1; return 0; } }); } public static int[] order(final long[] array) { return sort(createOrder(array.length), new IntComparator() { public int compare(Integer first, Integer second) { if (array[first] < array[second]) return -1; if (array[first] > array[second]) return 1; return 0; } }); } public static int[] unique(int[] array) { return unique(array, 0, array.length); } public static int[] unique(int[] array, int from, int to) { if (from == to) return new int[0]; int count = 1; for (int i = from + 1; i < to; i++) { if (array[i] != array[i - 1]) count++; } int[] result = new int[count]; result[0] = array[from]; int index = 1; for (int i = from + 1; i < to; i++) { if (array[i] != array[i - 1]) result[index++] = array[i]; } return result; } public static char[] unique(char[] array) { return unique(array, 0, array.length); } public static char[] unique(char[] array, int from, int to) { if (from == to) return new char[0]; int count = 1; for (int i = from + 1; i < to; i++) { if (array[i] != array[i - 1]) count++; } char[] result = new char[count]; result[0] = array[from]; int index = 1; for (int i = from + 1; i < to; i++) { if (array[i] != array[i - 1]) result[index++] = array[i]; } return result; } public static int maxElement(int[] array) { return maxElement(array, 0, array.length); } public static int maxElement(int[] array, int from, int to) { int result = Integer.MIN_VALUE; for (int i = from; i < to; i++) result = Math.max(result, array[i]); return result; } public static int[] order(final double[] array) { return sort(createOrder(array.length), new IntComparator() { public int compare(Integer first, Integer second) { return Double.compare(array[first], array[second]); } }); } public static int[] reversePermutation(int[] permutation) { int[] result = new int[permutation.length]; for (int i = 0; i < permutation.length; i++) result[permutation[i]] = i; return result; } public static void reverse(int[] array) { for (int i = 0, j = array.length - 1; i < j; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } } public static void reverse(char[] array) { for (int i = 0, j = array.length - 1; i < j; i++, j--) { char temp = array[i]; array[i] = array[j]; array[j] = temp; } } private static long maxElement(long[] array, int from, int to) { long result = Long.MIN_VALUE; for (int i = from; i < to; i++) result = Math.max(result, array[i]); return result; } public static int minPosition(int[] array) { return minPosition(array, 0, array.length); } public static int maxPosition(int[] array) { return maxPosition(array, 0, array.length); } public static int minPosition(int[] array, int from, int to) { if (from >= to) return -1; int min = array[from]; int result = from; for (int i = from + 1; i < to; i++) { if (array[i] < min) { min = array[i]; result = i; } } return result; } public static int maxPosition(int[] array, int from, int to) { if (from >= to) return -1; int max = array[from]; int result = from; for (int i = from + 1; i < to; i++) { if (array[i] > max) { max = array[i]; result = i; } } return result; } public static int[] multiplyPermutations(int[] first, int[] second) { int count = first.length; int[] result = new int[count]; for (int i = 0; i < count; i++) { result[i] = first[second[i]]; } return result; } public static int[] compress(int[]... arrays) { int totalLength = 0; for (int[] array : arrays) totalLength += array.length; int[] all = new int[totalLength]; int delta = 0; for (int[] array : arrays) { System.arraycopy(array, 0, all, delta, array.length); delta += array.length; } sort(all, IntComparator.DEFAULT); all = unique(all); for (int[] array : arrays) { for (int i = 0; i < array.length; i++) array[i] = Arrays.binarySearch(all, array[i]); } return all; } public static int minElement(int[] array) { return array[minPosition(array)]; } public static long[] partialSums(int[] array) { long[] result = new long[array.length + 1]; for (int i = 0; i < array.length; i++) result[i + 1] = result[i] + array[i]; return result; } public static void orderBy(int[] base, int[]... arrays) { int[] order = ArrayUtils.order(base); order(order, base); for (int[] array : arrays) order(order, array); } public static void orderBy(long[] base, long[]... arrays) { int[] order = ArrayUtils.order(base); order(order, base); for (long[] array : arrays) order(order, array); } public static void order(int[] order, int[] array) { ensureCapacityInt(order.length); for (int i = 0; i < order.length; i++) tempInt[i] = array[order[i]]; System.arraycopy(tempInt, 0, array, 0, array.length); } public static void order(int[] order, long[] array) { ensureCapacityLong(order.length); for (int i = 0; i < order.length; i++) tempLong[i] = array[order[i]]; System.arraycopy(tempLong, 0, array, 0, array.length); } public static long[] asLong(int[] array) { long[] result = new long[array.length]; for (int i = 0; i < array.length; i++) result[i] = array[i]; return result; } public static int count(int[] array, int value) { int result = 0; for (int i : array) { if (i == value) result++; } return result; } public static int count(char[] array, char value) { int result = 0; for (char i : array) { if (i == value) result++; } return result; } public static int count(boolean[] array, boolean value) { int result = 0; for (boolean i : array) { if (i == value) result++; } return result; } public static int[] merge(int[] first, int[] second) { int[] result = new int[first.length + second.length]; int firstIndex = 0; int secondIndex = 0; int index = 0; while (firstIndex < first.length && secondIndex < second.length) { if (first[firstIndex] < second[secondIndex]) result[index++] = first[firstIndex++]; else result[index++] = second[secondIndex++]; } System.arraycopy(first, firstIndex, result, index, first.length - firstIndex); System.arraycopy(second, secondIndex, result, index, second.length - secondIndex); return result; } public static boolean nextPermutation(int[] array) { return nextPermutation(array, IntComparator.DEFAULT); } private static boolean nextPermutation(int[] array, IntComparator comparator) { int size = array.length; int last = array[size - 1]; for (int i = size - 2; i >= 0; i--) { int current = array[i]; if (comparator.compare(last, current) > 0) { for (int j = size - 1; j > i; j--) { if (comparator.compare(array[j], current) > 0) { swap(array, i, j); inPlaceReverse(array, i + 1, size); return true; } } } last = current; } return false; } private static void inPlaceReverse(int[] array, int first, int second) { for (int i = first, j = second - 1; i < j; i++, j--) swap(array, i, j); } private static void swap(int[] array, int first, int second) { if (first == second) return; int temp = array[first]; array[first] = array[second]; array[second] = temp; } public static <V> void reverse(V[] array) { for (int i = 0, j = array.length - 1; i < j; i++, j--) { V temp = array[i]; array[i] = array[j]; array[j] = temp; } } public static IntComparator compareBy(final int[]... arrays) { return new IntComparator() { public int compare(Integer first, Integer second) { for (int[] array : arrays) { if (array[first] != array[second]) return Integer.compare(array[first], array[second]); } return 0; } }; } public static long minElement(long[] array) { return array[minPosition(array)]; } public static long maxElement(long[] array) { return array[maxPosition(array)]; } public static int minPosition(long[] array) { return minPosition(array, 0, array.length); } public static int maxPosition(long[] array) { return maxPosition(array, 0, array.length); } public static int minPosition(long[] array, int from, int to) { if (from >= to) return -1; long min = array[from]; int result = from; for (int i = from + 1; i < to; i++) { if (array[i] < min) { min = array[i]; result = i; } } return result; } public static int maxPosition(long[] array, int from, int to) { if (from >= to) return -1; long max = array[from]; int result = from; for (int i = from + 1; i < to; i++) { if (array[i] > max) { max = array[i]; result = i; } } return result; } public static int[] createArray(int count, int value) { int[] array = new int[count]; Arrays.fill(array, value); return array; } public static long[] createArray(int count, long value) { long[] array = new long[count]; Arrays.fill(array, value); return array; } public static double[] createArray(int count, double value) { double[] array = new double[count]; Arrays.fill(array, value); return array; } public static boolean[] createArray(int count, boolean value) { boolean[] array = new boolean[count]; Arrays.fill(array, value); return array; } public static char[] createArray(int count, char value) { char[] array = new char[count]; Arrays.fill(array, value); return array; } public static <T> T[] createArray(int count, T value) { @SuppressWarnings("unchecked") T[] array = (T[]) Array.newInstance(value.getClass(), count); Arrays.fill(array, value); return array; } } class Graph { public static final int REMOVED_BIT = 0; protected int vertexCount; protected int edgeCount; private int[] firstOutbound; private int[] firstInbound; private Edge[] edges; private int[] nextInbound; private int[] nextOutbound; private int[] from; private int[] to; private long[] weight; private long[] capacity; private int[] reverseEdge; private int[] flags; public Graph(int vertexCount) { this(vertexCount, vertexCount); } public Graph(int vertexCount, int edgeCapacity) { this.vertexCount = vertexCount; firstOutbound = new int[vertexCount]; Arrays.fill(firstOutbound, -1); from = new int[edgeCapacity]; to = new int[edgeCapacity]; nextOutbound = new int[edgeCapacity]; flags = new int[edgeCapacity]; } public static Graph createGraph(int vertexCount, int[] from, int[] to) { Graph graph = new Graph(vertexCount, from.length); for (int i = 0; i < from.length; i++) graph.addSimpleEdge(from[i], to[i]); return graph; } public static Graph createWeightedGraph(int vertexCount, int[] from, int[] to, long[] weight) { Graph graph = new Graph(vertexCount, from.length); for (int i = 0; i < from.length; i++) graph.addWeightedEdge(from[i], to[i], weight[i]); return graph; } public static Graph createFlowGraph(int vertexCount, int[] from, int[] to, long[] capacity) { Graph graph = new Graph(vertexCount, from.length * 2); for (int i = 0; i < from.length; i++) graph.addFlowEdge(from[i], to[i], capacity[i]); return graph; } public static Graph createFlowWeightedGraph(int vertexCount, int[] from, int[] to, long[] weight, long[] capacity) { Graph graph = new Graph(vertexCount, from.length * 2); for (int i = 0; i < from.length; i++) graph.addFlowWeightedEdge(from[i], to[i], weight[i], capacity[i]); return graph; } public static Graph createTree(int[] parent) { Graph graph = new Graph(parent.length + 1, parent.length); for (int i = 0; i < parent.length; i++) graph.addSimpleEdge(parent[i], i + 1); return graph; } public int addEdge(int fromID, int toID, long weight, long capacity, int reverseEdge) { ensureEdgeCapacity(edgeCount + 1); if (firstOutbound[fromID] != -1) nextOutbound[edgeCount] = firstOutbound[fromID]; else nextOutbound[edgeCount] = -1; firstOutbound[fromID] = edgeCount; if (firstInbound != null) { if (firstInbound[toID] != -1) nextInbound[edgeCount] = firstInbound[toID]; else nextInbound[edgeCount] = -1; firstInbound[toID] = edgeCount; } this.from[edgeCount] = fromID; this.to[edgeCount] = toID; if (capacity != 0) { if (this.capacity == null) this.capacity = new long[from.length]; this.capacity[edgeCount] = capacity; } if (weight != 0) { if (this.weight == null) this.weight = new long[from.length]; this.weight[edgeCount] = weight; } if (reverseEdge != -1) { if (this.reverseEdge == null) { this.reverseEdge = new int[from.length]; Arrays.fill(this.reverseEdge, 0, edgeCount, -1); } this.reverseEdge[edgeCount] = reverseEdge; } if (edges != null) edges[edgeCount] = createEdge(edgeCount); return edgeCount++; } protected final GraphEdge createEdge(int id) { return new GraphEdge(id); } public final int addFlowWeightedEdge(int from, int to, long weight, long capacity) { if (capacity == 0) { return addEdge(from, to, weight, 0, -1); } else { int lastEdgeCount = edgeCount; addEdge(to, from, -weight, 0, lastEdgeCount + entriesPerEdge()); return addEdge(from, to, weight, capacity, lastEdgeCount); } } protected int entriesPerEdge() { return 1; } public final int addFlowEdge(int from, int to, long capacity) { return addFlowWeightedEdge(from, to, 0, capacity); } public final int addWeightedEdge(int from, int to, long weight) { return addFlowWeightedEdge(from, to, weight, 0); } public final int addSimpleEdge(int from, int to) { return addWeightedEdge(from, to, 0); } public final int vertexCount() { return vertexCount; } public final int edgeCount() { return edgeCount; } protected final int edgeCapacity() { return from.length; } public final Edge edge(int id) { initEdges(); return edges[id]; } public final int firstOutbound(int vertex) { int id = firstOutbound[vertex]; while (id != -1 && isRemoved(id)) id = nextOutbound[id]; return id; } public final int nextOutbound(int id) { id = nextOutbound[id]; while (id != -1 && isRemoved(id)) id = nextOutbound[id]; return id; } public final int firstInbound(int vertex) { initInbound(); int id = firstInbound[vertex]; while (id != -1 && isRemoved(id)) id = nextInbound[id]; return id; } public final int nextInbound(int id) { initInbound(); id = nextInbound[id]; while (id != -1 && isRemoved(id)) id = nextInbound[id]; return id; } public final int source(int id) { return from[id]; } public final int destination(int id) { return to[id]; } public final long weight(int id) { if (weight == null) return 0; return weight[id]; } public final long capacity(int id) { if (capacity == null) return 0; return capacity[id]; } public final long flow(int id) { if (reverseEdge == null) return 0; return capacity[reverseEdge[id]]; } public final void pushFlow(int id, long flow) { if (flow == 0) return; if (flow > 0) { if (capacity(id) < flow) throw new IllegalArgumentException("Not enough capacity"); } else { if (flow(id) < -flow) throw new IllegalArgumentException("Not enough capacity"); } capacity[id] -= flow; capacity[reverseEdge[id]] += flow; } public int transposed(int id) { return -1; } public final int reverse(int id) { if (reverseEdge == null) return -1; return reverseEdge[id]; } public final void addVertices(int count) { ensureVertexCapacity(vertexCount + count); Arrays.fill(firstOutbound, vertexCount, vertexCount + count, -1); if (firstInbound != null) Arrays.fill(firstInbound, vertexCount, vertexCount + count, -1); vertexCount += count; } protected final void initEdges() { if (edges == null) { edges = new Edge[from.length]; for (int i = 0; i < edgeCount; i++) edges[i] = createEdge(i); } } public final void removeVertex(int vertex) { int id = firstOutbound[vertex]; while (id != -1) { removeEdge(id); id = nextOutbound[id]; } initInbound(); id = firstInbound[vertex]; while (id != -1) { removeEdge(id); id = nextInbound[id]; } } private void initInbound() { if (firstInbound == null) { firstInbound = new int[firstOutbound.length]; Arrays.fill(firstInbound, 0, vertexCount, -1); nextInbound = new int[from.length]; for (int i = 0; i < edgeCount; i++) { nextInbound[i] = firstInbound[to[i]]; firstInbound[to[i]] = i; } } } public final boolean flag(int id, int bit) { return (flags[id] >> bit & 1) != 0; } public final void setFlag(int id, int bit) { flags[id] |= 1 << bit; } public final void removeFlag(int id, int bit) { flags[id] &= -1 - (1 << bit); } public final void removeEdge(int id) { setFlag(id, REMOVED_BIT); } public final void restoreEdge(int id) { removeFlag(id, REMOVED_BIT); } public final boolean isRemoved(int id) { return flag(id, REMOVED_BIT); } public final Iterable<Edge> outbound(final int id) { initEdges(); return new Iterable<Edge>() { public Iterator<Edge> iterator() { return new EdgeIterator(id, firstOutbound, nextOutbound); } }; } public final Iterable<Edge> inbound(final int id) { initEdges(); initInbound(); return new Iterable<Edge>() { public Iterator<Edge> iterator() { return new EdgeIterator(id, firstInbound, nextInbound); } }; } protected void ensureEdgeCapacity(int size) { if (from.length < size) { int newSize = Math.max(size, 2 * from.length); if (edges != null) edges = resize(edges, newSize); from = resize(from, newSize); to = resize(to, newSize); nextOutbound = resize(nextOutbound, newSize); if (nextInbound != null) nextInbound = resize(nextInbound, newSize); if (weight != null) weight = resize(weight, newSize); if (capacity != null) capacity = resize(capacity, newSize); if (reverseEdge != null) reverseEdge = resize(reverseEdge, newSize); flags = resize(flags, newSize); } } private void ensureVertexCapacity(int size) { if (firstOutbound.length < size) { int newSize = Math.max(size, 2 * from.length); firstOutbound = resize(firstOutbound, newSize); if (firstInbound != null) firstInbound = resize(firstInbound, newSize); } } protected final int[] resize(int[] array, int size) { int[] newArray = new int[size]; System.arraycopy(array, 0, newArray, 0, array.length); return newArray; } private long[] resize(long[] array, int size) { long[] newArray = new long[size]; System.arraycopy(array, 0, newArray, 0, array.length); return newArray; } private Edge[] resize(Edge[] array, int size) { Edge[] newArray = new Edge[size]; System.arraycopy(array, 0, newArray, 0, array.length); return newArray; } public final boolean isSparse() { return vertexCount == 0 || edgeCount * 20 / vertexCount <= vertexCount; } protected class GraphEdge implements Edge { protected int id; protected GraphEdge(int id) { this.id = id; } public int getSource() { return source(id); } public int getDestination() { return destination(id); } public long getWeight() { return weight(id); } public long getCapacity() { return capacity(id); } public long getFlow() { return flow(id); } public void pushFlow(long flow) { Graph.this.pushFlow(id, flow); } public boolean getFlag(int bit) { return flag(id, bit); } public void setFlag(int bit) { Graph.this.setFlag(id, bit); } public void removeFlag(int bit) { Graph.this.removeFlag(id, bit); } public int getTransposedID() { return transposed(id); } public Edge getTransposedEdge() { int reverseID = getTransposedID(); if (reverseID == -1) return null; initEdges(); return edge(reverseID); } public int getReverseID() { return reverse(id); } public Edge getReverseEdge() { int reverseID = getReverseID(); if (reverseID == -1) return null; initEdges(); return edge(reverseID); } public int getID() { return id; } public void remove() { removeEdge(id); } public void restore() { restoreEdge(id); } } public class EdgeIterator implements Iterator<Edge> { private int edgeID; private final int[] next; private int lastID = -1; public EdgeIterator(int id, int[] first, int[] next) { this.next = next; edgeID = nextEdge(first[id]); } private int nextEdge(int id) { while (id != -1 && isRemoved(id)) id = next[id]; return id; } public boolean hasNext() { return edgeID != -1; } public Edge next() { if (edgeID == -1) throw new NoSuchElementException(); lastID = edgeID; edgeID = nextEdge(next[lastID]); return edges[lastID]; } public void remove() { if (lastID == -1) throw new IllegalStateException(); removeEdge(lastID); lastID = -1; } } } class MiscUtils { public static final int[] DX4 = { 1, 0, -1, 0 }; public static final int[] DY4 = { 0, -1, 0, 1 }; public static final int[] DX8 = { 1, 1, 1, 0, -1, -1, -1, 0 }; public static final int[] DY8 = { -1, 0, 1, 1, 1, 0, -1, -1 }; public static final int[] DX_KNIGHT = { 2, 1, -1, -2, -2, -1, 1, 2 }; public static final int[] DY_KNIGHT = { 1, 2, 2, 1, -1, -2, -2, -1 }; private static final String[] ROMAN_TOKENS = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; private static final int[] ROMAN_VALUES = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; public static long josephProblem(long n, int k) { if (n == 1) return 0; if (k == 1) return n - 1; if (k > n) return (josephProblem(n - 1, k) + k) % n; long count = n / k; long result = josephProblem(n - count, k); result -= n % k; if (result < 0) result += n; else result += result / (k - 1); return result; } public static boolean isValidCell(int row, int column, int rowCount, int columnCount) { return row >= 0 && row < rowCount && column >= 0 && column < columnCount; } public static List<Integer> getPath(int[] last, int destination) { List<Integer> path = new ArrayList<Integer>(); while (destination != -1) { path.add(destination); destination = last[destination]; } inPlaceReverse(path); return path; } private static void inPlaceReverse(List<Integer> list) { for (int i = 0, j = list.size() - 1; i < j; i++, j--) swap(list, i, j); } private static void swap(List<Integer> list, int first, int second) { if (first == second) return; int temp = list.get(first); list.set(first, list.get(second)); list.set(second, temp); } public static List<Integer> getPath(int[][] lastIndex, int[][] lastPathNumber, int destination, int pathNumber) { List<Integer> path = new ArrayList<Integer>(); while (destination != -1 || pathNumber != 0) { path.add(destination); int nextDestination = lastIndex[destination][pathNumber]; pathNumber = lastPathNumber[destination][pathNumber]; destination = nextDestination; } inPlaceReverse(path); return path; } public static long maximalRectangleSum(long[][] array) { int n = array.length; int m = array[0].length; long[][] partialSums = new long[n + 1][m + 1]; for (int i = 0; i < n; i++) { long rowSum = 0; for (int j = 0; j < m; j++) { rowSum += array[i][j]; partialSums[i + 1][j + 1] = partialSums[i][j + 1] + rowSum; } } long result = Long.MIN_VALUE; for (int i = 0; i < m; i++) { for (int j = i; j < m; j++) { long minPartialSum = 0; for (int k = 1; k <= n; k++) { long current = partialSums[k][j + 1] - partialSums[k][i]; result = Math.max(result, current - minPartialSum); minPartialSum = Math.min(minPartialSum, current); } } } return result; } public static int parseIP(String ip) { String[] components = ip.split("[.]"); int result = 0; for (int i = 0; i < 4; i++) result += (1 << (24 - 8 * i)) * Integer.parseInt(components[i]); return result; } public static String buildIP(int mask) { StringBuilder result = new StringBuilder(); for (int i = 0; i < 4; i++) { if (i != 0) result.append('.'); result.append(mask >> (24 - 8 * i) & 255); } return result.toString(); } public static long binarySearch(long from, long to, Function<Long, Boolean> function) { while (from < to) { long argument = from + (to - from) / 2; if (function.value(argument)) to = argument; else from = argument + 1; } return from; } public static <T> boolean equals(T first, T second) { return first == null && second == null || first != null && first.equals(second); } public static boolean isVowel(char ch) { ch = Character.toUpperCase(ch); return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'Y'; } public static boolean isStrictVowel(char ch) { ch = Character.toUpperCase(ch); return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'; } public static String convertToRoman(int number) { StringBuilder result = new StringBuilder(); for (int i = 0; i < ROMAN_TOKENS.length; i++) { while (number >= ROMAN_VALUES[i]) { number -= ROMAN_VALUES[i]; result.append(ROMAN_TOKENS[i]); } } return result.toString(); } public static int convertFromRoman(String number) { int result = 0; for (int i = 0; i < ROMAN_TOKENS.length; i++) { while (number.startsWith(ROMAN_TOKENS[i])) { number = number.substring(ROMAN_TOKENS[i].length()); result += ROMAN_VALUES[i]; } } return result; } public static int distance(int x1, int y1, int x2, int y2) { int dx = x1 - x2; int dy = y1 - y2; return dx * dx + dy * dy; } public static <T extends Comparable<T>> T min(T first, T second) { if (first.compareTo(second) <= 0) return first; return second; } public static <T extends Comparable<T>> T max(T first, T second) { if (first.compareTo(second) <= 0) return second; return first; } public static void decreaseByOne(int[]... arrays) { for (int[] array : arrays) { for (int i = 0; i < array.length; i++) array[i]--; } } public static int[] getIntArray(String s) { String[] tokens = s.split(" "); int[] result = new int[tokens.length]; for (int i = 0; i < result.length; i++) result[i] = Integer.parseInt(tokens[i]); return result; } } class IntegerUtils { public static long gcd(long a, long b) { a = Math.abs(a); b = Math.abs(b); while (b != 0) { long temp = a % b; a = b; b = temp; } return a; } public static int gcd(int a, int b) { a = Math.abs(a); b = Math.abs(b); while (b != 0) { int temp = a % b; a = b; b = temp; } return a; } public static int[] generatePrimes(int upTo) { int[] isPrime = generateBitPrimalityTable(upTo); List<Integer> primes = new ArrayList<Integer>(); for (int i = 0; i < upTo; i++) { if ((isPrime[i >> 5] >>> (i & 31) & 1) == 1) primes.add(i); } int[] array = new int[primes.size()]; for (int i = 0; i < array.length; i++) array[i] = primes.get(i); return array; } public static boolean[] generatePrimalityTable(int upTo) { boolean[] isPrime = new boolean[upTo]; if (upTo < 2) return isPrime; Arrays.fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i < upTo; i++) { if (isPrime[i]) { for (int j = i * i; j < upTo; j += i) isPrime[j] = false; } } return isPrime; } public static int[] generateBitPrimalityTable(int upTo) { int[] isPrime = new int[(upTo + 31) >> 5]; if (upTo < 2) return isPrime; Arrays.fill(isPrime, -1); isPrime[0] &= -4; for (int i = 2; i * i < upTo; i++) { if ((isPrime[i >> 5] >>> (i & 31) & 1) == 1) { for (int j = i * i; j < upTo; j += i) isPrime[j >> 5] &= -1 - (1 << (j & 31)); } } return isPrime; } public static int[] generateDivisorTable(int upTo) { int[] divisor = new int[upTo]; for (int i = 1; i < upTo; i++) divisor[i] = i; for (int i = 2; i * i < upTo; i++) { if (divisor[i] == i) { for (int j = i * i; j < upTo; j += i) divisor[j] = i; } } return divisor; } public static long powerInFactorial(long n, long p) { long result = 0; while (n != 0) { result += n /= p; } return result; } public static int sumDigits(CharSequence number) { int result = 0; for (int i = number.length() - 1; i >= 0; i--) result += digitValue(number.charAt(i)); return result; } public static int digitValue(char digit) { if (Character.isDigit(digit)) return digit - '0'; if (Character.isUpperCase(digit)) return digit + 10 - 'A'; return digit + 10 - 'a'; } public static int longCompare(long a, long b) { if (a < b) return -1; if (a > b) return 1; return 0; } public static long[][] generateBinomialCoefficients(int n) { long[][] result = new long[n + 1][n + 1]; for (int i = 0; i <= n; i++) { result[i][0] = 1; for (int j = 1; j <= i; j++) result[i][j] = result[i - 1][j - 1] + result[i - 1][j]; } return result; } public static long[][] generateBinomialCoefficients(int n, long module) { long[][] result = new long[n + 1][n + 1]; if (module == 1) return result; for (int i = 0; i <= n; i++) { result[i][0] = 1; for (int j = 1; j <= i; j++) { result[i][j] = result[i - 1][j - 1] + result[i - 1][j]; if (result[i][j] >= module) result[i][j] -= module; } } return result; } public static long[] generateBinomialRow(int n, long module) { long[] result = generateReverse(n + 1, module); result[0] = 1; for (int i = 1; i <= n; i++) result[i] = result[i - 1] * (n - i + 1) % module * result[i] % module; return result; } public static int[] representationInBase(long number, int base) { long basePower = base; int exponent = 1; while (number >= basePower) { basePower *= base; exponent++; } int[] representation = new int[exponent]; for (int i = 0; i < exponent; i++) { basePower /= base; representation[i] = (int) (number / basePower); number %= basePower; } return representation; } public static int trueDivide(int a, int b) { return (a - trueMod(a, b)) / b; } public static long trueDivide(long a, long b) { return (a - trueMod(a, b)) / b; } public static int trueMod(int a, int b) { a %= b; a += b; a %= b; return a; } public static long trueMod(long a, long b) { a %= b; a += b; a %= b; return a; } public static long factorial(int n) { long result = 1; for (int i = 2; i <= n; i++) result *= i; return result; } public static long factorial(int n, long mod) { long result = 1; for (int i = 2; i <= n; i++) result = result * i % mod; return result % mod; } public static List<Pair<Long, Integer>> factorize(long number) { List<Pair<Long, Integer>> result = new ArrayList<Pair<Long, Integer>>(); for (long i = 2; i * i <= number; i++) { if (number % i == 0) { int power = 0; do { power++; number /= i; } while (number % i == 0); result.add(Pair.makePair(i, power)); } } if (number != 1) result.add(Pair.makePair(number, 1)); return result; } public static List<Long> getDivisors(long number) { List<Pair<Long, Integer>> primeDivisors = factorize(number); return getDivisorsImpl(primeDivisors, 0, 1, new ArrayList<Long>()); } private static List<Long> getDivisorsImpl( List<Pair<Long, Integer>> primeDivisors, int index, long current, List<Long> result) { if (index == primeDivisors.size()) { result.add(current); return result; } long p = primeDivisors.get(index).first; int power = primeDivisors.get(index).second; for (int i = 0; i <= power; i++) { getDivisorsImpl(primeDivisors, index + 1, current, result); current *= p; } return result; } public static long power(long base, long exponent) { if (exponent == 0) return 1; long result = power(base, exponent >> 1); result = result * result; if ((exponent & 1) != 0) result = result * base; return result; } public static long power(long base, long exponent, long mod) { if (base >= mod) base %= mod; if (exponent == 0) return 1 % mod; long result = power(base, exponent >> 1, mod); result = result * result % mod; if ((exponent & 1) != 0) result = result * base % mod; return result; } public static long lcm(long a, long b) { return a / gcd(a, b) * b; } public static long[] generateFibonacci(long upTo) { int count = 0; long last = 0; long current = 1; while (current <= upTo) { long next = last + current; last = current; current = next; count++; } return generateFibonacci(count, -1); } public static long[] generateFibonacci(int count, long module) { long[] result = new long[count]; if (module == -1) { if (count != 0) result[0] = 1; if (count > 1) result[1] = 1; for (int i = 2; i < count; i++) result[i] = result[i - 1] + result[i - 2]; } else { if (count != 0) result[0] = 1 % module; if (count > 1) result[1] = 1 % module; for (int i = 2; i < count; i++) result[i] = (result[i - 1] + result[i - 2]) % module; } return result; } public static long[] generateHappy(int digits) { long[] happy = new long[(1 << (digits + 1)) - 2]; happy[0] = 4; happy[1] = 7; int first = 0; int last = 2; for (int i = 2; i <= digits; i++) { for (int j = 0; j < last - first; j++) { happy[last + 2 * j] = 10 * happy[first + j] + 4; happy[last + 2 * j + 1] = 10 * happy[first + j] + 7; } int next = last + 2 * (last - first); first = last; last = next; } return happy; } public static long[] generateFactorial(int count, long module) { long[] result = new long[count]; if (module == -1) { if (count != 0) result[0] = 1; for (int i = 1; i < count; i++) result[i] = result[i - 1] * i; } else { if (count != 0) result[0] = 1 % module; for (int i = 1; i < count; i++) result[i] = (result[i - 1] * i) % module; } return result; } public static long reverse(long number, long module) { return power(number, module - 2, module); } public static boolean isPrime(long number) { if (number < 2) return false; for (long i = 2; i * i <= number; i++) { if (number % i == 0) return false; } return true; } public static long[] generateReverse(int upTo, long module) { long[] result = new long[upTo]; if (upTo > 1) result[1] = 1; for (int i = 2; i < upTo; i++) result[i] = (module - module / i * result[((int) (module % i))] % module) % module; return result; } public static long[] generateReverseFactorials(int upTo, long module) { long[] result = generateReverse(upTo, module); if (upTo > 0) result[0] = 1; for (int i = 1; i < upTo; i++) result[i] = result[i] * result[i - 1] % module; return result; } public static long[] generatePowers(long base, int count, long mod) { long[] result = new long[count]; if (count != 0) result[0] = 1 % mod; for (int i = 1; i < count; i++) result[i] = result[i - 1] * base % mod; return result; } public static long nextPrime(long from) { if (from <= 2) return 2; from += 1 - (from & 1); while (!isPrime(from)) from += 2; return from; } public static long binomialCoefficient(int n, int m, long mod) { if (m < 0 || m > n) return 0; if (2 * m > n) m = n - m; long result = 1; for (int i = n - m + 1; i <= n; i++) result = result * i % mod; return result * BigInteger.valueOf(factorial(m, mod)) .modInverse(BigInteger.valueOf(mod)).longValue() % mod; } public static boolean isSquare(long number) { long sqrt = Math.round(Math.sqrt(number)); return sqrt * sqrt == number; } public static long findCommon(long aRemainder, long aMod, long bRemainder, long bMod) { long modGCD = gcd(aMod, bMod); long gcdRemainder = aRemainder % modGCD; if (gcdRemainder != bRemainder % modGCD) return -1; aMod /= modGCD; aRemainder /= modGCD; bMod /= modGCD; bRemainder /= modGCD; long aReverse = BigInteger.valueOf(aMod) .modInverse(BigInteger.valueOf(bMod)).longValue(); long bReverse = BigInteger.valueOf(bMod) .modInverse(BigInteger.valueOf(aMod)).longValue(); long mod = aMod * bMod; return (bReverse * aRemainder % mod * bMod + aReverse * bRemainder % mod * aMod) % mod * modGCD + gcdRemainder; } public static long[] generatePowers(long base, long maxValue) { if (maxValue <= 0) return new long[0]; int size = 1; long current = 1; while (maxValue / base >= current) { current *= base; size++; } return generatePowers(base, size, Long.MAX_VALUE); } } interface IntComparator extends Comparator<Integer> { public static final IntComparator DEFAULT = new IntComparator() { public int compare(Integer first, Integer second) { if (first < second) return -1; if (first > second) return 1; return 0; } }; public static final IntComparator REVERSE = new IntComparator() { public int compare(Integer first, Integer second) { if (first < second) return 1; if (first > second) return -1; return 0; } }; public int compare(Integer first, Integer second); } class BidirectionalGraph extends Graph { public int[] transposedEdge; public BidirectionalGraph(int vertexCount) { this(vertexCount, vertexCount); } public BidirectionalGraph(int vertexCount, int edgeCapacity) { super(vertexCount, 2 * edgeCapacity); transposedEdge = new int[2 * edgeCapacity]; } public static BidirectionalGraph createGraph(int vertexCount, int[] from, int[] to) { BidirectionalGraph graph = new BidirectionalGraph(vertexCount, from.length); for (int i = 0; i < from.length; i++) graph.addSimpleEdge(from[i], to[i]); return graph; } public static BidirectionalGraph createWeightedGraph(int vertexCount, int[] from, int[] to, long[] weight) { BidirectionalGraph graph = new BidirectionalGraph(vertexCount, from.length); for (int i = 0; i < from.length; i++) graph.addWeightedEdge(from[i], to[i], weight[i]); return graph; } public static BidirectionalGraph createFlowGraph(int vertexCount, int[] from, int[] to, long[] capacity) { BidirectionalGraph graph = new BidirectionalGraph(vertexCount, from.length * 2); for (int i = 0; i < from.length; i++) graph.addFlowEdge(from[i], to[i], capacity[i]); return graph; } public static BidirectionalGraph createFlowWeightedGraph(int vertexCount, int[] from, int[] to, long[] weight, long[] capacity) { BidirectionalGraph graph = new BidirectionalGraph(vertexCount, from.length * 2); for (int i = 0; i < from.length; i++) graph.addFlowWeightedEdge(from[i], to[i], weight[i], capacity[i]); return graph; } @Override public int addEdge(int fromID, int toID, long weight, long capacity, int reverseEdge) { int lastEdgeCount = edgeCount; super.addEdge(fromID, toID, weight, capacity, reverseEdge); super.addEdge(toID, fromID, weight, capacity, reverseEdge == -1 ? -1 : reverseEdge + 1); this.transposedEdge[lastEdgeCount] = lastEdgeCount + 1; this.transposedEdge[lastEdgeCount + 1] = lastEdgeCount; return lastEdgeCount; } @Override protected int entriesPerEdge() { return 2; } @Override public final int transposed(int id) { return transposedEdge[id]; } @Override protected void ensureEdgeCapacity(int size) { if (size > edgeCapacity()) { super.ensureEdgeCapacity(size); transposedEdge = resize(transposedEdge, edgeCapacity()); } } } class Pair<U, V> implements Comparable<Pair<U, V>> { public final U first; public final V second; public static <U, V> Pair<U, V> makePair(U first, V second) { return new Pair<U, V>(first, second); } private Pair(U first, V second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null); } @Override public int hashCode() { int result = first != null ? first.hashCode() : 0; result = 31 * result + (second != null ? second.hashCode() : 0); return result; } public Pair<V, U> swap() { return makePair(second, first); } @Override public String toString() { return "(" + first + "," + second + ")"; } @SuppressWarnings({ "unchecked" }) public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) first).compareTo(o.first); if (value != 0) return value; return ((Comparable<V>) second).compareTo(o.second); } } interface Edge { public int getSource(); public int getDestination(); public long getWeight(); public long getCapacity(); public long getFlow(); public void pushFlow(long flow); public boolean getFlag(int bit); public void setFlag(int bit); public void removeFlag(int bit); public int getTransposedID(); public Edge getTransposedEdge(); public int getReverseID(); public Edge getReverseEdge(); public int getID(); public void remove(); public void restore(); } interface Function<A, V> { public abstract V value(A argument); }
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 solution(); stack<clock_t> times; void start_t() { times.push(clock()); } void stop_t(string out) { clock_t now = clock(); clock_t past = times.top(); times.pop(); double delta = now - past; cout << out << ": " << fixed << delta / (double)CLOCKS_PER_SEC << endl; } int main() { ios::sync_with_stdio(false); int t; cin >> t; for (int i = 0; i < t; i++) solution(); return 0; } void solution() { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (((k - d1 - d2) % 3 == 0) && (k >= d1 + d2)) { if (((n - k - max(d1, d2) - abs(d1 - d2)) % 3 == 0) && (n >= k + max(d1, d2) + abs(d1 - d2))) { cout << "yes" << endl; return; } } if (((k - d1 - 2 * d2) % 3 == 0) && (k >= d1 + 2 * d2)) { if (((n - k - 2 * d1 - d2) % 3 == 0) && (n >= k + 2 * d1 + d2)) { cout << "yes" << endl; return; } } if (((k - d2 - 2 * d1) % 3 == 0) && (k >= d2 + 2 * d1)) { if (((n - k - d1 - 2 * d2) % 3 == 0) && (n >= k + d1 + 2 * d2)) { cout << "yes" << endl; return; } } if (((k + d2 + d1) % 3 == 0) && (((k + d2 + d1) / 3) >= max(d1, d2))) { if (((n - k - d1 - d2) % 3 == 0) && (n >= k + d1 + d2)) { cout << "yes" << endl; return; } } 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
#include <bits/stdc++.h> using namespace std; int tests; long long n, k, d1, d2; void read() { cin >> tests; } void solve() { while (tests--) { cin >> n >> k >> d1 >> d2; long long left, a, b, c, x, diff; if (tests == 99943) { } if (n % 3 != 0) goto _next; x = k + d2 - d1; if (x >= 0 && x % 3 == 0) { b = x / 3; a = d1 + b; c = b - d2; if (a >= 0 && b >= 0 && c >= 0 && a >= b && b >= c && a <= n / 3 && b <= n / 3 && c <= n / 3) { cout << "yes\n"; continue; } } x = k - d1 - d2; if (x >= 0 && x % 3 == 0) { b = x / 3; a = d1 + b; c = b + d2; if (a >= 0 && b >= 0 && c >= 0 && a >= b && b <= c && a <= n / 3 && b <= n / 3 && c <= n / 3) { cout << "yes\n"; continue; } } x = k + d1 + d2; if (x >= 0 && x % 3 == 0) { b = x / 3; a = b - d1; c = b - d2; if (a >= 0 && b >= 0 && c >= 0 && a <= b && b >= c && a <= n / 3 && b <= n / 3 && c <= n / 3) { cout << "yes\n"; continue; } } x = k + d1 - d2; if (x >= 0 && x % 3 == 0) { b = x / 3; a = b - d1; c = d2 + b; if (a >= 0 && b >= 0 && c >= 0 && a <= b && b <= c && a <= n / 3 && b <= n / 3 && c <= n / 3) { cout << "yes\n"; continue; } } _next: cout << "no\n"; } } int main() { read(); 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.lang.*; import java.util.*; import java.text.*; public class Main { static int[][] poss={{1,1},{1,-1},{-1,1},{-1,-1}}; public static void main(String[] args) throws IOException { //BufferedReader cin = new BufferedReader(new FileReader("te.txt")); BufferedReader cin = new BufferedReader(new InputStreamReader(System.in)); String line; StringTokenizer st; line = cin.readLine(); st = new StringTokenizer(line); int T = Integer.parseInt(st.nextToken()); long n,k,d1,d2,d1o,d2o; long w1,w2,w3,max,ave; boolean ans; for(int ti=0;ti<T;ti++){ line = cin.readLine(); st = new StringTokenizer(line); n=Long.parseLong(st.nextToken()); k=Long.parseLong(st.nextToken()); d1o=Long.parseLong(st.nextToken()); d2o=Long.parseLong(st.nextToken()); ans=false; for(int i=0;i<4;i++){ if(n%3!=0)continue; //Mistake!!!!!! d1,d2 need to be init to orig every time!!! d1=d1o*poss[i][0]; d2=d2o*poss[i][1]; if((2*d1+d2+k)%3!=0)continue; w1=(2*d1+d2+k)/3; w2=w1-d1; w3=w2-d2; max=Math.max(Math.max(w1,w2),w3); if(w1>=0&&w2>=0&&w3>=0){ if(n>=3*max){ ans=true; break; } } } if(ans)System.out.println("yes"); else System.out.println("no"); } cin.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; const int inf = 1e9, mod = 1e9 + 7, N = 3e5; const long long linf = 1e18; bool check(long long n, long long k, long long a, long long b, long long c) { if (a < 0 || b < 0 || c < 0) return false; long long mn = min(min(a, b), c); a -= mn; b -= mn; c -= mn; long long d = k - (a + b + c); if (d < 0 || d % 3 != 0) return false; long long mx = max(max(a, b), c); d = (n - k - (mx - a) - (mx - b) - (mx - c)); if (d % 3 == 0 && d >= 0) return true; return false; } long long n, k, d1, d2, T; void solve() { scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2); if (n % 3 != 0) { printf("no\n"); return; } if (check(n, k, d1 + d2, d2, 0) || check(n, k, 0, d1, d1 + d2) || check(n, k, d2, d2 + d1, d1) || check(n, k, d1, 0, d2)) { printf("yes\n"); } else { printf("no\n"); } } int main() { cin >> T; for (int i = 1; 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.*; import java.util.*; public class SolutionC { public void solve(){ int t = nextInt(); for (int i = 0; i < t; i++) { long n = nextLong(), k = nextLong(), d1 = nextLong(), d2= nextLong(); if(n%3 != 0 ){ out.println("no"); continue; } long a = (2 * d1 + d2 + k ),b=0,c=0; if(a % 3 == 0){ a/=3; b = a - d1; c = b - d2; if(valid(a,b,c,n)){ out.println("yes"); continue; } } a = 2 * d1 - d2 + k; if(a%3 == 0){ a/=3; b = a - d1; c = b + d2; if(valid(a,b,c,n)){ out.println("yes"); continue; } } a = -2 * d1 + d2 + k; if(a%3 == 0){ a/=3; b = a + d1; c = b - d2; if(valid(a,b,c,n)){ out.println("yes"); continue; } } a = -2 * d1 - d2 + k; if(a%3 == 0){ a/=3; b = a + d1; c = b + d2; if(valid(a,b,c,n)){ out.println("yes"); continue; } } out.println("no"); } } boolean valid(long a, long b, long c , long n){ if(a <= n/3 && b <= n/3 && c<= n/3 && a >= 0 && b >= 0 && c >= 0) return true; return false; } public void run(){ solve(); out.close(); } public static void main(String args[]){ new SolutionC().run(); } BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); String line; StringTokenizer st; public String nextLine(){ try { line = bf.readLine(); st = new StringTokenizer(line); } catch (IOException e) { return null; } return line; } public String nextString(){ while (st == null || !st.hasMoreElements()) { try { line = bf.readLine(); st = new StringTokenizer(line); } catch (IOException e) { return null; } } return st.nextToken(); } public int nextInt(){ return Integer.parseInt(nextString()); } public long nextLong(){ return Long.parseLong(nextString()); } public double nextDouble(){ return Double.parseDouble(nextString()); } }
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.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; import static java.lang.Math.min; import static java.lang.Math.max; /* br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); */ public class Main { private static BufferedReader br; private static StringTokenizer st; private static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //int qq = 1; //int qq = Integer.MAX_VALUE; int qq = readInt(); for(int casenum = 1; casenum <= qq; casenum++) { long n = readLong(); long k = readLong(); long d1 = readLong(); long d2 = readLong(); if(n%3!=0) { pw.println("no"); continue; } n /= 3; if((k-d1-d2)%3 == 0) { long x = (k-d1-d2)/3; long y = x+d1; long z = x+d2; if(valid(x, y, z, n)) { pw.println("yes"); continue; } } if((k-d1+d2)%3 == 0) { long x = (k-d1+d2)/3; long y = x+d1; long z = x-d2; if(valid(x, y, z, n)) { pw.println("yes"); continue; } } if((k+d1-d2)%3 == 0) { long x = (k+d1-d2)/3; long y = x-d1; long z = x+d2; if(valid(x, y, z, n)) { pw.println("yes"); continue; } } if((k+d1+d2)%3 == 0) { long x = (k+d1+d2)/3; long y = x-d1; long z = x-d2; if(valid(x, y, z, n)) { pw.println("yes"); continue; } } pw.println("no"); } pw.close(); } public static boolean valid(long a, long b, long c, long n) { return Math.min(a, Math.min(b, c)) >= 0 && Math.max(a, Math.max(b, c)) <= n; } private static void exitImmediately() { pw.close(); System.exit(0); } private static long readLong() throws IOException { return Long.parseLong(nextToken()); } private static double readDouble() throws IOException { return Double.parseDouble(nextToken()); } private static int readInt() throws IOException { return Integer.parseInt(nextToken()); } private static String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { if(!br.ready()) { exitImmediately(); } st = new StringTokenizer(br.readLine().trim()); } 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
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.ObjectInputStream.GetField; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class Q2 { static int MOD = 1000000007; static boolean b[]; static boolean boo[][]; static ArrayList<Integer>[] amp, amp2; static HashMap<ArrayList<Integer>,Integer> ts1 = new HashMap<>(); static int sum[],dist[],array[]; static long ans = 0; static int p = 0; static int color[][],brr[][]; static HashSet<Integer>[][] hrr; static FasterScanner sc = new FasterScanner(); static Queue<Integer> q = new LinkedList<>(); static Queue<Pair> qu = new LinkedList<>(); static ArrayList<Pair>[][] arr; static ArrayList<Graph>[] amp1; static ArrayList<Integer> parent = new ArrayList<>(); static BufferedWriter log; static TreeMap<Integer,LinkedList<Pair>> tm; static HashSet<Integer> hs; static Stack<Integer> s = new Stack<>(); static Pair prr[]; static char ch[][]; static int prime[]; static long d,x,y; static int n,k,m; public static void main(String[] args) throws IOException { log = new BufferedWriter(new OutputStreamWriter(System.out)); int t = sc.nextInt(); while(t-->0){ long n = sc.nextLong(), k = sc.nextLong(), d1 = sc.nextLong(), d2 = sc.nextLong(); long n1 = n-k; if((k-(2*d2+d1))>=0 && (k-(2*d2+d1))%3==0 && (n1-(2*d1+d2))>=0 && (n1-(2*d1+d2))%3==0){ System.out.println("yes"); continue; } if((k-(2*d2-d1))>=0 && (k-(2*d2-d1))%3==0 && (d2+(k-(2*d2-d1))/3-d1)>=0 && (n1-(d1+d2))>=0 && (n1-(d1+d2))%3==0){ System.out.println("yes"); continue; } if((k-(d2+d1))>=0 && (k-(d2+d1))%3==0 && (n1-(long)(Math.abs(d2-d1)+Math.max(d2, d1)))>=0 && (n1-(long)(Math.abs(d2-d1)+Math.max(d2, d1)))%3==0){ System.out.println("yes"); continue; } if((k-(d2-d1))>=0 && (k-(d2-d1))%3==0 && ((k-(d2-d1))/3-d1)>=0 && (n1-(d1+2*d2))>=0 && (n1-(d1+2*d2))%3==0){ System.out.println("yes"); continue; } System.out.println("no"); } log.close(); } public static void print2DArray(int arr[][],int row, int column){ for(int i = 0;i<row;i++){ for(int j = 0;j<column;j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } } public static void dfs(int x){ b[x] = true; for(int i =0 ;i<amp[x].size();i++){ if(!b[amp[x].get(i)]){ dfs(amp[x].get(i)); } } } public static void bfs2D(int x,int y){ tm = new TreeMap<>(); LinkedList<Pair> hs1 = new LinkedList<>(); hs = new HashSet<>(); hs1.add(new Pair(x,y)); boo[x][y] = true; hrr[x][y].add(brr[x][y]); tm.put(hrr[x][y].size(),hs1); while(!tm.isEmpty()){ hs1 = tm.get(tm.firstKey()); //System.out.println(tm); Pair p = hs1.pollFirst(); //System.out.println(p); hs.add(brr[p.u][p.v]); if(hs1.isEmpty()) tm.remove(tm.firstKey()); for(Pair temp:arr[p.u][p.v]) { if(!boo[temp.u][temp.v]){ if(!hs.contains(brr[temp.u][temp.v])){ color[temp.u][temp.v] = color[p.u][p.v]+1; } else color[temp.u][temp.v] = color[p.u][p.v]; if(tm.containsKey(color[temp.u][temp.v])){ if(hs.contains(brr[temp.u][temp.v])) tm.get(color[temp.u][temp.v]).addLast(new Pair(temp.u,temp.v)); else{ tm.get(color[temp.u][temp.v]).addFirst(new Pair(temp.u,temp.v)); } } else{ LinkedList<Pair> temp1 = new LinkedList<>(); temp1.add(new Pair(temp.u,temp.v)); tm.put(color[temp.u][temp.v], temp1); } boo[temp.u][temp.v] = true; } } } } public static void bfs(int x,int val){ b[x] = true; q.add(x); sum[x] = val; while(!q.isEmpty()){ int y = q.poll(); // System.out.println(y+"*"+sum[y]); for(int i = 0;i<amp[y].size();i++) { if(!b[amp[y].get(i)]){ q.add(amp[y].get(i)); b[amp[y].get(i)] = true; sum[amp[y].get(i)] = sum[y]*(-1); } } } } public static void toThread(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { //InputReader(System.in); // pw = new PrintWriter(System.out); //soln(); //pw.close(); //soln(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } public static long mod(int x, int y){ long ans = 1; while(y>0){ if(y%2==1){ ans = ((ans%MOD)*(x%MOD))%MOD; y-=1; } else{ x = (int) (((x%MOD)*(x%MOD))%MOD); y = y/2; } } return ans; } static void findFactors(ArrayList<Integer> primeDivisors, ArrayList<Integer> multiplicity, int currentDivisor, long currentResult) { if (currentDivisor == primeDivisors.size()) { // ts.add(currentResult); return; } for (int i = 0; i <= multiplicity.get(currentDivisor); ++i) { findFactors(primeDivisors, multiplicity, currentDivisor + 1, currentResult); currentResult *= primeDivisors.get(currentDivisor); } } public static void seive(long n){ b = new boolean[(int) (n+1)]; Arrays.fill(b, true); b[0] =false; b[1] = true; for(int i = 2;i*i<=n;i++){ if(b[i]){ for(int p = 2*i;p<=n;p+=i){ b[p] = false; } } } for(int i = 0;i<n;i++) if(b[i]) parent.add(i); } public static void factors(int n){ b = new boolean[n+1]; prime = new int[n+1]; Arrays.fill(b, true); b[0] =false; b[1] = false; for(int i = 2;i*i<=n;i++){ if(b[i]){ prime[i] = i; for(int p = 2*i;p<=n;p+=i){ if(b[p]){ b[p] = false; prime[p] = i; } } } } for(int i = 2;i<=n;i++){ if(b[i]) prime[i] = i; } } public static void buildWeightedGraph(int m){ for(int i = 0; i<m;i++){ int x = sc.nextInt(), y = sc.nextInt(), w = sc.nextInt(); amp1[--x].add(new Graph(--y,w)); amp1[y].add(new Graph(x,w)); } } public static void extendedEuclidian(long a, long b){ if(b == 0) { d = a; x = 1; y = 0; } else { extendedEuclidian(b, a%b); int temp = (int) x; x = y; y = temp - (a/b)*y; } } private static void permutation(String prefix, String str) { int n = str.length(); // if (n == 0) ts.add(prefix); // else { for (int i = 0; i < n; i++) permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n)); //} } public static char give(char c1,char c2){ if(c1!='a' && c2!='a') return 'a'; if(c1!='b' && c2!='b') return 'b'; return 'c'; } static class Graph{ int vertex; long weight; Graph(int v,int w){ vertex = v; weight = w; } } public static void buildTree(int n){ int arr[] = sc.nextIntArray(n); for(int i = 0;i<n;i++){ int x = arr[i]-1; amp[i+1].add(x); amp[x].add(i+1); } } public static void buildGraph(int n){ for(int i =0;i<n;i++){ int x = sc.nextInt(), y = sc.nextInt(); amp[--x].add(--y); //amp[y].add(x); } } static class Pair implements Comparable<Pair> { int u; int v; int z; public Pair(){ } public Pair(int u, int v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31*hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return (u == other.u && v == other.v); } public int compareTo(Pair other) { return Integer.compare(u, other.u) != 0 ? (Integer.compare(u, other.u)) : (Integer.compare(v, other.v)); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } static class SegmentTree{ long st[]; char ch[]; boolean lazy[]; SegmentTree(int n){ int size = 4*n; //ch = str.toCharArray(); st = new long[size]; lazy = new boolean[size]; build(ch,0,n-1,0); } long build(char str[], int ss, int se, int si){ if(ss==se){ return 0; } int mid = (ss+se)/2; st[si] = build(str,ss,mid,si*2+1) + build(str,mid+1,se,si*2+2); return st[si]; } /*void updateRange(int node, int start, int end, int l, int r, boolean val) { // System.out.println(lazy[node]+" "+node); if(lazy[node]) { // This node needs to be updated long temp = st[node][0]; st[node][0] = st[node][1]; st[node][1] = temp; // Update it if(start != end) { lazy[node*2+1] = lazy[node]; // Mark child as lazy lazy[node*2+2] = lazy[node]; // Mark child as lazy } lazy[node] = false; // Reset it } if(start > end || start > r || end < l) // Current segment is not within range [l, r] return; if(start >= l && end <= r) { // Segment is fully within range if(val){ long temp = st[node][0]; st[node][0] = st[node][1]; st[node][1] = temp; if(start != end) { // Not leaf node lazy[node*2+1] = val; // Mark child as lazy lazy[node*2+2] = val; } } return; } int mid = (start + end) / 2; updateRange(node*2+1, start, mid, l, r, val); // Updating left child updateRange(node*2 + 2, mid + 1, end, l, r, val); // Updating right child st[node][0] = st[node*2+1][0] + st[node*2+2][0]; st[node][1] = st[node*2+1][1] + st[node*2+2][1];// Updating root with max value } long[] queryRange(int node, int start, int end, int l, int r) { if(start > end || start > r || end < l) return new long[]{0,0}; // Out of range if(lazy[node]) { long temp = st[node][0]; st[node][0] = st[node][1]; st[node][1] = temp; // Update it if(start != end) { lazy[node*2+1] = lazy[node]; // Mark child as lazy lazy[node*2+2] = lazy[node]; // Mark child as lazy } lazy[node] = false; // Reset it } if(start >= l && end <= r) // Current segment is totally within range [l, r] return st[node]; int mid = (start + end) / 2; long p1[] = queryRange(node*2+1, start, mid, l, r); // Query left child long p2[] = queryRange(node*2 + 2, mid + 1, end, l, r); // Query right child p1[0]+=p2[0]; p1[1]+=p2[1]; return p1; }*/ long get(int qs, int qe, int ss, int se, int si){ if(qe<ss || qs>se){ return 0; } if(qs<=ss && qe>=se) return st[si]; int mid = (ss+se)/2; return (get(qs,qe,ss,mid,2*si+1)+get(qs,qe, mid+1, se, 2*si+2)); } void update(int ss,int se,int si,int idx){ if(ss==se){ st[si]++; //str[idx] = 1; } else{ int mid = (ss+se)/2; if(ss<=idx && idx<=mid) update(ss,mid,2*si+1,idx); else update(mid+1,se,2*si+2,idx); st[si] = (st[2*si+1]+st[2*si+2]); } } void print(){ for(int i = 0;i<st.length;i++){ System.out.print(st[i]+" "); } System.out.println(); } } static int convert(int x){ int cnt = 0; String str = Integer.toBinaryString(x); //System.out.println(str); for(int i = 0;i<str.length();i++){ if(str.charAt(i)=='1'){ cnt++; } } int ans = (int) Math.pow(3, 6-cnt); return ans; } static class Node2{ Node2 left = null; Node2 right = null; Node2 parent = null; int data; } static class BinarySearchTree{ Node2 root = null; int height = 0; int max = 0; int cnt = 1; ArrayList<Integer> parent = new ArrayList<>(); HashMap<Integer, Integer> hm = new HashMap<>(); public void insert(int x){ Node2 n = new Node2(); n.data = x; if(root==null){ root = n; } else{ Node2 temp = root,temp2 = null; while(temp!=null){ temp2 = temp; if(x>temp.data) temp = temp.right; else temp = temp.left; } if(x>temp2.data) temp2.right = n; else temp2.left = n; n.parent = temp2; parent.add(temp2.data); } } public Node2 getSomething(int x, int y, Node2 n){ if(n.data==x || n.data==y) return n; else if(n.data>x && n.data<y) return n; else if(n.data<x && n.data<y) return getSomething(x,y,n.right); else return getSomething(x,y,n.left); } public Node2 search(int x,Node2 n){ if(x==n.data){ max = Math.max(max, n.data); return n; } if(x>n.data){ max = Math.max(max, n.data); return search(x,n.right); } else{ max = Math.max(max, n.data); return search(x,n.left); } } public int getHeight(Node2 n){ if(n==null) return 0; height = 1+ Math.max(getHeight(n.left), getHeight(n.right)); return height; } } static long findDiff(long[] arr, long[] brr, int m){ int i = 0, j = 0; long fa = 1000000000000L; while(i<m && j<m){ long x = arr[i]-brr[j]; if(x>=0){ if(x<fa) fa = x; j++; } else{ if((-x)<fa) fa = -x; i++; } } return fa; } public static long max(long x, long y, long z){ if(x>=y && x>=z) return x; if(y>=x && y>=z) return y; return z; } static long modInverse(long a, long mOD2){ return power(a, mOD2-2, mOD2); } static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y/2, m) % m; p = (p * p) % m; return (y%2 == 0)? p : (x * p) % m; } static long power2(long x,BigInteger y,long m){ long ans = 1; BigInteger two = new BigInteger("2"); while(y.compareTo(BigInteger.ZERO)>0){ if(y.getLowestSetBit()==y.bitCount()){ x = (x*x)%MOD; y = y.divide(two); } else{ ans = (ans*x)%MOD; y = y.subtract(BigInteger.ONE); } } return ans; } static BigInteger power2(BigInteger x, BigInteger y, BigInteger m){ BigInteger ans = new BigInteger("1"); BigInteger one = new BigInteger("1"); BigInteger two = new BigInteger("2"); BigInteger zero = new BigInteger("0"); while(y.compareTo(zero)>0){ //System.out.println(y); if(y.mod(two).equals(one)){ ans = ans.multiply(x).mod(m); y = y.subtract(one); } else{ x = x.multiply(x).mod(m); y = y.divide(two); } } return ans; } static BigInteger power(BigInteger x, BigInteger y, BigInteger m) { if (y.equals(0)) return (new BigInteger("1")); BigInteger p = power(x, y.divide(new BigInteger("2")), m).mod(m); p = (p.multiply(p)).mod(m); return (y.mod(new BigInteger("2")).equals(0))? p : (p.multiply(x)).mod(m); } public static long gcd(long n, long m){ if(m!=0) return gcd(m,n%m); else return n; } public static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 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 int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
JAVA
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; long long dist[3][3]; int id[3]; int main() { memset(dist, -1, sizeof dist); int tcase; long long n, k, d1, d2; scanf("%d", &tcase); while (tcase--) { scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2); long long remain = n - k; dist[1][0] = dist[0][1] = d1; dist[1][2] = dist[2][1] = d2; for (int i = 0; i < 3; i++) id[i] = i; bool ok = false; do { long long a = dist[id[0]][id[1]], b = dist[id[1]][id[2]], c = dist[id[0]][id[2]]; if (c == -1) c = a + b; if (b == -1) b = c - a; if (a == -1) a = c - b; if (a < 0 || b < 0) continue; if (k - 2 * a - b >= 0 && (k - 2 * a - b) % 3 == 0) { if (remain - a - 2 * b >= 0 && (remain - a - 2 * b) % 3 == 0) { ok = true; } } } while (next_permutation(id, id + 3) && !ok); 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 main() { int i, j, t, tt, ii, jj; long long n, k, p, k1, need, d1, d2, x1, x2, x3, mid, min, max; bool flag; scanf("%d", &tt); for (t = 1; t <= tt; t++) { cin >> n >> k >> d1 >> d2; flag = false; for (i = 1; i <= 2; i++) for (j = 1; j <= 2; j++) { if (i == 1) ii = 1; else ii = -1; if (j == 1) jj = 1; else jj = -1; k1 = k + ii * d1 + jj * d2; if (k1 % 3 == 0) { x2 = k1 / 3; x1 = x2 - ii * d1; x3 = x2 - jj * d2; max = x1; if (x2 > max) max = x2; if (x3 > max) max = x3; min = x1; if (x2 < min) min = x2; if (x3 < min) min = x3; if (min >= 0) { mid = x1 + x2 + x3 - max - min; need = max - min + max - mid; if (n - k >= need) { p = n - k - need; if (p % 3 == 0) flag = true; } } } } if (flag) printf("yes\n"); else printf("no\n"); } }
CPP
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
t = int(input()) ret = [] while t>0: t-=1 n,k,d1,d2 = map(int,input().split()) # ans = [] y1 = (k-(d1-d2))//3 x1 = y1+d1 z1 = y1-d2 # ans = [y1,z1,x1] # ans = sorted(ans) # ans1 = 2*ans[2]-(ans[0]+ans[1]) ans1 = 2*x1-(z1+y1) if x1+y1+z1==k and min(z1,y1)>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue # ans = [] y1 = (k-(d1+d2))//3 x1 = y1+d1 z1 = y1+d2 if d1>=d2: # ans = [y1,z1,x1] ans1 = 2*x1-(y1+z1) else: # ans = [y1,x1,z1] ans1 = 2*z1-(y1+x1) # ans = sorted(ans) # ans1 = 2*ans[2]-(ans[0]+ans[1]) if x1+y1+z1==k and y1>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue y1 = (k-(d2-d1))//3 x1 = y1-d1 z1 = y1+d2 # ans = [x1,y1,z1] # ans = sorted(ans) ans1 = 2*z1-(x1+y1) if x1+y1+z1==k and min(x1,y1)>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue y1 = (k+(d2+d1))//3 x1 = y1-d1 z1 = y1-d2 # ans = [x1,y1,z1] # ans = sorted(ans) ans1 = 2*y1-(x1+z1) if x1+y1+z1==k and min(x1,z1)>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue # if d1>=d2: # ans.append(2*d1-d2) # ans.append(d2+2*(d1-d2)) # else: # ans.append(2*d2-d1) # ans.append(d1+2*(d2-d1)) # ans+=[d1+2*d2,d2+2*d1,d1+d2] # done = False # print(ans) # for a in ans: # # if (a==0 and (n-k)%3==0) or (a!=0 and (n-k)//a>1 and (n-k)%a==0): # if (a<=n-k) and (n-k-a)%3==0: # print(a) # done = True # break # if done: # print('yes') # else: ret.append('no') print(*ret, sep = '\n')
PYTHON3
451_C. Predict Outcome of the Game
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≀ t ≀ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≀ n ≀ 1012; 0 ≀ k ≀ n; 0 ≀ d1, d2 ≀ k) β€” data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
2
9
#include <bits/stdc++.h> using namespace std; const long long INFLL = (1LL << 62); const int INF = (1 << 30), MOD = (int)1e9 + 7; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int q; cin >> q; while (q--) { long long n, k, a, b; cin >> n >> k; cin >> a >> b; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 || j == 0) { continue; } long long d1 = (long long)a * i; long long d2 = (long long)b * j; if ((d1 + k + d1 + d2) % 3 == 0) { long long x1 = (d1 + k + d1 + d2) / 3; long long x3 = (d1 + k) - 2 * x1; long long x2 = d2 + x3; if (x1 >= 0 && x1 <= k && x2 >= 0 && x2 <= k && x3 >= 0 && x3 <= k) { if ((k - x1 - x2 - x3) % 3 == 0) { long long t = (k - x1 - x2 - x3) / 3; x1 += t, x2 += t, x3 += t; long long m = max(x1, max(x2, x3)); long long r = (m - x1) + (m - x2) + (m - x3); long long tmp = n - k - r; if (tmp >= 0 && tmp % 3 == 0) { cout << "yes\n"; goto wtf; } } } } } } cout << "no\n"; wtf: continue; } return 0; }
CPP