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 step giant step
a^x=b(mod n) n是素数或不是素数都可以
求解上式 0<=x<n的解
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std; #define MOD 76543
int hs[MOD],head[MOD],next[MOD],id[MOD],top; void insert(int x,int y){
int k=x%MOD;
hs[top]=x,id[top]=y,next[top]=head[k],head[k]=top++;
} int find(int x){
int k=x%MOD;
for(int i=head[k];i!=-;i=next[i])
if(hs[i]==x)
return id[i];
return -;
} int BSGS(int a,int b,int n){
memset(head,-,sizeof(head));
top=;
if(b==)return ;
int m=sqrt(n*1.0),j;
long long x=,p=;
for(int i=;i<m;++i,p=p*a%n)insert(p*b%n,i);
for(long long i=m;;i+=m){
if((j=find(x=x*p%n))!=-)return i-j;
if(i>n)break;
}
return -;
} int main(){ int P,B,N;
int ans; while(~scanf("%d%d%d",&P,&B,&N)){
ans=BSGS(B,N,P);
if(ans==-){
printf("no solution\n");
}
else{
printf("%d\n",ans);
}
} return ;
}
参考:http://www.cnblogs.com/yuiffy/p/3877381.html
http://www.cnblogs.com/kuangbin/archive/2013/08/24/3278852.html
POJ - 2417 Discrete Logging(Baby-Step Giant-Step)的更多相关文章
- POJ 2417 Discrete Logging(离散对数-小步大步算法)
Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...
- POJ 2417 Discrete Logging (Baby-Step Giant-Step)
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2819 Accepted: 1386 ...
- BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4860 Accept ...
- BSGS(Baby Steps,Giant Steps)算法详解
BSGS(Baby Steps,Giant Steps)算法详解 简介: 此算法用于求解 Ax≡B(mod C): 由费马小定理可知: x可以在O(C)的时间内求解: 在x=c之后又会循环: 而BS ...
- 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 BSGS
http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...
- POJ 2417 Discrete Logging 离散对数
链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- 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* ...
随机推荐
- SGU101 求有重边的无向图欧拉迹
题意:好多木棒,俩端有数字(0--6)标记,按数字相同的端首尾相连成一条直线(木棒可以相同).即求有重边的无向图欧拉迹. 先判定是否为欧拉图,俩个条件,不说了.如果是欧拉图,输出路经. 方法:dfs遍 ...
- eclipse 安卓虚拟机安装apk 及常见问题
首先必须启动虚拟机然后如图操作:
- Java使用IText(VM模版)导出PDF,IText导出word(二)
===============action=========================== //退款导出word public void exportWordTk() throws IOE ...
- codevs——1690 开关灯
1690 开关灯 USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description YYX家门前的街上有N( ...
- ubuntu安装软件或upgrade出现 You might want to run 'apt-get -f install' to correct these
今天在ubuntu下安装任何软件都提示以下错误: You might want to run 'apt-get -f install' to correct these:The following p ...
- LazyTableImages范例解析
近来想了解iPhone的多线程处理,查阅到了官方范例LazyTableImages,发现代码中有不仅有多线程处理,还有XML解析和异步下载图片,感觉官方例子真是全面啊,便花了时间好好研究下. Lazy ...
- linux命令stat,查看文件详细信息
可以查看文件的各类具体信息:文件权限的数字形式0664:uid.gid的权限的数字形式等 更多用法参考stat --help lsattr test.sh 查看文件的其他属性:只读属性.只可以追加写属 ...
- 使用crontab定时执行脚本时别忘了输出重定向>
原文:https://blog.csdn.net/solmyr_biti/article/details/50683279 -------------------------------------- ...
- Golang Global Variable access
golang 中全局变量的问题. ------------------------------------------------------------------ 17down votefavor ...
- fuel 安装openstack