【BZOJ3239】Discrete Logging

Description

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

BL== 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".

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

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

题解:BSGS裸题

#include <cstdio>
#include <cstring>
#include <map>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
map<ll,int> mp;
int main()
{
ll A,B,P,i,x,y,m;
while(scanf("%lld%lld%lld",&P,&A,&B)!=EOF)
{
mp.clear(),mp[B]=0,m=ceil(sqrt(P));
for(x=1,i=1;i<=m;i++) x=x*A%P,mp[x*B%P]=i;
for(y=1,i=1;i<=m;i++)
{
y=y*x%P;
if(mp.find(y)!=mp.end())
{
printf("%lld\n",i*m-mp[y]);
break;
}
}
if(i==m+1) printf("no solution\n");
}
return 0;
}

【BZOJ3239】Discrete Logging BSGS的更多相关文章

  1. 【bzoj3239】Discrete Logging

    [吐槽] 这题和[bzoj]2480一毛一样. 就是输入顺序和输出变了一下. 传送门:http://www.cnblogs.com/chty/p/6043707.html

  2. 【poj2417】 Discrete Logging

    http://poj.org/problem?id=2417 (题目链接) 题意 求解$${A^X≡B~(mod~P)}$$ Solution BSGS. 细节 map TLE飞,只好写了hash挂链 ...

  3. 【BZOJ】【3239】Discrete Logging

    BSGS BSGS裸题,嗯题目中也有提示:求a^m (mod p)的逆元可用快速幂,即 pow(a,P-m-1,P) * (a^m) = 1 (mod p) /******************** ...

  4. 【BZOJ2242】计算器(BSGS,快速幂)

    [BZOJ2242]计算器(BSGS,快速幂) 题面 BZOJ 洛谷 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最小非负整数x: 3.给 ...

  5. 【POJ 2417】 Discrete Logging

    [题目链接] http://poj.org/problem?id=2417 [算法] Baby-Step,Giant-Step算法 [代码] #include <algorithm> #i ...

  6. 【BZOJ2242】[SDOI2011]计算器 BSGS

    [BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...

  7. 【译文】Java Logging

    本文讲Java内置的java.util.logging软件包中的 api.主要解释怎样使用该api添加logging到你的application中,怎样加配置它等.但是本文不谈你应该把什么东西写到日志 ...

  8. 【BZOJ-3122】随机数生成器 BSGS

    3122: [Sdoi2013]随机数生成器 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1362  Solved: 531[Submit][Sta ...

  9. 【poj3358】消因子+BSGS 或 消因子+欧拉定理 两种方法

    题意:给你一个分数,求它在二进制下的循环节的长度,还有第一个循环节从哪一位开始. For example, x = 1/10 = 0.0001100110011(00110011)w and 0001 ...

随机推荐

  1. 持续集成之代码质量管理-Sonar

    原文:http://blog.csdn.net/abcdocker/article/details/53840582 Sonar介绍 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Son ...

  2. Linux——解决RedHat6/CentOS6系统中“弹出界面eth0:设备似乎不存在”的问题

    刚刚在自己的CentOS6系统中执行service network restart时,竟然提示: 弹出界面 eth0: 设备 似乎不存在, 初始化操作将被延迟.   [失败] 这事可真神奇.于是手动编 ...

  3. Python——学好Python必读的几篇文章

    作为脚本语言Python上手容易,但要学好Python能写出一手漂亮的.Pythonic的Python代码并非一日之功,本文的目的在于推荐 一些优秀的Python相关的文章(至于书大家可以看dip.l ...

  4. java的IO操作:字节流与字符流操作

    流的概念 程序中的输入输出都是以流形式,流中保存的实际上都是字节文件. 字节流与字符流 字节流的操作: 1)输入:inputStream, 2)输出:outPutStream; 字符流的操作: 1)输 ...

  5. Phalcon 訪问控制列表 ACL(Access Control Lists ACL)

    Phalcon在权限方面通过 Phalcon\Acl 提供了一个轻量级的 ACL(訪问控制列表). Access Control Lists (ACL) 同意系统对用户的訪问权限进行控制,比方同意訪问 ...

  6. Android Volley框架的几种post提交请求方式

    首先简单描述一下Google的Android开发团队在2013年推出的一个网络通信框架Volley.它的设计目标是进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如下载文件等,Vol ...

  7. Redis之持久化和主从同步

    Redis作为内存数据库,所有数据都保存在内存中, 一旦程序停止工作, 数据都将丢失. 需要我们重新从其他地方加载数据. 不过Redis提供了两种方式保存Redis中的数据一种是dump内存直接存入r ...

  8. css中div标签不置顶

    设置div属性垂直对齐方式为:top <div style="vertical-align: top;"></div>

  9. oracle如何将数据导入到另一个表空间

    某个用户的数据在USER表空间里,如果多个用户的数据都在USERS表空间内,将严重影响系统性能,一般在系统迁移的时候,在新的系统里希望导入 一个独立的用户表空间,但是经常无法导入用户指定的缺省表空间, ...

  10. DataUml Design 教程2-实体建模

    DataUml Design 实体建模基于UML类图标准来设计,支持一对一.一对多.多对多关联.模型与开发语言和数据库绑定.1.新建数据模型    1).选择“数据模型” 节点,点击鼠标右键,在菜单中 ...