1 条题解

  • 0

    题意分析

    题目要求在超级碗比赛期间,当地的黑客组织了一个赌局,赌的是比赛的两个最终得分的和(ss)以及它们的绝对差(dd)。给定这两个数字 ssdd,我们需要推断出这两个得分。如果不存在这样的得分,则输出“impossible”。

    解题思路

    1. 数学推导:设两个得分分别为 xxyy,其中 xyx \geq y。根据题意,有:

      • x+y=sx + y = s
      • xy=dx - y = d
    2. 解方程组:将这两个方程联立,可以解得:

      • x=s+d2x = \frac{s + d}{2}
      • y=sd2y = \frac{s - d}{2}
    3. 合法性检查:为了确保 xxyy 是有效的得分,必须满足以下条件:

      • xxyy 都是非负整数。
      • xyx \geq y(即 d0d \geq 0)。
      • sds \geq d(因为 x+yxyx + y \geq x - y)。

    实现步骤

    1. 输入处理:读取输入的测试用例数量 nn,然后逐个处理每个测试用例的 ssdd
    2. 计算得分:对于每个测试用例,计算 xxyy
    3. 合法性验证
      • 检查 sds \geq dsds - d 是偶数(确保 xxyy 是整数)。
      • 检查 xxyy 是否为非负整数。
    4. 输出结果:如果合法,输出 xxyy;否则,输出“impossible”。

    代码实现

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<vector>
    #include<set>
    #include<string>
    #include<cmath>
    #include<cstring>
    #define ll long long
    #define pb push_back
    #define pm make_pair
    #define fi first
    #define se second
    using namespace std;
    const int MAX = 2e5 + 5;
    ll a,b;
    int main()
    {
    	int t;
    	cin>>t;
    	while(t--) {
    		scanf("%lld%lld",&a,&b);
    		if(a<b) {
    			puts("impossible");continue;
    		}
    		ll ans1 = (a+b)>>1;
    		if(ans1*2 != (a+b)) {
    			puts("impossible");continue;
    		}
    		ll ans2 = a-ans1;
    		if(ans1 < ans2) swap(ans1,ans2);
    		printf("%lld %lld\n",ans1,ans2);
    	}
    	return 0 ;
     }
    • 1

    信息

    ID
    1302
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    3
    已通过
    2
    上传者