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

· · 题解

题目

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

思路

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

两个解是否符合要求

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

代码

#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();
}