F . F. F.
题意: 给定 a , b a,b a,b,求出满足 c d − e f = a b \frac{c}{d}-\frac{e}{f}=\frac{a}{b} dc−fe=ba的任意一组 c , d , e , f c,d,e,f c,d,e,f。
数据范围: a , b ≤ 2 × 1 0 6 a,b\leq 2\times 10^6 a,b≤2×106, 1 ≤ d < b , f < b 1\leq d1≤d<b,f<b,且 1 ≤ c , e ≤ 4 × 1 0 12 1\leq c,e \leq 4\times 10^{12} 1≤c,e≤4×1012
题解: 通分得到: c f − d e d f = a b \frac{cf-de}{df}=\frac{a}{b} dfcf−de=ba,那么自然会考虑到分子分母对应相等解决。
#include
using namespace std;
typedef long long ll;
const int N = 2e6 + 10;
int pri[N], cnt;
int Mipri[N];
bool st[N];
void xs(int n) {
st[0] = st[1] = true;
Mipri[1] = 1;
for(int i = 2; i <= n; i++) {
if(!st[i]) pri[cnt++] = i, Mipri[i] = i;
for(int j = 0; j < cnt && 1ll * i * pri[j] <= n; j++) {
Mipri[i * pri[j]] = pri[j];
st[i * pri[j]] = true;
if(i % pri[j] == 0) break;
}
}
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
void exgcd(ll a, ll b, ll &x, ll &y) {
if(!b) {
x = 1, y = 0;
return ;
}
exgcd(b, a % b, y, x);
y -= a / b * x;
}
int main()
{
xs(N - 1);
int T; scanf("%d", &T);
while(T--) {
int a, b;
scanf("%d%d", &a, &b);
int g = gcd(a, b);
if(g > 1) {
int f = 1, e = 1, d = b / g, c = (a + b) / g;
printf("%d %d %d %d\n", c, d, e, f);
continue;
}
ll f = 1, d = b, p = Mipri[b];
while(p > 1 && d % p == 0) d /= p, f *= p;
if(d == 1) {
puts("-1 -1 -1 -1");
continue;
}
ll c, e;
exgcd(d, f, e, c);
e = -e;
if(e <= 0 || c <= 0) {
ll e1 = (e % f + f) % f;
ll c1 = (c % d + d) % d;
ll cnt = max(0ll, max((e1 - e) / f, (c1 - c) / d));
e += f * cnt, c += d * cnt;
}
e *= a, c *= a;
printf("%lld %lld %lld %lld\n", c, d, e, f);
}
return 0;
}