Codeforces round #258 (div2) C solution report

Source: Internet
Author: User

C. predict outcome of the gametime limit per Test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

There areNGames in a football tournament. Three teams are participating in it. CurrentlyKGames had already been played.

You are an avid football fan, but recently you missed the wholeKGames. Fortunately, you remember a guess of your friend for theseKGames. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will beD1 and that of between second and third team will beD2.

You don't want any of team win the tournament, that is each team shoshould have the same number of wins afterNGames. That's why you want to know: Does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?

Note that outcome of a match can not be a draw, it has to be either win or loss.

Input

The first line of the input contains a single integer corresponding to number of test casesT(1? ≤?T? ≤? 105 ).

Each of the nextTLines will contain four space-separated IntegersN,?K,?D1 ,?D2 (1? ≤?N? ≤? 1012; 0? ≤?K? ≤?N; 0? ≤?D1 ,?D2? ≤?K)-Data for the current test case.

Output

For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes ).

Sample test (s) Input
53 0 0 03 3 0 06 4 1 06 3 3 03 3 3 2
Output
yesyesyesnono
Note

Sample 1. There has not been any match up to now (K? =? 0 ,?D1? =? 0 ,?D2? =? 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.

Sample 2. You missed all the games (K? =? 3).D1? =? 0 andD2? =? 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes ".

Sample 3. You missed 4 matches, andD1? =? 1 ,?D2? =? 0. these four matches can be: 1-2 (WIN 2), 1-3 (Win 3), 1-2 (win 1), 1-3 (win 1 ). currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. two remaining matches can be: 1-2 (WIN 2), 1-3 (Win 3 ). in the end all the teams have equal number of WINS (2 wins ).

Question:

In N games, you missed K games and then missed the length of K. The difference between a team and a team is D1, and the difference between a team and a team is D2. If possible, if the three teams win the same number of times, yes is output; otherwise, no is output.

Solution:

We can imagine three columns as interesting and complex simulation questions. We know the absolute values of the height difference between the first column and the second column, the absolute value of the height difference between the second pillar and the third pillar. Now there are n-k bricks (1 height), which requires that the height of the three pillars be the same.

First, we have given D1 and D2 The following basic conditions:

1. D1, 0, D2;

2. 0, D1, d1 + D2;

3. 0, D2, d1 + D2;

4. 0, D1, d1-d2;

0, D2, d2-d1;

Where 4 and 5 these two cases, depending on whether the d1-d2 is positive number, in fact two is a situation = _ = #.

Then, according to the five cases, for each case, x, y, z must be greater than or equal to 0, and X + Y + z <= K & (k-x-y-z) % 3 = 0. Here we want to ensure that the enumeration meets the basic requirements: the number of matches for the three teams is K.

Next, we need to ensure that (n-k) % 3 = 0 & N/3> = x, y, z ).

Code:

#include <iostream>#include <cstdio>#define LL long longusing namespace std;LL n, k, d1, d2;bool check(LL x, LL y, LL z) {if (x < 0 || y < 0 || z < 0)  return false;if (x + y + z > k)  return false;if ((k-(x+y+z))%3 != 0)  return false;LL tmp = x+y+z+(n-k);LL dv = tmp/3;LL md = tmp%3;if (md == 0 && x <= dv && y <= dv && z <= dv)return true;elsereturn false;}void solve() {cin >> n >> k >> d1 >> d2;if (check(d1, 0, d2))printf("yes\n");else if (check(0, d1, d1+d2))printf("yes\n");else if (check(0, d2, d1+d2))printf("yes\n");else if (check(0, d1, d1-d2))printf("yes\n");else if (check(0, d2, d2-d1))printf("yes\n");elseprintf("no\n");}int main() {int tcase;cin >> tcase;while (tcase--) {solve();}}

Codeforces round #258 (div2) C solution report

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.