https://ac.nowcoder.com/acm/contest/889/B

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

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

#include<bits/stdc++.h>
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);
}
}
}

2019牛客暑期多校训练营(第九场) - B - Quadratic equation - 二次剩余的更多相关文章

  1. 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem

    题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3  4 2 3 4 输出:0 0 1 题解: 认真想一 ...

  2. 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)

    题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...

  3. 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)

    layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...

  4. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  5. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  6. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...

  7. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

  8. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  9. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  10. 2019牛客暑期多校训练营(第八场)E.Explorer

    链接:https://ac.nowcoder.com/acm/contest/888/E来源:牛客网 Gromah and LZR have entered the fifth level. Unli ...

随机推荐

  1. spring注解版

    第一.spring框架快速入门 1.1什么是spring 框架 Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of C ...

  2. JS利用XMLHttpRequest拦截ajax请求

    function XMLHttpRequestBreak(fun=()=>false){ let f = XMLHttpRequest.prototype.open; let add = fun ...

  3. MongoDB数据库的基本操作命令

    启动服务 net start mongodb 使用 登录本机mongodb Mongodb服务启动之后,打开命令行工具. 登录 mongo 127.0.0.1:27017 27017是mongodb的 ...

  4. JSP——JSTL定制标签 - 递归标签显示属性结构

    编写定制标签分为三个步骤:编写标签处理器.配置标签.使用标签. 1.标签处理器 标签处理器和标签是一一对应的关系 package com.oolong.utils.customtags;   impo ...

  5. yum源相关

    yum软件仓库默认配置文件/etc/yum.conf,此文件定义了yum在线下载的rpm包存放位置及下载后是否保存. [root@localhost ~]# head /etc/yum.conf[ma ...

  6. vue项目如何部署到Tomcat中

    vue项目如何部署到Tomcat中 1,假设你要访问的项目名称为'hms' 2,在Tomcat的webapps下创建hms文件夹, 3,配置config/index.js文件,build: {} 选项 ...

  7. PythonScript_demo--搭建PXE服务器

    前言 是一个测试向的Demo,在实验环境中改改还是可以用的,有助理解PXE服务器的原理.可以结合PXE服务器原理细节来看,传送门:点这里 软件环境 系统 RHEL7 软件 Python 27 RHEL ...

  8. 【DVWA】Brute Force(暴力破解)通关教程

    日期:2019-08-01 14:49:47 更新: 作者:Bay0net 介绍:一直以为爆破很简单,直到学习了 Burp 的宏录制和匹配关键词,才发现 burp 能这么玩... 0x01. 漏洞介绍 ...

  9. MM相关号码范围IMG设定

    一.定义各物料类型的号码范围——MMNR 路径:後勤系統 - 一般 > 物料主檔> 基本設定 > 物料類型 >定義各物料類型的號碼範圍 2.定义供应商主档记录号码范围——OMS ...

  10. 启动elasticsearch-head显示集群健康值:未连接

    ES启动后,进行es header访问的话,使用localhost:9100会显示集群健康值未连接 2种情况(均为windows10环境下): 1:未在elasticsearch-6.8.0\conf ...