POJ 2417 Discrete Logging

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4860   Accepted: 2211

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
 /*BSGS算法+逆元*/
这个主要是用来解决这个题: A^x=B(mod C)(C是质数),都是整数,已知A、B、C求x。 我在网上看了好多介绍,觉得他们写得都不够碉,我看不懂…于是我也来写一发。 先把x=i*m+j,其中m=ceil(sqrt(C)),(ceil是向上取整)。 这样原式就变为A^(i*m+j)=B(mod C), 再变为A^j=B*A^(-m*i) (mod C), 先循环j=~(C-),把(A^j,j)加入hash表中,这个就是Baby Steps 下面我们要做的是枚举等号右边,从hash表中找看看有没有,有的话就得到了一组i j,x=i*m+j,得到的这个就是正确解。 所以,接下来要解决的就是枚举B*A^(-m*i) (mod C)这一步(这就是Giant Step A^(-m*i)相当于1/(A^(m*i)),里面有除法,在mod里不能直接用除法,这时候我们就要求逆元。 /*百度百科: 若ax≡1 mod f, 则称a关于模f的乘法逆元为x。也可表示为ax≡1(mod f)。
当a与f互素时,a关于模f的乘法逆元有唯一解。如果不互素,则无解。如果f为素数,则从1到f-1的任意数都与f互素,即在1到f-1之间都恰好有一个关于模f的乘法逆元。
*/ 然后我们用超碉的exgcd求逆元,exgcd(扩展欧几里德算法)就是在求AB的最大公约数z的同时,求出整数x和y,使xA+yB=z。算法实现就是gcd加几个语句。
然后我们再来看一下exgcd怎么求逆元:
对xA+yB=z, 变成这样xA = z - yB,取B=C(C就是我们要mod的那个) 推导出 xA % C = z %C 只要 z%C== 时,就可以求出A的逆元x 但用exgcd求完,x可能是负数,还需要这样一下:x=(x%C+C)%C //--exgcd介绍完毕-- 再看我们的题目, exgcd(A^(m*i) , C)=z,当C是质数的时候z肯定为1,这样exgcd求得的x就是逆元了。 因为x就是A^(m*i)的逆元,P/(A^(m*i))=P*x,所以 B*A^(-m*i) = B/(A^(m*i)) = B*x(mod C) 这样我们的式子A^j=B*A^(-m*i) (mod C)的等号右边就有了,就是B*x,就问你怕不怕! 枚举i,求出右边在hash里找,找到了就返回,无敌! /*---------分割线-----------------------*/
#include<cmath>
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define mod 100007
#define ll long long
struct hash
{
ll a[mod+],v[mod+];
hash(){memset(a,-,sizeof(a));}
int locate(ll x)
{
ll l=x%mod;
while(a[l]!=x&&a[l]!=-) l=(l+)%mod;
return l;
}
void insert(ll x,int i)
{
ll l=locate(x);
if(a[l]==-)
{
a[l]=x;
v[l]=i;
}
}
int get(ll x)
{
ll l=locate(x);
return (a[l]==x)?v[l]:-;
}
void clear()
{
memset(a,-,sizeof(a));
}
}s;
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==)
{
x=;
y=;
return ;
}
exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-a/b*y;
}
int main()
{
ll p,b,n;
while(scanf("%I64d%I64d%I64d",&p,&b,&n)==)
{
s.clear();
ll m=ceil(sqrt(p));
ll t=;
for(int i=;i<m;++i)
{
s.insert(t,i);
t=(t*b)%p;
}
ll d=,ans=-;
ll x,y;
for(int i=;i<m;++i)
{
exgcd(d,p,x,y);
x=((x*n)%p+p)%p;
y=s.get(x);
if(y!=-)
{
ans=i*m+y;
break;
}
d=(d*t)%p;
}
if(ans==-)
printf("no solution\n");
else printf("%I64d\n",ans);
} return ;
}

BSGS算法+逆元 POJ 2417 Discrete Logging的更多相关文章

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

    d. 式子B^L=N(mod P),给出B.N.P,求最小的L. s.下面解法是设的im-j,而不是im+j. 设im+j的话,貌似要求逆元什么鬼 c. /* POJ 2417,3243 baby s ...

  2. POJ 2417 Discrete Logging ( Baby step giant step )

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 ...

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

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

  4. poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2831   Accepted: 1391 ...

  5. POJ 2417 Discrete Logging BSGS

    http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...

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

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

  7. POJ 2417 Discrete Logging 离散对数

    链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...

  8. poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)

    http://poj.org/problem?id=2417 A^x = B(mod C),已知A,B.C.求x. 这里C是素数,能够用普通的baby_step. 在寻找最小的x的过程中,将x设为i* ...

  9. POJ 2417 Discrete Logging

    http://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html 给p,a,b求a^n==b%p #include<a ...

随机推荐

  1. ueditor和thinkphp框架整合修改版

    基于tp官网上的一篇文章修改的  因为tp中所有目录其实都是性对于入口文件的 在原来的基础上略做修改后 已经做到 无论项目放在www下的任何位置 图片在编辑器中回填后都能正常显示! http://fi ...

  2. Neuroph studio 入门教程

    PERCEPTRON Perceptron is a simple two layer neural network with several neurons in input layer, and ...

  3. python设计模式之内置装饰器使用(四)

    前言 python内部有许多内建装饰器,它们都有特别的功能,下面对其归纳一下. 系列文章 python设计模式之单例模式(一) python设计模式之常用创建模式总结(二) python设计模式之装饰 ...

  4. 338.Counting Bits---位运算---《剑指offer》32

    题目链接:https://leetcode.com/problems/counting-bits/description/ 题目大意:求解从0到num的所有数的二进制表示中所有1的个数. 法一:暴力解 ...

  5. Machine Learning系列--L0、L1、L2范数

    今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化.我们先简单的来理解下常用的L0.L1.L2和核范数规则化.最后聊下规则化项参数的选择问题.这里因为篇幅比较庞大,为了不吓到大家,我将这个五个 ...

  6. HTML+CSS小技巧

    网页标题前引入ico图标 <link rel="shortcut icon" href="img/icoTest.ico">

  7. python_xlsxwriter模块

    1.workbook类 add_worksheet 用于添加一个新的工作表,sheetname为工作表名称,默认是sheet1,例如: worksheet = workbook.add_workshe ...

  8. js自动检索输入文章长度

    1. 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  9. csu 1798(树上最远点对,线段树+lca)

    1798: 小Z的城市 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 60  Solved: 16[Submit][Status][Web Board] ...

  10. WP主题模板制作修改教程

    WP主题模板制作修改教程 实际上,当我们打开某个主题的文件夹时,看到的并不止这两个文件,而是更多.但一般来说,在一个完整的 WP 主题文件夹中都应该包含下列文件(也称为模板文件):页面 模板文件 用途 ...