链接:https://ac.nowcoder.com/acm/contest/889/B
来源:牛客网
题目描述
Amy asks Mr. B problem B. Please help Mr. B to solve the following problem.
Let p = 1000000007.
Given two integers b and c, please find two integers (0≤x≤y<p), such that
输入描述:
The first line contains an integer t, which is the number of test cases (1 <= t <= 10).
In the following t lines, each line contains two integers b and c (0 <= b, c < p).
输出描述:
For each test case, please output x, y in one line.
If there is a solution, because x <= y, the solution is unique.
If there is no solution, please output -1, -1
示例1
输入
复制
10
4 4
5 6
10 10
10 25
20000 100000000
0 5
3 6
220 284
0 1
1000000000 1000000000
输出
复制
2 2
2 3
-1 -1
5 5
10000 10000
474848249 525151758
352077071 647922939
448762649 551237578
-1 -1
366417496 633582504
这个题是队友过的,用到了二次剩余定理,记录下来。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll pow_mod(ll a, ll n, ll p) {
ll res = 1;
while (n) {
if (n & 1) res = res * a % p;
n >>= 1;
a = a * a % p;
}
return res;
}
ll legendre(ll a, ll p) {
return pow_mod(a, (p - 1) >> 1, p);
}
ll mod(ll a, ll p) {
a %= p;
if (a < 0) a += p;
return a;
}
struct node {
static ll p, omega;
ll a, b;
node(ll a, ll b): a(a % p), b(b % p) {}
};
node operator *(const node &p, const node &q) {
int m = node::p;
return node(p.a * q.a + p.b * q.b % m * node::omega,
p.a * q.b + q.a * p.b);
}
node pow_mod(node a, ll n) {
node result(1, 0);
while (n > 0) {
if ((n & 1) == 1) {
result = result * a;
}
a = a * a;
n >>= 1;
}
return result;
}
ll node::p, node::omega;
ll modsqr(ll a, ll p) {
if (p == 2) return 1;
if (legendre(a, p) + 1 == p) return -1;
if ((((p + 1) >> 1) & 1) == 0) return pow_mod(a, (p + 1) >> 2, p);
ll a_0 = -1;
while (true) {
a_0 = rand() % p;
if (legendre(mod(a_0 * a_0 - a, p), p) + 1 == p) break;
}
node::p = p;
node::omega = mod(a_0 * a_0 - a, p);
node ret = pow_mod(node(a_0, 1), (p + 1) >> 1);
//assert(ret.b == 0);
return ret.a;
}
const long long mood=1000000007;
int main ()
{
ll b,c;
ll s2=pow_mod(2,mood-2,mood);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld %lld",&b,&c);
ll t=b*b-4*c;
t=t%mood;
ll x_y=modsqr(t,mood);
if(x_y==-1)
{
printf("-1 -1\n");
}
else
{
if(x_y*2>mood)
x_y=mood-x_y;
ll x=(((x_y+b)%mood)*s2)%mood;
ll y=((b-x)+mood)%mood;
if(x>y)
swap(x,y);
if((x+y)%mood==b && (x*y)%mood==c)
printf("%lld %lld\n",x,y);
else
printf("-1 -1\n");
}
}
return 0;
}
努力加油a啊,(o)/~