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 ...
随机推荐
- 《Programming WPF》翻译 第6章 4.应用程序全球化
原文:<Programming WPF>翻译 第6章 4.应用程序全球化 如果你打算发布你的应用程序到全球各地,你可能需要为不同地区的用户界面准备不同的版本.至少,这需要解决将文本翻译成适 ...
- 关于在C#中实现AOP 拦截编程模式的新的探索
前面有篇文章,是从其他个人博客中贴过来的.地址:http://www.lanhusoft.com/Article/240.html 作者总结实现的挺好. 但是.不能不考虑性能!!使用 ContextB ...
- 测试jsp
一. get.jsp <%@ page contentType="text/html;charset=UTF-8"%> <%@ page import=" ...
- openwrt下和云端通讯的例程,
openwrt下和云端通讯的例程 ibcurl 官方文档请参考这里 http://curl.haxx.se/libcurl/c/curl_easy_setopt.html 刚接触 libcurl的时候 ...
- UML--一些图
通过UML来表示汽车,简洁明了. 统一建模语言--UML. 参与者Actor,参与者代表了现实世界的人.人. 用例use case,就是参与者要做什么并且获得什么.事. 业务场景,用例场景.规则. 业 ...
- 页和区 sql server
原文地址:http://msdn.microsoft.com/zh-cn/library/ms190969.aspx SQL Server 中数据存储的基本单位是页.为数据库中的数据文件(.mdf 或 ...
- Hdu3072-Intelligence System(强连通求最小值)
After a day, ALPCs finally complete their ultimate intelligence system, the purpose of it is of cour ...
- [Oracle] 参数修改小结
v$parameter Oracle参数的修改比较复杂,有些参数是可以在session级别修改,有些则必须在system级别修改,有些参数修改后马上生效(不需要重启),有些参数则必须重启才能生效,那么 ...
- python高级编程 编写一个包1
#目的是:编写,发行python包可重复过程"""1:是缩短开始真正工作之前所需要的设置时间,也就是提供模板2:提供编写包的标准化方法3:简化测试驱动开发方法的使用4:为 ...
- String和StringBuilder 的使用区别
String 类有不可变性,每次执行操作时都会创建一个新的String对像,需要对该对象分配新的空间. StringBuilder 解决了对字符串重复修改过程中创建大量对象的问题.初始化一个Strin ...