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

Description

Given a prime P, 2 <= P < 231, 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

思路:baby_step,giant_step算法模板题

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<queue>
6 #include<vector>
7 #include<math.h>
8 #include<string.h>
9 #include<map>
10 #include<set>
11 using namespace std;
12 typedef long long LL;
13 LL mod[65539];
14 bool judge(LL n)
15 {
16 LL ac=sqrt(1.0*n);
17 if(ac*ac==n)
18 return true;
19 else return false;
20 }
21 map<LL,LL>my;
22 set<LL>que;
23 set<LL>::iterator it;
24 LL quick(LL n,LL m,LL p);
25 LL gcd(LL n,LL m)
26 {
27 if(m==0)
28 return n;
29 else return gcd(m,n%m);
30 }
31 pair<LL,LL>Pc(LL n,LL m)
32 {
33 if(m==0)
34 return make_pair(1,0);
35 else
36 {
37 pair<LL,LL>N=Pc(m,n%m);
38 return make_pair(N.second,N.first-(n/m)*N.second);
39 }
40 }
41 typedef struct pp
42 {
43 LL x;
44 LL id;
45 } ss;
46 ss table[655390];
47 ss tt[655390];
48 bool cmp(pp p,pp q)
49 {
50 if(p.x==q.x)
51 return p.id<q.id;
52 return p.x<q.x;
53 }
54 int main(void)
55 {
56 LL P,B,N;
57 while(scanf("%lld %lld %lld",&P,&B,&N)!=EOF)
58 {
59 bool a=judge(P);
60 LL ask=sqrt(1.0*P);
61 if(!a)
62 ask+=1;
63 int i,j;
64 mod[0]=1;
65 int vv=0;
66 table[0].id=0;
67 table[0].x=1;
68 int cn=1;
69 for(i=1; i<=ask; i++)
70 {
71 mod[i]=mod[i-1]*B%P;
72 table[i].id=i;
73 table[i].x=mod[i];
74 }
75 sort(table,table+ask+1,cmp);
76 tt[0].id=table[0].id;
77 tt[0].x=table[0].x;
78 LL yy=tt[0].x;
79 for(i=1;i<=ask;i++)
80 {
81 if(table[i].x!=yy)
82 {
83 yy=table[i].x;
84 tt[cn].x=yy;
85 tt[cn].id=table[i].id;
86 cn++;
87 }
88 }
89 int fl=0;
90 LL ack;
91 LL nn=quick(B,P-2,P);
92 nn=quick(nn,ask,P);
93 LL ni=1;
94 for(i=0; i<=ask; i++)
95 {
96 LL ap=ni*N%P;
97 ni%=P;
98 int l=0;
99 int r=cn-1;
100 LL ic=-1;
101 while(l<=r)
102 {
103 int mid=(l+r)/2;
104 if(tt[mid].x>=ap)
105 {
106 ic=mid;
107 r=mid-1;
108 ack=tt[ic].id;
109 }
110 else l=mid+1;
111 }
112 if(ic!=-1&&tt[ic].x==ap)
113 {
114 //printf("%lld\n",table[ic].x);printf("%d\n",i) ;
115 fl=1;
116 break;
117 }
118 ni=(ni*nn)%P;
119 }
120 if(!fl)
121 printf("no solution\n");
122 else printf("%lld\n",ack+(LL)i*ask);
123 }
124 return 0;
125 }
126 LL quick(LL n,LL m,LL p)
127 {
128 n%=p;
129 LL ans=1;
130 while(m)
131 {
132 if(m&1)
133 {
134 ans=ans*n%p;
135 }
136 n=n*n%p;
137 m/=2;
138 }
139 return ans;
140 }

Discrete Logging(poj2417)的更多相关文章

  1. Discrete Logging(POJ2417 + BSGS)

    题目链接:http://poj.org/problem?id=2417 题目: 题意: 求一个最小的x满足a^x==b(mod p),p为质数. 思路: BSGS板子题,推荐一篇好的BSGS和扩展BS ...

  2. POJ2417 Discrete Logging【BSGS】

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

  3. [POJ2417]Discrete Logging(指数级同余方程)

    Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...

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

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

  5. 【BSGS】BZOJ3239 Discrete Logging

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

  6. poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step

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

  7. BSGS算法+逆元 POJ 2417 Discrete Logging

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

  8. 【BZOJ3239】Discrete Logging BSGS

    [BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...

  9. Discrete Logging

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5865   Accepted: 2618 ...

随机推荐

  1. Python获取随机数

    Python当中,可用random模块来获取随机数 import random """ random模块,用于获取随机数 """ print ...

  2. Redis学习小结

    在7月中旬,我成功入职实习,通过进入公司,认识到了个人与企业巨大的差距,首先就是对于中间件的使用,ElasticSearch.Redis.Kafka等等,都是听过却从未使用过的,然而在任务下达之后,激 ...

  3. 使用input+datalist简单实现实时匹配的可编辑下拉列表-并解决选定后浏览器默认只显示value的可读性问题

    问题背景 最近小伙伴提了一个希望提高后台下拉列表可操作性的需求,原因是下拉列表选项过多,每次下拉选择比较费时费力且容易出错,硬着头皮啃了啃前端知识,网上搜寻了一些下拉列表实现的资料,这里总结一下. P ...

  4. Spring Security 基于URL的权限判断

    1.  FilterSecurityInterceptor 源码阅读 org.springframework.security.web.access.intercept.FilterSecurityI ...

  5. javaSE高级篇3 — 网络编程 — 更新完毕

    网络编程基础知识 先来思考两个问题( 在这里先不解决 ) 如何准确的找到一台 或 多台主机? 找到之后如何进行通讯? 网络编程中的几个要素 IP 和 端口号 网络通讯协议:TCP / UDP 最后一句 ...

  6. Kafka(一)【概述、入门、架构原理】

    目录 一.Kafka概述 1.1 定义 二.Kafka快速入门 2.1 安装部署 2.2 配置文件解析 2.3Kafka群起脚本 2.4 topic(增删改查) 2.5 生产和消费者命令行操作 三.K ...

  7. 【JavaWeb安全】RMI-Remote Method Invocator

    RMI-Remote Method Invocator 什么是RMI?RMI有什么用? RMI允许用户通过数据传输,调用远程方法,在远程服务器处理数据.例如将1,3传到远程服务器的加法运算器,加法运算 ...

  8. LINUX 安装增强 前置安装文件

    yum install kernel yum install kernel-devel yum install gcc yum install make

  9. SpringMVC responseBody注解分析

    @responsebody表示该方法的返回结果直接写入HTTP response body中一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@respo ...

  10. 【编程思想】【设计模式】【行为模式Behavioral】Publish_Subscribe

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/publish_subscribe.py #!/usr/b ...