Discrete Logging
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5577   Accepted: 2494

Description

Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that

    B

L

 == N (mod P)

Input

Read several lines of input, each containing P,B,N separated by a space.

Output

For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print "no solution".

Sample Input

5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111

Sample Output

0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587

Hint

The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat's theorem that states

   B

(P-1)

 == 1 (mod P)

for any prime P and some other (fairly rare) numbers known as base-B pseudoprimes. A rarer subset of the base-B pseudoprimes, known as Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A corollary to Fermat's theorem is that for any m

   B

(-m)

 == B

(P-1-m)

 (mod P) .

Source

 
高次同余方程。   BL == N (mod P)求解最小的L
BSGS模板题目。
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
struct Thash{
static const int MOD=;
static const int MAXN=1e6+;
int tot,head[MOD+],next[MAXN],h[MAXN],val[MAXN];
inline void clear(){tot=;memset(head,,sizeof head);}
inline void insert(int H,int VAL){
for(int i=head[H%MOD];i;i=next[i]) if(h[i]==H){val[i]=VAL;return ;}
h[++tot]=H;val[tot]=VAL;next[tot]=head[H%MOD];head[H%MOD]=tot;
}
inline int get(int H){
for(int i=head[H%MOD];i;i=next[i]) if(h[i]==H) return val[i];
return ;
}
}M;
inline ll fpow(ll a,ll p,ll mod){
int res=;
for(;p;p>>=,a=a*a%mod) if(p&) res=res*a%mod;
return res;
}
int BSGS(ll A,ll B,ll mod){
A%=mod;
if(!A){
if(!B) return ;
return -;
}
ll m=sqrt(mod)+,ni=fpow(A,mod-m-,mod);
ll t=,y=;
M.clear();
M.insert(,m+);
for(int i=;i<m;i++){
t=t*A%mod;
if(!M.get(t)) M.insert(t,i);
}
for(int i=;i<m;i++){
int u=M.get(B*y%mod);
if(u){
if(u==m+) u=;
return i*m+u;
}
y=y*ni%mod;
}
return -;
}
int main(){
int a,b,c,ans(-);
while(scanf("%d%d%d",&c,&a,&b)==){
ans=BSGS(a,b,c);
if(~ans) printf("%d\n",ans);
else puts("no solution");
}
return ;
}

POJ2417 Discrete Logging【BSGS】的更多相关文章

  1. POJ2417 Discrete Logging【BSGS】(模板题)

    <题目链接> 题目大意: P是素数,然后分别给你P,B,N三个数,然你求出满足这个式子的L的最小值 : BL== N (mod P). 解题分析: 这题是bsgs算法的模板题. #incl ...

  2. 【BSGS】BZOJ3239 Discrete Logging

    3239: Discrete Logging Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 729  Solved: 485[Submit][Statu ...

  3. BZOJ 3239 Discrete Logging(BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...

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

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

  5. 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 \)这里-的作用是避 ...

  6. poj2417 Discrete Logging BSGS裸题

    给a^x == b (mod c)求满足的最小正整数x, 用BSGS求,令m=ceil(sqrt(m)),x=im-j,那么a^(im)=ba^j%p;, 我们先枚举j求出所有的ba^j%p,1< ...

  7. POJ2417 Discrete Logging | A,C互质的bsgs算法

    题目: 给出A,B,C 求最小的x使得Ax=B  (mod C) 题解: bsgs算法的模板题 bsgs 全称:Baby-step giant-step 把这种问题的规模降低到了sqrt(n)级别 首 ...

  8. BZOJ2242 [SDOI2011]计算器 【BSGS】

    2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 4741  Solved: 1796 [Submit][Sta ...

  9. POJ2417 Discrete Logging

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

随机推荐

  1. odoo报表条码无法显示解决

    当服务器为Linux(Ubuntu)时,ODOO打印的报表上是有条码的,却显示空白框框. 问题在于服务器上没有安装条码的字体,reportlab渲染条码图形失败,导致显示不正常. 将附件中的字体下载, ...

  2. Maven入门学习

    1 Maven的安装 maven下载路径:http://maven.apache.org/download.cgi 我是在win7上安装了,安装后在cmd输入 mvn -v: C:\Users\*** ...

  3. IOS下拉放大图片

    代码地址如下:http://www.demodashi.com/demo/11623.html 一.实现效果图 现在越来越多的APP中存在下拉放大图片的效果,今天贡献一下我的实现这种方法的原理,和我遇 ...

  4. Spring 中bean的作用、定义

    Spring 中bean的作用.定义: 创建一个bean定义,其实质是用该bean定义对应的类来创建真正实例的"配方(recipe)".把bean定义看成一个配方很有意义,它与cl ...

  5. vue 父组件如何调用子组件的函数Methods

    答案就是使用ref即可. <countdown ref="countdown"></countdown> beforeDestroy () { // 切换页 ...

  6. 使用SVN管理unityproject

     我们的项目使用SVN管理.这几天遇到了几个问题,攻克了一下.顺便做了一个总结. 1.关于使用SVN管理unity项目的一些设置和说明 首先在unity中进行两部操作:Edit->Proje ...

  7. 关于angularjs在IE里的坑——F12工具打开,功能正常,关闭之后,angularjs not working

    前端时间在做项目的时候,用到了angularjs,期间,发现了一个奇葩的问题,就是在IE11浏览器下,点击下方图1上箭头所示的位置,将此处的开关变为图2中箭头所示的样子,但是发觉没有反应,开关还是灰色 ...

  8. openWRT自学---对官方的开发指导文档的解读和理解 记录3:一些常用方法

    1.约定 configuration files follow the convention:  <name>.conf init files follow the convention: ...

  9. 1211日课后shell总结

      1211shell   作者:高波 归档:学习笔记 2017年12月11日13:10:56 快捷键: Ctrl + 1 标题1 Ctrl + 2 标题2 Ctrl + 3 标题3 Ctrl + 4 ...

  10. figure margins too large错误解决

    使用Rstudio,遇到下面这个错误: figure margins too large 这是因为界面右下角的“plot”窗口太小,显示不了,将右下角的窗口调大就能解决