2020牛客暑期多校训练营(第三场)——F

Fraction Construction Problem

题目描述

There are t queries.
In each query, you are given two positive integers a and b (a, b \le 2 \times 10^6a,b≤2×106).

Please print a line consists of four positive integers c,d,e,f satisfying the following constraints:

• \frac{c}{d} - \frac{e}{f} = \frac{a}{b}dc​−fe​=ba​
• d < b and f < b
• 1 \le c,e \le 4 \times 10^{12}1≤c,e≤4×1012

If there are multiple solutions, you can print anyone.

If there are no solution, please print “-1 -1 -1 -1” in this line.

输入描述:

The first line contains one integer t (1 \le t \le 10^51≤t≤105) — the number of test cases.

The only line of each test case contains two integers a and b (1 \le a, b \le 2 \times 10^61≤a,b≤2×106).

输出描述:

For each test case print four integers — c,d,e,f. If there are no solution, c,d,e,f should all be -1.

示例1

输入
3
4 1
1 6
37 111
输出
-1 -1 -1 -1
1 2 1 3
145 87 104 78
题意:很简单就不说了。
题解:
a,b不互质,分子分母约分一下,约分结果乘二减一下即可。
a,b互质,找到两个数满足,两数相乘等于b,且两数互质,然后利用拓展欧几里得解即可。

#include
using namespace std;
typedef long long ll;
int gcd(int a,int b) {
	return b?gcd(b,a%b):a;
}
int exgcd(int a,int b,ll &x,ll &y){
	if(b==0){
		x=1,y=0;
		return a;
	}
	int d=exgcd(b,a%b,x,y);
	ll z=x;
	x=y;
	y=z-y*1ll*(a/b);
	return d;
}
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int a,b;
		scanf("%d %d",&a,&b);
		int gc=gcd(a,b);
		if(gc!=1) {
			printf("%d %d %d %d\n",2*a/gc,b/gc,a/gc,b/gc);
		} else {
			int d=0,f=0;
			for(int i=2; i*i<=b; ++i) {
				if(b%i==0&&gcd(i,b/i)==1) {
					d=i;
					f=b/i;
					break;
				}
			}
			if(d==0&&f==0){
				printf("-1 -1 -1 -1\n");
			}else{
				ll c,e;
				int x=exgcd(f,d,c,e);
				c*=1ll*a,e*=1ll*a;
				if(c>0&&e<0){
					printf("%lld %d %lld %d\n",c,d,-e,f);
				}else{
					printf("%lld %d %lld %d\n",e,f,-c,d);
				}
			}
		}
	}
	return 0;
}

你可能感兴趣的:(数论与数学)