spoj3105 MOD - Power Modulo Inverted(exbsgs)

传送门

 

关于exbsgs是个什么东东可以去看看yyb大佬的博客->这里

 1 //minamoto
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<map>
 6 #define ll long long
 7 #define GG {puts("No Solution");}
 8 using namespace std;
 9 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
10 char buf[1<<21],*p1=buf,*p2=buf;
11 inline ll read(){
12     #define num ch-'0'
13     char ch;bool flag=0;ll res;
14     while(!isdigit(ch=getc()))
15     (ch=='-')&&(flag=true);
16     for(res=num;isdigit(ch=getc());res=res*10+num);
17     (flag)&&(res=-res);
18     #undef num
19     return res;
20 }
21 map<ll,ll> mp;
22 inline ll gcd(ll a,ll b){
23     if(!b) return a;
24     while(b^=a^=b^=a%=b);
25     return a;
26 }
27 inline ll ksm(ll a,ll b,ll p){
28     ll res=1;
29     while(b){
30         if(b&1) (res*=a)%=p;
31         (a*=a)%=p,b>>=1;
32     }
33     return res;
34 }
35 inline void ex_BSGS(ll y,ll z,ll p){
36     if(z==1) return (void)(puts("0"));
37     int k=0,a=1;
38     while(true){
39         int d=gcd(y,p);if(d==1) break;
40         if(z%d) return (void)(GG);
41         z/=d,p/=d,++k,a=1ll*a*y/d%p;
42         if(z==a) return (void)(printf("%d\n",k));
43     }
44     mp.clear();
45     int m=sqrt(p)+1;
46     for(int i=0,t=z;i<m;++i,t=1ll*t*y%p) mp[t]=i;
47     for(int i=1,tt=ksm(y,m,p),t=1ll*a*tt%p;i<=m;++i,t=1ll*t*tt%p){
48         int j=mp.find(t)==mp.end()?-1:mp[t];
49         if(j>=0&&i*m-j+k>=0) return (void)(printf("%d\n",i*m-j+k));
50     }
51     GG;
52 }
53 int main(){
54 //    freopen("testdata.in","r",stdin);
55     while(true){
56         int x=read(),z=read(),k=read();
57         if(x==0&&z==0&&k==0) break;
58         ex_BSGS(x,k,z);
59     }
60     return 0;
61 }

 

posted @ 2018-10-03 20:38  bztMinamoto  阅读(277)  评论(0编辑  收藏  举报
Live2D