题目连接:https://ac.nowcoder.com/acm/contest/5668/Fhtml
题目描述
There are t queries.
In each query, you are given two positive integers
and
Please print a line consists of four positive integers
satisfying the following constraints:java
If there are multiple solutions, you can print anyone.ios
If there are no solution, please print “-1 -1 -1 -1” in this line.web
输入描述
The first line contains one integer
— the number of test cases.算法
The only line of each test case contains two integers and 数组
输出描述app
For each test case print four integers — . If there are no solution, should all be .svg
输入
this
输出
spa
在d,f找到以后就开始找c和e了。
已知d*f = b,通分后可得 (d、f、a都知道,求c、e),求解c和e,即用扩展欧几里得算法。
注意:求出c和e以后判断一下谁正谁负,毕竟d和f和b的两个相异质因数是不知道对应谁的,能够经过c和e的正负性判断。
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pdd; #define INF 0x7f7f7f #define mem(a,b) memset(a , b , sizeof(a)) #define FOR(i, x, n) for(int i = x;i <= n; i++) // const ll mod = 1e9 + 7; const int maxn = 5e6 + 10; // const double eps = 1e-6; int prime[1005]; // 素数数组 bool is_prime[maxn]; // is_prime[i]表示i为素数 int p; void sieve() // 返回n之内的素数个数 { int n = maxn; mem(is_prime, true); is_prime[0] = is_prime[1] = false; is_prime[2] = true; for(int i = 2;i * i <= n; i++) { if(is_prime[i]) { prime[++p] = i; for(int j = 2 * i;j <= n; j += i) { is_prime[j] = false; } } } } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } ll ex_gcd(ll a, ll b, ll &x, ll &y) { ll res, t; if(!b) { x = 1; y = 0; return a; } res = ex_gcd(b, a % b, x, y); t = x; x = y; y = t - (a / b) * y; return res; } ll solve_ex_gcd(ll a, ll b, ll c, ll &x, ll &y) { ll inv = ex_gcd(a, b, x, y); if(c % inv) { x = -1; y = -1; return -1; } x *= (c / inv); y *= (c / inv); return 0; } void solve() { ll a, b; cin >> a >> b; ll k = gcd(a, b); if(k != 1) { a /= k; b /= k; cout << a + 1 << " " << b << " " << 1 << " " << b << endl; return ; } if(b == 1 || is_prime[b]) cout << "-1 -1 -1 -1" << endl; else { ll tmp = 0, time = 0; ll bb = b; for(int i = 1;i <= p && bb != 1; i++) // 找两个素因数 { if(bb % prime[i] == 0) { tmp = prime[i]; while(bb % prime[i] == 0) { time++; bb /= prime[i]; } break; } } if(bb == 1) { cout << "-1 -1 -1 -1" << endl; return ; } ll d = 1; for(int i = 1;i <= time; i++) { d *= tmp; // b的一个因数 } ll f = bb; // b的另外一个因数 ll c = 0, e = 0; solve_ex_gcd(f, d, a, c, e); if(c < 0 && e > 0) { cout << e << " " << f << " " << -c << " " << d << endl; } else cout << c << " " << d << " " << -e << " " << f << endl; } } signed main() { ios_base::sync_with_stdio(false); //cin.tie(nullptr); //cout.tie(nullptr); #ifdef FZT_ACM_LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #else ios::sync_with_stdio(false); sieve(); int T = 1; cin >> T; while(T--) solve(); #endif return 0; }