POJ 2417 Discrete Logging
http://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html
给p,a,b求a^n==b%p
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<map>
#define ll long long
ll p;
std::map<ll , int> mp;
ll Pow(ll x,ll y){
ll res=;
while (y){
if (y%) res=(res*x)%p;
x=(x*x)%p;
y/=;
}
return res;
}
ll solve(ll a,ll b){
a%=p;
if (a==){
if (b==) return ;
else return -;
}
mp.clear();
ll m=sqrt(p)+,t=,I=;
for (int i=;i<m;i++){
t=t*a%p;
if (!mp[t]) mp[t]=i;
}
mp[]=m+;
ll Im=Pow(a,p--m);
for (int k=;k<m;k++){
int i=mp[I*b%p];
if (i!=){
if (i==m+) i=;
return i+k*m;
}
I=I*Im%p;
}
return -;
}
int main(){
ll a,b;
while (scanf("%lld%lld%lld",&p,&a,&b)!=EOF){
ll ans=solve(a,b);
if (ans==-) puts("no solution");
else printf("%lld\n",ans);
}
}
POJ 2417 Discrete Logging的更多相关文章
- BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4860 Accept ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- POJ 2417 Discrete Logging (Baby-Step Giant-Step)
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2819 Accepted: 1386 ...
- 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 ...
- POJ 2417 Discrete Logging ( Baby step giant step )
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3696 Accepted: 1727 ...
- POJ 2417 Discrete Logging 离散对数
链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...
- 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* ...
- POJ 2417 Discrete Logging BSGS
http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...
- POJ 2417 Discrete Logging(离散对数-小步大步算法)
Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...
随机推荐
- Oracle静态监听和动态监听
一.静态注册 静态注册指实例启动时读取listener.ora配置文件,将实例和服务注册到监听程序.无论何时启动一个数据库,默认都有两条信息注册到监听器中:实例和服务.SID_LIST_LISTENE ...
- 【转】Android源码下载过程的一些注意事项
原文网址:http://www.360doc.com/content/14/0113/11/11948835_344809459.shtml 其它一些事项说明: 1.在源代码下载过程中,我们在源代码下 ...
- ps查看内存占用排序
ps -eo rss,pmem,pcpu,vsize,args | sort -k 1 -r -n | less 解析一下: ps 都知道,是linux,unix显示进程信息的, -e 是显示所有进程 ...
- hdu 5344 MZL's xor(数学之异或)
Problem Description MZL loves xor very much.Now he gets an array A.The length of A ≤i,j≤n) The xor ...
- 系统中断与SA_RESTART
今天在调试程序时,sem_timedwait居然返回了一个Interrupted system call,错误码为EINTR.系统中断这东西我一向只闻其名,不见其"人",不想今天遇 ...
- pyqt labe界面超级链接例子学习
def bz(self): self.lable1=QtGui.QLabel(u'<br><a href=http://windows.microsoft.com/zh-cn/win ...
- HttpServletRequest对象请求转发和HttpServletResponse对象请求重定向之间的区别
HttpServletRequest对象request代表浏览器请求对象,HttpServletResponse对象代表服务器响应对象,当用浏览器访问web服务器,发出请求时,Servlet调用ser ...
- MySql命令——命令行客户机的分隔符
delimiter // create procedure productpricint() begin select avg(price) as priceaverage from product; ...
- Javascript - IE8下parseInt()方法的取值异常
公司的测试小妹妹跑来对我说,下拉框第9项始终无法正确提交的时候,我还以为见鬼了. parseInt()会把'0'开头的数字以8进制来解析,当有大于7的数字时候就按10进制来解析. // p ...
- (转)javascript中 window.location= window.location;是什么意思
重载当前页面.但是,重载后,一些$_GET或者$_POST $_REQUEST 表单提交的数据会清空. 重新加载当前页面的资源,就是刷新本页面