跳到正文

题解

题解:P16672 【MX-J30-T1】「FDOI-R1」擦肩而过

ycy25约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年6月14日
更新
2026年6月14日
篇幅
约 1 分钟
获赞
1
题目
P16672

版本

  1. v1当前

为什么会有 t=0t=0 这种情况啊啊啊!!!

思路

方程还是挺容易解的。

相当于已知 x−(a+b)c=±tx-(a+b)c=\pm t,求解 cc。

最后解出来 c=x±ta+bc=\large\frac{x\pm t}{a+b}。

分别判断 c1=x+ta+bc_1=\large\frac{x+t}{a+b} 与 c2=x−ta+bc_2=\large\frac{x-t}{a+b} 是否是非负整数即可。

你以为这样就完了吗?

打完代码提交上去,就会发现所有测试点全部 WA 了!

为什么呢?

我们发现,事实上 tt 可以等于 00!此时会出现 c1=c2c_1=c_2 的情况,这种特殊情况我们也需考虑。

代码

C++
#include<cstdio>
int main()
{
    int t1;
    scanf("%d",&t1);
    while(t1--)
    {
        int x,a,b,t;
        scanf("%d%d%d%d",&x,&a,&b,&t);
        int c1=(x+t)/(a+b),c2=(x-t)/(a+b);
        int ans=0;
        if(c1>=0&&(x+t)%(a+b)==0)ans++;
        if(c2>=0&&(x-t)%(a+b)==0)ans++;
        if(ans==2&&t==0)ans--;//处理t=0情况
        printf("%d\n",ans);
        return 0;
    }
}
原文