Predict Outcome of the Game CodeForces - 451C (分情况讨论)

19 篇文章 0 订阅
8 篇文章 0 订阅

There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.

You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.

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

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

Input

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

Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case.

Output

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

Example
Input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
Output
yes
yes
yes
no
no
Note

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

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

Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).

absolute difference:中文意思是绝对值之差,比赛的时候题意看错了。

题意是这样的:三个队(a,b,c)举行n场比赛,k场已经结束,且(|a-b|=d1,|b-c|=d2)(a,b,c分别代表每队赢的次数)(而比赛的时候就认为a-b=d1,b-c=d2,把题目简单化了)问是否可能存在一种情况使得三个队赢的场数都一样,如果存在,输出yes否则no。

解:根据|a-b|=d1,|b-c|=d2可分别得出四个式子:1、a-b=d1       2、b-a=d1,     3、b-c=d2       4、c-b=d2,  然后四个式子两两联立(1-3 ,1-4 ,2-3 ,2-4联立)得到四种情况,可分别求出a,b,c的值,然后判断a,b,c都必须大于等于0,因为它们代表的是三个队赢的次数,不能为负,还有就是他们三个的最大值不能大于n/3,否则就无法让三队赢得一样的场数了。开始可以先判断n是否是3的倍数。详细看代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
using namespace std;
typedef long long LL;
LL n,k,d1,d2,a,b,c,tmp;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d%I64d%I64d%I64d",&n,&k,&d1,&d2);
        if(n%3)
        {
            printf("no\n");
            continue;
        }
        tmp=n/3;
        c=(k-2*d2-d1)/3,b=c+d2,a=b+d1;//1-3 联立分别求出a,b,c
        //判断条件a,b,c中最小值大于等于0,最大值小于等于n/3
        //并且一定要判断一下(k-2*d2-d1)%3==0这个条件,因为题目给的k值不一定满足条件,这里卡了很久
        //下面的判断都是一个道理了
        if((k-2*d2-d1)%3==0&&c>=0&&a<=tmp)
        {
            printf("yes\n");
            continue;
        }
        b=(k-d1-d2)/3,a=b+d1,c=b+d2;//1-4联立
        if((k-d1-d2)%3==0&&b>=0&&a<=tmp&&c<=tmp)
        {
            printf("yes\n");
            continue;
        }
        c=(k+d1-2*d2)/3,b=c+d2,a=c+d2-d1;//2-3联立
        if((k+d1-2*d2)%3==0&&c>=0&&a>=0&&b<=tmp&&a<=tmp)
        {
            printf("yes\n");
            continue;
        }
        a=(k-2*d1-d2)/3,b=a+d1,c=a+d1+d2;//2-4联立
        if((k-2*d1-d2)%3==0&&a>=0&&c<=tmp)
        {
            printf("yes\n");
        }
        else printf("no\n");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值