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 ...
随机推荐
- Java面试题之十
五十六.java中有几种类型的流?JDK 为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类? 主要有字节流,字符流.字节流继承于InputStream .OutputStream,字符流 ...
- Horner规则
霍纳(Horner)规则是采用最少的乘法运算策略,求多项式 A(x) = a[n]x^n + a[n-1]x^(n-1) + ... + a[1]x^1 + a[0]x^0 在x处的值. 该规则为 A ...
- STL List容器
转载http://www.cnblogs.com/fangyukuan/archive/2010/09/21/1832364.html 各个容器有很多的相似性.先学好一个,其它的就好办了.先从基础开始 ...
- 本地plsqldev.exe连接远端oracle数据库
先看百度经验:http://jingyan.baidu.com/article/48b558e3540ecf7f38c09a3c.html 这里如果我们只有安装plsql工具,下载oracle精简版本 ...
- Android之字符串的拆分-split
字符串的拆分可以利用android的 split 来简单实现 具体看如下代码: String s3 = "Real-How-To"; String [] temp = null; ...
- web前端之 CSS
CSS概述 CSS 指层叠样式表 (Cascading Style Sheets),说白了就是给html代码穿上好看的衣服,让页面变得好看 CSS存在形式 1.在标签的属性中设置,优先级较高 代码如下 ...
- Non-negative Partial Sums(单调队列)
Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- SQL参数化
本文来自:caodonglin 一.SQL参数化为什么能防注入? 因为执行计划被重用了,所以可以防SQL注入. 下面有两段SQL 正常SQL: 1 select COUNT(1) from C ...
- uva 11210 Chinese Mahjong(暴力搜索)
Chinese Mahjong Mahjong () is a game of Chinese origin usually played by four persons with tiles res ...
- css_day5