【BZOJ3239】Discrete Logging BSGS
【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 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
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
题解: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的更多相关文章
- 【bzoj3239】Discrete Logging
[吐槽] 这题和[bzoj]2480一毛一样. 就是输入顺序和输出变了一下. 传送门:http://www.cnblogs.com/chty/p/6043707.html
- 【poj2417】 Discrete Logging
http://poj.org/problem?id=2417 (题目链接) 题意 求解$${A^X≡B~(mod~P)}$$ Solution BSGS. 细节 map TLE飞,只好写了hash挂链 ...
- 【BZOJ】【3239】Discrete Logging
BSGS BSGS裸题,嗯题目中也有提示:求a^m (mod p)的逆元可用快速幂,即 pow(a,P-m-1,P) * (a^m) = 1 (mod p) /******************** ...
- 【BZOJ2242】计算器(BSGS,快速幂)
[BZOJ2242]计算器(BSGS,快速幂) 题面 BZOJ 洛谷 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最小非负整数x: 3.给 ...
- 【POJ 2417】 Discrete Logging
[题目链接] http://poj.org/problem?id=2417 [算法] Baby-Step,Giant-Step算法 [代码] #include <algorithm> #i ...
- 【BZOJ2242】[SDOI2011]计算器 BSGS
[BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...
- 【译文】Java Logging
本文讲Java内置的java.util.logging软件包中的 api.主要解释怎样使用该api添加logging到你的application中,怎样加配置它等.但是本文不谈你应该把什么东西写到日志 ...
- 【BZOJ-3122】随机数生成器 BSGS
3122: [Sdoi2013]随机数生成器 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1362 Solved: 531[Submit][Sta ...
- 【poj3358】消因子+BSGS 或 消因子+欧拉定理 两种方法
题意:给你一个分数,求它在二进制下的循环节的长度,还有第一个循环节从哪一位开始. For example, x = 1/10 = 0.0001100110011(00110011)w and 0001 ...
随机推荐
- Office如何加密解密
1 任意打开一个EXCEL文档,并点击工具,选项,切换到安全性选项卡,任意设置密码 设置密码保护之后再次打开就需要输入密码 我们使用该软件Office PassWord Remover打开该文档( ...
- .Net——实现IConfigurationSectionHandler接口定义处理程序处理自己定义节点
除了使用.net里面提供的内置处理程序来处理我们的自己定义节点外,我们还能够通过多种方法,来自己定义处理类处理我们的自己定义节点,本文主要介绍通过实现IConfigurationSectionHand ...
- java泛型 7 泛型的基本介绍和使用
现在开始深入学习Java的泛型了,以前一直只是在集合中简单的使用泛型,根本就不明白泛型的原理和作用.泛型在java中,是一个十分重要的特性,所以要好好的研究下. 一.泛型的基本概念 泛型的定义:泛型是 ...
- 获取request中的查询参数
//获取request中的查询参数 public static Map<String, Object> getRequestParamsByMap(HttpServletRequest r ...
- Android设置拍照或者上传本地图片
效果例如以下: 看代码: MainActivity类中: package com.example.ceshidemo; import java.io.ByteArrayOutputStream; im ...
- spring自己主动装配Bean属性
spring提供了3种类型的自己主动装配 byName:把与Bean的属性具有同样名字(或者ID)的其它Bean自己主动装配到Bean的相应属性中. byType:把与Bean的属性具有同样类型的其它 ...
- nnlog-yaml
from nnlog import Logger# log=Logger(file_name='my.log',level='debug',# when='S',backCount=5,interva ...
- Mac 常用命令介绍
1.查看所有shell cat /etc/shells 2.查看当前使用的shell类型 $ echo $SHELL 3.
- C++语言基础(12)-虚函数
一.虚函数使用的注意事项 1.只需要在虚函数的声明处加上 virtual 关键字,函数定义处可以加也可以不加. 2.为了方便,你可以只将基类中的函数声明为虚函数,这样所有子类中具有遮蔽(覆盖)关系的同 ...
- C++语言基础(4)-构造函数和析构函数
一.构造函数 类似于java,C++中也有构造函数的概念,相关用法如下: 1.1 构造函数的定义 #include <iostream> using namespace std; clas ...