POJ2417 Discrete Logging【BSGS】(模板题)
<题目链接>
题目大意:
P是素数,然后分别给你P,B,N三个数,然你求出满足这个式子的L的最小值 : BL== N (mod P)。
解题分析:
这题是bsgs算法的模板题。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//baby_step giant_step
// a^x = b (mod n) n为素数,a,b < n
// 求解上式 0<=x < n的解
#define MOD 76543
int hs[MOD],head[MOD],next[MOD],id[MOD],top;
void insert(int x,int y)
{
int k = x%MOD;
hs[top] = x, id[top] = y, next[top] = head[k], head[k] = top++;
} int find(int x)
{
int k = x%MOD;
for(int i = head[k]; i != -; i = next[i])
if(hs[i] == x)
return id[i];
return -;
} int BSGS(int a,int b,int n)
{
memset(head,-,sizeof(head));
top = ;
if(b == )return ;
int m = sqrt(n*1.0), j;
long long x = , p = ;
for(int i = ; i < m; ++i, p = p*a%n)insert(p*b%n,i);
for(long long i = m; ;i += m)
{
if( (j = find(x = x*p%n)) != - )return i-j;
if(i > n)break;
}
return -;
} int main()
{
int a,b,n;
while(scanf("%d%d%d",&n,&a,&b) == )
{
int ans = BSGS(a,b,n);
if(ans == -)printf("no solution\n");
else printf("%d\n",ans);
}
return ;
}
2018-08-09
POJ2417 Discrete Logging【BSGS】(模板题)的更多相关文章
- poj2417 Discrete Logging BSGS裸题
给a^x == b (mod c)求满足的最小正整数x, 用BSGS求,令m=ceil(sqrt(m)),x=im-j,那么a^(im)=ba^j%p;, 我们先枚举j求出所有的ba^j%p,1< ...
- POJ2417 Discrete Logging
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- POJ2417 Discrete Logging【BSGS】
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5577 Accepted: 2494 ...
- POJ2417 Discrete Logging | A,C互质的bsgs算法
题目: 给出A,B,C 求最小的x使得Ax=B (mod C) 题解: bsgs算法的模板题 bsgs 全称:Baby-step giant-step 把这种问题的规模降低到了sqrt(n)级别 首 ...
- 【BZOJ3239】Discrete Logging BSGS
[BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...
- [POJ2417]Discrete Logging(指数级同余方程)
Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...
- POJ 2417 Discrete Logging BSGS
http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...
- 【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 729 Solved: 485[Submit][Statu ...
- Discrete Logging
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5865 Accepted: 2618 ...
随机推荐
- 批量下载Coursera及其他场景上的文件
以下方法同样适用于其他场景的批量下载. 最近在学习Coursera退出的深度学习课程,我希望把课程提供的作业下载下来以备以后复习,但是课程有很多文件,比如说脸部识别一课中的参数就多达226个csv文件 ...
- POI读取Excel(xls、xlsx均可以)——(四)
maven构建的项目-->pom.xml文件 eclipse提供Dependencies直接添加依赖jar包的工具:直接搜索poi以及poi-ooxml即可,maven会自动依赖需要的jar包: ...
- C++ explicit 关键字
原文转自:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造 ...
- Debian ifconfig 命令找不到
如何配置让 Debian 非特权用户也可以使用 ifconfig . ifconfig 在 /sbin 目录下,新建一个用户时, Debian 默认从 /etc/skel/ 复制配置文件, /sbin ...
- ubuntu 删除自带软件的方法
$ sudo dpkg -l | grep -i "need2del" $ sudo dpkg -P 或者: $ sudo apt-get --purge remove need2 ...
- crontab在/var/log/目录下没有cron.log文件
1.修改rsyslog文件: /etc/rsyslog.d/50-default.conf 将 rsyslog 文件中的 #cron.* 前的 # 删掉: 2.重启rsyslog服务: s ...
- Go语言规格说明书 之 Go语句(Go statements)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...
- OCM_第八天课程:Section4 —》数据管理
注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...
- 关系操作符 < > = == <= >= !=
基本类型可以用所有的操作符 对象要用equal eqauls() //用法 System.out.println(v1.equals(v2)); JAVA学习(二) STRING使用EQUALS方法和 ...
- Laravel Blade 模板 @section/endsection 与 @section/show, @yield 的区别
base layout 中需要使用 @section("section_name") 区块链是什么? @show 继承的 blade 中需要使用 @section("se ...