【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. org.hibernate.exception.ConstraintViolationException: could not delete:

    转自:http://fireinwind.iteye.com/blog/848515 异常描述: org.hibernate.exception.ConstraintViolationExceptio ...

  2. jQuery编程小结

    加载jQuery 1.坚持使用CDN来加载jQuery,这种别人服务器免费帮你托管文件的便宜干嘛不占呢.点击查看使用CDN的好处,点此查看一些主流的jQuery CDN地址. <script t ...

  3. U盘制作linux centos6.5

    2015年8月4日 1.下载ULTIso软件,注册 2.DVD1的那个拖进去 3.[一定要双击那个进去]才“写入硬盘镜像”,否则只“写入‘,还是一个iso文件,不是提取出来的文件. 4.覆盖相应的文件 ...

  4. MongoDB笔记(三):Document 数据插入、删除、更新

    实例数据库caliven.集合users 一.插入文档 1.插入文档 db.[documentName].insert({}) 2.批量插入文档 .shell 这样执行是错误的db.[document ...

  5. tcp/ip --- IP路由选择及子网寻址

    IP路由选择 当一个IP数据包准备好了的时候,IP数据包(或者说是路由器)是如何将数据包送到目的地的呢?它是怎么选择一个合适的路径来"送货"的呢? 最特殊的情况是目的主机和主机直连 ...

  6. C#--I/O流操作文本文件之StreamWrite类和StreamReader类

    使用I/O流操作文本文件时主要用到StreamWrite类和StreamRead类. 1.StreamWrite类 (1)StreamWrite类专门用来处理文本文件的类.能够方便地想文本文件里写入字 ...

  7. Silverlight实例教程 - Validation数据验证DataAnnotation机制和调试技巧(转载)

    Silverlight 4 Validation验证实例系列 Silverlight实例教程 - Validation数据验证开篇 Silverlight实例教程 - Validation数据验证基础 ...

  8. 李洪强漫谈iOS开发[C语言-003]-开发概述程序设计语言

    李洪强iOS开发之程序设计语言 printf 是打印的意思- 格式化输出 f: format 格式化 C语言编译器 编译器的功能就是将高级语言的源代码,翻译成机器可以识别的二进制文件就是可执 行文件- ...

  9. 谱聚类python实践

    聚类后: # -*- coding: utf-8 -*-"""Created on 09 05 2017 @author: similarface"" ...

  10. Windows Server 2008 R2入门之用户管理

    一.用户账户概述: ”用户”是计算机的使用者在计算机系统中的身份映射,不同的用户身份拥有不同的权限,每个用户包含一个名称和一个密码: 在Windows中,每个用户帐户有一个唯一的安全标识符(Secur ...