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*M+j。从而原始变为A^M^i * A^j = B(mod C),D = A^M,那么D^i * A^j = B(mod C ),
预先将A^j存入hash表中,然后枚举i(0~M-1),依据扩展欧几里得求出A^j。再去hash表中查找对应的j,那么x = i*M+j。
确定x是否有解,就是在循环i的时候推断对应A^j是否有解。并且最小的解x一定在(0~C-1),由于gcd(D^i,C) = 1.
假设(0~C-1)无解,那么一定无解。
由于A^x%C(C是素数)有循环节。A^x%C = A^(x%phi[c])%C,循环节的长度为phi(C),即C-1,x >= C以后開始新一轮的循环,因此(0~C-1)内无解的话。一定无解。
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0) using namespace std;
const int maxn = 499991; bool hash[maxn+10];
int idx[maxn+10];
LL val[maxn+10];
//插入哈希表
void insert(int id, LL vv)
{
int v = vv % maxn;
while(hash[v] && val[v] != vv)
{
v++;
if(v == maxn)
v -= maxn;
}
if(!hash[v])
{
hash[v] = true;
idx[v] = id;
val[v] = vv;
}
}
//查找vv相应的jj,A^jj = vv
int found(LL vv)
{
int v = vv%maxn;
while(hash[v] && val[v] != vv)
{
v++;
if(v == maxn)
v -= maxn;
}
if(hash[v] == false)
return -1;
return idx[v];
} void extend_gcd(LL a, LL b, LL &x, LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return;
}
extend_gcd(b,a%b,x,y);
LL t = x;
x = y;
y = t-a/b*y;
}
/*
A^x = B(mod C)
令x = i*M+j, 当中M = ceil(sqrt(C*1.0)),(0 <= i,j < M)
那么原式变为A^M^i*A^j = B(mod c)
先枚举j(0~M-1),将A^j%C存入hash表中
令D = A^M%C,X = A^j,那么D^i*X = B(mod C)
枚举i(0~M-1)求得D^i设为DD。DD*X = B(mod C)
DD,C已知,由于C是素数,gcd(DD,C) = 1,依据扩展欧几里得知在[0,C-1]内有唯一一个解X。
然后在hash表中查找X相应的jj。即A^jj = X。 那么x = i*M+jj,若找不到jj无解。
*/
LL baby_step(LL A, LL B, LL C)
{
memset(hash,false,sizeof(hash));
memset(idx,-1,sizeof(idx));
memset(val,-1,sizeof(val)); LL M = ceil(sqrt(C*1.0));
//将A^j存入hash表中
LL D = 1;
for(int j = 0; j < M; j++)
{
insert(j,D);
D = D*A%C;
}
//D = A^M%C,res = D^i,求方程res*X = B(mod C)中的X,去找X相应的jj,那么x=i*M+jj.
LL res = 1,x,y;
for(int i = 0; i < M; i++)
{
extend_gcd(res,C,x,y);
x = x*B;
x = (x%C+C)%C;
int jj = found(x);
if(jj != -1)
{
return (LL)i*M+jj;
}
res = res*D%C;
}
return -1;
} int main()
{
LL A,B,C;
while(~scanf("%lld %lld %lld",&C,&A,&B))
{
LL res = baby_step(A,B,C);
if(res == -1)
printf("no solution\n");
else
printf("%lld\n",res);
}
return 0;
}
poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)的更多相关文章
- 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(离散对数-小步大步算法)
		Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ... 
- 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://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html 给p,a,b求a^n==b%p #include<a ... 
随机推荐
- GString及IntelliJIdea中调试Groovy的操作步骤
			今天是学习Groovy的第一天,首先我觉得学习任何一种语言都要先弄清楚这种语言的特性,因为只有了解了特性之后学习才能达到好的效果,那么groovy的特点是什么的.我觉得groovy是一种动态语言,动态 ... 
- Eclipse中的SVN的冲突解决方案详解
			版本冲突原因: 假设A.B两个用户都在版本号为100的时候,更新了kingtuns.txt这个文件,A用户在修改完成之后提交kingtuns.txt到服务器,这个时候提交成功,这个时候kingtuns ... 
- Cracking the coding interview--问题与解答
			http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ... 
- windows使用nginx+memcached实现负载均衡和session或者缓存共享
			windows使用nginx+memcached实现负载均衡和session或者缓存共享 两台server server1:115.29.186.215 windows2008 64位操作系统 ser ... 
- 【LaTeX排版】LaTeX论文排版<三>
			A picture is worth a thousand words(一图胜千言).图在论文中的重要性不言而喻,本文主要解说图的制作与插入. 1.图像的插入 图像能够分为两大类:位图和向量图 ... 
- robot framework 使用四:分层设计和截图以及注意事项
			再说一下眼下的主要环境信息和版本号: 操作系统:win7 64位 python版本号:2.7.6 RIDE版本号:1.2.3 selenium2library:1.5.0 selenium:2.40. ... 
- 从lca到树链剖分 bestcoder round#45 1003
			bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或) ... 
- python之字符串的分割和拼接
			关于string的split 和 join 方法 对导入os模块进行os.path.splie()/os.path.join() 貌似是处理机制不一样,但是功能上一样. 1.string.split( ... 
- oracle 11g impdp时 报ORA-12899(转)
			源库ZHS16BGK,汉字在数据库存放的时候占用两个字节 目标库UTF8,汉字在数据库里存放的时候占用三个字节 由于字符集不同,导致现在数据库impdp的时候有些表的字段长度不够,出现ORA-1289 ... 
- ZOJ 2334(Monkey King-左偏树第一题)
			Monkey King Time Limit: 10 Seconds Memory Limit: 32768 KB Once in a forest, there lived N aggre ... 
