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)的更多相关文章

  1. BSGS算法+逆元 POJ 2417 Discrete Logging

    POJ 2417 Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4860   Accept ...

  2. poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2831   Accepted: 1391 ...

  3. POJ 2417 Discrete Logging (Baby-Step Giant-Step)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2819   Accepted: 1386 ...

  4. 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 ...

  5. POJ 2417 Discrete Logging ( Baby step giant step )

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 ...

  6. POJ 2417 Discrete Logging 离散对数

    链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...

  7. POJ 2417 Discrete Logging(离散对数-小步大步算法)

    Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...

  8. POJ 2417 Discrete Logging BSGS

    http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...

  9. POJ 2417 Discrete Logging

    http://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html 给p,a,b求a^n==b%p #include<a ...

随机推荐

  1. 屌丝程序猿赚钱之道之taobao 2

    续上篇,之前写的案例,都是比較0基础的. 案例4:  代写情书.软文.论文等等. 这是我一个同学的真实故事.     我隔壁寝室的小王平时没事就爱谢谢博客.逛逛论坛.大二的时候接触了威客网,開始在网上 ...

  2. CentOS 如何使用第三方软件库-EPEL与RPMForge、RPMFusion软件库

    在CentOS下运行yum install flash-plugin或yum install mplayer的时候,提示库里没有找到这个软件?为什么会这样?因为CentOS是RHEL编译过来的,去掉了 ...

  3. HGE引擎 - 绘制,声音,碰撞处理

    原帖地址:http://blog.csdn.net/i_dovelemon/article/details/8818037 另外,年代久远,该引擎官网早已上不去了!!! 1.库的安装和下载 从官网上h ...

  4. Linux訪问受限,訪问Linux总是Forbidden

    很多其它具体文档:http://download.csdn.net/download/zml_2015/8843061 有些人在依照要求一模一样照做之后,加上了全部的权限.可还是无法訪问,仍然是各种f ...

  5. 高性能 TCP &amp; UDP 通信框架 HP-Socket v3.2.2 正式公布

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包括服务端组件.client组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#. ...

  6. ASP.NET Core MVC Hello World

    ASP.NET Core 现在ASP.NET Core还在不断成长.更新中,说不定到了明天又换了个模样,就如同一个小孩,从蹒跚学步,到奔向未来. 所以我们可以相应的去理解更新中所发生的变化,包容它.呵 ...

  7. JavaScript权威指南科03章 种类、值和变量(1)

    种类.值和变量 数据类型分类: 基本类型(primitive type):数位 弦 布尔值 null undefined 对象类型(object type): 对象是属性的集合,每一个属性都由&quo ...

  8. 教你如何使用U盘装系统

    首先,你必须有一个4G以上U菜,然后,U光盘制作软件(这里我们使用url=KRVS0FUdaNAMKPUXUxjEijxBMalUjaJHph-tL-x4gXGSwVNUW3fj6RfuZtrMg1Y ...

  9. 1pdf

    Document doc = new Document(new iTextSharp.text.Rectangle(564, 351));  PdfWriter writer= PdfWriter.G ...

  10. 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch

    原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...