了解 mpz_invert

Why*_*ard 2 c precision gnu gmp

我对多精度算术相当陌生,经过几天的尝试解决这个问题后,我不知所措。我试图将一个数的倒数取到大量的十进制位,并且一直在尝试找出如何使用 GMP 或 mpz/mpf 包来做到这一点。然而,我对理解此链接中的示例有点迷失:

http://sepwww.stanford.edu/sep/claudio/Research/Prst_ExpRefl/ShtPSPI/intel/mkl/10.0.3.020/examples/gmp/source/mpz_invert_example.c

/* to compute the inverse of op1 modulo op2 and put result in rop    */
/*                 p*x = s*n + 1    ( rop = p, op1 = x, op2 = n )    */
/*                                                                   */
    n = mpz_invert ( rop, op1, op2 );
Run Code Online (Sandbox Code Playgroud)

我在我的 ide 中复制了这个例子,编译并运行,我得到了正确的输出:

/* rop =  2288                                                       */
/* n = 1 
Run Code Online (Sandbox Code Playgroud)

但是我不明白2288是什么?即计算 op1 以 op2 为模的逆并将结果放入 rop 中

谁能解释一下这个数字是如何获得的?

或一个简单的例子说:

1875^-6

或查看以下链接: How does onecalculate 2 ^ -18 using GMP?

采取:

1 / (1875 ^ 6)

任何帮助将非常感激!

Jac*_*ack 6

mpz_invert用于计算一个非常具体的值,该值是 的模乘法逆元a mod b

\n\n

基本上rop是一个整数,op1*rop \xe2\x89\xa1 1 (mod op2)这意味着(op1*rop - 1) mod op2 = 0

\n\n

确实如此,与

\n\n
op1 = 12682271391376756984\nop2 = 3527\nrop = 2288\n\nop1 * rop = 29017036943470019979392\n(op1 * rop - 1) mod op2 = 29017036943470019979391 % 3527 = 0\n
Run Code Online (Sandbox Code Playgroud)\n