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$ 。

题解

最容易想的一种情况是:

\begin{equation} {\lfloor \frac{m}{a} \rfloor} \cdot a + m\ \%\ a = m  \end{equation}

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

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

\begin{equation} {\lfloor \frac{m}{a} \rfloor} \cdot a + a - (a - m\ \%\ a) = m \nonumber \end{equation}

即,

\begin{equation} ({\lfloor \frac{m}{a} \rfloor} + 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 << "\n";
            return;
        }
        b = r - (a - m % a);
        c = r;
        if (l <= b and b <= r) {
            cout << a << ' ' << b << ' ' << c << "\n";
            return;
        }
    }
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

 

posted @ 2020-07-20 16:45  Kanoon  阅读(231)  评论(0编辑  收藏  举报