Discrete Logging
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5865   Accepted: 2618

Description

Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that

    B

L

 == N (mod P)

Input

Read several lines of input, each containing P,B,N separated by a space.

Output

For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print "no solution".

Sample Input

5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111

Sample Output

0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587

Hint

The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat's theorem that states

   B

(P-1)

 == 1 (mod P)

for any prime P and some other (fairly rare) numbers known as base-B
pseudoprimes. A rarer subset of the base-B pseudoprimes, known as
Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A
corollary to Fermat's theorem is that for any m

   B

(-m)

 == B

(P-1-m)

 (mod P) .

Source

BSGS模板题
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#define LL long long
using namespace std;
LL a,b,c;
map<LL,LL>mp;
LL fastpow(LL a,LL p,LL c)
{
LL base=a;LL ans=;
while(p!=)
{
if(p%==)ans=(ans*base)%c;
base=(base*base)%c;
p=p/;
}
return ans;
}
int main()
{
// a^x = b (mod c)
while(scanf("%lld%lld%lld",&c,&a,&b)!=EOF)
{
LL m=ceil(sqrt(c));// 注意要向上取整
mp.clear();
if(a%c==)
{
printf("no solution\n");
continue;
}
// 费马小定理的有解条件
LL ans;//储存每一次枚举的结果 b* a^j
for(LL j=;j<=m;j++) // a^(i*m) = b * a^j
{
if(j==)
{
ans=b%c;
mp[ans]=j;// 处理 a^0 = 1
continue;
}
ans=(ans*a)%c;// a^j
mp[ans]=j;// 储存每一次枚举的结果
}
LL t=fastpow(a,m,c);
ans=;//a ^(i*m)
LL flag=;
for(LL i=;i<=m;i++)
{
ans=(ans*t)%c;
if(mp[ans])
{
LL out=i*m-mp[ans];// x= i*m-j
printf("%lld\n",(out%c+c)%c);
flag=;
break;
} }
if(!flag)
printf("no solution\n");
} return ;
}

Discrete Logging的更多相关文章

  1. 【BSGS】BZOJ3239 Discrete Logging

    3239: Discrete Logging Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 729  Solved: 485[Submit][Statu ...

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

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

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

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

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

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

  5. POJ2417 Discrete Logging【BSGS】

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5577   Accepted: 2494 ...

  6. 【BZOJ3239】Discrete Logging BSGS

    [BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...

  7. [POJ2417]Discrete Logging(指数级同余方程)

    Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...

  8. BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)

    我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...

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

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

随机推荐

  1. 算法导论学习之线性时间求第k小元素+堆思想求前k大元素

    对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思 ...

  2. STL vector的介绍(1)

    尝试下翻译STL里面的一些easy和算法.四级过了.六级刚考.顺便练练自己的英语水平.翻译的不好的地方请大神多多不吝赐教哈.方便我改正. 原来均来自:http://www.cplusplus.com/ ...

  3. mysql最新版中文参考手册在线浏览

    MySQL是最流行的开放源码SQL数据库管理系统,具有快速.可靠和易于使用的特点.同时MySQL也是一种关联数据库管理系统,具有很高的响应速度和灵活性.又因为mysql拥有良好的连通性.速度和安全性, ...

  4. IDEA-Maven的环境配置及使用

    一.Maven的下载 IDEA的往期下载地址:https://www.jetbrains.com/ 1.点击进入 1.往期的下载地址:http://www.apache.org/ 操作步骤:我们点击进 ...

  5. 2016/2/24 1,dotctype有几种? 2,了解html的发展历史

    1,dotctype有几种?DOCTYPE是document type(文档类型)的简写,用来说明你用的XHTML或者HTML是什么版本. 其中的DTD(例如上例中的xhtml1-transition ...

  6. Hessian Matrix 多元函数的极值 半正定矩阵 正定矩阵

    https://baike.baidu.com/item/黑塞矩阵/2248782?fr=aladdin 海塞矩阵 Hasse https://baike.baidu.com/item/半正定矩阵

  7. linux系统无法上外网,路由器可以上网,可以ping通路由器,ping不通外网IP

    临时生效方法(添加路由网关),执行: #route add default gw 192.168.92.1   #根据实际网关IP填写 如果不行,使用下面方法: 一:使用 route 命令添加使用ro ...

  8. 并不对劲的bzoj5342:loj2554:uoj401:p4566: [Ctsc2018]青蕈领主

    题目大意 \(T\)(\(T\leq100\))组询问 有\(1\)到\(n\)(\(n\leq50000\))这\(n\)个整数组成的一个排列 定义这个排列的一个子区间是"连续" ...

  9. Python装饰器单例

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dutsoft/article/details/52057981#!/usr/bin/python#c ...

  10. c#自动登录

    c#自动登录 http://blog.csdn.net/eastmount/article/details/9703757