Link to the original question:
meaning of the questions :
\ (Known (X + Y) \) % \ (P = B \) , \ ((X * Y) \) % \ (P = C \) \ (\ Space \) find x, y. If exists output \ (- 1 \ space-1 \)
Ideas :
\ (By squaring the difference equation (XY) ^ 2 \) = \ ((X + Y) ^ 2 \) \ (- \) \ (. 4 * (X + Y) \) = \ ((2-4C B ^ ) \) % \ (p \) ,
\ (we will think of the presence of quadratic residue X ^ 2 \ equiv d (mod \ space p), (d is a quadratic residue modulo p) to solve for xy, then and b (xy) to obtain the X-, the y-\)
\ (Euler's criterion used here to calculate Legendre symbol: \)
\ (Legendre symbol can be calculated when p is a prime number of cases of discrimination quadratic residue problem, but ya comparable to a symbol determination condition is any integer, p is an arbitrary odd number \)
\ (for the given subject species 1000000007 p is a prime number so we do with Euler's criterion \).
\ (according to Euler's criterion: when a is a MOD \ have a ^ \ frac {p-1 } {2} \ equiv \ space1 \) for the squared space p of the remaining \ ((mod \ space p) \ space, when a is not a quadratic residue a ^ \ frac . 1-P {} {2} \ equiv \-Space. 1 (MOD \ Space P) \)
\ (so we have to determine (b ^ 2-4c by flash power) ^ \ frac {p-1 } {2 } is determined is equal to \ space mod-1 (no solution), the solution and finally x, y is obtained. \)
(First use markdown editor Burongyia ...)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1000000007;
ll q_pow(ll a,ll b){
ll res=1;
while(b){
if(b&1)res=res*a % mod;
a=a*a%mod;
b>>=1;
}
return res;
}
int main(){
int T;
cin >> T;
while(T--){
ll b,c;
cin >> b >> c;
ll delta = (b * b - 4 * c + mod) % mod;
if(q_pow (delta,(mod - 1) / 2) == mod - 1) { puts("-1 -1"); continue; }
ll a = q_pow (delta,((mod + 1) / 2 + 1) / 2);
ll x = ((b - a + mod) * q_pow(2, mod - 2)) % mod;
ll y = (b + mod - x) % mod;
printf("%lld %lld\n", min(x,y),max(x,y));
}
return 0;
}