【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 729 Solved: 485
[Submit][Status][Discuss]
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
0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587
题解
BSGS模板题
简单说下BSGS(baby step giant step)
用于解决离散对数问题即求解ax≡b(mod p)中的x
先处理出sqrt(p)范围内的b*ax的值,丢进map里
然后依次求出k*ak*sqrt(p)(k=1……sqrt(p))看答案是否有出现过
本质上就是分块的思想
代码
//by 减维
#include<set>
#include<map>
#include<queue>
#include<ctime>
#include<cmath>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define il inline
#define rg register
#define db double
#define mpr make_pair
#define maxn
#define inf (1<<30)
#define eps 1e-8
#define pi 3.1415926535897932384626L
using namespace std; inline int read()
{
int ret=;bool fla=;char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-'){fla=;ch=getchar();}
while(ch>=''&&ch<=''){ret=ret*+ch-'';ch=getchar();}
return fla?-ret:ret;
} ll x,y,p; ll ksm(ll x,ll y,ll p)
{
ll ret=;x%=p;
for(;y;y>>=,x=x*x%p)
if(y&) ret=ret*x%p;
return ret;
} ll bsgs(ll x,ll y,ll p)
{
map<ll,ll> mp;
x%=p;y%=p;
if(!x&&!y) return ;
if(y==) return ;
if(!x) return -;
ll m=sqrt(p+0.5);
ll o=y;
for(int i=;i<=m;++i,o=o*x%p)
if(!mp.count(o)) mp[o]=i;
ll tmp=ksm(x,m,p);o=tmp;
for(int i=;i<=m;++i,o=o*tmp%p)
if(mp.count(o)) return ((i*m-mp[o])%p+p)%p;
return -;
} int main()
{
while(scanf("%lld%lld%lld",&p,&x,&y)!=EOF)
{
ll ans=bsgs(x,y,p);
if(ans==-) printf("no solution\n");
else printf("%lld\n",ans);
}
return ;
}
【BSGS】BZOJ3239 Discrete Logging的更多相关文章
- 【BZOJ】3239: Discrete Logging
http://www.lydsy.com/JudgeOnline/problem.php?id=3239 题意:原题很清楚了= = #include <bits/stdc++.h> usi ...
- POJ2417 Discrete Logging【BSGS】
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5577 Accepted: 2494 ...
- POJ2417 Discrete Logging【BSGS】(模板题)
<题目链接> 题目大意: P是素数,然后分别给你P,B,N三个数,然你求出满足这个式子的L的最小值 : BL== N (mod P). 解题分析: 这题是bsgs算法的模板题. #incl ...
- bzoj 3239: Discrete Logging && 2480: Spoj3105 Mod【BSGS】
都是BSGS的板子题 此时 \( 0 \leq x \leq p-1 \) 设 \( m=\left \lceil \sqrt{p} \right \rceil ,x=i*m-j \)这里-的作用是避 ...
- BZOJ2242 [SDOI2011]计算器 【BSGS】
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 4741 Solved: 1796 [Submit][Sta ...
- Python开发【杂货铺】:模块logging
logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式 ...
- 【Python】 日志管理logging
logging *****本文参考了http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html ■ 最最基本的用法 logging模块用 ...
- 【python】实用的logging封装
#!/usr/bin/python import logging import logging.handlers def set_logger(filename, logmod): log_size ...
- Codeforces1106F 【BSGS】【矩阵快速幂】【exgcd】
首先矩阵快速幂可以算出来第k项的指数,然后可以利用原根的性质,用bsgs和exgcd把答案解出来 #include<bits/stdc++.h> using namespace std; ...
随机推荐
- thinkphp5判断移动或pc端访问并调用不同模板
废话不多说,直接上代码 先修改\thinkphp\library\think\view\driver\Think.php文件 把 public function __construct($config ...
- Centos/linux下的JDK安装
1.连接到centos的远程主机上 #你的用户名 ssh root@xxxxx 2.输入密码登录 3.最好将镜像改为阿里云的镜像 http://mirrors.aliyun.com/help/cent ...
- 来了解一下Mysql索引的相关知识:基础概念、性能影响、索引类型、创建原则、注意事项
索引的基础概念索引类似于书籍的目录,要想找到一本书的某个特定主题,需要先查找书的目录,定位对应的页码:存储引擎使用类似的方式进行数据查询,先去索引当中找到对应的值,然后根据匹配的索引找到对应的数据行 ...
- DLL文件修复
当你在Windows计算机中安装非操作系统的软件时,往往会覆盖或改写系统共享文件, 如动态链接库(.dll文件)和可执行文件(.exe文件). * 对于Windows系统来说,当用户操作不当(如非正常 ...
- window 环境变量保存位置在哪里?
注册表-regedit.exe 用户变量: HKEY_CURRENT_USER\Environment 系统变量: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSe ...
- hihoCoder 树结构判定(并查集)
思路:树满足两个条件: 1.顶点数等于边数加一 2.所有的顶点在一个联通块 那么直接dfs或者并查集就可以了. AC代码 #include <stdio.h> #include<st ...
- DataTable筛选某列最大值
dt.Compute("max(列名)",""); Compute函数的参数就两个:Expression,和Filter. Expresstion是计算表达式, ...
- Android开发之Android Context Menu
1 Creatinga上下文菜单, Todefine上下文菜单的外观和行为,推翻youractivity的上下文菜单回滚方法,onCreateContextMenu()和onContextItemSe ...
- R语言自动化报告格式——knitr
R语言自动化报告格式--knitr 相关文献: R语言自动化报告格式--knitr 资讯 | R Notebooks 即将发布 ------------------------------------ ...
- strstr()函数实现
/* 函数要求:写一个函数模拟strstr()函数,设计中不得使用其他库函数. 函数原型:const char *strstr(const char *str1,const char *str2); ...