• poj2417 Baby-StepGiant-StepAlgorithm a^x=b%P


    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    typedef long long LL;
    struct Node{
      int idx;
      LL val;
      Node(int cidx=0, LL cval=0){
         idx=cidx;
         val=cval;
      }
      bool operator <(const Node &rhs)const{
         return val<rhs.val||( val == rhs.val && idx<rhs.idx );
      }
    }P[1000000];
    LL pow_mod(LL a, LL n, LL mod)
    {
         LL ans=1;
         while(n)
         {
             if(n&1)ans=(ans*a)%mod;
             n>>=1;
             a=(a*a)%mod;
         }
         return ans;
    }
    int bitsearch(LL d,int n)
    {
         int L=0,R=n-1;
         while(L<=R)
         {
             int mid=(L+R)>>1;
             if(P[mid].val==d) return P[mid].idx;
             if(P[mid].val<d) L=mid+1;
             else R=mid-1;
         }
         return -1;
    }
    LL log_mod(LL a, LL b, LL n)
    {
        LL m,v,e=1;
        m=(sqrt(n+0.5))+1;
        v=pow_mod(a,n-m-1,n);
        P[0]=Node(0,1);
        for(int i=1; i<m; i++)
        {
             e=(e*a)%n;
             P[i]=Node(i,e);
        }
        sort(P,P+m);
        int cnt=1;
        for(int i=0; i<m; i++)
            if(P[i].val!=P[cnt-1].val) P[cnt++]=P[i];
        for(int i=0; i<m; i++)
        {
              int loc=bitsearch(b,cnt);
              if(loc!=-1){
                return i*m+loc;
              }
              b=(b*v)%n;
        }
    
        return -1;
    }
    int main()
    {
        LL P,B,N;
        while(scanf("%I64d%I64d%I64d",&P,&B,&N)==3)
        {
            LL d =log_mod(B,N,P);
            if(d==-1)puts("no solution");
            else printf("%I64d
    ",d);
        }
        return 0;
    }
  • 相关阅读:
    python 项目实例
    flash教程
    flask request
    systemd-unit
    kubernets HA集群手动部署
    zookeeper(1)-简单介绍
    apache与nginx原理
    技术文章整理
    CMS垃圾回收器
    Zookeeper
  • 原文地址:https://www.cnblogs.com/Opaser/p/4813707.html
Copyright © 2020-2023  润新知