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- ...
随机推荐
- 锋利的Jquery摘要
一本好书值得去反复回味 第一章: jquery中的$(document).ready(function(){})与js中的windows.onload()的比较 $(document).ready ...
- nifi1.6.0汉化
1.1 测试机 l Apache NiFi 1.6.0 l HDP 2.6.3 l 集群规模:单节点 l 操作系统:CentOs7 l 以下所有操作均在root用户下执行 1.2 安装环境 ...
- 超强js博客值得学习!!!
再读ecmascript 摘要: 这几天,又花了点时间看了下ecmascript.以下是我摘录出来的一些理解.在此记录下.第一部分:关于变量对象的理解1) 什么是变量对象?数据的存取与读取机制,就是变 ...
- Emit方式调用方法
object objRet = Delegate.CreateDelegate(typeof(Func<Guid, int, decimal>), inst, "HelloWor ...
- SpringBoot入门之简单配置
今天下载了<JavaEE开发的颠覆者SpringBoot实战>这本书,发现Spring还有好多遗漏的部分,算是又恶补了一下,今天主要是学习下SpringBoot的配置. 一.基本配置 1. ...
- Oracle 11g一步步安装详解
本文所需的安装包都存放在我网盘中,需要的私聊~ 一.安装VMware tools工具(非必须) 1.虚拟机上点击安装 因为我在虚拟机中做的,后面oracle 安装是图形化安装,需要屏幕大一点,不然有些 ...
- Java开源生鲜电商平台-系统架构与技术选型(源码可下载)
Java开源生鲜电商平台-系统架构与技术选型(源码可下载) 1. 硬件环境 公司服务器 2. 软件环境 2.1 操作系统 Linux CentOS 6.8系列 2.2 反向代理/web服务器 ...
- kmeans算法思想及其python实现
第十章 利用k-均值聚类算法对未标注的数据进行分组 一.导语 聚类算法可以看做是一种无监督的分类方法,之所以这么说的原因是它和分类方法的结果相同,区别它的类别没有预先的定义.簇识别是聚类算法中经常使用 ...
- php递归实现无限级分类树
作者: PHP中文网|标签:PHP 递归 无限级树|2017-5-18 18:09 无限级树状图可以说是无限级栏目的一个显著特征,我们接下来就来看看两种不同的写法. 一.数据库设计 1 2 3 ...
- python中@classmethod @staticmethod区别
Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...