zoukankan      html  css  js  c++  java
  • Baby Step Giant Step model

    ******************************************** */
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    //baby_step giant_step
    // a^x = b (mod n) n为素数,a,b < n
    // 求解上式 0<=x < n的解
    #define MOD 76543
    int hs[MOD],head[MOD],next[MOD],id[MOD],top;
    void insert(int x,int y)
    {
        int k = x%MOD;
        hs[top] = x, id[top] = y, next[top] = head[k], head[k] = top++;
    }
    int find(int x)
    {
        int k = x%MOD;
        for(int i = head[k]; i != -1; i = next[i])
            if(hs[i] == x)
                return id[i];
        return -1;
    }
    
    
    
    int BSGS(int a,int b,int n)
    {                                                                                                                                                                                                                               
        memset(head,-1,sizeof(head));
        top = 1;
        if(b == 1)return 0;
        int m = sqrt(n*1.0), j;
        long long x = 1, p = 1;
        for(int i = 0; i < m; ++i, p = p*a%n)insert(p*b%n,i);
        for(long long i = m; ;i += m)
        {
            if( (j = find(x = x*p%n)) != -1 )return i-j;
            if(i > n)break;
        }
        return -1;
    }
    

      

  • 相关阅读:
    jeesite导入导出
    jeesite下载
    百度echart
    js获取日期
    清除svn
    父子窗口
    JS高级程序设计之高级技巧
    JS中离线应用与客户端存储
    JS最佳实践
    JSON之JS高级程序设计笔记
  • 原文地址:https://www.cnblogs.com/forgot93/p/4367128.html
Copyright © 2011-2022 走看看