2019牛客暑期多校训练营(第九场) - B - Quadratic equation - 二次剩余
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 - 二次剩余的更多相关文章
- 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem
题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3 4 2 3 4 输出:0 0 1 题解: 认真想一 ...
- 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)
题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...
- 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem
链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客暑期多校训练营(第八场)E.Explorer
链接:https://ac.nowcoder.com/acm/contest/888/E来源:牛客网 Gromah and LZR have entered the fifth level. Unli ...
随机推荐
- python爬取智联招聘职位信息(单进程)
我们先通过百度搜索智联招聘,进入智联招聘官网,一看,傻眼了,需要登录才能查看招聘信息 没办法,用账号登录进去,登录后的网页如下: 输入职位名称点击搜索,显示如下网页: 把这个URL:https://s ...
- git远程相关
git remote add origin git仓库地址 // 添加了远程仓库 git remote remove origin // 移除远程仓库 git push -u origin maste ...
- Unity3D_(游戏)跳一跳超简单制作过程
跳一跳 工程文件界面 游戏界面 脚本 using DG.Tweening; using System.Collections; using System.Collections.Generic; us ...
- JavaWeb--Servlet 详解
一.基本概念 Servlet是运行在Web服务器上的小程序,通过http协议和客户端进行交互.这里的客户端一般为浏览器,发送http请求(request)给服务器(如Tomcat).服务器接收到请求后 ...
- 胜利点20191010-6 alpha week 1/2 Scrum立会报告+燃尽图 04
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8749 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 杨萍队名:胜 ...
- Golang协程实现流量统计系统(2)
从进程开始,搜索和理解进程 Google 搜索关键词: C fork example 什么是fork Fork系统调用用于创建一个称为子进程的新进程,该子进程与进行fork()调用的进程(父进程)同时 ...
- win10上的docker怎么设置开机不要自动启动 [问题点数:20分,结帖人xyq1986]
次win开机都自动启动docker,感觉很耗资源,docker只是有时开发时需要用到,在docker的setting上的Start Docker Desktop when you log in取消了也 ...
- nginx-location正则表达式匹配规则及动静分离
nginx-location正则表达式匹配规则及动静分离 发表于 2018年03月5日 | 分类于 nginx| 0 nginx,location常用正则表达式,及nginx动静分离 nginx ...
- android 播放音乐媒体文件(一)
Audio formats and codecs Format / Codec Encoder Decoder Details Supported File Type(s) / Container F ...
- 【DVWA】Command Injection(命令注入)通关教程
日期:2019-08-01 16:05:34 更新: 作者:Bay0net 介绍:利用命令注入,来复习了一下绕过过滤的方法,还可以写一个字典来 fuzz 命令注入的点. 0x01. 漏洞介绍 仅仅需要 ...