[bzoj2242][SDOI2011][计算器] (Baby-Step-Giant-Step+快速幂+exgcd)
Description
Input
输入包含多组数据。
Output
Sample Input
3 1
2 1 3
2 2 3
2 3 3
【样例输入2】
3 2
2 1 3
2 2 3
2 3 3
【数据规模和约定】
对于100%的数据,1<=y,z,p<=10^9,为质数,1<=T<=10。
Sample Output
2
1
2
【样例输出2】
2
1
0
HINT
Source
#include <map>
#include <math.h>
#include <stdio.h>
#define L long long
#define inf ~0U>>1
#define dmin(a,b) ((a)<(b)?(a):(b)) template<class Type>inline void Rin(Type &x){
x=;Type c=getchar(),f=;
for(;c<||c>;c=getchar())
if(c==)f=-;
for(;c>&&c<;c=getchar())
x=(x<<)+(x<<)+c-;
x*=f;
} std::map<int,int>mp; int T,Kd; struct Pep{
L fir,sec; Pep(L _=,L __=) : fir(_),sec(__) {}
}; L gcd(L a,L b){
return b?gcd(b,a%b):a;
} Pep exgcd(L a,L b){
if(!b)return Pep(,);
Pep temp=exgcd(b,a%b);
return Pep(temp.sec,temp.fir-a/b*temp.sec);
} L frog1(L y,int p,L mo){
L res=1LL;
while(p){
if(p&)res=(res*y)%mo;
y=(y*y)%mo;
p>>=;
}
return res;
} void frog2(int a,int b,L p){
p=-p;
int t=gcd(a,p);
if(b%t){puts("Orz, I cannot find x!");return;}
a/=t; b/=t; p/=t;
Pep temp=exgcd(a,p);
temp.fir=(L)(temp.fir*b)%p;
while(temp.fir<)temp.fir+=p;
printf("%d\n",temp.fir);
} void frog3(int A,int B,L p){
A%=p;
if((!A) && (!B)){puts("");return;}
if((!A)){puts("Orz, I cannot find x!");return;}
mp.clear();
L m=ceil(sqrt(p)),temp=1LL;
mp[]=m+;
for(L i=;i<m;i++){
(temp*=A)%=p;
if(!mp[temp])mp[temp]=i;
}
L D=frog1(A,p-m-,p),ine=1LL;
for(L k=;k<m;k++){
int i=mp[B*ine%p];
if(i){
if(i==m+)i=;
printf("%lld\n",k*m+i);
return;
}
ine=ine*D%p;
}
puts("Orz, I cannot find x!");
} int main(){
Rin(T); Rin(Kd);
while(T--){
int y,z,p;
Rin(y),Rin(z),Rin(p);
if(Kd==)printf("%lld\n",frog1(y,z,p));
else if(Kd==)frog2(y,z,p);
else frog3(y,z,p);
}
return ;
}
[bzoj2242][SDOI2011][计算器] (Baby-Step-Giant-Step+快速幂+exgcd)的更多相关文章
- 【BZOJ2242】【SDoi2011】计算器 快速幂+EXGCD+BSGS
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- 『高次同余方程 Baby Step Giant Step算法』
高次同余方程 一般来说,高次同余方程分\(a^x \equiv b(mod\ p)\)和\(x^a \equiv b(mod\ p)\)两种,其中后者的难度较大,本片博客仅将介绍第一类方程的解决方法. ...
- POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)
不理解Baby Step Giant Step算法,请戳: http://www.cnblogs.com/chenxiwenruo/p/3554885.html #include <iostre ...
- 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法
先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝 扩展Baby Step Gian ...
- 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)
什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...
- 【POJ2417】baby step giant step
最近在学习数论,然而发现之前学的baby step giant step又忘了,于是去翻了翻以前的代码,又复习了一下. 觉得总是忘记是因为没有彻底理解啊. 注意baby step giant step ...
- [置顶] hdu2815 扩展Baby step,Giant step入门
题意:求满足a^x=b(mod n)的最小的整数x. 分析:很多地方写到n是素数的时候可以用Baby step,Giant step, 其实研究过Baby step,Giant step算法以后,你会 ...
- HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法
联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- HDU 2815 扩展baby step giant step 算法
题目大意就是求 a^x = b(mod c) 中的x 用一般的baby step giant step 算法会超时 这里参考的是http://hi.baidu.com/aekdycoin/item/2 ...
- 【学习笔记】Baby Step Giant Step算法及其扩展
1. 引入 Baby Step Giant Step算法(简称BSGS),用于求解形如\(a^x\equiv b\pmod p\)(\(a,b,p\in \mathbb{N}\))的同余方程,即著名的 ...
随机推荐
- 编译Ruby2.0 问题解决
compiling readline.creadline.c: In function ‘Init_readline’:readline.c:1886:26: error: ‘Function’ un ...
- C# 简单实现直线方程,抛物线方程(转载)
http://www.cnblogs.com/hsiang/archive/2017/01/17/6294864.html 本例子是简单的在WinForm程序中实现在坐标系中绘制直线方程,抛物线方程, ...
- XML消息解析_php
初识php——微信消息处理 <?php $test = new weixin(); $test->Message(); class weixin{ public function Mess ...
- Maven之项目搭建与第一个helloworld(多图)
这次记录第一个搭建一个maven的helloworld的过程. 转载 1.搭建web工程肯定得new 一个 maven工程,假如project中没有直接看到maven工程,那么选择Other,然后在W ...
- ACM_抢糖果
抢糖果 Time Limit: 2000/1000ms (Java/Others) Problem Description: 今天计实班的生活委员心情大好,在永诚超市狂购了好多好多糖果,好开心~o(∩ ...
- Hive insert into directory 命令输出的文件没有列分隔符分析和解决
参考资料:http://stackoverflow.com/questions/16459790/hive-insert-overwrite-directory-command-output-is-n ...
- 1807. [NOIP2014]寻找道路P2296 寻找道路
题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...
- 冒泡 [Python]
冒泡Python class BubbleSort: def __init__(self): self.initArr() def initArr(self): self.arrInfo = [60, ...
- Java 基础入门随笔(3) JavaSE版——逻辑运算符、位运算符
上一节写了一些运算符的注意事项,这节开头依然是对运算符的一些注意点的阐述! 比较运算符除了>.>=.<.<=.==.!=之外需要注意instanceof:检查是否是类的对象,例 ...
- java如何区分同时继承的父类和实现的接口中相同的方法
基类代码: public class Father { public Father() { System.out.println("基类构造函数{"); show(); Syste ...