跳到正文

题解

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

_zylenox_约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年5月30日
收录
2026年7月4日
更新
2026年11月17日
篇幅
约 2 分钟
获赞
1
题目
P16672

版本

  1. v1当前

题目

求关于 cc 的方程 ∣x−ac−bc∣=t|x-ac-bc|=t 的非负整数解个数

思路

带绝对值的一元一次方程最多两个解,求

  • x−ac−bc=tx-ac-bc=t
  • x−ac−bc=−tx-ac-bc=-t

两个解是否符合要求

注意,当 tt 为 00 时应该特判,否则 00 分
(不要问我为什么知道)

代码

C++
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
namespace zyll {
	ll x, a ,b ,t;
	int main() {
		ll T;
        cin >> T;
        while (T--) {
            cin >> x >> a >> b >> t;
            ll ans = 0;
            if (t == 0 && x % (a + b) == 0) ans++;
            else {
                if (x - t >= 0 && (x - t) % (a + b) == 0) ans++;
                if (x + t >= 0 && (x + t) % (a + b) == 0) ans++;
            }
            cout << ans << "\n";
        }
		return 0;
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	return zyll::main();
}
原文