Discrete Logging(poj2417)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5120 | Accepted: 2319 |
Description
B
L
== N (mod P)
Input
Output
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)的更多相关文章
- Discrete Logging(POJ2417 + BSGS)
题目链接:http://poj.org/problem?id=2417 题目: 题意: 求一个最小的x满足a^x==b(mod p),p为质数. 思路: BSGS板子题,推荐一篇好的BSGS和扩展BS ...
- POJ2417 Discrete Logging【BSGS】
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5577 Accepted: 2494 ...
- [POJ2417]Discrete Logging(指数级同余方程)
Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...
- POJ 2417 Discrete Logging (Baby-Step Giant-Step)
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2819 Accepted: 1386 ...
- 【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 729 Solved: 485[Submit][Statu ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4860 Accept ...
- 【BZOJ3239】Discrete Logging BSGS
[BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...
- Discrete Logging
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5865 Accepted: 2618 ...
随机推荐
- shell 脚本的基本定义
注意不能有控制,指令之间 [1]shell脚本的基础知识 (1)shell脚本的本质 编译型语言 解释型语言 shell脚本语言是解释型语言 shell脚本的本质 shell命令的有序集合 (2)sh ...
- 日常Java 2021/11/21
Java文档注释 Java支持三种注释方式.前两种分别是Ⅱ和/产*,第三种被称作说明注释,它以产开始,以*I结束.说明注释允许你在程序中嵌入关于程序的信息.你可以使用javadoc工具软件来生成信息, ...
- 事务(@Transactional注解)的用法和实例
参数 @Transactional可以配制那些参数及以其所代表的意义: 参数 意义 isolation 事务隔离级别 propagation 事务传播机制 readOnly 事务读写性 noRollb ...
- 重量级&轻量级
重量级 就是说包的大小,还有就是与个人项目的耦合程度,重量级的框架与项目耦合程度大些 代表EJB容器的服务往往是"买一送三",不要都不行 轻量级 就是相对较小的包,当然与项目的耦合 ...
- Mybatis中 SIMPLE、REUSE、BATCH的区别
Executor分成两大类,一类是CacheExecutor,另一类是普通Executor. 普通类又分为: ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情.它为每个语句的执行 ...
- 过滤敏感词工具类SensitiveFilter
网上过滤敏感词工具类有的存在挺多bug,这是我自己改用的过滤敏感词工具类,目前来说没啥bug,如果有bug欢迎在评论指出 使用前缀树 Trie 实现的过滤敏感词,树节点用静态内部类表示了,都写在一个 ...
- Mybatis-Plus默认主键策略导致自动生成19位长度主键id的坑
原创/朱季谦 某天检查一位离职同事写的代码,发现其对应表虽然设置了AUTO_INCREMENT自增,但页面新增功能生成的数据主键id很诡异,长度达到了19位,且不是从1开始递增的-- 我检查了一下,发 ...
- postgresql很强大,为何在中国,mysql成为主流?
你找一个能安装起来的数据库,都可以学,不管什么版本. 数据库的基本功,是那些基本概念(SQL,表,存储过程,索引,锁,连接配置等等),这些在任何一个版本中都是一样的. 目录 postgresql很强大 ...
- [BUUCTF]REVERSE——helloword
helloword 题目是安卓逆向.安卓逆向工具下载地址 用APKIDE打开附件,ctf+f调出检索版,检索main函数,能看到flag字符串 flag{7631a988259a00816deda84 ...
- mkdir创建目录时,如果上级目录没有是创建不成功的
mkdir创建目录时,如果上级目录没有是创建不成功的 ,此时必须用 mkdirs()方法方可.