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
|
t = int(raw_input())
def check(a, b, c, n, k):
# print a, b, c
val = min(a, b, c)
if val < 0:
a += -val
b += -val
c += -val
# print a, b, c
s = a + b + c
if k < s:
return False
if (k-s) % 3 != 0:
return False
games = n - k
if (games+s) % 3 == 0:
m = max(a, b, c)
if games >= m*3 - s:
return True
else:
return False
else:
return False
for _t in xrange(t):
valid = False
n, k, d1, d2 = map(int, raw_input().split())
valid = valid or check(+d2+d1, +d2, 0, n, k)
valid = valid or check(-d2+d1, -d2, 0, n, k)
valid = valid or check(+d2-d1, +d2, 0, n, k)
valid = valid or check(-d2-d1, -d2, 0, n, k)
if valid:
print 'yes'
else:
print 'no'
|
PYTHON
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
long n=sc.nextLong();
long k=sc.nextLong();
long d1=sc.nextLong();
long d2=sc.nextLong();
boolean b=false;
if(n%3==0){
long w1=(k-(2*d1)+d2),w2=0,w3=0;
if(w1%3==0){
w1=w1/3;
w2=w1+d1;
w3=w1+d1-d2;
if(w3>=0&&w2>=0&&w1>=0&&w3<=n/3&&w2<=n/3&&w1<=n/3){b=true;}
}
w1=(k-(2*d1)-d2);w2=0;w3=0;
if(w1%3==0){
w1=w1/3;
w2=w1+d1;
w3=w1+d1+d2;
if(w3>=0&&w2>=0&&w1>=0&&w3<=n/3&&w2<=n/3&&w1<=n/3){b=true;}
}
w1=(k+(2*d1)+d2);w2=0;w3=0;
if(w1%3==0){
w1=w1/3;
w2=w1-d1;
w3=w1-d1-d2;
if(w3>=0&&w2>=0&&w1>=0&&w3<=n/3&&w2<=n/3&&w1<=n/3){b=true;}
}
w1=(k+(2*d1)-d2);w2=0;w3=0;
if(w1%3==0){
w1=w1/3;
w2=w1-d1;
w3=w1-d1+d2;
if(w3>=0&&w2>=0&&w1>=0&&w3<=n/3&&w2<=n/3&&w1<=n/3){b=true;}
}
}
if(b) System.out.println("yes");
else System.out.println("no");
}
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long n, k, d1, d2;
scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
if (n % 3) {
puts("no");
continue;
}
bool ok = false;
for (int s1 = -1; s1 <= 1; s1 += 2) {
for (int s2 = -1; s2 <= 1; s2 += 2) {
long long delta1 = d1 * s1;
long long delta2 = d2 * s2;
long long t = k - delta1 - delta2;
if (t < 0 || t % 3 != 0) continue;
long long x2 = t / 3;
long long x1 = x2 + delta1;
long long x3 = x2 + delta2;
assert(x1 + x2 + x3 == k);
if (x1 < 0 || x3 < 0) continue;
long long M = max(x1, max(x2, x3));
long long need = M - x1 + M - x2 + M - x3;
if (need <= n - k && (n - k - need) % 3 == 0) {
ok = true;
break;
}
}
if (ok) break;
}
printf("%s\n", 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
|
from itertools import product
def try_all(n, k, d1, d2):
for k1, k2 in filter(lambda (k1, k2): (k - k1 - k2) % 3 == 0, set(product((-d1, d1), (-d2, d2)))):
w2 = (k - k1 - k2) / 3
w1 = w2 + k1
w3 = w2 + k2
if w1 >= 0 and w2 >= 0 and w3 >= 0:
winamax = max(w1, w2, w3)
necessaire = 3 * winamax - w1 - w2 - w3
if n - k >= necessaire and (n - k - necessaire) % 3 == 0:
return 'yes'
return 'no'
t = int(raw_input())
for _ in range(t):
n, k, d1, d2 = map(int, raw_input().split())
print try_all(n, k, d1, d2)
|
PYTHON
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int nTest;
long long n, k, d1, d2;
bool isOK(long long a, long long b, long long c) {
if (a < 0) {
b -= a;
c -= a;
a -= a;
}
if (b < 0) {
c -= b;
a -= b;
b -= b;
}
if (c < 0) {
a -= c;
b -= c;
c -= c;
}
if (a + b + c > k) return 0;
if ((k - (a + b + c)) % 3 != 0) return 0;
long long m = max(a, max(b, c));
if (n - k < m - a + m - b + m - c) return 0;
if (((n - k) - (m - a + m - b + m - c)) % 3 != 0) return 0;
return 1;
}
int main() {
ios_base ::sync_with_stdio(0);
for (cin >> nTest; nTest; nTest--) {
cin >> n >> k >> d1 >> d2;
bool ok = 0;
ok |= isOK(0, d1, d1 + d2);
ok |= isOK(0, d1, d1 - d2);
ok |= isOK(d1, 0, d2);
ok |= isOK(-d1, 0, -d2);
ok |= isOK(d2 + d1, d2, 0);
ok |= isOK(d2 - d1, d2, 0);
cout << (ok ? "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>
using namespace std;
long long n, i, j, k, l, t = 1, ans = 0, flag = 0, d1, d2, a, b, c;
bool f(long long a, long long b, long long c, long long n, long long k) {
long long j = k - (a + b + c);
if (j % 3 != 0) return false;
long long x = j / 3;
a += x, b += x, c += x;
if (a < 0 || b < 0 || c < 0 || a + b + c != k) return false;
long long y = max(max(a, b), c);
n -= k;
n -= 3 * y - a - b - c;
if (n < 0 || n % 3 != 0) return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> k >> d1 >> d2;
if (n % 3 != 0) {
cout << "no\n";
continue;
}
if (f(0, d1, d1 + d2, n, k) || f(0, d1, d1 - d2, n, k) ||
f(0, -d1, -d1 + d2, n, k) || f(0, -d1, -d1 - d2, n, k))
cout << "yes\n";
else
cout << "no\n";
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br #c++ upperbound br(array,element)
#from __future__ import print_function, division #while using python2
def modinv(n,p):
return pow(n,p-2,p)
def calc(a, b, c, r):
# print(a, b, c)
if a < 0 or b < 0 or c < 0:
return False
temp = [a, b, c]
temp.sort(reverse=True)
a, b, c = temp
if a > b and a > c:
x = a - b
y = a - c
r -= (x + y)
if r < 0:
return False
if r % 3 == 0:
return True
else:
return False
if a == b:
if b == c:
return r % 3 == 0
x = b - c
r -= x
if r < 0:
return False
return r % 3 == 0
def main():
#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')
for case in range(int(input())):
n, k, d1, d2 = [int(x) for x in input().split()]
possible = False
# a >= b and b >= c
s = k + d1 + d2 + d1
if s % 3 == 0:
a = s // 3
b = a - d1
c = b - d2
possible = possible or calc(a, b, c, n-k)
# a >= b and b < c
s = k + d1 + d1 - d2
if s % 3 == 0:
a = s // 3
b = a - d1
c = b + d2
possible = possible or calc(a, b, c, n-k)
# a < b and b >= c
s = k - d1 - d1 + d2
if s % 3 == 0:
a = s // 3
b = a + d1
c = b - d2
possible = possible or calc(a, b, c, n-k)
# a < b and b < c
s = k - d1 - d1 - d2
if s % 3 == 0:
a = s // 3
b = a + d1
c = b + d2
possible = possible or calc(a, b, c, n-k)
if possible:
print("yes")
else:
print("no")
#------------------ Python 2 and 3 footer by Pajenegod and c1729-----------------------------------------
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s:self.buffer.write(s.encode('ascii'))
self.read = lambda:self.buffer.read().decode('ascii')
self.readline = lambda:self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
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
|
#include <bits/stdc++.h>
using namespace std;
int check(long long n, long long k, long long x, long long y, long long z) {
if (k >= x + y + z && (k - (x + y + z)) % 3 == 0) {
n -= k;
n -= z - x;
n -= z - y;
if (n >= 0 && n % 3 == 0) {
return 1;
}
}
return 0;
}
int main() {
int T;
cin >> T;
while (T--) {
long long n, k, d, dd;
cin >> n >> k >> d >> dd;
if (check(n, k, 0, dd, d + dd) || check(n, k, 0, min(d, dd), max(d, dd)) ||
(d >= dd && check(n, k, 0, d - dd, d)) ||
(d < dd && check(n, k, 0, dd - d, dd)) || check(n, k, 0, d, dd + d)) {
puts("yes");
} else
puts("no");
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long inf = 1e18 + 5;
const long long MX = 303030;
int cox[4] = {1, -1, 0, 0};
int coy[4] = {0, 0, 1, -1};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long leastbit(long long a) { return a & (-a); }
long long C(int a, int b) {
long long res = 1;
for (int i = 0; i < b; i++) res = res * (a - i) / (i + 1);
return res;
}
long long powmod(long long a, long long b) {
if (b == 0) return 1;
long long cnt = powmod(a, b / 2);
(cnt *= cnt) %= mod;
if (b & 1) {
(cnt *= a) %= mod;
}
return cnt;
}
bool sol(long long n, long long k, long long a, long long b) {
long long c = (k - a - 2 * b);
if (c < 0 || c % 3 != 0) return 0;
return a * 2 + b <= n - k;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
long long n, k, a, b;
cin >> n >> k >> a >> b;
if (n % 3) {
cout << "no" << endl;
continue;
}
if (sol(n, k, a, b) || sol(n, k, min(a, b), abs(a - b)) ||
sol(n, k, b, a) || sol(n, k, abs(b - a), min(a, b)))
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
bool b;
long long n, k, d1, d2;
void solve(long long d1, long long d2) {
long long x1, x2, x3;
x1 = (2 * d1 + d2 + k) / 3;
x3 = k + d1 - 2 * x1;
x2 = k - x1 - x3;
long long m = max(max(x1, x2), x3);
long long left = n - k - (m - x1) - (m - x2) - (m - x3);
if (x1 - x2 != d1 || x2 - x3 != d2 || x1 < 0 || x2 < 0 || x3 < 0 ||
x1 + x2 + x3 != k || left < 0 || left % 3 || x1 > n / 3 || x2 > n / 3 ||
x3 > n / 3)
b = false;
else
b = true;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%I64d %I64d %I64d %I64d", &n, &k, &d1, &d2);
if (n % 3)
printf("no\n");
else {
b = true;
solve(d1, d2);
if (b) {
printf("yes\n");
continue;
}
b = true;
solve(d1, -d2);
if (b) {
printf("yes\n");
continue;
}
b = true;
solve(-d1, d2);
if (b) {
printf("yes\n");
continue;
}
b = true;
solve(-d1, -d2);
if (b) {
printf("yes\n");
continue;
}
printf("no\n");
}
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
long long n, k, d1, d2;
int check(long long d1, long long d2) {
if ((k + d1 + d1 + d2) % 3 != 0) return 0;
long long x = (k + d1 + d1 + d2) / 3, y = x - d1, z = k - x - y;
return x >= 0 && y >= 0 && z >= 0 && x <= n / 3 && y <= n / 3 && z <= n / 3;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
if (n % 3 == 0 && (check(d1, d2) || check(d1, -d2) || check(-d1, d2) ||
check(-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;
using lld = int64_t;
class S {
public:
tuple<lld, lld, lld> sp(lld n, lld k, lld d1, lld d2) {
lld a = k + d2 + 2 * d1;
if (a % 3 == 0) {
a /= 3;
} else {
return make_tuple(-1, -1, -1);
}
lld b = a - d1;
lld c = k - a - b;
if (a < 0 || b < 0 || c < 0) {
return make_tuple(-1, -1, -1);
} else if (a > n / 3 || b > n / 3 || c > n / 3) {
return make_tuple(-1, -1, -1);
}
return make_tuple(a, b, c);
}
string solve(lld n, lld k, lld d1, lld d2) {
if (n % 3 != 0) {
return "no";
}
lld a, b, c;
std::tie(a, b, c) = sp(n, k, d1, d2);
if (a != -1 && b != -1 && c != -1) {
return "yes";
}
std::tie(a, b, c) = sp(n, k, -d1, d2);
if (a != -1 && b != -1 && c != -1) {
return "yes";
}
std::tie(a, b, c) = sp(n, k, d1, -d2);
if (a != -1 && b != -1 && c != -1) {
return "yes";
}
std::tie(a, b, c) = sp(n, k, -d1, -d2);
if (a != -1 && b != -1 && c != -1) {
return "yes";
}
return "no";
}
void solve() {
lld t;
cin >> t;
lld n, k, d1, d2;
for (lld i = 0; i < t; ++i) {
cin >> n >> k >> d1 >> d2;
string res = solve(n, k, d1, d2);
cout << res << endl;
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
S s;
s.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
|
t = int(input());
while t > 0:
t -= 1;
n, k, d1, d2 = map(long, raw_input().split());
n -= k;
flag = 0;
if n-d2-2*d1 >= 0 and (n-d2-2*d1) % 3 == 0 and 2*d2+d1<=k and (k-2*d2-d1)%3==0:
flag = 1;
elif d2>=d1 and n-d1-d2 >= 0 and (n-d1-d2) % 3 == 0 and 2*d2-d1<=k and (k-2*d2+d1) % 3== 0:
flag = 1;
elif d1 >= d2 and n+d2-2*d1>=0 and (n+d2-2*d1)%3==0 and (k-d1-d2)%3==0 and d1+d2<=k:
flag = 1;
elif d1>=d2 and n-d2-d1>=0 and (n-d2-d1)%3==0 and d1*2-d2<=k and (k-d1*2+d2) %3==0:
flag = 1;
elif d2>=d1 and d2+d1<=k and n+d1-2*d2>=0 and (n+d1-2*d2)%3==0 and(k-d1-d2)%3==0:
flag = 1;
elif d1+2*d2<=n and (n-d1-2*d2)%3==0 and 2*d1+d2<=k and (k-2*d1-d2)%3==0:
flag = 1
print 'yes' if flag else 'no'
|
PYTHON
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
vector<bool> ans;
bool ok(long long n, long long k, long long d1, long long d2) {
if ((k - (d2 + 2 * d1)) >= 0 && (k - (d2 + 2 * d1)) % 3 == 0 &&
(n - (k + d1 + 2 * d2)) >= 0 && (n - (k + d1 + 2 * d2)) % 3 == 0)
return true;
if (k + d1 + d2 >= 0 && (k + d1 + d2) % 3 == 0 &&
((k + d1 + d2) / 3) - max(d1, d2) >= 0 && (n - (k + d1 + d2)) >= 0 &&
(n - (k + d1 + d2)) % 3 == 0)
return true;
if (k - d1 - d2 >= 0 && (k - d1 - d2) % 3 == 0 &&
(n - (k + min(d1, d2) + 2 * (max(d1, d2) - min(d1, d2)))) >= 0 &&
(n - (k + min(d1, d2) + 2 * (max(d1, d2) - min(d1, d2)))) % 3 == 0)
return true;
if (k - (2 * d2 + d1) >= 0 && (k - (2 * d2 + d1)) % 3 == 0 &&
(n - (k + d2 + 2 * d1)) >= 0 && (n - (k + d2 + 2 * d1)) % 3 == 0)
return true;
return false;
}
int main() {
long long t, n, k, d1, d2;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n >> k >> d1 >> d2;
if (ok(n, k, d1, d2))
ans.push_back(1);
else
ans.push_back(0);
}
for (int i = 0; i < ans.size(); i++) {
if (ans[i] == 0)
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>
int t;
long long n, k, d1, d2, td1, td2, x1, x2, x3;
bool solve() {
if (n % 3) return false;
for (int i = -1; i <= 1; ++i)
for (int j = -1; j <= 1; ++j) {
if (i == 0 || j == 0) continue;
d1 = i * td1;
d2 = j * td2;
if ((d1 * 2 + d2 + k) % 3 || (d2 - d1 + k) % 3 || (k - d1 - 2 * d2) % 3)
continue;
x1 = d1 * 2 + d2 + k;
x2 = d2 - d1 + k;
x3 = k - d1 - 2 * d2;
if (x1 <= n && x2 <= n && x3 <= n && x1 >= 0 && x2 >= 0 && x3 >= 0)
return true;
}
return false;
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%lld%lld%lld%lld", &n, &k, &td1, &td2);
puts(solve() ? "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;
long long n, k;
bool check(long long d1, long long d2) {
long long x, y, z;
if ((2 * d1 + d2 - k) % -3) return false;
x = (2 * d1 + d2 - k) / -3;
z = d1 + d2 + x;
y = k - x - z;
if (x < 0 || y < 0 || z < 0) return false;
if (d1 > 0 && k - 2 * x - z < 0) return false;
if (d1 < 0 && k - 2 * x - z > 0) return false;
if (d2 > 0 && 2 * z + x - k < 0) return false;
if (d2 < 0 && 2 * z + x - k > 0) return false;
if ((n - k + x + y + z) % 3) return false;
long long ans = (n - k + x + y + z) / 3;
return ans >= x && ans >= y && ans >= z;
}
int main() {
int t;
scanf("%d", &t);
for (int i = 0; i < t; ++i) {
long long d1, d2;
scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
if (check(d1, d2) || check(-d1, -d2) || check(-d1, d2) || check(d1, -d2))
printf("yes\n");
else
printf("no\n");
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a[5];
int T;
cin >> T;
while (T--) {
long long n, d1, d2, k, t;
scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
if (n % 3 != 0) {
printf("no\n");
continue;
}
t = n / 3;
if ((k - d1 + d2) % 3 == 0) {
a[2] = (k - d1 + d2) / 3;
a[1] = d1 + a[2];
a[3] = -d2 + a[2];
if (!(a[1] < 0 || a[2] < 0 || a[3] < 0 || a[1] > k || a[2] > k ||
a[3] > k)) {
sort(a + 1, a + 4);
if (a[3] <= t && a[2] <= t && a[1] <= t) {
printf("yes\n");
continue;
}
}
}
if ((k + d1 + d2) % 3 == 0) {
a[2] = (k + d1 + d2) / 3;
a[1] = -d1 + a[2];
a[3] = -d2 + a[2];
if (!(a[1] < 0 || a[2] < 0 || a[3] < 0 || a[1] > k || a[2] > k ||
a[3] > k)) {
sort(a + 1, a + 4);
if (a[3] <= t && a[2] <= t && a[1] <= t) {
printf("yes\n");
continue;
}
}
}
if ((k - d1 - d2) % 3 == 0) {
a[2] = (k - d1 - d2) / 3;
a[1] = d1 + a[2];
a[3] = d2 + a[2];
if (!(a[1] < 0 || a[2] < 0 || a[3] < 0 || a[1] > k || a[2] > k ||
a[3] > k)) {
sort(a + 1, a + 4);
if (a[3] <= t && a[2] <= t && a[1] <= t) {
printf("yes\n");
continue;
}
}
}
if ((k + d1 - d2) % 3 == 0) {
a[2] = (k + d1 - d2) / 3;
a[1] = -d1 + a[2];
a[3] = d2 + a[2];
if (!(a[1] < 0 || a[2] < 0 || a[3] < 0 || a[1] > k || a[2] > k ||
a[3] > k)) {
sort(a + 1, a + 4);
if (a[3] <= t && a[2] <= t && a[1] <= t) {
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.util.*;
import java.io.*;
public class a {
public static void main(String[] args) throws IOException
{
input.init(System.in);
PrintWriter out = new PrintWriter(System.out);
int T = input.nextInt();
for(int t = 0; t<T; t++)
{
long n = input.nextLong(), k = input.nextLong(), d1 = input.nextLong(), d2 = input.nextLong();
if(n%3 != 0) out.println("no");
else
{
if(k >= 2*d1 + d2 && (k-2*d1-d2)%3 == 0 && d1+d2+(k-2*d1-d2)/3 <= n/3) out.println("yes");
else if(k >= d1 + 2*d2 && (k-d1-2*d2)%3 == 0 && d1+d2+(k-d1-2*d2)/3 <= n/3) out.println("yes");
else if(k >= d1 + d2 && (k-d1-d2)%3 == 0 && Math.max(d1, d2) + (k-d1-d2)/3 <= n/3) out.println("yes");
else if(k >= 2*Math.max(d1, d2)-Math.min(d1, d2) && (k-2*Math.max(d1, d2)+Math.min(d1, d2))%3 == 0 && Math.max(d1, d2) + (k-2*Math.max(d1, d2)+Math.min(d1, d2))/3 <= n/3) out.println("yes");
else out.println("no");
}
}
out.close();
}
public static class input {
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() );
}
}
}
|
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 com.sun.org.apache.bcel.internal.generic.FieldGen;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Template implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
try {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
} catch (Exception e) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
}
class GraphBuilder {
int n, m;
int[] x, y;
int index;
int[] size;
GraphBuilder(int n, int m) {
this.n = n;
this.m = m;
x = new int[m];
y = new int[m];
size = new int[n];
}
void add(int u, int v) {
x[index] = u;
y[index] = v;
size[u]++;
size[v]++;
index++;
}
int[][] build() {
int[][] graph = new int[n][];
for (int i = 0; i < n; i++) {
graph[i] = new int[size[i]];
}
for (int i = index - 1; i >= 0; i--) {
int u = x[i];
int v = y[i];
graph[u][--size[u]] = v;
graph[v][--size[v]] = u;
}
return graph;
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine());
} catch (Exception e) {
return null;
}
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException {
int[] res = new int[size];
for (int i = 0; i < size; i++) {
res[i] = readInt();
}
return res;
}
long[] readLongArray(int size) throws IOException {
long[] res = new long[size];
for (int i = 0; i < size; i++) {
res[i] = readLong();
}
return res;
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
<T> List<T>[] createGraphList(int size) {
List<T>[] list = new List[size];
for (int i = 0; i < size; i++) {
list[i] = new ArrayList<>();
}
return list;
}
public static void main(String[] args) {
new Template().run();
// new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start();
}
long timeBegin, timeEnd;
void time() {
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
long memoryTotal, memoryFree;
void memory() {
memoryFree = Runtime.getRuntime().freeMemory();
System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
+ " KB");
}
public void run() {
try {
timeBegin = System.currentTimeMillis();
memoryTotal = Runtime.getRuntime().freeMemory();
init();
solve();
out.close();
if (System.getProperty("ONLINE_JUDGE") == null) {
time();
memory();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
boolean can(long n, long k, long d1, long d2) {
if (n % 3 != 0) return false;
long first = 0;
long second = first + d1;
long third = second + d2;
long min = Math.min(first, Math.min(second, third));
first -= min;
second -= min;
third -= min;
long over = first + second + third;
if (over > k) return false;
if ((k - over) % 3 != 0) return false;
first += (k - over) / 3;
second += (k - over) / 3;
third += (k - over) / 3;
long max = Math.max(first, Math.max(second, third));
if (max > n / 3) return false;
return true;
}
boolean ans(long n, long k, long d1, long d2) {
return can(n, k, d1, d2)
|| can(n, k, -d1, d2)
|| can(n, k, d1, -d2)
|| can(n, k, -d1, -d2);
}
void solve() throws IOException {
int t = readInt();
while (t-- > 0) {
out.println(ans(readLong(), readLong(), readLong(), readLong()) ? "yes" : "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 maxi(long long int a, long long int b, long long int c) {
if (a < b) {
return (b > c) ? b : c;
} else {
return (a > c) ? a : c;
}
}
bool cal(long long int a, long long int b, long long int c, long long int k,
long long int n) {
if (c < 0) {
return false;
} else {
if (a < 0 || b < 0 || c < 0) {
return false;
} else {
long long int t1 = a;
long long int t2 = b;
long long int t3 = c;
long long int temp = n - k;
if (a == b && b == c && temp >= 0) {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
} else if (a == b && b != c) {
if (b > c) {
long long int g = b - c;
temp = temp - g;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
} else {
long long int g = c - b;
temp = temp - 2 * g;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
}
} else if (b == c && a != b) {
if (b > a) {
long long int g = b - a;
temp = temp - g;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
} else {
long long int g = a - b;
temp = temp - 2 * g;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
}
} else if (a == c && c != b) {
if (c > b) {
long long int g = c - b;
temp = temp - g;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
} else {
long long int g = b - c;
temp = temp - 2 * g;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
}
} else if (a != c && c == b) {
if (c > a) {
long long int g = c - a;
temp = temp - g;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
} else {
long long int g = a - c;
temp = temp - 2 * g;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
}
} else if (a != b && b != c && a != c) {
long long int m = maxi(a, b, c);
if (a == m) {
long long int g = a - b;
long long int g1 = a - c;
temp = temp - g - g1;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
} else if (b == m) {
long long int g = b - a;
long long int g1 = b - c;
temp = temp - g - g1;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
} else {
long long int g = c - b;
long long int g1 = c - a;
temp = temp - g - g1;
if (temp < 0) {
return false;
} else {
if (temp % 3 == 0) {
return true;
} else {
return false;
}
}
}
}
}
}
}
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
long long int a, b, c;
if (k == 0) {
if (n % 3 == 0) {
cout << "yes\n";
} else {
cout << "no\n";
}
} else {
bool h4 = false;
bool h1 = false;
bool h2 = false;
bool h3 = false;
c = (k - d1 - 2 * d2);
if (c % 3 == 0) {
c = c / 3;
b = d2 + c;
a = d1 + b;
h1 = cal(a, b, c, k, n);
}
c = (k - d1 + 2 * d2);
if (c % 3 == 0) {
c = c / 3;
b = c - d2;
a = d1 + b;
h2 = cal(a, b, c, k, n);
}
c = (k + d1 - 2 * d2);
if (c % 3 == 0) {
c = c / 3;
b = c + d2;
a = b - d1;
h3 = cal(a, b, c, k, n);
}
c = (k + d1 + 2 * d2);
if (c % 3 == 0) {
c = c / 3;
b = c - d2;
a = b - d1;
h4 = cal(a, b, c, k, n);
}
if (h1 == true || h2 == true || h3 == true || h4 == true) {
cout << "yes\n";
} else {
cout << "no\n";
}
}
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
long long n, k, x, y, z;
bool work(long long d1, long long d2) {
if (k - 2 * d2 - d1 < 0) return false;
if ((k - 2 * d2 - d1) % 3) return false;
z = (k - 2 * d2 - d1) / 3;
y = d2 + z;
x = d1 + y;
if (x < 0 || y < 0) return false;
long long l = max(max(x, y), z), r = max(max(x, y), z) + n;
while (l <= r) {
long long mid = (l + r) >> 1;
long long tmp = (mid - x) + (mid - y) + (mid - z);
if (tmp == n) return true;
if (tmp < n)
l = mid + 1;
else
r = mid - 1;
}
return false;
}
int main() {
int w;
int T;
scanf("%d", &T);
while (T--) {
long long d1, d2;
cin >> n >> k >> d1 >> d2;
n -= k;
bool ok = false;
ok |= work(d1, d2);
ok |= work(-d1, d2);
ok |= work(d1, -d2);
ok |= work(-d1, -d2);
ok ? puts("yes") : 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>
int64_t max(int64_t a, int64_t b) {
if (a < b) return b;
return a;
}
int64_t min(int64_t a, int64_t b) {
if (a < b) return a;
return b;
}
int main() {
int64_t t, i, n, k, d1, d2;
scanf("%" PRId64, &t);
for (i = 0; i < t; ++i) {
scanf("%" PRId64, &n);
scanf("%" PRId64, &k);
scanf("%" PRId64, &d1);
scanf("%" PRId64, &d2);
if (n % 3 != 0) {
printf("no\n");
continue;
} else {
int64_t x1, x2, x3;
if ((k - d1 + d2) % 3 == 0) {
x2 = (k - d1 + d2) / 3;
x1 = x2 + d1;
x3 = x2 - d2;
int64_t r = n - k - (x1 - x2) - (x1 - x3);
if (x1 >= 0 && x2 >= 0 && x3 >= 0 && r >= 0 && r % 3 == 0) {
printf("yes\n");
continue;
}
}
if ((k + d1 + d2) % 3 == 0) {
x2 = (k + d1 + d2) / 3;
x1 = x2 - d1;
x3 = x2 - d2;
int64_t r = n - k - (x2 - x1) - (x2 - x3);
if (x1 >= 0 && x2 >= 0 && x3 >= 0 && r >= 0 && r % 3 == 0) {
printf("yes\n");
continue;
}
}
if ((k - d1 - d2) % 3 == 0) {
x2 = (k - d1 - d2) / 3;
x1 = x2 + d1;
x3 = x2 + d2;
if (x1 > x3) {
int64_t r = n - k - (x1 - x2) - (x1 - x3);
if (x1 >= 0 && x2 >= 0 && x3 >= 0 && r >= 0 && r % 3 == 0) {
printf("yes\n");
continue;
}
} else {
int64_t r = n - k - (x3 - x1) - (x3 - x2);
if (x1 >= 0 && x2 >= 0 && x3 >= 0 && r >= 0 && r % 3 == 0) {
printf("yes\n");
continue;
}
}
}
if ((k + d1 - d2) % 3 == 0) {
x2 = (k + d1 - d2) / 3;
x1 = x2 - d1;
x3 = x2 + d2;
int64_t r = n - k - (x3 - x1) - (x3 - x2);
if (x1 >= 0 && x2 >= 0 && x3 >= 0 && r >= 0 && r % 3 == 0) {
printf("yes\n");
continue;
}
}
printf("no\n");
}
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int f(long long t, long long d1, long long d2);
int f(long long t, long long d1, long long d2) {
long long r = t + d2 - d1;
long long m = r / 3;
if ((!(r % 3)) && ((m >= d2)) && (m >= -d1) && (m >= 0)) return 1;
return 0;
}
int main() {
long long n;
long long k;
int t;
cin >> t;
long long d1, d2;
while (t--) {
cin >> n >> k >> d1 >> d2;
int x = 0;
if (!(n % 3)) {
x |= f(k, d1, d2) && f(n - k, -d1, -d2);
x |= f(k, -d1, d2) && f(n - k, d1, -d2);
x |= f(k, d1, -d2) && f(n - k, -d1, d2);
x |= f(k, -d1, -d2) && f(n - k, d1, d2);
}
cout << (x ? "yes" : "no") << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int test;
cin >> test;
while (test--) {
long long int n, k;
cin >> n >> k;
long long int d1, d2;
cin >> d1 >> d2;
vector<long long int> p1 = {d1, -d1};
vector<long long int> p2 = {d2, -d2};
bool b = 0;
for (auto i : p1) {
for (auto j : p2) {
long long int rem = k - (i + 2 * j);
if (rem < 0) {
continue;
} else if (rem % 3) {
continue;
} else {
rem /= 3;
long long int s1 = rem + i + j;
long long int s2 = rem + j;
long long int s3 = rem;
if (s1 >= 0 && s2 >= 0 && s3 >= 0) {
long long int maxi = max(s1, max(s2, s3));
long long int left = maxi - s1 + maxi - s2 + maxi - s3;
long long int temp = n - k - left;
if (temp < 0) {
continue;
} else if (temp % 3) {
continue;
} else {
b = 1;
break;
}
} else {
continue;
}
}
}
}
if (b == 0) {
cout << "no" << endl;
} else {
cout << "yes" << 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 Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
for (int i = 0; i < m; i++) {
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;
}
long avg = n/3;
long x = 0;
long w1 = 0;
long w2 = 0;
long w3 = 0;
// d1 > 0, d2 > 0
x = k-d1-d2;
if (x%3 == 0) {
x /= 3;
w1 = x + d1;
w2 = x;
w3 = x + d2;
if (w1 >= 0 && w2 >= 0 && w3 >= 0 && w3 <= avg && w2 <= avg && w1 <= avg) {
System.out.println("yes");
continue;
}
}
// d1 > 0, d2 < 0
x = k-d1+d2;
if (x%3 == 0) {
x /= 3;
w1 = x + d1;
w2 = x;
w3 = x - d2;
if (w1 >= 0 && w2 >= 0 && w3 >= 0 && w3 <= avg && w2 <= avg && w1 <= avg) {
System.out.println("yes");
continue;
}
}
// d1 < 0, d2 > 0
x = k+d1-d2;
if (x%3 == 0) {
x /= 3;
w1 = x - d1;
w2 = x;
w3 = x + d2;
if (w1 >= 0 && w2 >= 0 && w3 >= 0 && w3 <= avg && w2 <= avg && w1 <= avg) {
System.out.println("yes");
continue;
}
}
// d1 < 0, d2 < 0
x = k + d1 + d2;
if (x % 3 == 0) {
x /= 3;
w1 = x - d1;
w2 = x;
w3 = x - d2;
if (w1 >= 0 && w2 >= 0 && w3 >= 0 && w3 <= avg && w2 <= avg && w1 <= avg) {
System.out.println("yes");
continue;
}
}
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
|
#!/usr/bin/env python
def read():
n = int(raw_input())
ret = []
for i in range(n):
ret.append(map(int, raw_input().split()))
return ret
def work(vvList):
for (n, k, d1, d2) in vvList:
def judge():
if n % 3 != 0:
print "no"
return
for mul1 in range(-1, 2, 2):
for mul2 in range(-1, 2, 2):
t = k - mul1 * d1 - mul1 * d1 - mul2 * d2
if t % 3 == 0 and t >= 0:
x1 = t / 3
x2 = t / 3 + mul1 * d1
x3 = t / 3 + mul1 * d1 + mul2 * d2
if 0 <= min(x1, x2, x3) and max(x1, x2, x3) <= n / 3:
print "yes"
return
t = k - mul1 * d1 - mul2 * d2
if t % 3 == 0 and t >= 0:
x1 = t / 3 + mul1 * d1
x2 = t / 3
x3 = t / 3 + mul2 * d2
if 0 <= min(x1, x2, x3) and max(x1, x2, x3) <= n / 3:
print "yes"
return
print "no"
judge()
if __name__ == "__main__":
work(read())
|
PYTHON
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
long long n, k, d1, d2;
long long a[10];
int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
cin >> n >> k >> d1 >> d2;
bool flag = 0;
long long a[3];
for (int i = 0; i < 4 && !flag; i++) {
if (i == 1) d1 = -d1;
if (i == 2) d1 = -d1, d2 = -d2;
if (i == 3) d1 = -d1;
long long x = 2 * d1 + d2 + k;
if (x % 3 || x < 0) continue;
x /= 3;
a[0] = x, a[1] = x - d1, a[2] = x - d1 - d2;
if (a[0] < 0 || a[1] < 0 || a[2] < 0) continue;
long long tmp = n;
if (tmp % 3) continue;
tmp /= 3;
if (tmp < a[0] || tmp < a[1] || tmp < a[2]) continue;
flag = 1;
}
if (flag)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class TaskC {
/**
* @param args
*/
public static void main(String[] args) {
TaskC task = new TaskC();
task.read();
task.write();
}
int t;
long n, k, d1, d2;
double w1, w2, w3;
double p;
public void read() {
InputStreamReader bin = new InputStreamReader(System.in);
BufferedReader brin = new BufferedReader(bin);
Scanner in = new Scanner(brin);
PrintWriter out = new PrintWriter(System.out);
t = in.nextInt();
for (int i = 0; i < t; i++) {
n = in.nextLong();
k = in.nextLong();
d1 = in.nextLong();
d2 = in.nextLong();
p = n / 3.;
w1 = (k + d2 - 2 * d1) / 3.;
w3 = -d1 - 2. * w1 + k;
if (3 * w1 >= k - 2 * d1 && 2 * w1 + w3 <= k && check()) {
out.println("yes");
} else {
w1 = (k - d2 - 2 * d1) / 3.;
w3 = -d1 - 2. * w1 + k;
if (3 * w1 <= k - 2 * d1 && 2 * w1 + w3 <= k && check()) {
out.println("yes");
} else {
w1 = (k + d2 + 2 * d1) / 3.;
w3 = d1 - 2. * w1 + k;
if (3 * w1 >= k + 2 * d1 && 2 * w1 + w3 >= k && check()) {
out.println("yes");
} else {
w1 = (k - d2 + 2 * d1) / 3.;
w3 = d1 - 2. * w1 + k;
if (3 * w1 <= k + 2 * d1 && 2 * w1 + w3 >= k && check()) {
out.println("yes");
} else {
out.println("no");
}
}
}
}
}
out.close();
in.close();
}
private boolean check() {
w2 = k - w1 - w3;
return isInt(w1) && isInt(w2) && isInt(w3) && isInt(p)&& w1 >= 0 && w1 <= Math.min(k, p) && w2 >= 0 && w2 <=Math.min(k, p) && w3 >= 0 && w3 <= Math.min(k, p);
}
public boolean isInt(double v) {
if ((v == Math.floor(v)) && !Double.isInfinite(v)) {
return true;
}
return false;
}
public void write() {
}
}
|
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 _, a, b, t, x, y, n, k;
bool V(long long i, long long j) {
long long u = k - i - j, g = -u / 3;
return !(u % 3 || i < g || 0 < g || j < g ||
n - u - max(i, max(j, 0ll)) * 3 < 0);
}
int main() {
for (cin >> _; _--;)
cin >> n >> k >> a >> b,
puts((!(n % 3) && (V(a, -b) || V(-a, -b) || V(a, b) || V(-a, b)))
? "yes"
: "no");
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
bool solve(long long a, long long b, long long c, long long n, long long k) {
long long g1 = k - a - b - c;
if (g1 < 0) g1 = -g1;
if (g1 % 3) return false;
long long Min = min(min(a, b), c);
if (a + b + c - 3 * Min > k) return false;
long long need = max(max(a, b), c);
long long gamesLeft = need * 3 - a - b - c;
if (gamesLeft > n - k) return false;
if (n % 3) return false;
return true;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long n, k, d1, d2;
scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
if (solve(d1, 0, d2, n, k) || solve(d1, 0, -d2, n, k) ||
solve(-d1, 0, d2, n, k) || solve(-d1, 0, -d2, n, k))
puts("yes");
else
puts("no");
}
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, k, d1, d2;
scanf("%lld", &n);
;
scanf("%lld", &k);
;
scanf("%lld", &d1);
;
scanf("%lld", &d2);
;
if (n % 3 != 0) {
printf("no\n");
return;
}
int bj = 0;
if ((k - d1 - d2) >= 0 && (k - d1 - d2) % 3 == 0) {
if (n / 3 >= (k - d1 - d2) / 3 + max(d1, d2)) bj = 1;
}
if ((k - d1 - d2 - d2) >= 0 && (k - d1 - d2 - d2) % 3 == 0) {
if (n / 3 >= (k - d1 - d2 - d2) / 3 + d1 + d2) bj = 1;
}
if ((k - d1 - d1 - d2) >= 0 && (k - d1 - d1 - d2) % 3 == 0) {
if (n / 3 >= (k - d1 - d1 - d2) / 3 + d1 + d2) bj = 1;
}
if (k + d1 + d2 <= n && (k + d1 + d2) % 3 == 0 &&
(k + d1 + d2) / 3 >= max(d1, d2))
bj = 1;
if (bj == 0)
printf("no\n");
else
printf("yes\n");
}
int main() {
int t;
scanf("%d", &t);
;
while (t--) {
solve();
}
}
|
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 do_solve(n, k, d1, d2):
w1 = 2 * d1 + k + d2
if w1 % 3:
return False
w1 /= 3
w2 = w1 - d1
w3 = w2 - d2
ws = [w1, w2, w3]
#print ws
if all(map(lambda x: x >= 0, ws)) \
and all(map(lambda x: x <= n / 3, ws)):
return True
else:
return False
def solve(n, k, d1, d2):
if n % 3:
return False
return do_solve(n, k, d1, d2) \
or do_solve(n, k, -d1, d2) \
or do_solve(n, k, d1, -d2) \
or do_solve(n, k, -d1, -d2)
T = int(raw_input())
for i in xrange(T):
(n, k, d1, d2) = map(int, raw_input().split())
if solve(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
|
from sys import stdin
def solve(n, k, d1, d2):
d = n - k
# a >= b and c >= b
c1 = (k - d1 + 2 * d2) // 3
a1 = d1 - d2 + c1
b1 = k - a1 - c1
# a >= b and c < b
c2 = (k - d1 - 2 * d2) // 3
a2 = d1 + d2 + c2
b2 = k - a2 - c2
# a < b and c < b
c3 = (k + d1 - 2 * d2) // 3
a3 = c3 + d2 - d1
b3 = k - a3 - c3
# a < b and c >= b
c4 = (k + d1 + 2 * d2) // 3
a4 = c4 - d1 - d2
b4 = k - a4 - c4
if c1 * 3 == 2 * d2 - d1 + k and a1 >= 0 and b1 >= 0 and c1 >= 0:
_max = max(a1, b1, c1)
diff = sum(_max - e for e in [a1, b1, c1])
if d >= diff and (d - diff) % 3 == 0:
return True
if c2 * 3 == k - d1 - 2 * d2 and a2 >= 0 and b2 >= 0 and c2 >= 0:
_max = max(a2, b2, c2)
diff = sum(_max - e for e in [a2, b2, c2])
if d >= diff and (d - diff) % 3 == 0:
return True
if c3 * 3 == k + d1 - 2 * d2 and a3 >= 0 and b3 >= 0 and c3 >= 0:
_max = max(a3, b3, c3)
diff = sum(_max - e for e in [a3, b3, c3])
if d >= diff and (d - diff) % 3 == 0:
return True
if c4 * 3 == k + d1 + 2 * d2 and a4 >= 0 and b4 >= 0 and c4 >= 0:
_max = max(a4, b4, c4)
diff = sum(_max - e for e in [a4, b4, c4])
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):
ans = 'yes' if solve(*map(int, test[i].split())) else 'no'
output.append(ans)
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
|
#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) {
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
|
//package round258;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class C2 {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
for(int T = ni();T >= 1;T--){
long n = nl(), K = nl();
long d1 = nl(), d2 = nl();
if(n % 3 != 0){
out.println("no");
continue;
}
if(check(n, K, d1, d1+d2)
|| check(n, K, d1, d1-d2)
|| check(n, K, -d1, -d1+d2)
|| check(n, K, -d1, -d1-d2)
){
out.println("yes");
}else{
out.println("no");
}
}
}
// 0<=d1<=d2
boolean check(long n, long K, long d1, long d2)
{
// a+(a-d1)+(a-d2)=k
// k+d1+d2
long a = d1+d2+K;
if(a % 3 != 0 || a/3-d1 < 0 || a/3 < 0 || a/3-d2 < 0){
return false;
}
long[] q = new long[]{0, d1, d2};
Arrays.sort(q);
d1 = q[1]-q[0];
d2 = q[2]-q[0];
long rem = n-K;
rem -= d1;
rem -= d2;
if(rem < 0){
return false;
}
return rem % 3 == 0;
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new C2().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.util.InputMismatchException;
import java.math.BigInteger;
import java.io.*;
/**
* Generated by Contest helper plug-in
* Actual solution is at the bottom
*/
public class Main {
public static void main(String[] args) {
InputReader in = new StreamInputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
run(in, out);
}
public static void run(InputReader in, PrintWriter out) {
Solver solver = new Task();
solver.solve(1, in, out);
Exit.exit(in, out);
}
}
abstract class InputReader {
private boolean finished = false;
public abstract int read();
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public void setFinished(boolean finished) {
this.finished = finished;
}
public abstract void close();
}
class StreamInputReader extends InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public StreamInputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public void close() {
try {
stream.close();
} catch (IOException ignored) {
}
}
}
class Exit {
private Exit() {
}
public static void exit(InputReader in, PrintWriter out) {
in.setFinished(true);
in.close();
out.close();
}
}
interface Solver {
public void solve(int testNumber, InputReader in, PrintWriter out);
}
class Task implements Solver {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long num = new BigInteger(in.readString()).longValue();
long[][] nums = new long[(int)num][4];
for (int i = 0; i<num; i++) {
nums[i][0] = new BigInteger(in.readString()).longValue();
nums[i][1] = new BigInteger(in.readString()).longValue();
nums[i][2] = new BigInteger(in.readString()).longValue();
nums[i][3] = new BigInteger(in.readString()).longValue();
}
for (int i = 0; i<num; i++) {
long n = nums[i][0];
if (n%3!=0)
out.println("no");
else {
long k = nums[i][1];
long d1 = nums[i][2];
long d2 = nums[i][3];
long max = Math.max(d1, d2);
long min = Math.min(d1, d2);
if (test(n,k,max+min,min) || test(n,k,max+min,max) || test(n,k,max,min) || test(n,k,max,max-min))
out.println("yes");
else
out.println("no");
}
}
}
private boolean test(long n, long k, long max, long mid) {
long req = max+mid;
if (k<req)
return false;
long modk = k-max-mid;
if (modk%3!=0)
return false;
if (max+modk/3>n/3)
return false;
return true;
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
long long n, k, d1, d2;
for (int i = 0; i < t; i++) {
cin >> n >> k >> d1 >> d2;
if ((n % 3 > 0) || (d1 > k) || (d2 > k)) {
cout << "no" << endl;
continue;
}
if ((((n - k - d1 - d2 - d2) % 3) == 0) && (n - k - d1 - d2 - d2 >= 0) &&
((k - d1 - d1 - d2) % 3 == 0) && (k - d1 - d1 - d2 >= 0)) {
cout << "yes" << endl;
continue;
}
if (((n - k - d1 - d2) % 3 == 0) && (n - k - d1 - d2 >= 0) &&
((k - d1 - d1 + d2) % 3 == 0) && (k - d1 - d1 + d2 >= 0) &&
((k - d1 - d1 + d2) / 3 >= d2 - d1)) {
cout << "yes" << endl;
continue;
}
if ((d1 >= d2) && (((n - k - d1 - d1 + d2) % 3) == 0) &&
(n - k - d1 - d1 + d2 >= 0) && ((k - d1 - d2) % 3 == 0) &&
(k - d1 - d2 >= 0)) {
cout << "yes" << endl;
continue;
}
if ((d2 > d1) && (((n - k - d2 - d2 + d1) % 3) == 0) &&
(n - k - d2 - d2 + d1 >= 0) && ((k - d2 - d1) % 3 == 0) &&
(k - d2 - d1 >= 0)) {
cout << "yes" << endl;
continue;
}
if ((((n - k - d1 - d1 - d2) % 3) == 0) && ((k - d1 - d2 - d2) % 3 == 0) &&
(k - d1 - d2 - d2 >= 0) && (n - k - d1 - d1 - d2 >= 0)) {
cout << "yes" << endl;
continue;
}
cout << "no" << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
inline int init() {
int now = 0, ju = 1;
char c;
bool flag = false;
while (1) {
c = getchar();
if (c == '-')
ju = -1;
else if (c >= '0' && c <= '9') {
now = now * 10 + c - '0';
flag = true;
} else if (flag)
return now * ju;
}
}
inline long long llinit() {
long long now = 0, ju = 1;
char c;
bool flag = false;
while (1) {
c = getchar();
if (c == '-')
ju = -1;
else if (c >= '0' && c <= '9') {
now = now * 10 + c - '0';
flag = true;
} else if (flag)
return now * ju;
}
}
long long n, k, x, y, t;
long long a, b, c;
long long minx;
int main() {
bool flag;
t = llinit();
long long tt;
while (t--) {
minx = 0x7f7f7f7f;
flag = false;
n = llinit();
k = llinit();
x = llinit();
y = llinit();
c = (k - x - (2 * y)) / 3;
b = c + y;
a = b + x;
tt = n - k - 2 * x - y;
if (a >= 0 && b >= 0 && c >= 0 && a + b + c == k && 2 * x + y <= n - k &&
tt % 3 == 0) {
flag = true;
}
b = (k - x - y) / 3;
c = b + y;
a = b + x;
minx = min(x, y);
tt = n - k - minx - 2 * abs(x - y);
if (a >= 0 && b >= 0 && c >= 0 && a + b + c == k &&
minx + 2 * abs(x - y) <= n - k && tt % 3 == 0) {
flag = true;
}
b = (k + x + y) / 3;
a = b - x;
c = b - y;
tt = n - k - x - y;
if (a >= 0 && b >= 0 && c >= 0 && a + b + c == k && x + y <= n - k &&
tt % 3 == 0) {
flag = true;
}
a = (k - 2 * x - y) / 3;
b = a + x;
c = b + y;
tt = n - k - x - 2 * y;
if (a >= 0 && b >= 0 && c >= 0 && a + b + c == k && x + 2 * y <= n - k &&
tt % 3 == 0) {
flag = true;
}
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
|
for _ in xrange(int(raw_input())):
n,k,d1,d2 = [int(c) for c in raw_input().split()]
M,m = max(d1,d2),min(d1,d2)
if n % 3:
print 'no'
else:
r = [(d1+d2,2*M-m),(2*M-m,d1+d2),(2*d1+d2,d1+2*d2),(d1+2*d2,2*d1+d2)]
if any(a <= k and (a-k)%3 == 0 and b <= n-k and (b-n+k)%3 == 0 for a,b in r):
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
|
q = input()
while q > 0:
n, k, d1, d2 = map(int, raw_input().split())
if d1 > d2:
d1, d2 = d2, d1
if k - 2 * d1 - d2 >= 0 and (k - 2 * d1 - d2) % 3 == 0 and \
(n - k) - d1 - 2 * d2 >= 0 and ((n - k) - d1 - 2 * d2) % 3 == 0:
print 'yes'
elif k - 2 * d2 - d1 >= 0 and (k - 2 * d2 - d1) % 3 == 0 and \
(n - k) - d2 - 2 * d1 >= 0 and ((n - k) - d2 - 2 * d1) % 3 == 0:
print 'yes'
elif k - 2 * d2 + d1 >= 0 and (k - 2 * d2 + d1) % 3 == 0 and \
(n - k) - d2 - d1 >= 0 and ((n - k) - d2 - d1) % 3 == 0:
print 'yes'
elif k - d1 - d2 >= 0 and (k - d1 - d2) % 3 == 0 and \
(n - k) - 2 * d2 + d1 >= 0 and ((n - k) - 2 * d2 + d1) % 3 == 0:
print 'yes'
else:
print 'no'
q -= 1
|
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
|
read = lambda: map(int, input().split())
f = lambda x, y, a, b: x > a or y > b or (a - x) % 3 or (b - y) % 3
g = lambda x, y, a, b: f(x, y, a, b) and f(x, y, b, a)
t = int(input())
for i in range(t):
n, k, d1, d2 = read()
r = n - k
d = d1 + d2
p = 2 * d2 - d1 if d2 > d1 else 2 * d1 - d2
print('no' if g(d, p, k, r) and g(d + d1, d + d2, k, r) else 'yes')
|
PYTHON3
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
long long n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
if (n % 3) {
cout << "no\n";
continue;
}
int flag = 0;
long long x1, x2, x3;
if (2 * d1 + d2 + k <= n && (2 * d1 + d2 + k) % 3 == 0) {
x1 = (2 * d1 + d2 + k) / 3;
x3 = x1 - (d1 + d2);
x2 = x1 - d1;
if (x1 >= 0 && x2 >= 0 && x3 >= 0 && x1 <= n / 3 && x2 <= n / 3 &&
x3 <= n / 3)
flag = 1;
}
if (2 * d1 - d2 + k >= 0 && 2 * d1 - d2 + k <= n &&
(2 * d1 - d2 + k) % 3 == 0) {
x1 = (2 * d1 - d2 + k) / 3;
x3 = x1 - (d1 - d2);
x2 = x1 - d1;
if (x1 >= 0 && x2 >= 0 && x3 >= 0 && x1 <= n / 3 && x2 <= n / 3 &&
x3 <= n / 3)
flag = 1;
}
if (-2 * d1 + d2 + k >= 0 && -2 * d1 + d2 + k <= n &&
(-2 * d1 + d2 + k) % 3 == 0) {
x1 = (-2 * d1 + d2 + k) / 3;
x3 = x1 - (-1 * d1 + d2);
x2 = x1 + d1;
if (x1 >= 0 && x2 >= 0 && x3 >= 0 && x1 <= n / 3 && x2 <= n / 3 &&
x3 <= n / 3)
flag = 1;
}
if (-2 * d1 - d2 + k >= 0 && -2 * d1 - d2 + k <= n &&
(-2 * d1 - d2 + k) % 3 == 0) {
x1 = (-2 * d1 - d2 + k) / 3;
x3 = x1 - (-1 * d1 - d2);
x2 = x1 + d1;
if (x1 >= 0 && x2 >= 0 && x3 >= 0 && x1 <= n / 3 && x2 <= n / 3 &&
x3 <= n / 3)
flag = 1;
}
if (flag)
cout << "yes\n";
else
cout << "no\n";
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int cases(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) {
return 1;
}
}
}
}
}
return 0;
}
int main() {
int t;
cin >> t;
while (t--) {
long long n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
int ans = cases(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.BigInteger;
import java.util.*;
import java.util.Map.Entry;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) throws Exception {
solve();
}
public static void solve() throws IOException {
FastScanner fscan = new FastScanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = fscan.nextInt();
nextCase:while(t-- > 0){
final long n = fscan.nextLong(),
k = fscan.nextLong(),
d1 = fscan.nextLong(),
d2 = fscan.nextLong();
if(n%3 != 0) {
bw.append("no\n");
continue nextCase;
}
for (int s1 = -1; s1 <= 1; s1++) {
for (int s2 = -1; s2 <= 1; s2++) {
if(s1 == 0 || s2 == 0) {
continue;
}
long td1 = s1 * d1;
long td2 = s2 * d2;
long x1 = (2*td1 + k + td2) / 3;
long x2 = x1 - td1;
long x3 = x2 - td2;
if((2*td1 + k + td2)%3 != 0)
continue;
if(x1 >= 0 && x1 <= n/3 && x2>=0 && x2 <= n/3 && x3>= 0 && x3 <= n/3){
if ((s1 > 0 && x1 >= x2 && s2 > 0 && x2 >= x3)||
(s1 > 0 && x1 >= x2 && s2 < 0 && x2 <= x3)||
(s1 < 0 && x1 <= x2 && s2 > 0 && x2 >= x3)||
(s1 < 0 && x1 <= x2 && s2 < 0 && x2 <= x3)
){
bw.append("yes\n");
continue nextCase;
}
}
}
}
bw.append("no\n");
}
bw.close();
}
}
class IO {
public static void readIntArray(int a[], int N, FastScanner fscan)
throws IOException {
readIntArray(a, N, 0, fscan);
}
public static void readIntArray(int a[], int N, int start, FastScanner fscan)
throws IOException {
for (int i = 0; i < N; i++) {
a[start + i] = fscan.nextInt();
}
}
public static void readArray(long a[], int N, FastScanner fscan)
throws IOException {
readArray(a, N, 0, fscan);
}
public static void readArray(long a[], int N, int start, FastScanner fscan)
throws IOException {
for (int i = 0; i < N; i++) {
a[start + i] = fscan.nextInt();
}
}
}
class FastScanner {
InputStream is;
byte buff[] = new byte[1024];
int currentChar = -1;
int buffChars = 0;
public FastScanner(InputStream inputStream) {
is = inputStream;
}
public boolean hasNext() throws IOException {
return currentChar == -1 || buffChars > 0;
}
public char nextChar() throws IOException {
// if we already have that next char read, just return else input
if (currentChar == -1 || currentChar >= buffChars) {
currentChar = 0;
buffChars = is.read(buff);
}
if (buffChars <= 0) {
throw new RuntimeException("No char found...");
}
int ch;
while (isSpace(ch = nextCharAsInt()))
;
return (char) ch;
}
public int nextCharAsInt() throws IOException {
// if we already have that next char read, just return else input
if (currentChar == -1 || currentChar >= buffChars) {
currentChar = 0;
buffChars = is.read(buff);
}
if (buffChars <= 0) {
return -1;
}
return (char) buff[currentChar++];
}
public String nextLine() throws IOException {
StringBuilder bldr = new StringBuilder();
int ch;
while (isLineEnd(ch = nextCharAsInt()))
// ignore empty lines ???
;
do {
bldr.append((char) ch);
} while (!isLineEnd(ch = nextCharAsInt()));
return bldr.toString();
}
public String nextString() throws IOException {
StringBuilder bldr = new StringBuilder();
int ch;
while (isSpace(ch = nextCharAsInt()))
;
do {
bldr.append((char) ch);
} while (!isSpace(ch = nextCharAsInt()));
return bldr.toString();
}
public int nextInt() throws IOException {
// considering ASCII files--> 8 bit chars, unicode files has 16 bit
// chars (byte1 then byte2)
int result = 0;
int sign = 1;
int ch;
while (isSpace(ch = nextCharAsInt()))
;
if (ch == '-') {
sign = -1;
ch = nextCharAsInt();
}
do {
if (ch < '0' || ch > '9')
throw new NumberFormatException("Found '" + ch
+ "' while parsing for int.");
result *= 10;
result += ch - '0';
} while (!isSpace(ch = nextCharAsInt()));
return sign * result;
}
public long nextLong() throws IOException {
// considering ASCII files--> 8 bit chars, unicode files has 16 bit
// chars (byte1 then byte2)
long result = 0;
int sign = 1;
int ch;
while (isSpace(ch = nextCharAsInt()))
;
if (ch == '-') {
sign = -1;
ch = nextCharAsInt();
}
do {
if (ch < '0' || ch > '9')
throw new NumberFormatException("Found '" + ch
+ "' while parsing for int.");
result *= 10;
result += ch - '0';
} while (!isSpace(ch = nextCharAsInt()));
return sign * result;
}
private boolean isLineEnd(int ch) {
return ch == '\n' || ch == '\r' || ch == -1;
}
private boolean isSpace(int ch) {
return ch == '\n' || ch == ' ' || ch == '\t' || ch == '\r' || ch == -1;
}
public void close() throws IOException {
is.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
|
for t in range(int(input())):
n,k,d1,d2=map(int,input().split())
#d1,d2=max(d11,d22),min(d11,d22)
flag=False
x=(k+(2*d1)+d2)//3
a,b,c=x,x-d1,x-d1-d2
n1=n//3
flag1=True
if n%3!=0 or (abs(n1-a)+abs(n1-b)+abs(n1-c))!=(n-k) or a+b+c!=k or a>n1 or b>n1 or c>n1 or a<0 or b<0 or c<0:
flag1=False
flag=flag or flag1
x=(k+(2*d1)-d2)//3
a,b,c=x,x-d1,x-d1+d2
n1=n//3
flag1=True
if n%3!=0 or (abs(n1-a)+abs(n1-b)+abs(n1-c))!=(n-k) or a+b+c!=k or a>n1 or b>n1 or c>n1 or a<0 or b<0 or c<0:
flag1=False
flag=flag or flag1
x=(k-(2*d1)+d2)//3
a,b,c=x,x+d1,x+d1-d2
n1=n//3
flag1=True
if n%3!=0 or (abs(n1-a)+abs(n1-b)+abs(n1-c))!=(n-k) or a+b+c!=k or a>n1 or b>n1 or c>n1 or a<0 or b<0 or c<0:
flag1=False
flag=flag or flag1
x=(k-(2*d1)-d2)//3
a,b,c=x,x+d1,x+d1+d2
n1=n//3
flag1=True
if n%3!=0 or (abs(n1-a)+abs(n1-b)+abs(n1-c))!=(n-k) or a+b+c!=k or a>n1 or b>n1 or c>n1 or a<0 or b<0 or c<0:
flag1=False
flag=flag or flag1
if flag:
print('yes')
else:
print('no')
|
PYTHON3
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
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;
}
if(n%3 == 0 && a <= n/3 && b <= n/3 && c <= n/3) {
found = true;
}
}
}
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
|
#include <bits/stdc++.h>
using namespace std;
bool play(long long x, long long y, long long z, long long rem) {
long long _1 = abs(x - y);
rem -= _1;
x = max(x, y);
y = max(x, y);
long long _2 = abs(x - z);
if (z > x)
rem -= (2 * _2);
else
rem -= _2;
x = max(x, z);
y = max(x, z);
z = max(x, z);
if (rem < 0)
return false;
else if (rem % 3 == 0)
return true;
return false;
}
int main() {
long long t;
cin >> t;
while (t--) {
long long n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
long long a[10], b[10], c[10];
a[0] = 0, b[0] = d1, c[0] = b[0] + d2;
a[1] = 0, b[1] = d1, c[1] = b[1] - d2;
a[2] = d1, b[2] = 0, c[2] = d2;
a[3] = d1, b[3] = 0, c[3] = ((-1) * d2);
a[4] = 0, b[4] = ((-1) * d1), c[4] = b[4] - d2;
a[5] = 0, b[5] = ((-1) * d1), c[5] = b[5] + d2;
a[6] = d1, b[6] = d1 + d1, c[6] = b[6] - d2;
a[7] = d1, b[7] = d1 + d1, c[7] = b[7] + d2;
if (c[6] < 0) {
long long x = abs(c[6] - 0);
a[6] += x;
b[6] += x;
c[6] += x;
}
if (b[4] < 0) {
long long x = abs(c[4] - 0);
a[4] += x;
b[4] += x;
c[4] += x;
}
if (b[5] < 0) {
long long x = abs(b[5] - 0);
a[5] += x;
b[5] += x;
c[5] += x;
}
bool valid[12];
valid[0] = false;
valid[1] = false;
valid[2] = false;
valid[3] = false;
valid[4] = false;
valid[5] = false;
valid[6] = false;
valid[7] = false;
if (c[1] < 0) {
long long x = abs(c[1] - 0);
a[1] += x;
b[1] += x;
c[1] += x;
}
if (c[3] < 0) {
long long x = abs(c[3] - 0);
a[3] += x;
b[3] += x;
c[3] += x;
}
if (a[0] >= 0 and b[0] >= 0 and c[0] >= 0 and a[0] + b[0] + c[0] <= k and
(k - a[0] - b[0] - c[0]) % 3 == 0)
valid[0] = true;
if (a[1] >= 0 and b[1] >= 0 and c[1] >= 0 and a[1] + b[1] + c[1] <= k and
(k - a[1] - b[1] - c[1]) % 3 == 0)
valid[1] = true;
if (a[2] >= 0 and b[2] >= 0 and c[2] >= 0 and a[2] + b[2] + c[2] <= k and
(k - a[2] - b[2] - c[2]) % 3 == 0)
valid[2] = true;
if (a[3] >= 0 and b[3] >= 0 and c[3] >= 0 and a[3] + b[3] + c[3] <= k and
(k - a[3] - b[3] - c[3]) % 3 == 0)
valid[3] = true;
if (a[4] >= 0 and b[4] >= 0 and c[4] >= 0 and a[4] + b[4] + c[4] <= k and
(k - a[4] - b[4] - c[4]) % 3 == 0)
valid[4] = true;
if (a[5] >= 0 and b[5] >= 0 and c[5] >= 0 and a[5] + b[5] + c[5] <= k and
(k - a[5] - b[5] - c[5]) % 3 == 0)
valid[5] = true;
if (a[6] >= 0 and b[6] >= 0 and c[6] >= 0 and a[6] + b[6] + c[6] <= k and
(k - a[6] - b[6] - c[6]) % 3 == 0)
valid[6] = true;
if (a[7] >= 0 and b[7] >= 0 and c[7] >= 0 and a[7] + b[7] + c[7] <= k and
(k - a[7] - b[7] - c[7]) % 3 == 0)
valid[7] = true;
if ((valid[0] and play(a[0], b[0], c[0], n - k)) |
(valid[1] and play(a[1], b[1], c[1], n - k)) |
(valid[2] and play(a[2], b[2], c[2], n - k)) |
(valid[3] and play(a[3], b[3], c[3], n - k)))
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.util. Scanner;
public class Outcome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i = 0; i < t; i ++) {
long n = sc.nextLong();
long k = sc.nextLong();
long d1 = sc.nextLong();
long d2 = sc.nextLong();
System.out.println(solve(n, k, d1, d2));
}
}
public static long det2(long a, long b, long c, long d) {
return (a * d) - (b * c);
}
public static long det(long a, long b, long c, long d, long e, long f, long g, long h, long i) {
return (a * det2(e, f, h, i) -
b * det2(d, f, g, i) +
c * det2(d, e, g, h));
}
public static boolean can_solve(long a, long b, long c, long j, long d, long e, long f, long k, long g, long h, long i, long l, long remaining) {
long den = det(a, b, c, d, e, f, g, h, i);
if(den == 0)
return false;
long x = det(j, b, c, k, e, f, l, h, i);
long y = det(a, j, c, d, k, f, g, l, i);
long z = det(a, b, j, d, e, k, g, h, l);
if((x % den != 0) || (y % den != 0) || (z % den != 0)) {
return false;
}
x = (x / den);
y = (y / den);
z = (z / den);
if (x < 0 || y < 0 || z < 0)
return false;
long total = remaining + (x + y + z);
if ((total % 3) != 0) {
return false;
}
long final_score_for_all = total / 3;
return final_score_for_all >= 0 && final_score_for_all >= x && final_score_for_all >= y && final_score_for_all >= z;
}
public static String solve(long n, long k, long d1, long d2) {
if (can_solve(1, 1, 1, k, 1, -1, 0, d1, 0, 1, -1, d2, n - k) ||
can_solve(1, 1, 1, k, 1, -1, 0, -d1, 0, 1, -1, d2, n - k) ||
can_solve(1, 1, 1, k, 1, -1, 0, d1, 0, 1, -1, -d2, n - k) ||
can_solve(1, 1, 1, k, 1, -1, 0, -d1, 0, 1, -1, -d2, n - k)){
return "yes";
}
return "no";
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import com.sun.prism.shader.Solid_TextureYV12_AlphaTest_Loader;
import java.util.*;
import java.io.*;
public class Main {
public static int solve(long n, long k, long d1, long d2) {
if (n % 3 != 0) {
return 0;
}
for (int ss = -1; ss <= 1; ss++) {
for (int ss2 = -1; ss2 <= 1; ss2++) {
if (ss == 0 || ss2 == 0) {
continue;
}
long D1 = d1 * ss;
long D2 = d2 * ss2;
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) {
assert(Math.abs(x1 - x2) == d1);
assert(Math.abs(x2 - x3) == d2);
return 1;
}
}
}
}
}
return 0;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner S = new Scanner(System.in);
int t=S.nextInt();
while(t>0){
--t;
long n=S.nextLong(),k=S.nextLong(),d1=S.nextLong(),d2=S.nextLong();
System.out.println(solve(n,k,d1,d2) == 1 ? "yes" : "no");
}
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
long long n, k, d1, d2;
bool f(long long a, long long b) {
long long c = k - a - b * 2;
if (c < 0 || c % 3) return 0;
return n / 3 >= a + b + c / 3;
}
int main() {
int t;
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> n >> k >> d1 >> d2;
if (n % 3) {
cout << "no\n";
continue;
}
if (f(d1, d2) || f(abs(d1 - d2), min(d1, d2)) || f(d2, d1) ||
f(min(d1, d2), abs(d1 - d2)))
cout << "yes\n";
else
cout << "no\n";
}
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
long long int check(long long int n, long long int k, long long int d1,
long long int d2) {
long long int b, c, a, w;
w = k - d1 + d2;
if (w % 3 == 0) {
w = w / 3;
b = n / 3;
b -= w;
a = b - d1;
c = b + d2;
n = n - k;
if (a >= 0 && b >= 0 && c >= 0 && w + d1 <= k && w - d2 <= k && w <= k &&
a <= n && b <= n && c <= n && w + d1 >= 0 && w - d2 >= 0 && w >= 0) {
return 1;
} else
return 0;
} else
return 0;
}
int main() {
ios::sync_with_stdio(false);
long long int t;
cin >> t;
while (t--) {
long long int n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
if (n % 3 != 0)
cout << "no\n";
else {
if (check(n, k, d1, d2) || check(n, k, -d1, d2) || check(n, k, d1, -d2) ||
check(n, k, -d1, -d2))
cout << "yes\n";
else
cout << "no\n";
}
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
new TaskC().solve(in, out);
out.close();
}
}
class TaskC {
public void solve(InputReader in,PrintWriter out) {
int t=in.nextInt();
for(int cas=0;cas<t;cas++) {
long n=in.nextLong();
long k=in.nextLong();
long d1=in.nextLong();
long d2=in.nextLong();
boolean flag=false;
long u=k-2*d1-d2;
if(u>=0 && u%3==0){
u=u/3;
if(good(u,u+d1,u+d1+d2,n-k)){
flag=true;
}
}
u=k-2*d1+d2;
if(u>=0 && u%3==0){
u=u/3;
if(good(u,u+d1,u+d1-d2,n-k)){
flag=true;
}
}
u=k+2*d1-d2;
if(u>=0 && u%3==0){
u=u/3;
if(good(u,u-d1,u-d1+d2,n-k)){
flag=true;
}
}
u=k+2*d1+d2;
if(u>=0 && u%3==0){
u=u/3;
if(good(u,u-d1,u-d1-d2,n-k)){
flag=true;
}
}
if(flag){
out.println("yes");
}
else{
out.println("no");
}
}
}
private boolean good(long u, long l, long m, long n) {
if(u<0 || l<0 || m<0)
return false;
long mx=Math.max(u, l);
mx=Math.max(mx, m);
long tot=mx-u+mx-l+mx-m;
if(n>=tot && (n-tot)%3==0)
return true;
return false;
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class C_258 {
public static void main(String[] args) throws IOException {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(input);
int t = Integer.parseInt(br.readLine());
for(int i = 0; i < t; i++){
String[] tokens = br.readLine().split(" ");
long n = Long.parseLong(tokens[0]);
long k = Long.parseLong(tokens[1]);
long d1 = Long.parseLong(tokens[2]);
long d2 = Long.parseLong(tokens[3]);
long w1 = 0L;
long w2 = 0L;
long w3 = 0L;
long d3 = d1;
long d4 = d2;
boolean flag = false;
if( ! flag && (k - d1 + d2 >= 0) && (k - d1 + d2) % 3 ==0 && trueC(k,d1,d2)){
long match = n/3;
w2 = (k - d1 + d2)/3;
w1 = d1 + w2;
w3 = w2 - d2;
long need = Math.abs(match - w1) + Math.abs(match - w2) + Math.abs(match - w3);
if(need + k == n){
flag = true;
}
}
if(!flag && (k + d1 + d2 >= 0) && (k + d1 + d2) % 3 ==0 && trueC(k,-1*d1,d2)){
d1 = -1*d1;
long match = n/3;
w2 = (k - d1 + d2)/3;
w1 = d1 + w2;
w3 = w2 - d2;
long need = Math.abs(match - w1) + Math.abs(match - w2) + Math.abs(match - w3);
if(need + k == n){
flag = true;
}
}
d1 = d3;
d2 = d4;
if(!flag && (k - d1 - d2 >= 0) && (k - d1 - d2) % 3 ==0 && trueC(k,d1,-1*d2)){
d2 = -1*d2;
long match = n/3;
w2 = (k - d1 + d2)/3;
w1 = d1 + w2;
w3 = w2 - d2;
long need = Math.abs(match - w1) + Math.abs(match - w2) + Math.abs(match - w3);
if(need + k == n){
flag = true;
}
}
d2 = d4;
d1 = d3;
if(!flag && (k + d1 - d2 >= 0) && (k + d1 - d2) % 3 ==0 && trueC(k,-1*d1,-1*d2)){
d2 = -1*d2;
d1 = -1*d1;
long match = n/3;
w2 = (k - d1 + d2)/3;
w1 = d1 + w2;
w3 = w2 - d2;
long need = Math.abs(match - w1) + Math.abs(match - w2) + Math.abs(match - w3);
if(need + k == n){
flag = true;
}
}
if(n % 3 != 0)
flag = false;
if(flag){
System.out.println("yes");
}
else{
System.out.println("no");
}
}
}
public static boolean trueC(long k, long d1, long d2){
long w2 = (k - d1 + d2)/3;
long w1 = d1 + w2;
long w3 = w2 - d2;
if(w2 < 0)
return false;
if(w1 < 0)
return false;
if(w3 < 0)
return false;
return w1 + w2 + w3 == k;
}
}
|
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
|
n=int(raw_input())
for x in range(n):
num = map(int, raw_input().split())
n=num[0]
k=num[1]
d1=num[2]
d2=num[3]
if n%3!=0:
print 'no'
continue
a=(k+2*d1+d2)/3
b=(k-d1+d2)/3
c=(k-d1-2*d2)/3
if ((n/3)-a)>=0 and ((n/3)-b)>=0 and ((n/3)-c)>=0 and a>=0 and b>=0 and c>=0 and (k+2*d1+d2)%3==0 and (k-d1+d2)%3==0 and (k-d1-2*d2)%3==0:
print 'yes'
continue
a=(k+2*d1-d2)/3
b=(k-d1-d2)/3
c=(k-d1+2*d2)/3
if ((n/3)-a)>=0 and ((n/3)-b)>=0 and ((n/3)-c)>=0 and a>=0 and b>=0 and c>=0 and (k+2*d1-d2)%3==0 and (k-d1-d2)%3==0 and (k-d1+2*d2)%3==0:
print 'yes'
continue
a=(k-2*d1+d2)/3
b=(k+d1+d2)/3
c=(k+d1-2*d2)/3
if ((n/3)-a)>=0 and ((n/3)-b)>=0 and ((n/3)-c)>=0 and a>=0 and b>=0 and c>=0 and (k-2*d1+d2)%3==0 and (k+d1+d2)%3==0 and (k+d1-2*d2)%3==0:
print 'yes'
continue
a=(k-2*d1-d2)/3
b=(k+d1-d2)/3
c=(k+d1+2*d2)/3
if ((n/3)-a)>=0 and ((n/3)-b)>=0 and ((n/3)-c)>=0 and a>=0 and b>=0 and c>=0 and (k-2*d1-d2)%3==0 and (k+d1-d2)%3==0 and (k+d1+2*d2)%3==0:
print 'yes'
continue
print 'no'
|
PYTHON
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int t = Integer.parseInt(br.readLine()); t-- > 0; ) {
StringTokenizer st = new StringTokenizer(br.readLine());
long n = Long.parseLong(st.nextToken()), k = Long.parseLong(st.nextToken()), a = Long.parseLong(st.nextToken()), b = Long.parseLong(st.nextToken()), max;
long x = (k + (2 * a) + b) / 3, w1 = x, w2 = w1 - a, w3 = w2 - b, req = 2 * w1 - w2 - w3;
if ((w1 >= 0 && w2 >= 0 && w3 >= 0) && (w1 + w2 + w3 == k) && (n - k - req) >= 0 && (n - k - req) % 3 == 0) {
System.out.println("yes");
continue;
}
x = (k + (2 * a) - b) / 3; w1 = x; w2 = w1 - a; w3 = w2 + b; max = Math.max(Math.max(w1, w2), w3); req = Math.abs(max - w1) + Math.abs(max - w2) + Math.abs(max - w3);
if ((w1 >= 0 && w2 >= 0 && w3 >= 0) && (w1 + w2 + w3 == k) && (n - k - req) >= 0 && (n - k - req) % 3 == 0) {
System.out.println("yes");
continue;
}
x = (k - (2 * a) + b) / 3; w1 = x; w2 = w1 + a; w3 = w2 - b; max = Math.max(Math.max(w1, w2), w3); req = Math.abs(max - w1) + Math.abs(max - w2) + Math.abs(max - w3);
if ((w1 >= 0 && w2 >= 0 && w3 >= 0) && (w1 + w2 + w3 == k) && (n - k - req) >= 0 && (n - k - req) % 3 == 0) {
System.out.println("yes");
continue;
}
x = (k - (2 * a) - b) / 3; w1 = x; w2 = w1 + a; w3 = w2 + b; req = 2 * w3 - w1 - w2;
if ((w1 >= 0 && w2 >= 0 && w3 >= 0) && (w1 + w2 + w3 == k) && (n - k - req) >= 0 && (n - k - req) % 3 == 0) {
System.out.println("yes");
continue;
}
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 fun1(long long n, long long k, long long d1, long long d2) {
long long a = (2ll * d1 + d2 + k) / 3ll;
long long b = a - d1;
long long c = b - d2;
if (a + b + c != k) return 0;
n -= k;
n -= (a - b);
n -= (a - c);
if (n < 0) return 0;
if (a < 0 || b < 0 || c < 0) return 0;
if (n % 3ll == 0ll) return 1;
return 0;
}
int fun2(long long n, long long k, long long d1, long long d2) {
long long a = (2ll * d1 - d2 + k) / 3ll;
long long b = a - d1;
long long c = b + d2;
long long mx = max(a, max(b, c));
if (a < 0 || b < 0 || c < 0) return 0;
if (a + b + c != k) return 0;
n -= k;
n -= (mx - a);
n -= (mx - b);
n -= (mx - c);
if (n < 0) return 0;
if (n % 3ll == 0ll) return 1;
return 0;
}
int fun3(long long n, long long k, long long d1, long long d2) {
long long b = (d1 + d2 + k) / 3ll;
long long a = b - d1;
long long c = b - d2;
long long mx = max(a, max(b, c));
if (a < 0 || b < 0 || c < 0) return 0;
if (a + b + c != k) return 0;
n -= k;
n -= (mx - a);
n -= (mx - b);
n -= (mx - c);
if (n < 0) return 0;
if (n % 3ll == 0ll) return 1;
return 0;
}
int fun4(long long n, long long k, long long d1, long long d2) {
long long b = (d1 - d2 + k) / 3ll;
long long a = b - d1;
long long c = b + d2;
long long mx = max(a, max(b, c));
if (a < 0 || b < 0 || c < 0) return 0;
if (a + b + c != k) return 0;
n -= k;
n -= (mx - a);
n -= (mx - b);
n -= (mx - c);
if (n < 0) return 0;
if (n % 3ll == 0ll) return 1;
return 0;
}
int main() {
int test;
cin >> test;
while (test--) {
long long n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
int ret = 0;
ret = ret | fun1(n, k, d1, d2);
ret = ret | fun2(n, k, d1, d2);
ret = ret | fun3(n, k, d1, d2);
ret = ret | fun4(n, k, d1, d2);
if (ret == 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.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
public class Solution {
static class Solver {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long n = in.nextLong();
long k = in.nextLong();
long d1 = in.nextLong();
long d2 = in.nextLong();
boolean can = get(n, k, d1, d2);
out.println(can ? "yes": "no");
}
private boolean get(long n, long k, long d1, long d2) {
if(n % 3L != 0) return false;
long[] dx = {d1, -d1};
long[] dy = {d2, -d2};
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
long x2 = (k - dx[i] + dy[j]);
if(x2 % 3L != 0) continue;
x2 /= 3L;
if(x2 >= 0 && x2 <= k) {
long x1 = dx[i] + x2;
long x3 = x2 - dy[j];
if(x1 >= 0 && x1 <= k && x3 >= 0 && x3 <= k) {
long to = n / 3L;
if(x1 <= to && x2 <= to && x3 <= to) {
return true;
}
}
}
}
}
return false;
}
private boolean doIt(long n, long k, long[] dl, long[] dr, long x) {
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
PriorityQueue<Long> pq = new PriorityQueue<>(Comparator.reverseOrder());
// System.out.println(dr[i] + " " + dl[j]);
if(3L * x + dl[j] + dr[i] > k) continue;
pq.add(x);
pq.add(x + dr[i]);
pq.add(x + dl[j]);
// System.out.println(pq);
long left = n - k;
long max = pq.poll();
long mid = pq.poll();
left -= (max - mid);
long small = pq.poll();
left -= (max - small);
if(left >= 0) {
if(left % 3L == 0) {
// System.out.println((x + dr[i]) + " " + x + " " + (x + dl[j]));
return true;
}
}
}
}
return false;
}
}
public static void main(String[] args) throws Exception {
InputReader in = new InputReader(System.in);
PrintWriter out =
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out), 1 << 14));
Solver solver = new Solver();
int t = in.nextInt();
for (int i = 1; i <= t; i++)
solver.solve(i, in, out);
out.close();
in.close();
}
static class InputReader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public InputReader(InputStream is) {
din = new DataInputStream(is);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public InputReader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String nextLine() {
StringBuilder sb = new StringBuilder();
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
sb.append((char) c);
}
return sb.toString();
}
public String next() {
StringBuilder sb = new StringBuilder();
byte c = read();
while (c <= ' ')
c = read();
while (c != ' ' && c != '\n' && c != '\t' && c != '\r') {
sb.append((char) c);
c = read();
}
return sb.toString();
}
public int nextInt() {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
public int[] nextIntArray(int n) {
int[] ret = new int[n];
for(int i = 0; i < n; ++i) ret[i] = nextInt();
return ret;
}
public long[] nextLongArray(int n) {
long[] ret = new long[n];
for(int i = 0; i < n; ++i) ret[i] = nextLong();
return ret;
}
private void fillBuffer() {
try {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
} catch (IOException e) {
throw new RuntimeException("trying to read from null input, verify constraints ");
}
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author PrateekNischal
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskCC solver = new TaskCC();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
}
class TaskCC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long n = in.nextLong();
long k = in.nextLong();
long d1 = in.nextLong();
long d2 = in.nextLong();
long w1, w2, w3;
boolean res = false;
if (n%3 != 0)
{
out.println("no");
return ;
}
long win = n/3;
//case 1: w1 > w2 , w2 > w3
w1 = (k + 2*d1 + d2);
w2 = (k - d1 + d2);
w3 = (k - d1 - 2*d2);
//out.println("dabba" + w1 + " " + w2 + " " + w3);
if (w1 >= 0 && w2 >= 0 && w3 >= 0)
{
if (w1 % 3 == 0 && w2 % 3 == 0 && w3 % 3 == 0)
{
if (w1 / 3 <= win && w2 / 3 <= win && w3 / 3 <= win) {
out.println("yes");
res = true;
}
}
}
if (!res)
{
w1 = (k + 2*d1 - d2);
w2 = (k - d1 - d2);
w3 = (k - d1 + 2*d2);
if (w1 >= 0 && w2 >= 0 && w3 >= 0)
{
if (w1 % 3 == 0 && w2 % 3 == 0 && w3 % 3 == 0)
{
if (w1 / 3 <= win && w2 / 3 <= win && w3 / 3 <= win) {
out.println("yes");
res = true;
}
}
}
}
if (!res)
{
w1 = (k - 2*d1 + d2);
w2 = (k + d1 + d2);
w3 = (k + d1 - 2*d2);
if (w1 >= 0 && w2 >= 0 && w3 >= 0)
{
if (w1 % 3 == 0 && w2 % 3 == 0 && w3 % 3 == 0)
{
if (w1 / 3 <= win && w2 / 3 <= win && w3 / 3 <= win) {
out.println("yes");
res = true;
}
}
}
}
if (!res)
{
w1 = (k - 2*d1 - d2);
w2 = (k + d1 - d2);
w3 = (k + d1 + 2*d2);
if (w1 >= 0 && w2 >= 0 && w3 >= 0)
{
if (w1 % 3 == 0 && w2 % 3 == 0 && w3 % 3 == 0)
{
if (w1 / 3 <= win && w2 / 3 <= win && w3 / 3 <= win) {
out.println("yes");
res = true;
}
}
}
}
if (!res)
out.println("no");
}
}
class InputReader
{
public BufferedReader reader;
public StringTokenizer tokenizer;
public int bufferSize = 32768;
public InputReader(InputStream stream)
{
reader = new BufferedReader(new InputStreamReader(stream), bufferSize);
tokenizer = null;
}
public String next()
{
while (tokenizer == null || !tokenizer.hasMoreTokens()){
try{
tokenizer = new StringTokenizer(reader.readLine());
}catch(IOException e){throw new RuntimeException(e);}
}
return tokenizer.nextToken();
}
public long nextLong(){
return Long.parseLong(next());
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.io.*;
import java.math.*;
import java.util.*;
public class CODEFORCES {
private InputStream is;
private PrintWriter out;
void solve() {
int t = ni();
while (t-- > 0) {
long n = nl(), k = nl(), d1 = nl(), d2 = nl();
if (n % 3 != 0) {
out.println("no");
continue;
}
boolean u = false;
long l = 0, r = k;
while (l <= r) {
long mid = l + r >> 1;
long w2 = mid;
long w1 = mid - d1;
long w3 = mid - d2;
if (w2 + w1 + w3 == k && w1 >= 0 && w2 >= 0 && w3 >= 0 && w1 <= n / 3 && w2 <= n / 3 && w3 <= n / 3) {
u = true;
break;
}
if (w3 < 0 || w1 < 0 || w2 < 0 || w1 + w2 + w3 < k)
l = mid + 1;
else
r = mid - 1;
}
l = 0;
r = k;
while (l <= r) {
long mid = l + r >> 1;
long w2 = mid;
long w1 = mid + d1;
long w3 = mid + d2;
if (w2 + w1 + w3 == k && w1 >= 0 && w2 >= 0 && w3 >= 0 && w1 <= n / 3 && w2 <= n / 3 && w3 <= n / 3) {
u = true;
break;
}
if (w3 < 0 || w1 < 0 || w2 < 0 || w1 + w2 + w3 < k)
l = mid + 1;
else
r = mid - 1;
}
l = 0;
r = k;
while (l <= r) {
long mid = l + r >> 1;
long w2 = mid;
long w1 = mid + d1;
long w3 = mid - d2;
if (w2 + w1 + w3 == k && w1 >= 0 && w2 >= 0 && w3 >= 0 && w1 <= n / 3 && w2 <= n / 3 && w3 <= n / 3) {
u = true;
break;
}
if (w3 < 0 || w1 < 0 || w2 < 0 || w1 + w2 + w3 < k)
l = mid + 1;
else
r = mid - 1;
}
l = 0;
r = k;
while (l <= r) {
long mid = l + r >> 1;
long w2 = mid;
long w1 = mid - d1;
long w3 = mid + d2;
if (w2 + w1 + w3 == k && w1 >= 0 && w2 >= 0 && w3 >= 0 && w1 <= n / 3 && w2 <= n / 3 && w3 <= n / 3) {
u = true;
break;
}
if (w3 < 0 || w1 < 0 || w2 < 0 || w1 + w2 + w3 < k)
l = mid + 1;
else
r = mid - 1;
}
if (u)
out.println("yes");
else
out.println("no");
}
}
void soln() throws Exception {
is = System.in;
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new CODEFORCES().soln();
}
// To Get Input
// Some Buffer Methods
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (!oj)
System.out.println(Arrays.deepToString(o));
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
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, InputReader in, PrintWriter out) {
long n = in.readLong();
long k = in.readLong();
long d1 = in.readLong();
long d2 = in.readLong();
boolean valid = false;
int[] x = new int[]{1, 1, -1, -1};
int[] y = new int[]{1, -1, 1, -1};
for (int i = 0; i < 4; i++) {
long a = 2 * d1 * x[i] + d2 * y[i] + k;
if (a % 3 == 0) {
a /= 3;
long b = a - d1 * x[i];
long c = b - d2 * y[i];
if (a >= 0 && b >= 0 && c >= 0) {
long max = Math.max(a, Math.max(b, c));
long cnt = k + max - a;
cnt += max - b;
cnt += max - c;
if (cnt <= n && (n - cnt) % 3 == 0) {
valid = true;
}
}
}
}
out.println(valid ? "yes" : "no");
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.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 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 {
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);
}
}
}
|
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.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) throws NumberFormatException,IOException {
Stdin in = new Stdin();
PrintWriter out = new PrintWriter(System.out);
int t=in.readInt();
long n,k,d1,d2;
for(int i=0;i<t;i++){
n=in.readLong();
k=in.readLong();
d1=in.readLong();
d2=in.readLong();
if(isPossible(n,k,d1,d2))
out.println("yes");
else
out.println("no");
}
out.flush();
out.close();
}
public static boolean isPossible(long n,long k,long d1,long d2){
if(n%3!=0)
return false;
long t1=0,t2=0,t3=0;
if((k-d1+d2)%3==0){
t2=(k-d1+d2)/3;
t1=t2+d1;
t3=t2-d2;
if(t1>=0&&t2>=0&&t3>=0&&t1<=n/3&&t2<=n/3&&t3<=n/3)
return true;
}
if((k-d1-d2)%3==0){
t2=(k-d1-d2)/3;
t1=t2+d1;
t3=t2+d2;
if(t1>=0&&t2>=0&&t3>=0&&t1<=n/3&&t2<=n/3&&t3<=n/3)
return true;
}
if((k+d1+d2)%3==0){
t2=(k+d1+d2)/3;
t1=t2-d1;
t3=t2-d2;
if(t1>=0&&t2>=0&&t3>=0&&t1<=n/3&&t2<=n/3&&t3<=n/3)
return true;
}
if((k+d1-d2)%3==0){
t2=(k+d1-d2)/3;
t1=t2-d1;
t3=t2+d2;
if(t1>=0&&t2>=0&&t3>=0&&t1<=n/3&&t2<=n/3&&t3<=n/3)
return true;
}
return false;
}
private static class Stdin {
InputStreamReader read;
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
private Stdin() {
read = new InputStreamReader(System.in);
br = new BufferedReader(read);
}
private String readNext() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
private int readInt() throws IOException, NumberFormatException {
return Integer.parseInt(readNext());
}
private long readLong() throws IOException, NumberFormatException {
return Long.parseLong(readNext());
}
}
}
|
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>
#pragma comment(linker, "/stack:225450978")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double Pi = 3.141592653589793116, EPS = 1e-9,
Gold = ((1 + sqrt(5)) / 2);
long long keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};
long long keyCount = sizeof(keymod) / sizeof(long long);
template <class T>
int getbit(T s, int i) {
return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return __builtin_popcountll(s);
}
auto TimeStart = chrono::steady_clock::now();
auto TimeEnd = chrono::steady_clock::now();
void ControlIO(int argc, char* argv[]);
void TimerStart();
void TimerStop();
void Exit();
long long n, k, d1, d2;
void Input() { cin >> n >> k >> d1 >> d2; }
void Solve() {
long long A = LINF, B = 0, C = 0;
for (long long i = 0; i < 2; i++) {
for (long long j = 0; j < 2; j++) {
if (i)
B = A - d1;
else
B = A + d1;
if (j)
C = B - d2;
else
C = B + d2;
if ((k - A - B - C + min(A, min(B, C)) * 3) % 3 != 0 ||
k - A - B - C + min(A, min(B, C)) * 3 < 0)
continue;
if ((A + B + C + n - k) % 3 == 0 &&
(A + B + C + n - k) / 3 >= max(A, max(B, C))) {
cout << "yes\n";
return;
}
}
}
cout << "no\n";
}
int main(int argc, char* argv[]) {
ControlIO(argc, argv);
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int T;
cin >> T;
TimerStart();
while (T--) {
Input();
Solve();
}
TimerStop();
return 0;
}
void ControlIO(int argc, char* argv[]) {}
void TimerStart() {}
void TimerStop() {}
void Exit() {
TimerStop();
exit(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;
struct print {
public:
static void lnrev(vector<int> x) {
long long int n = x.size();
for (int i = n - 1; i >= 0; i--) cout << x[i] << " ";
cout << endl;
}
static void ln(vector<long long int> y) {
long long int n = y.size();
for (long long int i = 0; i < n; i++) cout << y[i] << " ";
cout << endl;
}
};
long long int SUM(vector<int> temp) {
long long int ans = 0;
for (long long int i = 0; i < temp.size(); i++) {
ans += temp[i];
}
return ans;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = ((res % p) * (x % p)) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (!a) return b;
return gcd(b % a, a);
}
int findGCD(int a, int b, int c) {
int arr[3];
arr[0] = a;
arr[1] = b;
arr[2] = c;
int result = arr[0];
for (int i = 1; i < 3; i++) result = gcd(arr[i], result);
return result;
}
vector<int> computeLPSArray(string pat) {
int M = pat.size();
vector<int> lps(M);
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode *next;
TreeNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};
class graph {
private:
vector<vector<int> > Adj;
int Edges;
int Vertices;
public:
graph(int V, int E) {
Vertices = V;
Edges = E;
int a, b;
Adj.resize(V);
for (long long int i = 0; i < E; i++) {
cin >> a >> b;
Adj[a - 1].emplace_back(b - 1);
Adj[b - 1].emplace_back(a - 1);
}
}
int BFS(int start, int dest) {
queue<pair<int, int> > st;
if (start == dest) return 0;
st.push({start, 0});
vector<int> visited(Vertices);
while (st.empty() == false) {
int curr = st.front().first;
int curr_dist = st.front().second;
if (curr == dest) return curr_dist;
visited[curr] = true;
st.pop();
for (int i = 0; i < Adj[curr].size(); i++) {
if (visited[Adj[curr][i]] == false) {
st.push({Adj[curr][i], curr_dist + 1});
}
}
}
}
bool checkedge(int a, int b) {
for (int i = 0; i < Adj[a].size(); i++) {
if (Adj[a][i] == (b)) return true;
}
return false;
}
};
map<vector<int>, int> G;
void SOL(vector<int> &V) {
queue<pair<vector<int>, int> > q;
q.push({V, 0});
while (q.empty() == false) {
vector<int> P = q.front().first;
int K = q.front().second;
if (K == P.size()) {
G[q.front().first]++;
q.pop();
continue;
}
q.pop();
for (int i = 0; i < P.size(); i++) {
vector<int> L = P;
int temp = L[K];
L[K] = L[i];
L[i] = temp;
q.push({L, K + 1});
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long int T;
cin >> T;
while (T--) {
long long int a, b, c;
long long int n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
if ((k + d1 + d2) % 3 == 0) {
b = (k + d1 + d2) / 3;
a = b - d1;
c = b - d2;
long long int temp = (max(a, max(b, c)));
if (a >= 0 and b >= 0 and c >= 0) {
long long int add = temp - a + (temp - b) + (temp - c);
if (add <= (n - k)) {
n -= add;
n -= k;
if (n % 3 == 0) {
cout << "yes" << endl;
continue;
}
}
}
}
if ((k - d1 + d2) % 3 == 0) {
b = (k - d1 + d2) / 3;
a = b + d1;
c = b - d2;
long long int temp = (max(a, max(b, c)));
if (a >= 0 and b >= 0 and c >= 0) {
long long int add = temp - a + (temp - b) + (temp - c);
if (add <= (n - k)) {
n -= add;
n -= k;
if (n % 3 == 0) {
cout << "yes" << endl;
continue;
}
}
}
}
if ((k + d1 - d2) % 3 == 0) {
b = (k + d1 - d2) / 3;
a = b - d1;
c = b + d2;
long long int temp = (max(a, max(b, c)));
if (a >= 0 and b >= 0 and c >= 0) {
long long int add = temp - a + (temp - b) + (temp - c);
if (add <= (n - k)) {
n -= add;
n -= k;
if (n % 3 == 0) {
cout << "yes" << endl;
continue;
}
}
}
}
if ((k - d1 - d2) % 3 == 0) {
b = (k - d1 - d2) / 3;
a = b + d1;
c = b + d2;
long long int temp = (max(a, max(b, c)));
if (a >= 0 and b >= 0 and c >= 0) {
long long int add = temp - a + (temp - b) + (temp - c);
if (add <= (n - k)) {
n -= add;
n -= k;
if (n % 3 == 0) {
cout << "yes" << endl;
continue;
}
}
}
}
cout << "no" << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
long long t, k, n, d1, d2, md;
bool jg(long long a, long long b, long long c) {
long long s = a + b + c;
if (k < s || (k - s) % 3) return 0;
long long t = n - k - (3 * max(max(a, b), c) - s);
if (t < 0 || t % 3) return 0;
return 1;
}
int main() {
cin >> t;
while (t--) {
cin >> n >> k >> d1 >> d2;
md = max(d1, d2);
if (jg(0, d1, d1 + d2) || jg(d1 + d2, d2, 0) || jg(d1, 0, d2) ||
jg(md - d1, md, md - d2))
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
class Graph {
public:
int V;
vector<vector<int> > adj;
Graph(int n) : V(n), adj(n) {}
void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
};
inline int gcd(int a, int b) {
while (a > 0 && b > 0) {
if (a > b)
a %= b;
else
b %= a;
}
return a + b;
}
long long solve(pair<long long, long long> p1, pair<long long, long long> p2,
long long d1, long long d2, long long k,
vector<long long>& ans) {
long long a1 = p1.first, b1 = p1.second, a2 = p2.first, b2 = p2.second;
long long y = (k - d1 / a1 - d2 / b2) / 3;
long long x = (d1 - b1 * y) / a1;
long long z = (d2 - a2 * y) / b2;
if ((k - d1 / a1 - d2 / b2) % 3 != 0 || y < 0 || x < 0 || z < 0) {
return -1;
}
assert(a1 * x + b1 * y == d1);
assert(a2 * y + b2 * z == d2);
assert(x + y + z == k);
ans.push_back(x);
ans.push_back(y);
ans.push_back(z);
return 0;
}
int main() {
long long t;
cin >> t;
vector<pair<pair<long long, long long>, pair<long long, long long> > > coeff;
coeff.push_back(make_pair(make_pair(-1, 1), make_pair(1, -1)));
coeff.push_back(make_pair(make_pair(-1, 1), make_pair(-1, 1)));
coeff.push_back(make_pair(make_pair(1, -1), make_pair(1, -1)));
coeff.push_back(make_pair(make_pair(1, -1), make_pair(-1, 1)));
for (long long i = 0; i < t; i++) {
long long n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
if (n % 3 != 0) {
cout << "no\n";
continue;
}
string output = "no\n";
for (long long j = 0; j < 4; j++) {
vector<long long> ans;
long long res = solve(coeff[j].first, coeff[j].second, d1, d2, k, ans);
if (res == -1) {
continue;
}
long long x = ans[0], y = ans[1], z = ans[2];
if (x <= n / 3 && y <= n / 3 && z <= n / 3) {
output = "yes\n";
break;
}
}
cout << output;
}
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 isok(long long n, long long k, long long d1, long long d2) {
if (n % 3) return false;
long long win[5], sum = 0, Max, Min;
win[3] = 0;
win[2] = win[3] + d2;
win[1] = win[2] + d1;
sum = win[1] + win[2] + win[3];
Max = max(win[1], max(win[2], win[3]));
Min = min(win[1], min(win[2], win[3]));
if (Max > n / 3 || Min < 0 || sum > k || (k - sum) % 3 != 0)
;
else if (n - k >= 3 * Max - sum)
return true;
win[2] = 0;
win[3] = win[2] + d2;
win[1] = win[2] + d1;
sum = win[1] + win[2] + win[3];
Max = max(win[1], max(win[2], win[3]));
Min = min(win[1], min(win[2], win[3]));
if (Max > n / 3 || Min < 0 || sum > k || (k - sum) % 3 != 0)
;
else if (n - k >= 3 * Max - sum)
return true;
win[3] = 0;
win[2] = win[3] + d2;
win[1] = win[2] - d1;
sum = win[1] + win[2] + win[3];
Max = max(win[1], max(win[2], win[3]));
Min = min(win[1], min(win[2], win[3]));
if (Max > n / 3 || Min < 0 || sum > k || (k - sum) % 3 != 0)
;
else if (n - k >= 3 * Max - sum)
return true;
win[1] = 0;
win[2] = win[1] + d1;
win[3] = win[2] - d2;
sum = win[1] + win[2] + win[3];
Max = max(win[1], max(win[2], win[3]));
Min = min(win[1], min(win[2], win[3]));
if (Max > n / 3 || Min < 0 || sum > k || (k - sum) % 3 != 0)
;
else if (n - k >= 3 * Max - sum)
return true;
win[2] = 0;
win[1] = win[2] + d1;
win[3] = win[2] + d2;
sum = win[1] + win[2] + win[3];
Max = max(win[1], max(win[2], win[3]));
Min = min(win[1], min(win[2], win[3]));
if (Max > n / 3 || Min < 0 || sum > k || (k - sum) % 3 != 0)
;
else if (n - k >= 3 * Max - sum)
return true;
win[1] = 0;
win[2] = win[1] + d1;
win[3] = win[2] + d2;
sum = win[1] + win[2] + win[3];
Max = max(win[1], max(win[2], win[3]));
Min = min(win[1], min(win[2], win[3]));
if (Max > n / 3 || Min < 0 || sum > k || (k - sum) % 3 != 0)
;
else if (n - k >= 3 * Max - sum)
return true;
return false;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long n, k, d1, d2;
scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2);
puts(isok(n, k, d1, d2) ? "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() {
long long i, k, j, m, t, n, d1, d2;
scanf("%lld", &t);
while (t--) {
scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2);
if (n % 3 != 0) {
printf("no\n");
continue;
}
n = n - k;
if (k >= 2 * d1 + d2 && (k - d1 - d1 - d2) % 3 == 0) {
m = n + 2 * d1 + d2 - 3 * (d1 + d2);
if (m % 3 == 0 && m >= 0) {
printf("yes\n");
continue;
}
}
if (k >= 2 * d2 + d1 && (k - d1 - d2 - d2) % 3 == 0) {
m = n + 2 * d2 + d1 - 3 * (d1 + d2);
if (m >= 0 && m % 3 == 0) {
printf("yes\n");
continue;
}
}
if (k >= d1 + d2 && (k - d1 - d2) % 3 == 0) {
m = n + d1 + d2 - 3 * max(d1, d2);
if (m >= 0 && m % 3 == 0) {
printf("yes\n");
continue;
}
}
if (k >= max(d1, d2) * 2 + max(d1, d2) - d1 - d2 &&
(k - max(d1, d2) * 2 - max(d1, d2) + d1 + d2) % 3 == 0) {
m = n - d1 - d2;
if (m >= 0 && m % 3 == 0) {
printf("yes\n");
continue;
}
}
printf("no\n");
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
def check(n, k, d1, d2):
if (k-d1-d2) % 3 != 0:
return False
y = (k-d1-d2) // 3
x = y+d1
z = y+d2
t = n // 3
if x < 0 or y < 0 or z < 0:
return False
if x > t or y > t or z > t:
return False
return True
t = int(input())
for _ in range(t):
n, k, d1, d2 = map(int, input().split())
if n % 3 != 0:
print ('no')
continue
if check(n, k, d1, d2):
print ('yes')
continue
if check(n, k, -d1, d2):
print ('yes')
continue
if check(n, k, d1, -d2):
print ('yes')
continue
if check(n, k, -d1, -d2):
print ('yes')
continue
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.*;
public class Main{
static boolean f(long n,long k,long d1,long d2){
if(n%3!=0)return false;
if(k<d1+d2+d2)return false;
k-=(d1+d2+d2);
if(k%3==0){
long cnt=k/3;
long mxa=cnt+d1+d2;
long temp=n/3;
if(mxa>temp)return false;
return true;
}
return false;
}
public static void main(String[] agrs){
Scanner cin=new Scanner(System.in);
int t=cin.nextInt();
for(int i=0;i<t;i++){
long n,d1,d2,k;
n=cin.nextLong();
k=cin.nextLong();
d1=cin.nextLong();
d2=cin.nextLong();
long temp=d1-d2>0?d1-d2:d2-d1;
long temp2=d1>d2?d2:d1;
//System.out.println(""+temp+" "+temp2);
if(f(n,k,d1,d2)||f(n,k,d2,d1)||f(n,k,temp2,temp)||f(n,k,temp,temp2))
System.out.println("yes");
else System.out.println("no");
//d1 d2
//d1>d2 d1-d2 d2
// d2 d1-d2
//d2 d1
}
}
}
|
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 sys
from random import randint
from time import time
t,=map(lambda x:int(x),sys.stdin.readline().strip('\n').split(" "))
for i in xrange(0,t):
n,k,d1,d2=map(lambda x:int(x),sys.stdin.readline().strip('\n').split(" "))
d1,d2=max(d1,d2),min(d1,d2)
if 2*d1+d2<=k and (2*d1+d2)%3==k%3 and (2*d2+d1)<=n-k and (2*d2+d1)%3==(n-k)%3:
print "yes"
elif (2*d1-d2)<=k and (2*d1-d2)%3==k%3 and (d2+d1)<=n-k and (d2+d1)%3==(n-k)%3:
print "yes"
elif (d1+d2)<=k and (d1+d2)%3==k%3 and (2*d1-d2)<=(n-k) and (2*d1-d2)%3==(n-k)%3:
print "yes"
elif (d1+2*d2)<=k and (d1+2*d2)%3==k%3 and (2*d1+d2)<=n-k and (2*d1+d2)%3==(n-k)%3:
print "yes"
else:
print "no"
|
PYTHON
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long d1, d2, n, k, a, b, c;
scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2);
int f = 0;
long long kk = n / 3;
double fa = (double)((k + d2) - 2 * d1) / 3;
if (fa >= 0 && fa == (long long)fa) {
a = (long long)fa;
b = d1 + a;
c = b - d2;
if (a >= 0 && b >= 0 && c >= 0 && b <= kk && c <= kk && a <= kk &&
(kk * 3 - a - b - c) == (n - k)) {
f = 1;
}
}
fa = (double)((k - d2) - 2 * d1) / 3;
if (fa >= 0 && fa == (long long)fa) {
a = (long long)fa;
b = d1 + a;
c = b + d2;
if (a >= 0 && b >= 0 && c >= 0 && b <= kk && c <= kk && a <= kk &&
(kk * 3 - a - b - c) == (n - k)) {
f = 1;
}
}
fa = (double)((k + d2) + 2 * d1) / 3;
if (fa >= 0 && fa == (long long)fa) {
a = (long long)fa;
b = a - d1;
c = b - d2;
if (a >= 0 && b >= 0 && c >= 0 && b <= kk && c <= kk && a <= kk &&
(kk * 3 - a - b - c) == (n - k)) {
f = 1;
}
}
fa = (double)((k - d2) + 2 * d1) / 3;
if (fa >= 0 && fa == (long long)fa) {
a = (long long)fa;
b = a - d1;
c = b + d2;
if (a >= 0 && b >= 0 && c >= 0 && b <= kk && c <= kk && a <= kk &&
(kk * 3 - a - b - c) == (n - k)) {
f = 1;
}
}
if (f == 1)
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
|
#!/usr/bin/env python
def read():
n = int(raw_input())
ret = []
for i in range(n):
ret.append(map(int, raw_input().split()))
return ret
def work(vvList):
for (n, k, d1, d2) in vvList:
def judge():
if n % 3 != 0:
print "no"
return
for mul1 in range(-1, 2, 2):
for mul2 in range(-1, 2, 2):
t = k - mul1 * d1 - mul1 * d1 - mul2 * d2
if t % 3 == 0 and t >= 0:
x1 = t / 3
x2 = t / 3 + mul1 * d1
x3 = t / 3 + mul1 * d1 + mul2 * d2
if 0 <= min(x1, x2, x3) and max(x1, x2, x3) <= n / 3:
print "yes"
return
print "no"
judge()
if __name__ == "__main__":
work(read())
|
PYTHON
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
long long n, k, d1, d2;
cin >> t;
while (t--) {
cin >> n >> k >> d1 >> d2;
bool answer = false;
if (d1 + d2 <= k && (k - (d1 + d2)) % 3 == 0) {
long long max = d1 > d2 ? d1 : d2;
if ((3 * max - d1 - d2) <= n - k) {
if (((n - k) - (3 * max - d1 - d2)) % 3 == 0) {
answer = true;
}
}
}
if (2 * d2 + d1 <= k && (k - 2 * d2 - d1) % 3 == 0) {
if ((2 * d1 + d2) <= n - k) {
if (((n - k) - ((2 * d1 + d2))) % 3 == 0) {
answer = true;
}
}
}
if (2 * d1 + d2 <= k && (k - 2 * d1 - d2) % 3 == 0) {
if ((2 * d2 + d1) <= n - k) {
if (((n - k) - ((2 * d2 + d1))) % 3 == 0) {
answer = true;
}
}
}
if (d2 >= d1 && 2 * d2 - d1 <= k && (k - 2 * d2 + d1) % 3 == 0) {
if (d2 + d1 <= n - k) {
if (((n - k) - (d2 + d1)) % 3 == 0) {
answer = true;
}
}
}
if (d1 >= d2 && 2 * d1 - d2 <= k && (k - 2 * d1 + d2) % 3 == 0) {
if (d2 + d1 <= n - k) {
if (((n - k) - (d2 + d1)) % 3 == 0) {
answer = true;
}
}
}
answer ? cout << "yes" : cout << "no";
cout << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 1, -1, -1};
int dy[] = {1, -1, -1, 1};
long long int n, k, d1, d2, rem;
bool _solve(long long int w1, long long int w2, long long int w3) {
long long int tar = n / 3;
if (w1 < 0 || w1 > k || w2 < 0 || w2 > k || w3 < 0 || w2 > k) return false;
if (tar >= w1 && tar >= w2 && tar >= w3 && abs(w1 - w2) == d1 &&
abs(w2 - w3) == d2)
return true;
return false;
}
bool solve() {
long long int _d1, w1, w2, w3, _d2;
for (int i = 0; i < 4; i++) {
_d1 = d1 * dx[i];
_d2 = d2 * dy[i];
w1 = (k + 2 * _d1 + _d2) / 3;
w2 = w1 - _d1;
w3 = w2 - _d2;
if (w1 + w2 + w3 != k) continue;
if (_solve(w1, w2, w3)) {
return true;
}
}
return false;
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n >> k >> d1 >> d2;
if (n % 3 == 0) {
if (solve())
cout << "yes" << endl;
else
cout << "no" << endl;
} else
cout << "no" << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
'''
x1 + x2 + x3 = k
|x1-x2| = d1
|x2-x3| = d2
x1 - x2 = d1
x2 - x3 = d2
2x1 + x3 = k + d1
x1 - x3 = d1 + d2
3x1 = k + 2d1+d2
x1 = (k+2d1+d2)/3
x2 = x1-d1
x3 = x2-d2
x1 - x2 = -d1
x2 - x3 = d2
x1 - x2 = d1
x2 - x3 = -d2
x1 - x2 = -d1
x2 - x3 = -d2
'''
def solve(n, k, d1, d2):
if n % 3 != 0: return False
for i in [-1, 1]:
for j in [-1, 1]:
t = k+2*i*d1+j*d2
if t % 3 != 0: continue
a = t/3
b = a - i*d1
c = b - j*d2
if a < 0 or b < 0 or c < 0: continue
if a <= n/3 and b <= n/3 and c <= n/3: return True
if a == b == c: return True
return False
t = int(raw_input())
for i in range(t):
n, k, d1, d2 = map(int, raw_input().split())
print 'yes' if solve(n, k, d1, d2) else 'no'
|
PYTHON
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
long long n;
int check(long long a, long long b, long long c, long long k) {
if (a < 0 or b < 0 or c < 0) return false;
if (a + b + c == k) {
long long greater = max(max(a, b), c);
long long rest = n - k;
long long need = (3LL * greater - (a + b + c));
if (rest >= need and ((rest - need) % 3L) == 0L) return true;
}
return false;
}
int solve() {
long long k, d1, d2;
cin >> n >> k >> d1 >> d2;
long long a, b, c;
c = (k - d1 - d2 - d2) / 3LL;
b = d2 + c;
a = d1 + b;
if (check(a, b, c, k)) return true;
c = (k - d1 + d2 + d2) / 3LL;
b = c - d2;
a = d1 + b;
if (check(a, b, c, k)) return true;
c = (k + d1 - d2 - d2) / 3LL;
b = d2 + c;
a = b - d1;
if (check(a, b, c, k)) return true;
c = (k + d1 + d2 + d2) / 3LL;
b = c - d2;
a = b - d1;
if (check(a, b, c, k)) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
if (solve())
cout << "yes" << '\n';
else
cout << "no" << '\n';
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
long long fun(long long x1, long long x2, long long x3, long long n) {
if ((n + x1 + x2 + x3) % 3 == 0) {
long long p = (n + x1 + x2 + x3) / 3;
if (p >= max(x1, max(x2, x3))) {
cout << "yes\n";
return 1;
}
}
return 0;
}
long long fp(long long d1, long long d2, long long n, long long k) {
long long x1, x2, x3, p = 0;
double X, Y, Z;
x1 = X = (2 * d1 + k + d2) / 3.0;
x2 = Y = (k - d1 + d2) / 3.0;
x3 = Z = (k - d1 - 2 * d2) / 3.0;
if (x1 >= 0 && x2 >= 0 && x3 >= 0) {
if (X == x1 && Y == x2 && Z == x3) p = fun(x1, x2, x3, n - k);
}
return p;
}
int main() {
long long i, j, k, l, m, n, t, p;
cin >> t;
while (t--) {
p = 0;
long long d1, d2, d3;
cin >> n >> k >> d1 >> d2;
if ((n == k && d1 != 0 && d2 != 0) || n % 3 != 0)
cout << "no\n";
else {
p = fp(d1, d2, n, k);
if (p == 1) continue;
p = fp(d1, -d2, n, k);
if (p == 1) continue;
p = fp(-d1, d2, n, k);
if (p == 1) continue;
p = fp(-d1, -d2, n, k);
if (p == 1) continue;
cout << "no\n";
}
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int dir[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
long long pow(long long n, long long m, long long mod = 0) {
if (m < 0) return 0;
long long ans = 1;
long long k = n;
while (m) {
if (m & 1) {
ans *= k;
if (mod) ans %= mod;
}
k *= k;
if (mod) k %= mod;
m >>= 1;
}
return ans;
}
struct baba {
int a, b, c, d;
};
bool operator<(const baba &x, const baba &y) {
return ((x.a < y.a) || (x.a == y.a && x.b < y.b)) ||
(x.a == y.a && x.b == y.b && x.c < y.c) ||
(x.a == y.a && x.b == y.b && x.c == y.c && x.d < y.d);
}
long long n, k, d1, d2, w1, w2, w3;
bool solve(long long n, long long k, long long d1, long long d2) {
if ((k - d2 + d1) % 3 == 0)
w2 = (k - d2 + d1) / 3;
else
return 0;
w1 = w2 - d1;
w3 = d2 + w2;
if (w1 >= 0 && w2 >= 0 && w3 >= 0) return 1;
return 0;
}
int main() {
int T;
long long w;
cin >> T;
while (T--) {
cin >> n >> k >> d1 >> d2;
if (n % 3 != 0) {
cout << "no\n";
continue;
}
if (solve(n, k, d1, d2)) {
w = max(max(w1, w2), w3);
if (w <= n / 3) {
cout << "yes\n";
continue;
}
}
if (solve(n, k, -d1, d2)) {
w = max(max(w1, w2), w3);
if (w <= n / 3) {
cout << "yes\n";
continue;
}
}
if (solve(n, k, d1, -d2)) {
w = max(max(w1, w2), w3);
if (w <= n / 3) {
cout << "yes\n";
continue;
}
}
if (solve(n, k, -d1, -d2)) {
w = max(max(w1, w2), w3);
if (w <= n / 3) {
cout << "yes\n";
continue;
}
}
cout << "no\n";
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
bool res(long long d1, long long d2, long long n, long long k) {
long long w1, w2, w3;
w1 = (2 * d1 + k + d2) / 3;
w2 = w1 - d1;
w3 = w2 - d2;
long long x = n / 3 - w1, y = n / 3 - w2, z = n / 3 - w3;
if (x >= 0 && y >= 0 && z >= 0 && x + y + z == n - k && w1 >= 0 && w2 >= 0 &&
w3 >= 0)
return true;
else
return false;
}
int main() {
long long T;
cin >> T;
for (long long i = 0; i < T; i++) {
long long n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
if (n % 3 != 0)
cout << "no" << endl;
else if (res(d1, d2, n, k) || res(-d1, d2, n, k) || res(d1, -d2, n, k) ||
res(-d1, -d2, n, k))
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
const long long modulus = 1000000007ll;
const int infty = numeric_limits<int>::max();
struct _ {
_() {
ios_base::Init i;
ios_base::sync_with_stdio(0);
cin.tie(0);
}
} _;
int main() {
int t;
cin >> t;
for (int u = 0; u < t; ++u) {
long long n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
long long l = n - k;
if (k >= d1 + d2 * 2 && (k - (d1 + d2 * 2)) % 3 == 0) {
if (l >= d1 * 2 + d2 && (l - (d1 * 2 + d2)) % 3 == 0) {
cout << "yes" << '\n';
continue;
}
}
if (d1 >= d2 && k >= d1 + d2 && (k - (d1 + d2)) % 3 == 0) {
if (l >= d1 + (d1 - d2) && (l - (d1 + d1 - d2)) % 3 == 0) {
cout << "yes" << '\n';
continue;
}
}
if (d2 >= d1 && k >= d2 + d1 && (k - (d2 + d1)) % 3 == 0) {
if (l >= d2 + (d2 - d1) && (l - (d2 + d2 - d1)) % 3 == 0) {
cout << "yes" << '\n';
continue;
}
}
if (k >= d1 * 2 + d2 && (k - (d1 * 2 + d2)) % 3 == 0) {
if (l >= d1 + d2 * 2 && (l - (d1 + d2 * 2)) % 3 == 0) {
cout << "yes" << '\n';
continue;
}
}
if (d1 >= d2 && k >= d1 + d1 - d2 && (k - (d1 + d1 - d2)) % 3 == 0) {
if (l >= d1 + d2 && (l - (d1 + d2)) % 3 == 0) {
cout << "yes" << '\n';
continue;
}
}
if (d2 >= d1 && k >= d2 + d2 - d1 && (k - (d2 + d2 - d1)) % 3 == 0) {
if (l >= d1 + d2 && (l - (d1 + d2)) % 3 == 0) {
cout << "yes" << '\n';
continue;
}
}
cout << "no" << '\n';
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.util.Scanner;
public class PredictGameOutcome {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long n = scan.nextLong();
for (int i = 0; i < n; i++) {
boolean yes = false;
long g, p, d1, d2;
g = scan.nextLong();
p = scan.nextLong();
d1 = scan.nextLong();
d2 = scan.nextLong();
if (g % 3 == 0) {
if (p >= (2 * d1 + d2) && (p - (2 * d1 + d2)) % 3 == 0) {
if ((g - p) >= (d1 + 2 * d2))
yes = true;
}
if (d1 > d2 && p >= (2 * d1 - d2)
&& (p - (2 * d1 - d2)) % 3 == 0) {
if ((g - p) >= (d1 + d2))
yes = true;
}
if (d1 <= d2 && p >= (2 * d2 - d1)
&& (p - (2 * d2 - d1)) % 3 == 0) {
if ((g - p) >= (d1 + d2))
yes = true;
}
if (p >= (d1 + d2) && (p - (d1 + d2)) % 3 == 0) {
if (d1 > d2) {
if ((g - p) >= (2 * d1 - d2))
yes = true;
} else {
if ((g - p) >= (2 * d2 - d1))
yes = true;
}
}
if (p >= (d1 + 2 * d2) && (p - (d1 + 2 * d2)) % 3 == 0) {
if ((g - p) >= (2 * d1 + d2))
yes = true;
}
if (yes)
System.out.println("yes");
else
System.out.println("no");
} else
System.out.println("no");
}
scan.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;
vector<long long> ans;
bool test(long long a, long long b, long long c) {
if (a < 0 || b < 0 || c < 0 || a + b + c > k) return false;
ans.clear();
ans.push_back(a);
ans.push_back(b);
ans.push_back(c);
sort(ans.begin(), ans.end());
long long sum = n - k, cnt = ans[0] + ans[1] + ans[2];
for (int i = 0; i < 2; ++i) {
long long x = ans[2] - ans[i];
sum -= x;
cnt += x;
}
if (sum >= 0 && sum % 3 == 0 && cnt <= n) {
return true;
}
return false;
}
int main() {
int t;
cin >> t;
while (t--) {
cin >> n >> k >> d1 >> d2;
bool ok = false;
if (test(0, d1, d1 + d2)) ok = true;
if (test(0, d1, d1 - d2)) ok = true;
if (test(d2 - d1, d2, 0)) ok = true;
if (test(d1 + d2, d2, 0)) ok = true;
if (test(d1, 0, d2)) ok = true;
if (ok && n % 3 == 0)
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;
int outcome(long long n, long long k, long long d1, long long d2,
long long d3) {
if (k < d1 + d2 + d3) return 0;
if ((k - d1 - d2 - d3) % 3) return 0;
long long diff = (k - d1 - d2 - d3) / 3;
d1 += diff, d2 += diff, d3 += diff;
if (n % 3) return 0;
if (d1 > n / 3 || d2 > n / 3 || d3 > n / 3) return 0;
return 1;
}
int main() {
int t;
scanf("%d", &t);
for (int tt = 0; tt < t; tt++) {
long long n, k, d1, d2;
scanf("%lld %lld %lld %lld", &n, &k, &d1, &d2);
if (outcome(n, k, d1 + d2, d2, 0) || outcome(n, k, d1, 0, d2) ||
outcome(n, k, 0, d1, d1 + d2) ||
outcome(n, k, 0 + max(0ll, d2 - d1), d1 + max(0ll, d2 - d1),
d1 - d2 + max(0ll, d2 - d1)))
printf("yes\n");
else
printf("no\n");
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.io.*;
import java.util.*;
public class Solution {
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
//Main
public static void main(String args[]) {
int test=1;
test=sc.nextInt();
while(test-->0) {
//Focus
long n=sc.nextLong(),k=sc.nextLong(),D1=sc.nextLong(),D2=sc.nextLong();
if(n%3!=0) {
out.println("no");
continue;
}
long moves[][]= {{1,1},{-1,-1},{1,-1},{-1,1}};
int f=0;
for(long m[]: moves) {
long d1=m[0]*D1,d2=m[1]*D2;
long val=k+2*d1+d2;
if(val%3!=0) continue;
long w1=val/3,w2=w1-d1,w3=w2-d2;
if(w1<=n/3 && w2<=n/3 && w3<=n/3 && w1>=0 && w2>=0 && w3>=0) f=1;
}
out.println(f==0?"no":"yes");
}
out.flush();
out.close();
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long n, k, d1, d2;
scanf("%I64d", &n);
scanf("%I64d", &k);
scanf("%I64d", &d1);
scanf("%I64d", &d2);
if (n % 3 != 0) {
printf("no\n");
continue;
}
n -= k;
if (d1 + d2 <= k && (k - d1 - d2) % 3 == 0) {
long long m = n;
m -= 2 * max(d1, d2) - min(d1, d2);
if (m >= 0 && m % 3 == 0) {
printf("yes\n");
continue;
}
}
if (d1 + 2 * d2 <= k && (k - d1 - 2 * d2) % 3 == 0) {
long long m = n;
m -= 2 * d1 + d2;
if (m >= 0 && m % 3 == 0) {
printf("yes\n");
continue;
}
}
if (2 * d1 + d2 <= k && (k - 2 * d1 - d2) % 3 == 0) {
long long m = n;
m -= d1 + 2 * d2;
if (m >= 0 && m % 3 == 0) {
printf("yes\n");
continue;
}
}
long long t = 2 * d1 + 2 * d2 - 3 * min(d1, d2);
if (t <= k && (k - t) % 3 == 0) {
long long m = n;
m -= d1 + d2;
if (m >= 0 && m % 3 == 0) {
printf("yes\n");
continue;
}
}
printf("no\n");
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 3;
bool check(long long n, long long k, long long dt1, long long dt2) {
if (n % 3 != 0) return false;
long long a, b, c;
for (int i = -1; i <= 1; i += 2)
for (int j = -1; j <= 1; j += 2) {
long long d1 = dt1, d2 = dt2;
d1 *= i;
d2 *= j;
if ((k + 2 * d1 + d2) % 3 != 0) continue;
a = (k + 2 * d1 + d2) / 3;
b = a - d1;
c = b - d2;
if (a >= 0 && a <= k && b >= 0 && b <= k && c >= 0 && c <= k)
if (a <= n / 3 && b <= n / 3 && c <= n / 3) return true;
}
return false;
}
int main() {
long long ms, ns, ks, ds1, ds2;
int m;
cin >> m;
for (int i = 0; i < m; i++) {
scanf("%I64d %I64d %I64d %I64d", &ns, &ks, &ds1, &ds2);
if (check(ns, ks, ds1, ds2))
cout << "yes\n";
else
cout << "no\n";
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.util.Scanner;
public class Snippet {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
long n, k, d1, d2;
for (int l = 0; l < t; ++l) {
n = sc.nextLong();
k = sc.nextLong();
d1 = sc.nextLong();
d2 = sc.nextLong();
if (n % 3 != 0) {
System.out.println("no");
continue;
}
n /= 3;
long tmp = 0;
boolean ok = false;
for (int i = -1; i <= 1; i += 2) {
for (int j = -1; j <= 1; j += 2) {
tmp = k;
tmp -= d1 * i;
tmp -= d1 * i;
tmp -= d2 * j;
if (tmp % 3 != 0) continue;
if (tmp < 0) continue;
tmp /= 3;
long x1 = tmp;
long x2 = tmp + d1 * i;
long x3 = x2 + d2 * j;
if (x1 < 0 || x2 < 0 || x3 < 0) continue;
if (x1 <= n && x2 <= n && x3 <= n) ok = true;
}
}
if (ok) System.out.println("yes");
else System.out.println("no");
}
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long n;
static long d1,d2,k;
public static boolean fun()
{
if(n%3!=0)
return false;
for(int i=-1;i<=1;i++)
{
for(int j=-1;j<=1;j++)
{
if(i==0 || j==0)
continue;
long D1=d1*i;
long D2=d2*j;
long x2=(k-D1+D2)/3;
if((k-D1+D2)%3 !=0)
continue;
if(x2>=0 && x2<=k)
{
long x1=x2+D1;
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;
}
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int t=Integer.parseInt(br.readLine());
StringBuilder ans=new StringBuilder("");
while(t-->0)
{
st=new StringTokenizer(br.readLine());
n=Long.parseLong(st.nextToken());
k=Long.parseLong(st.nextToken());
d1=Long.parseLong(st.nextToken());
d2=Long.parseLong(st.nextToken());
if(fun())
ans.append("yes");
else ans.append("no");
ans.append("\n");
}
System.out.print(ans);
}
}
|
JAVA
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.util.*;
public class Main {
public static boolean Solved(long n, long k, long d1, long d2) {
if (n % 3 != 0)
return false;
// case1 > >
boolean flag = false;
while (!flag) {
long c = k - d1 - d2 - d2;
if (c < 0 || c % 3 != 0)
break;
c /= 3;
long b = c + d2;
long a = b + d1;
if (a < 0 || b < 0 || c < 0 || a > n / 3)
break;
flag = true;
}
;
// case2 < >
while (!flag) {
long b = k + d1 + d2;
if (b < 0 || b % 3 != 0)
break;
b /= 3;
long a = b - d1;
long c = b - d2;
if (a < 0 || b < 0 || c < 0 || b > n / 3)
break;
flag = true;
}
// case3 > <
while (!flag) {
long b = k - d1 - d2;
if (b < 0 || b % 3 != 0)
break;
b /= 3;
long a = b + d1;
long c = b + d2;
b = Math.max(a, c);
if (a < 0 || b < 0 || c < 0 || b > n / 3)
break;
flag = true;
}
// case4 < <
while (!flag) {
long a = k - d1 - d1 - d2;
if (a < 0 || a % 3 != 0)
break;
a /= 3;
long b = a + d1;
long c = b + d2;
if (a < 0 || b < 0 || c < 0 || c > n / 3)
break;
flag = true;
}
return flag;
}
public static void main(String[] argv) {
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
for (int nT = 0; nT < T; ++nT) {
long n = cin.nextLong();
long k = cin.nextLong();
long d1 = cin.nextLong();
long d2 = cin.nextLong();
System.out.println(Solved(n, k, d1, d2) ? "yes" : "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
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
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, InputReader in, PrintWriter out) {
long n = in.nextLong();
long k = in.nextLong();
long d1 = in.nextLong();
long d2 = in.nextLong();
int[][] coef = {{1, -1, 1, -1}, {1, 1, -1, -1}};
for (int i = 0; i < 4; i++) {
if (sol(n, k, 0, coef[0][i] * d1, coef[0][i] * d1 + coef[1][i] * d2)) {
out.println("yes");
return;
}
}
out.println("no");
}
long minCost(long score1, long score2, long score3) {
long maxScore = Math.max(score1, Math.max(score2, score3));
long res = 3 * maxScore - score1 - score2 - score3;
return res;
}
long minDone(long score1, long score2, long score3) {
long minScore = Math.min(score1, Math.min(score2, score3));
long res = score1 + score2 + score3 - 3 * minScore;
return res;
}
boolean sol(long n, long k, long score1, long score2, long score3) {
long costRem = n - k - minCost(score1, score2, score3);
long doneRem = k - minDone(score1, score2, score3);
if (costRem >= 0 && costRem % 3 == 0 && doneRem >= 0 && doneRem % 3 == 0) {
return true;
}
return false;
}
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer stt;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
return null;
}
}
public String next() {
while (stt == null || !stt.hasMoreTokens()) {
stt = new StringTokenizer(nextLine());
}
return stt.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;
bool ans;
long long n, k, d1, d2, tmp, a, b, c;
scanf("%d", &t);
while (t--) {
scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
if (n % 3 != 0) {
printf("no\n");
continue;
}
ans = false;
do {
tmp = k - 2 * d1 - d2;
if (tmp % 3 != 0) break;
a = tmp / 3;
b = a + d1;
c = a + d1 + d2;
if (a < 0 || b < 0 || c < 0) break;
if (max(a, max(b, c)) <= n / 3) ans = true;
} while (false);
do {
tmp = k - 2 * d1 + d2;
if (tmp % 3 != 0) break;
a = tmp / 3;
b = a + d1;
c = a + d1 - d2;
if (a < 0 || b < 0 || c < 0) break;
if (max(a, max(b, c)) <= n / 3) ans = true;
} while (false);
do {
tmp = k + 2 * d1 - d2;
if (tmp % 3 != 0) break;
a = tmp / 3;
b = a - d1;
c = a - d1 + d2;
if (a < 0 || b < 0 || c < 0) break;
if (max(a, max(b, c)) <= n / 3) ans = true;
} while (false);
do {
tmp = k + 2 * d1 + d2;
if (tmp % 3 != 0) break;
a = tmp / 3;
b = a - d1;
c = a - d1 - d2;
if (a < 0 || b < 0 || c < 0) break;
if (max(a, max(b, c)) <= n / 3) ans = true;
} while (false);
if (ans) {
printf("yes\n");
} else {
printf("no\n");
}
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Codeforces451C
{
public static void main(String[] args)
{
try
{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(f.readLine());
for(int i = 0; i < t; i++)
{
StringTokenizer st = new StringTokenizer(f.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());
if(n%3 != 0)
System.out.println("no");
else
{
switch(1)
{
case 1:
{
long a = k - d1 - 2*d2;
long b = n - k - 2*d1 - d2;
if(a >= 0 && a%3 == 0 && b >= 0 && b%3 == 0)
{
System.out.println("yes");
break;
}
}
case 2:
{
long a = k - d1 - d2;
if(a >= 0 && a%3 == 0)
{
long b1 = n-k+d1-2*d2;
long b2 = n-k-2*d1+d2;
if(d2 > d1 && b1 >= 0 && b1%3 == 0)
{
System.out.println("yes");
break;
}
else if(d2 <= d1 && b2 >= 0 && b2%3 == 0)
{
System.out.println("yes");
break;
}
}
}
case 3:
{
long a;
if(d1 > d2)
{
a = k - 2*d1 + d2;
}
else
{
a = k + d1 - 2*d2;
}
long b = n - k - d1 - d2;
if(a >= 0 && a%3 == 0 && b >= 0 && b%3 == 0)
{
System.out.println("yes");
break;
}
}
case 4:
{
long a = k - d2 - 2*d1;
long b = n - k - 2*d2 - d1;
if(a >= 0 && a%3 == 0 && b >= 0 && b%3 == 0)
{
System.out.println("yes");
break;
}
}
default:
{
System.out.println("no");
}
}
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
|
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.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
public class Main{
// ArrayList<Integer> lis = new ArrayList<Integer>();
// ArrayList<String> lis = new ArrayList<String>();
// PriorityQueue<P> que = new PriorityQueue<P>();
// PriorityQueue<Integer> que = new PriorityQueue<Integer>();
// Stack<Integer> que = new Stack<Integer>();
// static long sum=0;
// 1000000007 (10^9+7)
static int mod = 1000000007;
//static int mod = 1000000009,r=0; ArrayList<Integer> l[]= new ArrayList[n];
// static int dx[]={1,-1,0,0};
// static int dy[]={0,0,1,-1};
// static int dx[]={1,-1,0,0,1,1,-1,-1};
// static int dy[]={0,0,1,-1,1,-1,1,-1};
//static Set<Integer> set = new HashSet<Integer>();
public static void main(String[] args) throws Exception, IOException{
//String line=""; throws Exception, IOException
//(line=br.readLine())!=null
//Scanner sc =new Scanner(System.in);
// !!caution!! int long //
Reader sc = new Reader(System.in);
// int n=0;
//,a=sc.nextInt(),b=sc.nextInt();
// int a=sc.nextInt(),b=sc.nextInt();
int T=sc.nextInt();
lp: for(int t=0;t<T;t++){
long n=sc.nextLong(),k=sc.nextLong(),d1=sc.nextLong(),d2=sc.nextLong();
if( n%3!=0 ){ System.out.println("no"); continue; }
long d[]={ d1+d2,d1-d2,-d1+d2,-d1-d2 };
long x[]={ 1,1,-1,-1 };
long y[]={ 1,-1,1,-1 };
for(int i=0;i<4;i++){
// if ( d[i]<0 )continue;
if( ( k-d[i] )%3==0 ){
long w=( k-d[i] )/3;
long ww= w+d1*x[i];
long www= w+d2*y[i];
if( w<0 || ww<0 || www<0 )continue;
if(w<=n/3 && ww<=n/3 && www<=n/3 ){ System.out.println("yes"); continue lp;}
}
}
System.out.println("no");
// w1+w2+w2==k wi<=n/3
}
}
static void db(Object... os){
System.err.println(Arrays.deepToString(os));
}
}
class P implements Comparable<P>{
// implements Comparable<Pair>
int id; long d;
P(int id,long d){
this.id=id;
this.d=d;
}
public int compareTo(P x){
return 0<=d-x.d?1:-1 ; //ascend
}
}
/*
class Pair implements Comparable<Pair>{
// implements Comparable<Pair>
int a,b;
Pair(int a,int b){
this.a=a;
this.b=b;
}
public int compareTo(Pair x){
return a-x.a; //descend
}
}*/
class Reader
{
private BufferedReader x;
private StringTokenizer st;
public Reader(InputStream in)
{
x = new BufferedReader(new InputStreamReader(in));
st = null;
}
public String nextString() throws IOException
{
while( st==null || !st.hasMoreTokens() )
st = new StringTokenizer(x.readLine());
return st.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(nextString());
}
public long nextLong() throws IOException
{
return Long.parseLong(nextString());
}
public double nextDouble() throws IOException
{
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.io.*;
import java.util.*;
public class Main {
static InputReader in = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
static int MOD = 1000000007;
static int M = 505;
static int oo = Integer.MAX_VALUE;
static int[] di = {-1, 1, 0, 0};
static int[] dj = {0, 0, -1, 1};
public static void main(String[] args) throws IOException {
int t = in.nextInt();
while(t --> 0) {
long n = in.nextLong();
long k = in.nextLong();
long d1 = in.nextLong();
long d2 = in.nextLong();
if( possible(n, k, d1, d2)
|| possible(n, k, -d1, d2)
|| possible(n, k, d1, -d2)
|| possible(n, k, -d1, -d2)
) {
out.println("yes");
}
else {
out.println("no");
}
}
out.close();
}
static boolean possible(long n, long k, long d1, long d2) {
long b = k - d1 - d2;
if(b < 0 || b % 3 != 0)
return false;
b /= 3;
long a = b + d1;
long c = b + d2;
if(a < 0 || c < 0)
return false;
long w = n / 3;
if(a > w || b > w || c > w)
return false;
return w - a + w - b + w - c == n - k;
}
static void shuffle(int[] a) {
Random r = new Random();
for(int i = a.length - 1; i > 0; --i) {
int si = r.nextInt(i);
int t = a[si];
a[si] = a[i];
a[i] = t;
}
}
static void shuffle(long[] a) {
Random r = new Random();
for(int i = a.length - 1; i > 0; --i) {
int si = r.nextInt(i);
long t = a[si];
a[si] = a[i];
a[i] = t;
}
}
static int lower_bound(int[] a, int n, int k) {
int s = 0;
int e = n;
int m;
while (e - s > 0) {
m = (s + e) / 2;
if (a[m] < k)
s = m + 1;
else
e = m;
}
return e;
}
static int lower_bound(long[] a, int n, long k) {
int s = 0;
int e = n;
int m;
while (e - s > 0) {
m = (s + e) / 2;
if (a[m] < k)
s = m + 1;
else
e = m;
}
return e;
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static class Pair implements Comparable<Pair> {
int first, second;
public Pair(int first, int second) {
super();
this.first = first;
this.second = second;
}
@Override
public int compareTo(Pair o) {
return this.first != o.first ? this.first - o.first : this.second - o.second;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + first;
result = prime * result + second;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (first != other.first)
return false;
if (second != other.second)
return false;
return true;
}
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
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 = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
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 {
res *= 10;
res += c - '0';
c = read();
} 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 = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
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 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;
const int INF = 1e+9;
const double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;
int main() {
FILE* ci;
FILE* co;
int t = 0;
cin >> t;
long long n, k, d1, d2;
long long A, B, C, pre, h;
for (int i = 0; i < t; i++) {
cin >> n >> k >> d1 >> d2;
if (n % 3 != 0) {
cout << "no" << endl;
continue;
}
pre = k - d1 + d2;
if (pre % 3 == 0) {
B = pre / 3;
A = d1 + B;
C = B - d2;
if (pre % 3 == 0 && A >= 0 && A <= k && B >= 0 && B <= k && C >= 0 &&
C <= k && A >= B && B >= C && A <= n / 3 && B <= n / 3 &&
C <= n / 3) {
cout << "yes" << endl;
continue;
}
}
pre = k - d1 - d2;
if (pre % 3 == 0) {
B = pre / 3;
A = d1 + B;
C = B + d2;
if (pre % 3 == 0 && A >= 0 && A <= k && B >= 0 && B <= k && C >= 0 &&
C <= k && A >= B && B < C && A <= n / 3 && B <= n / 3 && C <= n / 3) {
cout << "yes" << endl;
continue;
}
}
pre = k + d1 + d2;
if (pre % 3 == 0) {
B = pre / 3;
A = B - d1;
C = B - d2;
if (pre % 3 == 0 && A >= 0 && A <= k && B >= 0 && B <= k && C >= 0 &&
C <= k && A < B && B >= C && A <= n / 3 && B <= n / 3 && C <= n / 3) {
cout << "yes" << endl;
continue;
}
}
pre = k + d1 - d2;
if (pre % 3 == 0) {
B = pre / 3;
A = B - d1;
C = B + d2;
if (pre % 3 == 0 && A >= 0 && A <= k && B >= 0 && B <= k && C >= 0 &&
C <= k && A < B && B < C && A <= n / 3 && B <= n / 3 && C <= n / 3) {
cout << "yes" << endl;
continue;
}
}
cout << "no" << endl;
}
return 0;
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
long long n, k, d1, d2, x1, x2, x3, sum = 0;
cin >> n >> k >> d1 >> d2;
if ((k - d1 - d2) % 3 == 0) {
x1 = (k - d1 - d2) / 3;
x2 = x1 + d1;
x3 = x1 + d2;
if (x1 >= 0 && x2 >= 0 && x3 >= 0) {
long long max_win = max(x1, max(x2, x3));
sum = 3 * max_win - x1 - x2 - x3;
if (sum >= 0 && sum <= n - k && (n - k - sum) % 3 == 0) {
puts("yes");
continue;
}
}
}
if ((k - d1 + d2) % 3 == 0) {
x1 = (k - d1 + d2) / 3;
x2 = x1 + d1;
x3 = x1 - d2;
if (x1 >= 0 && x2 >= 0 && x3 >= 0) {
long long max_win = max(x1, max(x2, x3));
sum = 3 * max_win - x1 - x2 - x3;
if (sum >= 0 && sum <= n - k && (n - k - sum) % 3 == 0) {
puts("yes");
continue;
}
}
}
if ((k + d1 - d2) % 3 == 0) {
x1 = (k + d1 - d2) / 3;
x2 = x1 - d1;
x3 = x1 + d2;
if (x1 >= 0 && x2 >= 0 && x3 >= 0) {
long long max_win = max(x1, max(x2, x3));
sum = 3 * max_win - x1 - x2 - x3;
if (sum >= 0 && sum <= n - k && (n - k - sum) % 3 == 0) {
puts("yes");
continue;
}
}
}
if ((k + d1 + d2) % 3 == 0) {
x1 = (k + d1 + d2) / 3;
x2 = x1 - d1;
x3 = x1 - d2;
if (x1 >= 0 && x2 >= 0 && x3 >= 0) {
long long max_win = max(x1, max(x2, x3));
sum = 3 * max_win - x1 - x2 - x3;
if (sum >= 0 && sum <= n - k && (n - k - sum) % 3 == 0) {
puts("yes");
continue;
}
}
}
puts("no");
}
}
|
CPP
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main {
FastIO io;
long n, k;
boolean check(long a, long b, long c) {
long s = a + b + c;
if (s > k || (k - s) % 3 != 0) {
return false;
}
long d = (k - s) / 3;
a += d;
b += d;
c += d;
if (a < 0 || b < 0 || c < 0) {
return false;
}
s += 3 * d;
long m = max(a, max(b, c));
s = 3 * m - s;
return k + s <= n && (n - k - s) % 3 == 0;
}
// File names!!!
void solve() throws Exception {
for (int tt = io.nextInt(); tt > 0; tt--) {
n = io.nextLong();
k = io.nextLong();
long d1 = io.nextLong();
long d2 = io.nextLong();
boolean ans = false;
ans |= check(0, d1, d1 + d2);
ans |= check(0, d1, d1 - d2);
ans |= check(0, -d1, -d1 + d2);
ans |= check(0, -d1, -d1 - d2);
io.println(ans ? "yes" : "no");
}
}
void run() {
try {
io = new FastIO();
solve();
io.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(abs(-1));
}
}
public static void main(String[] args) {
try {
Locale.setDefault(Locale.US);
} catch (Exception ignore) {
}
new Main().run();
}
class FastIO extends PrintWriter {
private BufferedReader in;
private StringTokenizer stok;
FastIO() {
super(System.out);
in = new BufferedReader(new InputStreamReader(System.in));
}
FastIO(String s) throws FileNotFoundException {
super("".equals(s) ? "output.txt" : s + ".out");
in = new BufferedReader(new FileReader("".equals(s) ? "input.txt" : s + ".in"));
}
@Override
public void close() {
super.close();
try {
in.close();
} catch (IOException ignored) {
}
}
String next() {
while (stok == null || !stok.hasMoreTokens()) {
try {
stok = new StringTokenizer(in.readLine());
} catch (Exception e) {
return null;
}
}
return stok.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
try {
return (char) in.read();
} catch (IOException e) {
return (char) -1;
}
}
String nextLine() {
try {
return in.readLine();
} catch (IOException e) {
return null;
}
}
char[] nextCharArray() {
return next().toCharArray();
}
}
void shuffle(int[] a) {
Random rand = new Random();
for (int i = 1; i < a.length; i++) {
int x = rand.nextInt(i + 1);
int chg = a[i];
a[i] = a[x];
a[x] = chg;
}
}
class IntArray {
private int capacity;
private int[] data;
private int size = 0;
IntArray(int capacity) {
this.capacity = capacity;
this.data = new int[capacity];
}
IntArray() {
this(16);
}
int get(int index) {
return data[index];
}
void add(int x) {
if (size == capacity) {
int[] newData = new int[capacity * 2];
System.arraycopy(data, 0, newData, 0, capacity);
data = newData;
capacity *= 2;
}
data[size++] = x;
}
}
}
|
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.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B {
static boolean first(long n, long k, long d1, long d2) {
if ((k - d1 - 2 * d2)% 3L != 0L || (k - d1 + d2) % 3L != 0L)
return false;
long d = n / 3L;
long c = (k - d1 - 2 * d2) / 3L;
long b = (k - d1 + d2) / 3L;
long a = k - b - c;
long r = n - k;
if (a < 0L || b < 0L || c < 0L || a > d || b > d || c > d)
return false;
if ((r + a + b + c) % 3L != 0L)
return false;
return true;
}
static boolean second(long n, long k, long d1, long d2) {
if ((k - d1 + 2 * d2) % 3L != 0L || (k - d1 - d2) % 3L != 0L)
return false;
long d = n / 3L;
long c = (k - d1 + 2 * d2) / 3L;
long b = (k - d1 - d2) / 3L;
long a = k - b - c;
long r = n - k;
if (a < 0L || b < 0L || c < 0L || a > d || b > d || c > d)
return false;
if ((r + a + b + c) % 3L != 0L)
return false;
return true;
}
static boolean third(long n, long k, long d1, long d2) {
if ((k - 2 * d2 + d1) % 3L != 0L || (k + d1 + d2) % 3L != 0L)
return false;
long d = n / 3L;
long c = (k - 2 * d2 + d1) / 3L;
long b = (k + d1 + d2) / 3L;
long a = k - b - c;
long r = n - k;
if (a < 0L || b < 0L || c < 0L || a > d || b > d || c > d)
return false;
if ((r + a + b + c) % 3L != 0L)
return false;
return true;
}
static boolean fourth(long n, long k, long d1, long d2) {
if ((k + d1 + 2 * d2) % 3L != 0L || (k + d1 - d2) % 3L != 0L)
return false;
long d = n / 3L;
long c = (k + d1 + 2 * d2) / 3L;
long b = (k + d1 - d2) / 3L;
long a = k - b - c;
long r = n - k;
if (a < 0L || b < 0L || c < 0L || a > d || b > d || c > d)
return false;
if ((r + a + b + c) % 3L != 0L)
return false;
return true;
}
public static void main(String[] args) throws IOException {
File inputFile = new File("entradaB");
if (inputFile.exists())
System.setIn(new FileInputStream(inputFile));
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder out = new StringBuilder();
String line = "";
while ((line = in.readLine()) != null) {
int nCases = Integer.parseInt(line.trim());
for (int nCase = 0; nCase < nCases; nCase++) {
long[] v = readLongs(in.readLine());
long n = v[0], k = v[1], d1 = v[2], d2 = v[3];
if (n % 3L != 0L)
out.append("no\n");
else {
if (first(n, k, d1, d2) || second(n, k, d1, d2) || third(n, k, d1, d2) || fourth(n, k, d1, d2))
out.append("yes\n");
else
out.append("no\n");
}
// First
}
}
System.out.print(out);
}
static int[] readInts(String line) {
StringTokenizer st = new StringTokenizer(line.trim());
int a[] = new int[st.countTokens()], index = 0;
while (st.hasMoreTokens())
a[index++] = Integer.parseInt(st.nextToken());
return a;
}
static long[] readLongs(String line) {
StringTokenizer st = new StringTokenizer(line.trim());
long a[] = new long[st.countTokens()];
int index = 0;
while (st.hasMoreTokens())
a[index++] = Long.parseLong(st.nextToken());
return a;
}
static double[] readDoubles(String line) {
StringTokenizer st = new StringTokenizer(line.trim());
double a[] = new double[st.countTokens()];
int index = 0;
while (st.hasMoreTokens())
a[index++] = Double.parseDouble(st.nextToken());
return a;
}
}
|
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
|
for t in xrange(input()):
n, k, d1, d2 = map(int, raw_input().split())
if n % 3: print "no"
else:
p = 0
for d1 in [-d1,d1]:
for d2 in [-d2,d2]:
x2 = (k-d1+d2)/3
if (k-d1+d2) % 3: continue
if x2 >= 0 and x2 <= k:
x1 = d1 + x2
x3 = x2 - d2
if x1 >= 0 and x1 <= k and x3 >= 0 and x3 <= k:
if x1 <= n/3 and x2 <= n/3 and x3 <= n/3:
p = 1
if p: print "yes"
else: print "no"
|
PYTHON
|
451_C. Predict Outcome of the Game
|
There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.
You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.
You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 β€ t β€ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 β€ n β€ 1012; 0 β€ k β€ n; 0 β€ d1, d2 β€ k) β data for the current test case.
Output
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Examples
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note
Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).
| 2
| 9
|
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
public class a {
public static class node implements Comparable<node> {
int index;
int val;
node(int index, int val) {
this.index = index;
this.val = val;
}
@Override
public int compareTo(node o) {
if (o.val < val)
return 1;
else if (o.val > val)
return -1;
else if (o.index < index)
return 1;
else
return -1;
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder q = new StringBuilder();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
//System.out.println(333333333326L - 258442058725L);
int f = Integer.parseInt(in.readLine());
for (int i = 0; i < f; i++) {
String y[] = in.readLine().split(" ");
long n = Long.parseLong(y[0]);
long k = Long.parseLong(y[1]);
long d1 = Long.parseLong(y[2]);
long d2 = Long.parseLong(y[3]);
if (n % 3 != 0) {
out.println("no");
continue;
}
long ff = n / 3;
// System.out.println(ff);
boolean vis[] = new boolean[5];
long a[][] = new long[5][5];
long one = k - d1 + d2;
long two = k - d1 - d2;
long three = k + d1 - d2;
long four = k + d1 + d2;
if (one % 3 == 0) {
vis[1] = true;
a[1][2] = one / 3;
a[1][1] = d1 + a[1][2];
a[1][3] = a[1][2] - d2;
}
if (two % 3 == 0) {
vis[2] = true;
a[2][2] = two / 3;
a[2][1] = d1 + a[2][2];
a[2][3] = d2 + a[2][2];
}
if (three % 3 == 0) {
vis[3] = true;
a[3][2] = three / 3;
a[3][1] = a[3][2] - d1;
a[3][3] = d2 + a[3][2];
}
if (four % 3 == 0) {
vis[4] = true;
a[4][2] = four / 3;
a[4][1] = a[4][2] - d1;
a[4][3] = a[4][2] - d2;
}
boolean flag = false;
for (int j = 1; j < 5; j++) {
if (vis[j]) {
if (a[j][1] >= 0 && a[j][1] <= ff && a[j][2] >= 0
&& a[j][2] <= ff && a[j][3] >= 0 && a[j][3] <= ff) {
// System.out.println(j);
//System.out.println(a[j][1] + " " + a[j][2] + " "
// + a[j][3]);
flag = true;
}
}
}
if (flag)
out.println("yes");
else
out.println("no");
}
out.close();
}
}
|
JAVA
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.