码迷,mamicode.com
首页 > 其他好文 > 详细

[BZOJ1467]Pku3243 clever Y

时间:2018-01-29 19:12:08      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:ios   one   zoj   miss   template   long   getch   0.11   soft   

传送门

传送门

exbsgs模板。

技术分享图片
//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<queue>
#include<ctime>
#include<cmath>
#include<map>
typedef long long LL;
using namespace std;
LL a,b,p,d,x,y; 

template<typename T> void read(T &x) {
    char ch=getchar(); x=0; T f=1;
    while(ch!=-&&(ch<0||ch>9)) ch=getchar();
    if(ch==-) f=-1,ch=getchar();
    for(;ch>=0&&ch<=9;ch=getchar()) x=x*10+ch-0; x*=f;
}

void exgcd(LL a,LL b,LL &d,LL &x,LL &y) {
    if(!b) { x=1; y=0; d=a; return; } 
    exgcd(b,a%b,d,y,x); y-=a/b*x;
}

map<LL,int>hash;
LL exbsgs() {
    int k=0; LL t=1;
    if(b==1) return 0;
    for(;;) {
        exgcd(a,p,d,x,y);
        if(d==1) break;
        if(b%d) return -1;
        b/=d; p/=d; t=t*(a/d)%p;
        k++; if(t==b) return k;
    }
    hash.clear();
    int m=sqrt(p); 
    LL inv;
    LL now=t,a_m=1;
    for(int i=1;i<=m;i++) {
        now=now*a%p;
        a_m=a_m*a%p; 
        if(now==b) return i;
        if(!hash[now]) hash[now]=i;
    }
    exgcd(a_m,p,x,inv,y);
    inv=(inv+p)%p;
    for(int i=1;i<=p/m+1;i++) {
        b=b*inv%p;
        if(hash[b]) return i*m+hash[b];
    }
    return -1;
}

int main() {
    for(;;) {
        read(a); read(p); read(b);
        if(!a&&!p&&!b) break;
        LL ans=exbsgs();
        if(ans==-1) puts("No Solution");
        else printf("%lld\n",ans);
    }
    return 0;
}
View Code

 

[BZOJ1467]Pku3243 clever Y

标签:ios   one   zoj   miss   template   long   getch   0.11   soft   

原文地址:https://www.cnblogs.com/Achenchen/p/8378695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!