【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3239

【题目大意】

  计算满足 Y^x ≡ Z ( mod P) 的最小非负整数

【题解】

  BSGS裸题。

【代码】

#include <cstdio>
#include <cmath>
#include <map>
#include <algorithm>
#include <tr1/unordered_map>
using namespace std::tr1;
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
int phi(int n){
int t=1,i;
for(i=2;i*i<=n;i++)if(n%i==0)for(n/=i,t*=i-1;n%i==0;n/=i,t*=i);
if(n>1)t*=n-1;
return t;
}
int pow(ll a,int b,int m){ll t=1;for(;b;b>>=1,a=a*a%m)if(b&1)t=t*a%m;return t;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int exgcd(int a,int b,int&x,int&y){
if(!b)return x=1,y=0,a;
int d=exgcd(b,a%b,x,y),t=x;
return x=y,y=t-a/b*y,d;
}
int bsgs(int a,int r,int m){
if(r>=m)return -1;
int i,g,x,c=0,at=int(2+sqrt(m));
for(i=0,x=1%m;i<50;i++,x=ll(x)*a%m)if(x==r)return i;
for(g=x=1;__gcd(int(ll(x)*a%m),m)!=g;c++)g=__gcd(x=ll(x)*a%m,m);
if(r%g)return -1;
if(x==r)return c;
unordered_map<int,int>u;
g=phi(m/g),u[x]=0;g=pow(a,g-at%g,m);
for(i=1;i<at;i++){
u.insert(P(x=ll(x)*a%m,i));
if(x==r)return c+i;
}
for(i=1;i<at;i++){
unordered_map<int,int>::iterator t=u.find(r=ll(r)*g%m);
if(t!=u.end())return c+i*at+t->second;
}return -1;
}
void solve(int y,int z,int p){
y%=p; z%=p;
int t=bsgs(y,z,p);
if(t==-1){puts("no solution");return;}
else printf("%d\n",t);
}
int main(){
int a,b,c;
while(~scanf("%d%d%d",&a,&b,&c))solve(b,c,a);
return 0;
}

BZOJ 3239 Discrete Logging(BSGS)的更多相关文章

  1. BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)

    我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...

  2. bzoj 3239: Discrete Logging && 2480: Spoj3105 Mod【BSGS】

    都是BSGS的板子题 此时 \( 0 \leq x \leq p-1 \) 设 \( m=\left \lceil \sqrt{p} \right \rceil ,x=i*m-j \)这里-的作用是避 ...

  3. POJ-2417-Discrete Logging(BSGS)

    Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an integer N, 1 <= N &l ...

  4. BZOJ 3239: Discrete Logging [BGSG]

    裸题 求\(ind_{n,a}b\),也就是\(a^x \equiv b \pmod n\) 注意这里开根不能直接下取整 这个题少了一些特判也可以过... #include <iostream& ...

  5. 【BZOJ5296】【CQOI2018】破解D-H协议(BSGS)

    [BZOJ5296][CQOI2018]破解D-H协议(BSGS) 题面 BZOJ 洛谷 Description Diffie-Hellman密钥交换协议是一种简单有效的密钥交换方法.它可以让通讯双方 ...

  6. LTE Manual ——Logging(翻译)

    LTE Manual ——Logging(翻译) (本文为个人学习笔记,如有不当的地方,欢迎指正!) 9 Logging   ns-3 日志功能可以用于监测或调试仿真程序的进展.日志输出可以通过 ma ...

  7. POJ 2417 Discrete Logging(离散对数-小步大步算法)

    Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...

  8. POJ 2417 Discrete Logging (Baby-Step Giant-Step)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2819   Accepted: 1386 ...

  9. [POJ2417]Discrete Logging(指数级同余方程)

    Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...

随机推荐

  1. 五分钟学习Java8的流编程

    1.概述 Java8中在Collection中增加了一个stream()方法,该方法返回一个Stream类型.我们就是用该Stream来进行流编程的: 流与集合不同,流是只有在按需计算的,而集合是已经 ...

  2. Chinese Rings (九连环+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目: Problem Description Dumbear likes to play th ...

  3. vue 表格阻止父元素冒泡事件

    思路如下:1.给复选框定义一个类型,type="selection" 2.在点击函数中就可以使用判断条件来进行复选框的阻止冒泡.rowDetailShow(row, event, ...

  4. monkey测试===修改adb的默认端口

    最近电脑上由于公司系统的原因,adb的端口被占用了,但是占用端口的进程是必须启动的,不能被杀死,在网上找了很多办法,大家都是说杀死占用端口的进程.这个方法并不适用我,所以在此给大家一个新的方法.新建一 ...

  5. 基数排序c++实现

    基数排序:是一种非比较型整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较.由于整数也可以表达字符串(比如名字或日期)和特定格式的浮点数,所以基数排序也不是只能使用于整数.但在 ...

  6. 【数位dp入门】【HDU4734】F(x)

    记录减的状态,表示还要凑多少才能达到当前值. 然后进行枚举即可.注意状态数不能重复. #include<bits/stdc++.h> #define N 10010 using names ...

  7. java web 资源文件读取

    前提:假设web应用test(工程名) webapps下面有一资源文件test.html 规则:在获取资源时一般使用的是相对路径,以符号/开头,而 / 代表什么取决于这个地址给谁使用.服务器使用时,/ ...

  8. C++11——Use auto keyword in C++11

    版权声明:本文系原创,转载请注明来源. Compile your program with: g++ -std=c++ -o target_name filen_ame.cpp or: g++ -st ...

  9. 19:django 分页

    分页是网站中比较常见的应用,django提供了一些类帮助管理分页的数据,这些类都位于django.core.paginator.py文件里面 分页类 构造函数 class Paginator(obje ...

  10. Nginx web proxy NFS服务

    1.nginx web 安装 配置 #systemctl stop firewalld #systemctl disabled firewalld #wget -O /etc/yum.repos.d/ ...