本文应该真的差不多就只有代码
不要脸地说一句其实要学的话只看代码也能知道方法了,还是写得比较结构化的
#include <cstdio>
typedef long long ll;
ll Qpow(ll x, ll n, ll p){
ll ans = 1;
while (n){
if (n & 1) ans = ans * x % p;
x = x * x % p; n >>= 1;
}return ans;
}
void Exgcd(ll a, ll p, ll &x, ll &y){
if (!p){x = 1, y = 0;}
else {Exgcd(p, a % p, y, x); y -= x * (a / p);}
}
ll GetExgcd(ll a, ll p){
ll res, tmp;
Exgcd(a, p, res, tmp);
return (res % p + p) % p;
}
struct Euler{
static const int maxn = 1e5;
bool book[maxn];
int prime[maxn], tot;
int phi[maxn];
void Work(int p){
tot = 0; phi[1] = 1;
for (int i = 2; i <= p; ++i){
if (!book[i]){
prime[++tot] = i;
phi[i] = i - 1;
}
for (int j = 1, nxt; j <= tot; ++i){
if ((nxt = i * prime[j]) > p) break;
book[nxt] = 1;
if (i % prime[j] == 0){
phi[nxt] = phi[i] * prime[j]; break;
}
phi[nxt] = phi[i] * (prime[j] - 1);
}
}
}
ll Get(ll a, ll p){
Work(p);
return Qpow(a, phi[p] - 1, p);
}
};
ll LinearRecurrence(ll a, ll p){
if (a == 1) return 1;
return LinearRecurrence(p % a, p) * (p - p / a);
}
ll Femat(ll a, ll p){
return Qpow(a, p - 2, p);
}
int main (){
ll a, p; scanf ("%lld%lld", &a, &p);
Euler res;
printf ("Femat: %lld\nExgcd: %lld\nEuler: %lld\n", Femat(a, p), GetExgcd(a, p), res.Get(a, p));
if (a <= p) printf ("LinearRecurrence: %lld", LinearRecurrence(a, p));
return 0;
}