博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2019牛客暑期多校训练营(第九场) - B - Quadratic equation - 二次剩余
阅读量:4681 次
发布时间:2019-06-09

本文共 2001 字,大约阅读时间需要 6 分钟。

假如我们能够求出 \(x-y\) 在模p意义的值,那么就可以和 \(x+y\) 联立解出来了。

由于 \((x-y)^2=(x+y)^2-4xy=b^2-4c\) ,那么设 \(n=b^2-4c\) ,就是要求解出一个二次剩余。

#include
using namespace std;typedef long long ll;const int p = 1e9 + 7;struct hh { ll x, y; hh() {}; hh(ll _x, ll _y) { x = _x; y = _y; }};ll w;hh mul(hh a, hh b, ll p) { hh ans; ans.x = (a.x * b.x % p + a.y * b.y % p * w % p) % p; ans.y = (a.x * b.y % p + a.y * b.x % p) % p; return ans;}hh quick1(hh a, ll b, ll p) { hh ans = hh(1, 0); while(b) { if(b & 1) ans = mul(ans, a, p); a = mul(a, a, p); b >>= 1; } return ans;}ll quick2(ll a, ll b, ll p) { ll ans = 1; while(b) { if(b & 1) ans = (ans * a) % p; b >>= 1; a = (a * a) % p; } return ans;}ll solve(ll a, ll p) { //求解 x^2=a(mod p) 的x的值 a %= p; //注意这句话 if(a == 0) return 0;//注意这句话 if(p == 2) return a; if(quick2(a, (p - 1) / 2, p) == p - 1) return -1; ll b, t; while(1) { b = rand() % p; t = b * b - a; w = (t % p + p) % p; if(quick2(w, (p - 1) / 2, p) == p - 1) break; } hh ans = hh(b, 1); ans = quick1(ans, (p + 1) / 2, p); return ans.x;}ll qpow(ll x, ll n, ll p) { ll res = 1; while(n) { if(n & 1) res = res * x % p; x = x * x % p; n >>= 1; } return res;}ll x, y;void solvexy(ll b, ll d) { y = (b + d) % p * qpow(2, p - 2, p) % p; x = (b - y + p) % p; if(x > y) swap(x, y); return;}int main() {#ifdef Yinku freopen("Yinku.in", "r", stdin);#endif // Yinku int t; scanf("%d", &t); while (t--) { ll b, c; scanf("%lld%lld", &b, &c); ll n = b * b - 4ll * c; n = (n % p + p) % p; ll d = solve(n, p); if (d == -1) printf("-1 -1\n"); else { solvexy(b, d); printf("%lld %lld\n", x, y); } }}

转载于:https://www.cnblogs.com/Yinku/p/11364041.html

你可能感兴趣的文章
编程之美 2.12 快速寻找满足条件的两个数 解法三证明 (算法导论 第二版 2.3-7 在n个元素的集合S中找到两个和为x的元素)...
查看>>
open_basedir restriction in effect,解决php引入文件权限问题
查看>>
微信小程序获取用户信息解密AES并且注意如何获取unionid
查看>>
JavaScript设计模式----1
查看>>
Qt实现半透明遮罩效果
查看>>
erlang调优方法
查看>>
Mysql linux -N命令
查看>>
daily scrum 12.5
查看>>
linux-ftp install
查看>>
NetXray
查看>>
局域网基本工作原理
查看>>
让历史告诉我们未来
查看>>
UVa540 Team Queue
查看>>
android 练习之路 (八)
查看>>
tp5 中 model 的聚合查询
查看>>
android wear开发之:增加可穿戴设备功能到通知中 - Adding Wearable Features to Notifications...
查看>>
几种内核对象的受信与非受信状态
查看>>
压缩文件函数库(转载)
查看>>
【转】ubuntu12.04没有/var/log/messages解决
查看>>
几种队列
查看>>