费马小定理:
若 p 为质数,则对于任意整数 a,有:
a
p
≡
a
(
m
o
d
 
p
)
a^{p}\equiv a(mod\:p)
ap≡a(modp)
欧拉定理:
若正整数 a , n 互质,则对于任意正整数 b,有:
a
b
≡
a
b
 
m
o
d
 
φ
(
m
)
(
m
o
d
 
m
)
a^{b}\equiv a^{b\:mod\:\varphi(m)}(mod\:m)
ab≡abmodφ(m)(modm)
扩展欧拉定理:
a
b
≡
{
a
b
                         
,
b
<
φ
(
m
)
a
b
 
m
o
d
 
φ
(
m
)
+
φ
(
m
)
,
b
≥
φ
(
m
)
a^{b}\equiv\left\{\begin{array}{cc}a^{b}\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:\:,b<\varphi(m)\\a^{b\:mod\:\varphi(m)+\varphi(m)},b\geq\varphi(m)\end{array}\right.
ab≡{ab,b<φ(m)abmodφ(m)+φ(m),b≥φ(m)
附一个代码(欧拉定理)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
ll a, b, m, p;
inline ll read(ll p) {
ll s = 0, w = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
for (; isdigit(c); c = getchar()) {
s = (s << 1) + (s << 3) + (c ^ 48);
if (s >= p)
s = s % p + p;
}
return s * w;
}
inline ll phi(ll n) {
if (n == 1) return 1;
ll ans = n, m = sqrt(n);
for (register ll i = 2; i <= m; i++)
if (n % i == 0) {
ans = ans / i * (i - 1);
while (n % i == 0) n /= i;
}
if (n > 1) ans = ans / n * (n - 1);
return ans;
}
inline ll pow(ll a, ll b, ll m) {
ll ans = 1;
while (b) {
if (b % 2) (ans *= a) %= m;
(a *= a) %= m;
b /= 2;
}
return ans;
}
int main() {
scanf("%lld%lld", &a, &m);
p = phi(m);
b = read(p);
printf("%lld", pow(a, b, m));
return 0;
}
终于补完了嘿嘿嘿