POJ-2417-Discrete Logging(BSGS)

时间:2021-11-28 20:46:30
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

Hint

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


 

题解

这道题是裸的BSGS,具体内容可以看hzw的博客—传送门

 #include<algorithm>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
ll p,b,n,s,x,y,m,k;
int exgcd(ll a,ll b){
if (!b){
x=; y=;
return a;
}
int d=exgcd(b,a%b);
ll t=x; x=y; y=t-(a/b)*y;
return d;
}
map<int,int> h;
int main(){
while (~scanf("%lld%lld%lld",&p,&b,&n)){
h.clear();
ll t=(ll)sqrt(p);
s=; h[]=t;
for (int i=;i<=t-;i++){
s=s*b%p;
if (!h[s]) h[s]=i;
}
s=s*b%p;
ll l=1e10,ans=n;
exgcd(s,p);
x=(x+p)%p;
for (int i=;i<=t;i++){
if (h[ans]){
if (h[ans]==t) h[ans]=;
l=i*t+h[ans];
break;
}
ans=ans*x%p;
}
if (l!=1e10) printf("%lld\n",l);
else puts("no solution");
}
return ;
}

POJ-2417-Discrete Logging(BSGS)的更多相关文章

  1. POJ 2417 Discrete Logging(离散对数-小步大步算法)

    Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...

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

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

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

    d. 式子B^L=N(mod P),给出B.N.P,求最小的L. s.下面解法是设的im-j,而不是im+j. 设im+j的话,貌似要求逆元什么鬼 c. /* POJ 2417,3243 baby s ...

  4. BZOJ 3239 Discrete Logging(BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...

  5. BSGS算法&plus;逆元 POJ 2417 Discrete Logging

    POJ 2417 Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4860   Accept ...

  6. poj 2417 Discrete Logging ---高次同余第一种类型。babystep&lowbar;gaint&lowbar;step

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2831   Accepted: 1391 ...

  7. POJ 2417 Discrete Logging &lpar; Baby step giant step &rpar;

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 ...

  8. POJ 2417 Discrete Logging BSGS

    http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...

  9. poj 2417 Discrete Logging&lpar;A&Hat;x&equals;B&lpar;mod c&rpar;&comma;普通baby&lowbar;step&rpar;

    http://poj.org/problem?id=2417 A^x = B(mod C),已知A,B.C.求x. 这里C是素数,能够用普通的baby_step. 在寻找最小的x的过程中,将x设为i* ...

  10. POJ 2417 Discrete Logging 离散对数

    链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...

随机推荐

  1. ASP&period;NET MVC 4中如何为不同的浏览器自适应布局和视图

    在ASP.NET MVC 4中,可以很简单地实现针对不同的浏览器自适应布局和视图.这个得归功于MVC中的"约定甚于配置"的设计理念. 默认的自适应 MVC 4自动地为移动设备浏览器 ...

  2. WWDC2016-session402-whatsNewInSwift3

    Dock 应用的介绍:1.设计到的东西多2.使用 swift 设计3.Dock 的代码量: 200,000行4.更少的重写相同功能的代码 swift.org 官网介绍 Swift Open Sourc ...

  3. 0&period; WP8&period;1学习笔记

    应用程序生命周期: 运行: 在程序NotRunning状态下点击图标,应用将处于Running状态,这会触发一个Actived事件 挂起: 在程序Running状态下, 点击返回键或win键会触发一个 ...

  4. 深入入门正则表达式(java)

    一.入门基础 1.元字符 很多人对正则表达式的印象就是乱码..许许多多的符号组合在一起,偶见单词,正则确实是这样的,所以下面我们要看看这些符号都是什么意思 有些符号不是大家看到的字面上的意思:比如“. ...

  5. LeetCode——Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  6. javascript 动态推断html元素

    在javascript中为了针对不同的元素运行不同的操作,须要在javascript中对触发事件的元素进行推断,然后运行不同的操作. 样例: html <input type='button' ...

  7. social-auth-app-django集成第三方登录

    GitHub:https://github.com/python-social-auth/social-app-django 官网文档:http://python-social-auth.readth ...

  8. SQLServer&&num;160&semi;远程链接MySql数据库详解

    SQLServer 远程链接MySql数据库详解 by:授客 QQ:1033553122 测试环境: Microsoft Windows XP Professional 版本2000 Service ...

  9. 负载均衡&commat;StackExchange&period;Redis实现Session外置--纯干货喂饱你

    Redis和StackExchange.Redis redis有多个数据库1.redis 中的每一个数据库,都由一个 redisDb 的结构存储.其中,redisDb.id 存储着 redis 数据库 ...

  10. iOS GCD之dispatch&lowbar;semaphore(信号量)

    前言 最近在看AFNetworking3.0源码时,注意到在 AFURLSessionManager.m 里面的 tasksForKeyPath: 方法 (L681),dispatch_semapho ...