题意:给出一个质数P,找出小于P的最大的质数N,求出N的阶乘模P。(P∈[1e10,1e14])
思路:威尔逊定理:一个数n若是质数, 则有 (n−1) ! ≡ n−1mod n. 于是可以先令ans=p−1, 再对p−1到q的数对p求逆元。p到q之间的距离不会超过300,Miller Robin大素数 判断可以找到最近的素数。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
using namespace std;
typedef unsigned long long ll;
const int S = 20;
ll mult_mod(ll a, ll b, ll c)
{
a %= c;
b %= c;
ll ret = 0, tmp = a;
while(b){
if(b & 1){
ret += tmp;
if(ret > c) ret -= c;
}
tmp <<= 1;
if(tmp > c) tmp -= c;
b >>= 1;
}
retur