Shank的大步小步算法(Shank‘s Baby-Step-Giant-Step Algorithm)

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <map>
 5 typedef long long LL;
 6 int mul_mod(int a,int b,int n){ // a、b都小于n
 7     return a * b % n;
 8 }
 9 int pow_mod(int a,int p,int n){
10     if(p == 0) return 1;
11     LL ans = pow_mod(a,p / 2,n);
12     ans = ans * ans % n;
13     if(p % 2 == 1) ans = ans * a % n;
14     return ans;
15 }
16 // 求x和y使得ax+by=d并且|x|+|y|最小。其中d=gcd(a,b)
17 void exgcd(int a,int b,int& d,int& x,int& y){
18     if(!b) d = a,x = 1,y = 0;
19     else{
20         exgcd(b,a % b,d,y,x);
21         y -= x * (a / b);
22     }
23 }
24 // 计算模n下a的逆,如果gcd(a,n) != 1,则逆不存在,返回-1
25 int inv(int a,int n){
26     int d,x,y;
27     exgcd(a,n,d,x,y);
28     return d == 1 ? (x + n) % n : -1;
29 }
30 // 求解模方程a^x=b(mod n)。n为素数,无解时返回-1.
31 int log_mod(int a,int b,int n){
32     int m,v,e = 1,i;
33     m = (int)sqrt(n + 0.5);
34     v = inv(pow_mod(a,m,n),n); // a^m的逆v=a^(-m)
35     std::map<int,int> x; // x[j]表示满足ei(=a^i)=j的最小的i
36     x[1] = 0;
37     for(int i = 1 ; i < m ; i++){ // 计算ei
38         e = mul_mod(e,a,n);
39         if(!x.count(e)) x[e] = i;
40     }
41     // 考虑a^(im)、a^(im+1)、...、a^(im+m-1)
42     for(int i = 0 ; i < m ; i++){
43         if(x.count(b)) return i * m + x[b];
44         b = mul_mod(b,v,n);
45     }
46     return -1;
47 }
48 int main(){
49     printf("%d\n",log_mod(3,4,5));
50     return 0;
51 }

 

转载于:https://www.cnblogs.com/cyb123456/p/5808865.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shank's algorithm is an algorithm for solving the discrete logarithm problem. In this case, we want to solve the equation 10 ≡ 5^a (mod 47). First, we need to find the order of 5 modulo 47. Since 47 is a prime number, we know that any integer a such that 1 ≤ a ≤ 46 is a primitive element modulo 47 if and only if gcd(a,47)=1 and a^(46) ≡ 1 (mod 47). Since 5 is a primitive element modulo 47, we know that the order of 5 modulo 47 is 46. Next, we need to compute the values of 5^0, 5^1, 5^2, ..., 5^22 mod 47 and store them in a table. We can use the repeated squaring method to compute these values efficiently. For example: 5^0 ≡ 1 (mod 47) 5^1 ≡ 5 (mod 47) 5^2 ≡ 25 (mod 47) 5^3 ≡ 43 (mod 47) 5^4 ≡ 38 (mod 47) 5^5 ≡ 6 (mod 47) 5^6 ≡ 30 (mod 47) 5^7 ≡ 41 (mod 47) 5^8 ≡ 19 (mod 47) 5^9 ≡ 24 (mod 47) 5^10 ≡ 37 (mod 47) 5^11 ≡ 45 (mod 47) 5^12 ≡ 7 (mod 47) 5^13 ≡ 35 (mod 47) 5^14 ≡ 31 (mod 47) 5^15 ≡ 18 (mod 47) 5^16 ≡ 33 (mod 47) 5^17 ≡ 44 (mod 47) 5^18 ≡ 3 (mod 47) 5^19 ≡ 15 (mod 47) 5^20 ≡ 32 (mod 47) 5^21 ≡ 14 (mod 47) 5^22 ≡ 42 (mod 47) Now, we can use Shank's algorithm to solve the equation 10 ≡ 5^a (mod 47). We can rewrite this equation as 5^a ≡ 10 (mod 47). We want to find a such that 0 ≤ a ≤ 45. We start by dividing the range [0, 45] into two equal parts: [0, 22] and [23, 45]. We compute 10 × 5^-0 ≡ 10 (mod 47) and 5^0, 5^1, 5^2, ..., 5^22 mod 47 using the table. We find that 10 ≡ 10 (mod 47) and 10 ≢ 1, 5, 25, 43, 38, 6, 30, 41, 19, 24, 37, 45, 7, 35, 31, 18, 33, 44, 3, 15, 32, 14, 42 (mod 47). Therefore, 10 is not in the first half of the table. Next, we compute 10 × 5^-23 ≡ 39 (mod 47) and 5^23, 5^24, 5^25, ..., 5^45 mod 47 using the table. We find that 39 ≢ 1, 5, 25, 43, 38, 6, 30, 41, 19, 24, 37, 45, 7, 35, 31, 18, 33, 44, 3, 15, 32, 14, 42 (mod 47). Therefore, 10 is not in the second half of the table. Since we have exhausted all the possibilities, we conclude that there is no solution to the equation 10 ≡ 5^a (mod 47).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值