3239: Discrete Logging

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 635  Solved: 413
[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

题目链接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=3239

Solution

    BSGS的模板题。。。。

    对于本题的做法。。一般是先设 L = i * e + j 或 L = i * e - j 。。。

    e = ceil(sqrt(P))。。就是假如算出 sqrt(P)= 1.14 ,e就等于2,往大的取整

    然后枚举 i 和 j 的值就能做到 O(sqrt(P))。。。

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
#define LL long long
using namespace std; LL P,B,N,e,now;
map<LL,int>mp;
LL pow(LL p,LL q){
LL s=1;
while(q){
if(q&1) s=s*p%P;
q>>=1;
p=p*p%P;
}
return s;
}
void solve(){
mp.clear();
if(N==1 && B>0){
printf("0\n");
return;
}
if( (!B) && (!N) ){printf("1\n");return;}
if(!B){printf("no solution\n");return;}
e=ceil(sqrt(P));
now=N%P;
for(int j=1;j<=e;j++){
now=now*B%P;
if(!mp[now]) mp[now]=j;
}
B=pow(B,e);
now=1;
for(int i=1;i<=e;i++){
now=now*B%P;
if(mp[now]>0){
N=e*i-mp[now];
printf("%lld\n",N);
return;
}
}
printf("no solution\n");
return;
}
int main(){
while(scanf("%lld%lld%lld",&P,&B,&N)!=EOF) solve();
return 0;
}

  

  

This passage is made by Iscream-2001.

最新文章

  1. php的mysql\mysqli\PDO(二)mysqli
  2. CMake命令/函数汇总(翻译自官方手册)
  3. 中文乱码~Windows 7
  4. Sql order by 和 group BY一起使用时需要注意
  5. archlinux 内核编译笔记
  6. Android调用WebService(转)
  7. 前台JSP页面独立化
  8. js学习记录
  9. DLL详解及Denpendcy Walker的使用
  10. node.js 中回调函数callback(转载),说的很清楚,看一遍就理解了
  11. JAVA程序员成长历程(二)
  12. docker搭建zabbix
  13. 这年头做开源项目,被冷嘲热讽,FreeSql 0.0.4
  14. Java基础--接口和抽象类的区别
  15. html计时发送验证码功能的实现
  16. Debian9服务器安装mysql
  17. spring 5.1.2 mvc RequestMappingHandlerMapping 源码初始化过程
  18. 【详解】JNI (Java Native Interface) (三)
  19. FineUI4.0以后如何调用JS事件
  20. OPENCV 常用函数

热门文章

  1. 迷你MVVM框架 avalonjs 0.84发布
  2. protobuf&#39;s custom-options
  3. ADT下载地址整理(转)
  4. oralce 知识
  5. php中++i 与 i++ 的区分详解
  6. C++中public、protected以及private的使用
  7. centos7 安装mongo
  8. UVa 11419 SAM I AM (最小覆盖数)
  9. Zookeeper客户端cli_st为何在crontab中运行不正常?
  10. linux每天一小步---head命令详解