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 ...
随机推荐
- 用VS2005编译生成Lua库文件和解释器
TMD,本来很简单的东西,网上说的乱七八糟,说的也不明白,大家抄来抄去,估计都不自己实践的..花了半个下午研究了一下,总结一下. 1)下载lua工程文件,地址为http://www.lua.org/f ...
- 使用Visual Studio 创建可视Web Part部件
使用Visual Studio 创建可视Web Part部件 可视Web Part部件是很强大的Web 部件.它提供内置设计器创建你的用户界面. 本文主要解说怎样使用Visual Studio 创建可 ...
- 安装pygame
pygame的安装 我们首先要去到:http://www.pygame.org/download.shtml 下载我们所需要的软件包: 我选择的是:pygame-1.9.2a0.win32-py3.2 ...
- CentOS 网络设置修改
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G) 系统版本:Centos-6.5-x86_64 路由器网关:192.168.1.1 步骤: 1.查看网络MAC地址 [ro ...
- 通信协议:HTTP、TCP、UDP(转)
原文出处: 碧雪轩的博客 TCP HTTP UDP: 都是通信协议,也就是通信时所遵守的规则,只有双方按照这个规则“说话”,对方才能理解或为之服务. TCP HTTP UDP三者的关系 ...
- STM32F103 TIM1输出PWM设置
//TIM1 分频 #define TIM1_DIV1 (1-1) #define TIM1_DIV2 (2-1) #define TIM1_DIV4 (4-1) #define TIM1_DIV8 ...
- WPF学习之绘图和动画--DarrenF
Blend作为专门的设计工具让WPF如虎添翼,即能够帮助不了解编程的设计师快速上手,又能够帮助资深开发者快速建立图形或者动画的原型. 1.1 WPF绘图 与传统的.net开发使用GDI+进行绘图不 ...
- ASM时的OFM特性对影的建数据文件名称的影响及为SYSTEM表空间的数据文件使用别名
客户遇到个DG的问题,存储使用的ASM管理,有多个磁盘盘. 在主库创建数据文件,备库自己主动创建的数据文件都在同一磁盘组,而且在主库创建数据文件是指定的是类似**.DBF的名字,到备库也变成了使用AS ...
- 445port入侵详细解释
445port入侵具体解释 关于"445port入侵"的内容445port入侵具体解释本站搜索很多其它关于"445port入侵"的内容 445port入侵, ...
- hdu 2191 悼念512四川汶川大地震遇难者——如今宝,感恩生活
悼念512四川汶川大地震遇难者--如今宝,感恩生活 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...