Discrete Logging

Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that

    B

L

 == 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".

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

HinThe 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) .


题解:这道题目是Baby-step giant-step的裸题吧,嗯嗯,不要忘了开long long就可以了。
 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#include<map>
#define ll long long
using namespace std; ll p,b,n; int ex_gcd(ll a,ll b,ll &x,ll &y)
{
if (!b){x=,y=;return a;}
else
{
int fzy=ex_gcd(b,a%b,x,y);
int t=x;x=y;
y=t-a/b*y;
return fzy;
}
}
void solve()
{
ll m=(ll)sqrt(p);
map<int,int>num;
map<int,bool>app;
app[]=,num[]=;
ll zhi=;
for (int i=;i<=m-;i++)
{
zhi=zhi*b%p;
if (!app[zhi])
{
app[zhi]=;
num[zhi]=i;
}
}
zhi=zhi*b%p;
ll x,y,nn=n;
int fzy=ex_gcd(zhi,p,x,y);
x=(x+p)%p;
for (int i=;i<=m;i++)
{
if (app[nn])
{
printf("%lld\n",i*m+num[nn]);
return;
}
else nn=nn*x%p;
}
printf("no solution\n");
}
int main()
{
while(~scanf("%d%d%d",&p,&b,&n))
solve();
}

[POJ2417]Discrete Logging(指数级同余方程)的更多相关文章

  1. POJ2417 Discrete Logging【BSGS】

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

  2. POJ2417 Discrete Logging

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

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

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

  4. 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< ...

  5. POJ2417 Discrete Logging | A,C互质的bsgs算法

    题目: 给出A,B,C 求最小的x使得Ax=B  (mod C) 题解: bsgs算法的模板题 bsgs 全称:Baby-step giant-step 把这种问题的规模降低到了sqrt(n)级别 首 ...

  6. Discrete Logging(poj2417)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5120   Accepted: 2319 ...

  7. POJ 2417 Discrete Logging (Baby-Step Giant-Step)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2819   Accepted: 1386 ...

  8. [poj2417]Discrete Logging_BSGS

    Discrete Logging poj-2417 题目大意:求$a^x\equiv b(mod\qquad c)$ 注释:O(分块可过) 想法:介绍一种算法BSGS(Baby-Step Giant- ...

  9. 【BSGS】BZOJ3239 Discrete Logging

    3239: Discrete Logging Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 729  Solved: 485[Submit][Statu ...

随机推荐

  1. fence_vmware_soap UnicodeEncodeError

    执行如下命令 fence_vmware_soap -z -l administrator@vsphere.local -p 2wsx@QAZ -a 10.0.2.200 -o list --ssl-i ...

  2. SC || Chapter 1

    第一章的重中之重就是这张图吧 (具体参见笔记) ┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉ 区分哪些属性是外部的(面向用户 ...

  3. equals和HashCode的羁绊

    equals和hashcode我一直没弄明白到底怎么回事,今天终于弄懂了. 如下图: 在Person类没有重写equals和hashcode方法时,是如下情况: 但是当我重写了equals方法时,是如 ...

  4. C08 C语言预处理命令

    目录 宏定义 文件包含 条件编译 预处理命令 C语言的预处理:在编译之前进行的处理,不进行编译. C语言的预处理功能有: 宏定义 文件包含 条件编译 预处理命令以符号“#”开头.. 宏定义 不带参数的 ...

  5. Android layout的XML

    [注]此文是在学习andriod中的一些理解和总结,若有错望留言指教,谢谢 1 <RelativeLayout xmlns:android="http://schemas.androi ...

  6. Bootstrap历练实例:导航内的下拉菜单

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  7. java中求几个字符串的最大公共子串 使用了比较器Comparator

    package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Compar ...

  8. 前端应该如何去认识http

    大家应该都知道http是什么吧,肯定会回答不就是浏览器地址那东西吗,有啥好说的,接下来咱们来深入刨析下http这东西. 什么叫http:超文本传输协议(HTTP)是用于传输诸如HTML的超媒体文档的应 ...

  9. 12Vim在系统配置中的应用示例

    Vim 在系统配置中的应用示例 1. 配置主机名称 为了便于咱局域网中查找某台特定的主机,后者对主机进行区分,除了要有IP地址外,还要为主机配置一个主机名,主机名之间可以通过这个类似于域名的名称来相互 ...

  10. linux文件属性文文件类型知识

    文件类型分别介绍: 1.普通文件:我们通过用ls  -l来查看xxx.sql的属性,可以看到第一列内容为-rw-r--r--,值得注意的是第一个符号是-(英文字符减号),在Linux中,以这样的字符开 ...