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. MPTCP 源码分析(四) 发送和接收数据

    简述:      MPTCP在发送数据方面和TCP的区别是可以从多条路径中选择一条 路径来发送数据.MPTCP在接收数据方面与TCP的区别是子路径对无序包 进行重排后,MPTCP的mpcb需要多所有子 ...

  2. 使用Tornado实现Ajax请求

    Ajax,指的是网页异步刷新,一般的实现均为js代码向server发POST请求,然后将收到的结果返回在页面上.   这里我编写一个简单的页面,ajax.html <html> <h ...

  3. cocos2d-x 3.1.1 学习笔记[11] http请求 + json解析

    //http须要引入的头文件和命名空间 #include <network/HttpClient.h> using namespace network; //json须要引入的头文件 #i ...

  4. Altium Designer 10 | 常用库及部分元件名中英文对照表

    ———————————————————————————————————————————— 常用库及部分元件名中英文对照表 - - - - - - - - - - - - - - - - - - - - ...

  5. (转)jquery实现图片轮播

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. C-类型转换(陷阱)

    getchar() 返回值为int类型 1.自动类型转换(运算符两边变量类型不同时) 1).两个变量类型自动转换成一样的类型(会根据参数类型自动转换, 而不是直接位转换), 且运算结果也是转换后的类型 ...

  7. linux 设置tomcat快捷启动方式

    在linux下搭建好tomcat之后,每次启动和关闭都要去tomcat的bin目录下执行./startup.sh和./shutdown.sh 这是很不方便的,下面介绍如何像执行ls mv cp等命令一 ...

  8. Atitit  文件上传  架构设计 实现机制 解决方案  实践java php c#.net js javascript  c++ python

    Atitit  文件上传  架构设计 实现机制 解决方案  实践java php c#.net js javascript  c++ python 1. 上传的几点要求2 1.1. 本地预览2 1.2 ...

  9. mongodb 实现关系型数据库中查询某一列 的效果

    近期在tornado\mongodb\ansible mongodb中有个find()方法非常牛逼,能够将集合中全部的表都传出来,一開始我这么写 class Module_actionHandler( ...

  10. 李洪强iOS开发之OC[003] - 用钥匙串存储信息模拟登陆