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- ...
随机推荐
- html5中的全局属性
在html5中,新增了一个"全局属性"的概念,所谓全局属性,是指可以对任何属性都使用的属性.下面列出常用的全局属性. 1.contentEditable属性,是微软开发的,该属性主 ...
- word search(二维数组中查找单词(匹配字符串))
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Convert Sorted Array to Binary Search Tree(将一个有序数组转换成一颗二叉搜索树)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- Java编程语言下Selenium驱动各个浏览器代码
这里采用的是Selenium3.7版本,首先介绍的是在Windows环境下运行的: 总结下注意事项: 1,设置各个浏览器的Driver路径 System.setProperty("" ...
- Codeforces Round #479 (Div. 3) A. Wrong Subtraction
题目网址:http://codeforces.com/contest/977/problem/A 题解:给你一个数n,进行k次变换,从末尾开始-1,512变成511,511变成510,510会把0消掉 ...
- Ocelot中文文档-请求Id和关联Id
Ocelot支持一个客户端以头的形式发送requestid. 如果设置了,一旦中间件管道中可用,Ocelot便会使用这个requestid进行日志记录. Ocelot也会使用指定头将requireid ...
- windows下安装mysql-5.7.11-winx64
1.解压. 2.将『D:\Program Files\mysql-5.7.11-winx64\bin』加入系统环境变量. 3.修改my-default.ini. 4.初始化data目录,在 ...
- swagger上传文件并支持jwt认证
背景 由于swagger不仅提供了自动实现接口文档的说明而且支持页面调试,告别postman等工具,无需开发人员手动写api文档,缩减开发成本得到大家广泛认可 但是由于swagger没有提供上传文件的 ...
- vue实现双向数据绑定的原理
vue实现双向数据绑定的原理就是利用了 Object.defineProperty() 这个方法重新定义了对象获取属性值(get)和设置属性值(set)的操作来实现的. 在MDN上对该方法的说明是:O ...
- Http Header信息
REMOTE_ADDR – 访问客户端的 IP 地址 HTTP_VIA – 如果有该条信息, 就证明您使用了代理服务器,代理服务器的地址就是后面的数值. HTTP_X_FORWARDED_FOR – ...