BZOJ_3239_Discrete Logging_BSGS
BZOJ_3239_Discrete Logging_BSGS
题意:Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 2 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that BL == N (mod P)
分析:BSGS裸题
数据很水,少了很多特判依然能过
BSGS思想核心思想:根号预处理+哈希/map
代码:
// luogu-judger-enable-o2
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <math.h>
using namespace std;
#define LL long long
map<LL,int> mp;
LL qp(LL x,LL y,LL mod){
LL re=1;
while(y){
if(y&1ll)re=re*x%mod;
x=x*x%mod;
y>>=1ll;
}
return re;
}
void exgcd(LL a,LL b,LL &x,LL &y,LL &p){
if(!b){x=1;y=0;p=a;return ;}
exgcd(b,a%b,y,x,p);
y-=(a/b)*x;
}
LL BSGS(LL n,LL a,LL b){
if(n==1)if(!b)return a!=1; else return -1;
if(b==1)if(a)return 0; else return -1;
if(a%n==0)if(!b)return 1; else return -1;
LL m=ceil(sqrt(n)),d=1,base=1;
mp.clear();
for(int i=0;i<m;i++)
{
if(!mp.count(base))mp[base]=i;
base=(base*a)%n;
}
for(int i=0;i<m;i++)
{
LL x,y,s;
exgcd(d,n,x,y,s);
x=(x*b%n+n)%n;
if(mp.count(x))return i*m+mp[x];
d=(d*base)%n;
}
return -1;
}
int main()
{
LL n,a,b;
while(scanf("%lld%lld%lld",&n,&a,&b)!=EOF)
{
LL x=BSGS(n,a,b);
if(x==-1)puts("no solution");
else printf("%lld\n",x);
}
}
BZOJ_3239_Discrete Logging_BSGS的更多相关文章
- [poj2417]Discrete Logging_BSGS
Discrete Logging poj-2417 题目大意:求$a^x\equiv b(mod\qquad c)$ 注释:O(分块可过) 想法:介绍一种算法BSGS(Baby-Step Giant- ...
随机推荐
- 移动web前端开发时注意事项(转)
在智能手机横行的时代,作为一个web前端,不会编写移动web界面,的确是件悲催的事情.当公司准备做一个微信的微网站时,作为一个多年经验的web前端码农,我迷茫了,真心不知道从何下手. 接下来就是搜一堆 ...
- Linux部署集群.NET网站
一.Linux下面安装需要软件 我们这里需要安装的软件有: 1) Mono 3.2.8 : C#跨平台编译器,能使.Net运行与Linux下,目前.net 4.0可以完美运行在该平台下 2) ngin ...
- SignUtil
最近接的新项目 加密比较多 我就记录下. SignUtil是jnewsdk-mer-1.0.0.jar com.jnewsdk.util中的一个工具类.由于我没有百度到对应的信息.所以我只能看源码 ...
- Angular TypeScript开发环境集成jQuery扩展插件
集成步骤: 1.安装jquery极其扩展插件库ts定义文件 npm install jquery --save npm install --save-dev @types/jquery npm ins ...
- Using Sass with the Angular CLI
https://www.tuicool.com/articles/mauiMzY One of the first things you'll usually do in a project is t ...
- andorid下从相册选取/拍照选取一张相片并剪切
http://www.2cto.com/kf/201401/270144.html 在Android编程中,从相册选取或是拍照选取一张照片然后对其进行剪切的需求非常的多 之前的一篇文章只说到如何从相册 ...
- pc端页面打包成安卓apk
一.phoneGap PhoneGap是一个采用HTML,CSS和JavaScript的技术,创建移动跨平台移动应用程序的快速开发平台.它使开发者能够在网页中调用IOS,Android,Palm,Sy ...
- AE的空间分析(转载)
1.1 ITopologicalOperator接口 1.1.1 ITopologicalOperator接口简介ITopologicalOperator接口用来通过对已存在的几何对象做空间拓扑运算以 ...
- ArcCore重构-生成%_offset.h文件
基于官方arc-stable-9c57d86f66be,AUTOSAR版本3.1.5 基本问题 ArcCore中,需要生成asm_offset.h和arch_offset.h这两个头文件,定义着代 ...
- self,和类实例化加不加括号的理解
# class Dog(object): # def talk(self): # print('汪汪~~~') # print(self) # self就是对象,默认将对象传递到类方法,self不需要 ...