什么是費馬小定理
費馬小定理是數論中的一個重要定理,在 1636 年提出。如果 \(p\) 是一個質數,而整數 \(a\) 不是 \(p\) 的倍數,則有 \(a^ {p-1}≡1(mod\) \(p)\)。
費馬小定理求逆元
#include<iostream>
#define ll long long
using namespace std;
ll quickpow(ll a, ll b, ll p){
ll temp = 1;
while(b){
if(b & 1) temp = (temp * a) % p;
a = (a * a) % p;
b >>= 1;
}
return temp;
}
int main()
{
ll a, p;
cin>>a>>p;
cout<<quickpow(a, p-2, p)<<endl;
return 0;
}