P10782 题解

· · 题解

思路

首先,如果有解,则 b 一定可以为 a 或 c,这里就以 b=a 为例,所以只要判有没有合法的 m 就行。显然若有解,则 m=\lfloor\frac ac\rfloor 一定可行,所以只要看 \lfloor\frac a{\lfloor\frac ac\rfloor}\rfloor=c 是否成立即可。

代码

# include <bits/stdc++.h>
# define I return
# define AK 0
# define IOI ;
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int t;
ll a, c;
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> t;
    while (t --) {
        cin >> a >> c;
        cout << (a < c || a / (a / c) != c ? -1 : a) << '\n';
    }
    I AK IOI
}