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的更多相关文章

  1. [poj2417]Discrete Logging_BSGS

    Discrete Logging poj-2417 题目大意:求$a^x\equiv b(mod\qquad c)$ 注释:O(分块可过) 想法:介绍一种算法BSGS(Baby-Step Giant- ...

随机推荐

  1. Spring Boot Kafka

    1.创建集群 http://kafka.apache.org/documentation/#quickstart 有一句我觉得特别重要: For Kafka, a single broker is j ...

  2. Eclipse两种部署web项目方法

    一).首先使用J2EE的Eclipse的Servers(可以从show view中取出). 1).通过Eclipse建立一个Dynamic Web Project 2).通过Servers视图来创建一 ...

  3. (七):C++分布式实时应用框架 2.0

    C++分布式实时应用框架 2.0 技术交流合作QQ群:436466587 欢迎讨论交流 上一篇:(六):大型项目容器化改造 版权声明:本文版权及所用技术归属smartguys团队所有,对于抄袭,非经同 ...

  4. win10更新失败——适用于Windows 10 Version 1709 的03累积更新,适合基于x64系统(KB4088776)更新失败

    相信最近很多人被windows的更新折磨坏了,下面来介绍一下解决办法,有用的话请点赞! 首先将C盘中的这个文件夹删除:"C:\Windows\System32\Tasks\Microsoft ...

  5. c#学习笔记 day_one

    C#学习笔记 day one Chapter 1 c#概述 1.1 c#概述 C#是微软设计的,简洁的,类型安全的,面向对象的语言.它以c/c++作为基础.它的开发环境是visual studio,最 ...

  6. npoi生成excel流并在客户端下载(html+后台 )

    //前端页面 <body> <input type="button" value="导出Excel" class="button&q ...

  7. 「SQL归纳」树形结构表的存储与查询功能的实现——通过路径方法(非递归)

    一.树形结构例子分析: 以360问答页面为例:http://wenda.so.com/c/ 我们通过观察URL,可以明确该页面的数据以树形结构存储,下面三块模块分别为: ①根节点 ②根节点的第一层子节 ...

  8. 瞎捣鼓的code highlight

    int a ; int b; public int  a ;int b   char c; h2 { text-align: left;}.postTitle{ background-color:#F ...

  9. python爬虫入门(九)Scrapy框架之数据库保存

    豆瓣电影TOP 250爬取-->>>数据保存到MongoDB 豆瓣电影TOP 250网址 要求: 1.爬取豆瓣top 250电影名字.演员列表.评分和简介 2.设置随机UserAge ...

  10. Myeclipse+selenium2.0+Junit+TestNg环境搭建

    这周末把自动化的环境搭好了,在网上也百度了很多,现在分享下,希望大家少走一点歪路. 需要用到的安装包都在这个里面,自取: 链接:https://pan.baidu.com/s/10ohf757ztgN ...