Codeforces Round #657 (Div. 2) B. Dubious Cyrpto(数论)

题目链接:https://codeforces.com/contest/1379/problem/B

题意

给出三个正整数 $l,r,m$,判断在区间 $[l,r]$ 内是否有 $a,b,c$ 满足存在正整数 $n$,使得 $n cdot a + b - c = m$ 。

题解

最容易想的一种情况是:

egin{equation} {lfloor frac{m}{a} floor} cdot a + m \% a = m  end{equation}

令 $b = l + m \% a, c = l$ 即可。

但是当 $m<a$ 时,${lfloor frac{m}{a} floor} = n = 0$,不满足 $n$ 为正整数的要求,或者 $m \% a$ 较大,超出了 $[l,r]$ 区间,此时可以将原式变换为:

egin{equation} {lfloor frac{m}{a} floor} cdot a + a - (a - m \% a) = m onumber end{equation}

即,

egin{equation} ({lfloor frac{m}{a} floor} + 1) cdot a - (a - m \% a) = m end{equation}

因为是减去一个数,为了尽量不超出区间,令 $b = r - (a - m \% a), c = r$,并且此时 $n$ 一定 $ge 1$ 。

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std;

void solve() {
    ll l, r, m; cin >> l >> r >> m;
    for (ll a = l; a <= r; ++a) {
        ll b = l + m % a;
        ll c = l;
        if (m / a > 0 and l <= b and b <= r) {
            cout << a << ' ' << b << ' ' << c << "
";
            return;
        }
        b = r - (a - m % a);
        c = r;
        if (l <= b and b <= r) {
            cout << a << ' ' << b << ' ' << c << "
";
            return;
        }
    }
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}
原文地址:https://www.cnblogs.com/Kanoon/p/13345796.html