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* ...
随机推荐
- js react 全选和反选
onCheckAll = (data) => { var CheckBox = document.getElementsByName(data); for(let i=0;i<CheckB ...
- java集合系列之ArrayList源码分析
java集合系列之ArrayList源码分析(基于jdk1.8) ArrayList简介 ArrayList时List接口的一个非常重要的实现子类,它的底层是通过动态数组实现的,因此它具备查询速度快, ...
- android EditText监听和长度监测事件
<?xml version="1.0" encoding="utf-8"?> <!-- 定义基础的LinearLayout布局 --> ...
- ivy 入门
ivy 入门 http://www.blogjava.net/aoxj/archive/2009/03/31/263012.html https://www.cnblogs.com/end/archi ...
- iOS开发之创建颜色渐变视图View
在iOS开发中有时需要自己自定义一个视图view的背景,而网上有人提出的在循环中不断alloc的方法设置其背景色渐变,会耗费很多内存和资源,极其不明智,而在CALayer中早就提供有图层渐变的类和相应 ...
- hdu 1679 The Unique MST (克鲁斯卡尔)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24152 Accepted: 8587 D ...
- react 创建组件 (四)Stateless Functional Component
上面我们提到的创建组件的方式,都是用来创建包含状态和用户交互的复杂组件,当组件本身只是用来展示,所有数据都是通过props传入的时候,我们便可以使用Stateless Functional Compo ...
- 九度OJ #1437 To Fill or Not to Fil
题目描写叙述: With highways available, driving a car from Hangzhou to any other city is easy. But since th ...
- AJAX核心XMLHTTPRequest对象
老早就写好了总结.今天整理发表一下. XMLHttpRequest对象是AJAX的核心技术,XMLHttpRequest 是XMLHTTP 组件的对象,通过这个对象.AJAX能够像桌面应用程序一样仅仅 ...
- C# 通过window消息控制指定控件的scroll滚动
[DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)] private st ...