考虑计算 (4^(p))%(9^9) 的一般情况,其中 p 是一个很大的数(在这种情况下 p = 4^(10^9))。考虑 4 模 9^9 的幂序列: (4^0)%(9^9) == 1, (4^1)%(9^9) == 4, (4^2)%(9 ^9) == 16,最终在t(t 未知)循环之后,序列将循环回到 (4^t)%(9^9) == 1,然后重复。 (注意数字必须是“互质数”,否则经过一些循环后,结果变为零并保持为零。)因此计算可以使用 p%t 代替 p:
(4^(p))%(9^9) == (4^(p%t))%(9^9)
这看起来很大,但 t 将
例如,假设您正在寻找 t,这样:
(4^(p))%9 == (4^(p%t))%9
对于这个更简单的例子:
(4^0)%9 == 1 ;m == 1, t == 0
(4^1)%9 == 4 ;m == 4, t == 1
(4^2)%9 == 7 ;m == 7, t == 2
(4^3)%9 == 1 ;m == 1, t == 3 (end of loop)
所以 t == 3(对于这个更简单的情况):
(4^(p))%9 == (4^(p%3))%9
正如 DAle 所说,还有与 totient 相关的欧拉定理:
https://en.wikipedia.org/wiki/Euler%27s_totient_function#Euler.27s_theorem
如果a和n互质,那么
(a^(ϕ(n)))%n = 1, where ϕ(n) is the totient function.
wiki 文章还包括一个用于 totient 函数的公式。
https://en.wikipedia.org/wiki/Euler%27s_totient_function#Euler.27s_product_formula
你可以使用 t = φ(9^9)。这不会是满足 (4^(t))%(9^9) = 1 的 t 的最小值,但已经足够了。
问题发布已经两天了,所以继续
t = φ(9^9) = φ(3^18) = (3^18)(1-1/3) = 2(3^17) = 258280326
使用循环方法找到一个较小的值,t = 3^17 = 129140163。然后将其用于内部幂模
p%t = ( (4^(10^9)) % 129140163 ) = 19457986
然后继续外幂模的过程
(4^(4^(10^9)))%(9%9) = (4^19457986)%(9^9) = 335228719
示例代码:
#include <stdio.h>
typedef unsigned long long uint64_t;
/* count number of cycles t such that */
/* (v^t)%m = 1 */
uint64_t cntcyc(uint64_t v, uint64_t m)
{
uint64_t t = 0;
uint64_t i = 1;
do {
i = (i * v) % m;
t++;
} while (i != 1);
return t;
}
/* v to power p modulo m */
uint64_t powmod(uint64_t v, uint64_t p, uint64_t m)
{
uint64_t s = v; /* repeated square */
uint64_t r = 1; /* result */
while(p){
if(p&1)
r = (r*s)%m;
s = (s*s)%m;
p >>= 1;
}
return r;
}
int main()
{
uint64_t r;
r = cntcyc(4ull, 387420489ull); /* 4, 9^9 */
printf("%llu\n", r); /* 129140163 */
r = powmod(4ull, 1000000000ull, r); /* (4^(10^9))%r */
printf("%llu\n", r); /* 19457986 */
r = powmod(4ull, r, 387420489ull); /* (4^r)%(9^9) */
printf("%llu\n", r); /* 335228719 */
r = 258280326ull; /* r = totient(9^9) */
r = powmod(4ull, 1000000000ull, r); /* (4^(10^9))%r */
printf("%llu\n", r); /* 19457986 */
r = powmod(4ull, r, 387420489ull); /* (4^r)%(9^9) */
printf("%llu\n", r); /* 335228719 */
r = 258280326ull; /* r = totient(9^9) */
r = powmod(4ull, 1000000000ull, r); /* (4^(10^9))%r */
/* show that r += 3^17 doesn't effect final result */
r += 129140163ull; /* r += 3^17 */
printf("%llu\n", r); /* 148598149 */
r = powmod(4ull, r, 387420489ull); /* (4^r)%(9^9) */
printf("%llu\n", r); /* 335228719 */
return 0;
}