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- ...
随机推荐
- ORACLE分页SQL语句(转载)
1.根据ROWID来分select * from t_xiaoxi where rowid in(select rid from (select rownum rn,rid from(select r ...
- Linux的关机详解
Linux如果是关机不想Windows,特别是命令界面.如果使用的是虚拟机,我们经常通过虚拟机来关闭.有点笨重啊.着特意找到关于Linux关机命令分享给大家. 在linux下一些常用的关机/重启命令有 ...
- java原子操作
一.何谓Atomic? Atomic一词跟原子有点关系,后者曾被人认为是最小物质的单位.计算机中的Atomic是指不能分割成若干部分的意思.如果一段代码被认为是Atomic,则表示这段代码在执行过程中 ...
- 使用mpvue开发微信小程序
更多内容请查看 我的新博客 地址 : 前言 16年小程序刚出来的时候,就准备花点时间去学学.无奈现实中手上项目太多,一个接着一个,而且也没有开发小程序的需求,所以就一拖再拖. 直到上周,终于有一个小程 ...
- 与班尼特·胡迪一起攻破浮空城 (HZNU-2264)
与班尼特·胡迪一起攻破浮空城 AC Time Limit: 1 s Memory Limit: 256 MB Description 桐人为了拯救被困在浮空城堡最顶层的亚丝娜,决定从第 ...
- EF Code First 数据迁移配置
这里我想讲清楚code first 数据迁移的两种模式,还有开发环境和生产环境数据迁移的最佳实践. 1.1 数据迁移综述 EF Code first 虽然已经有了几种不同的数据库初始化策略,但是大部分 ...
- SOFA 源码分析 — 负载均衡和一致性 Hash
前言 SOFA 内置负载均衡,支持 5 种负载均衡算法,随机(默认算法),本地优先,轮询算法,一致性 hash,按权重负载轮询(不推荐,已被标注废弃). 一起看看他们的实现(重点还是一致性 hash) ...
- Hadoop生态圈初识
一.简介 Hadoop是一个由Apache基金会所开发的分布式系统基础架构.Hadoop的框架最核心的设计就是:HDFS和MapReduce.HDFS为海量的数据提供了存储,则MapReduce为海量 ...
- Effective C++ 读书笔记(46-50)
条款四十六:需要类型转换时请为模板定义非成员函数 条款四十七:请使用traits classes 表现类型信息 1.整合重载技术后,traits classes 有可能在编译期对类型执行if...el ...
- Tomcat PermGen space的解决方案
Tomcat报告 Caused by: java.lang.OutOfMemoryError: PermGen space异常 内存溢出PermGen space的全称是Permanent Gener ...