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 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模板题

简单说下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的更多相关文章

  1. 【BZOJ】3239: Discrete Logging

    http://www.lydsy.com/JudgeOnline/problem.php?id=3239 题意:原题很清楚了= = #include <bits/stdc++.h> usi ...

  2. POJ2417 Discrete Logging【BSGS】

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5577   Accepted: 2494 ...

  3. POJ2417 Discrete Logging【BSGS】(模板题)

    <题目链接> 题目大意: P是素数,然后分别给你P,B,N三个数,然你求出满足这个式子的L的最小值 : BL== N (mod P). 解题分析: 这题是bsgs算法的模板题. #incl ...

  4. 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 \)这里-的作用是避 ...

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

    2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 4741  Solved: 1796 [Submit][Sta ...

  6. Python开发【杂货铺】:模块logging

    logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式 ...

  7. 【Python】 日志管理logging

    logging *****本文参考了http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html ■ 最最基本的用法 logging模块用 ...

  8. 【python】实用的logging封装

    #!/usr/bin/python import logging import logging.handlers def set_logger(filename, logmod): log_size ...

  9. Codeforces1106F 【BSGS】【矩阵快速幂】【exgcd】

    首先矩阵快速幂可以算出来第k项的指数,然后可以利用原根的性质,用bsgs和exgcd把答案解出来 #include<bits/stdc++.h> using namespace std; ...

随机推荐

  1. Halcon一日一练:图像拼接技术2:步骤与例程

    上一篇主要介绍了图像拼接的一些原理和方法,这一篇将主要介绍步骤和例程: 接上一篇: 基于特征的接拼方法,分为四个步骤 1.特征检测:从图像中检测出显著且独特的图像特征,诸如:闭合区域,直线段,边缘,轮 ...

  2. Java经典编程题50道之十四

    输入某年某月某日,判断这一天是这一年的第几天? public class Example14 {    public static void main(String[] args) {         ...

  3. C/C++语言简介之优缺点

    一.优点1.简洁紧凑.灵活方便 C语言一共只有32个关键字,9种控制语句,程序书写形式自由,区分大小写.把高级语言的基本结构和语句与低级语言的实用性结合起来.C 语言可以像汇编语言一样对位.字节和地址 ...

  4. chrome浏览器下JavaScript实现clipboard时无法访问剪切板解决方案

    在用JavaScript实现某个简单的复制到剪切板功能的时候,会考虑一下浏览器兼容性,主要是重点在IE和FireFox,把这个两个浏览器搞定后,基本上其他浏览器也不用太操心了,Chrome也一样,没出 ...

  5. pro asp.net mvc 5笔记

    1.Ninject条件绑定方法When(predicate)WhenClassHas<T>()WhenInj ectedInto<T>()例: kernel.Bind<I ...

  6. 报错信息 The jsp:param action must not be used outside the jsp:include, jsp:forward, or jsp:params elements 的原因及解决办法

    如果你的代码是这样的话就会报错 <jsp:forward page="02.jsp"></jsp:forward> <jsp:param value= ...

  7. java10 - 泛型与枚举

    java10泛型与枚举 泛型和枚举都是JDK1.5版本之后加入的新特性,泛型将程序代码的类型检查提前到了编译期间进行,枚举类型增强了程序代码的健壮性. 1.泛型类 class VariableType ...

  8. MySQL 日志的类型

    日志文件对于一个服务器来说是非常重要的,它记录着服务器的运行信息,许多操作都会写日到日志文件,通过日志文件可以监视服务器的运行状态及查看服务器的性能,还能对服务器进行排错与故障处理,MySQl中有六种 ...

  9. CodeForces-748B

    关键在于判断是否能够得到解决办法,我的思路就是用一个数组来记录每个小写字母对应的按键,如果它出现对应两个级以上不同的按键那么就说明不能得出解决办法,直接打印'-1'.如果能够得出解决办法,就扫描一下数 ...

  10. 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据

     目录 前言 1 Get方式和Post方式接口说明 2 OkHttp库简单介绍及环境配置 3 具体实现 前言 本文具体实现思路和大部分代码参考自<第一行代码>第2版,作者:郭霖:但是文中讲 ...