POJ 3243 Clever Y 扩展BSGS

http://poj.org/problem?id=3243

这道题的输入数据输入后需要将a和b都%p

https://blog.csdn.net/zzkksunboy/article/details/73162229

在大约sqrt( p )的复杂度求出 ( a^x ) % p = b % p中的x

扩展bsgs增加了对p不是素数的情况的处理。

扩展bsgs在处理过a,b,p之后进行bsgs的时候x处理不到num以下的部分,这部分在处理a,b,p的时候处理过了(b=1输出num)所以不用考虑。

所以实际上扩展bsgs和普通bsgs只多了一个处理的循环部分。

ac一次之后我又把多余的(非预处理循环内的)判定a==b和b==1的情况的部分删除了,也是对的,最开始在这些没必要的部分上纠结真是画蛇添足了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<map>
 7 using namespace std;
 8 #define LL long long
 9 LL p,a,b;
10 map<LL,LL>q;
11 LL mgcd(LL x,LL y){return y==0?x:mgcd(y,x%y);}
12 int main(){
13     while(~scanf("%lld%lld%lld",&a,&p,&b)){
14         if(a==0&&p==0&&b==0)break;b=b%p;a=a%p;
15         LL x=1,y=1,t=0,d=mgcd(a,p),ff=0;
16         while(d!=1){
17             if(b==1){printf("%lld\n",t);ff=1;break;}
18             if(b%d!=0){printf("No Solution\n");ff=1;break;}
19             ++t;a/=d;b/=d;p/=d;
20             y=(y*a)%p;a=a*d;
21             d=mgcd(a,p);
22         }if(ff)continue;
23         a%=p;b%=p;
24         LL w=(LL)sqrt((double)p);if(w*w!=p)++w;
25         q[b]=-1; for(int i=1;i<=w;++i){x=(x*a)%p;LL z=(b*x)%p;q[z]=i;}
26         bool f=0;LL m=10000000000000000LL;
27         for(int i=1;i<=w;++i){
28             y=(y*x)%p;
29             if(q[y]!=0){
30                 LL z=q[y];if(z==-1) z=0;
31                 z=(LL)i*w-z;f=1;
32                 m=min(z+t,m);
33             }
34         }
35         q[b]=0;x=1;
36         for(int i=1;i<=w;++i){x=(x*a)%p;LL z=(b*x)%p;q[z]=0;}
37         if(!f)printf("No Solution\n");
38         else printf("%lld\n",m);
39     }
40     return 0;
41 }

原文地址:https://www.cnblogs.com/137shoebills/p/9216879.html

时间: 2024-08-15 06:27:00

POJ 3243 Clever Y 扩展BSGS的相关文章

poj 3243 Clever Y 高次方程

1 Accepted 8508K 579MS C++ 2237B/** 2 hash的强大,,还是高次方程,不过要求n不一定是素数 3 **/ 4 #include <iostream> 5 #include <cstdio> 6 #include <cmath> 7 #include <cstring> 8 #include <algorithm> 9 using namespace std; 10 long long a,b,n; 11 co

POJ 3243 Clever Y BSGS

Clever Y Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6861   Accepted: 1676 Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, Y, Z, we all know how to figure out K fast. However, give

POJ 3243 Clever Y Extended-Baby-Step-Giant-Step

题目大意:给定A,B,C,求最小的非负整数x,使A^x==B(%C) 传说中的EXBSGS算法0.0 卡了一天没看懂 最后硬扒各大神犇的代码才略微弄懂点0.0 參考资料: http://quartergeek.com/bsgs/ http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 这两位写的比較具体0.0 能够用于參考 对拍时发现自己代码各种脑残0.0 伤不起啊 #include<cmath> #include<cstd

【POJ 3243】Clever Y 拓展BSGS

调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那是因为聪哥早就会了,所以他觉得学这个没用,信他才怪233 #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef lo

【POJ】3243 Clever Y

http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <iostream> typedef long long ll; using namespace st

BZOJ 3243 Clever Y

Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, Y, Z, we all know how to figure out K fast. However, given X, Z, K, could you figure out Y fast? Input Input data consists of no more than 20 test ca

Clever Y POJ - 3243 (扩展BSGS)

Clever Y POJ - 3243 题意:给a,c,b,求最小的x使得 ax≡b (mod c). 扩展BSGS算法~ 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #define ll long long 6 using namespace std; 7 const int mod=99991; 8 ll head[mod],nex

【EXT-BSGS算法求离散对数】POJ Clever Y 3243

Clever Y Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7259 Accepted: 1795 Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, Y, Z, we all know how to figure out K fast. However, given X,

POJ 2115 C Looooops(扩展欧几里得应用)

题目地址:POJ 2115 水题..公式很好推.最直接的公式就是a+n*c==b+m*2^k.然后可以变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod(2^k)的最小解.(真搞不懂为什么训练的时候好多人把青蛙的约会都给做出来了,这题却一直做不出来.....这两道不都是推公式然后变形吗.....) 代码如下: #include <iostream> #include <cstdio> #include <string> #incl