Fansblog
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3170 Accepted Submission(s): 671
Problem Description
Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people visited this blog.One day, he find the visits has reached P , which is a prime number.He thinks it is a interesting fact.And he remembers that the visits had reached another prime number.He try to find out the largest prime number Q ( Q < P ) ,and get the answer of Q! Module P.But he is too busy to find out the answer. So he ask you for help. ( Q! is the product of all positive integers less than or equal to n: n! = n * (n-1) * (n-2) * (n-3) *… * 3 * 2 * 1 . For example, 4! = 4 * 3 * 2 * 1 = 24 )
Input
First line contains an number T(1<=T<=10) indicating the number of testcases.
Then T line follows, each contains a positive prime number P (1e9≤p≤1e14)
Output
For each testcase, output an integer representing the factorial of Q modulo P.
Sample Input
1
1000000007
Sample Output
328400734
题意
给出一个质数 q ,找出比它小的最大的质数 p ,计算 p 的阶乘模除 q 的值( ( p ! ) % q )
分析
这是一道有关于数论的题,其中用到Miller_Rabin素数测试、威尔逊定理和费马小定理( “ ≡ ”代表模等于 )
Miller_Rabin素数测试:用来找出比 q 小的最大的质数
(具体内容见:https://blog.csdn.net/ZengAMing/article/details/51867240)
威尔逊定理: (p−1)!≡p−1≡−1 (mod p) (p is a prime)
费马小定理: gcd(a,p)=1 a(p−1)≡1(mod p)(p is a prime)
我们要计算的答案是 ( p ! ) % q ;
根据威尔逊定理可知,
( (q-1) ! ) % q = ( ( p ! ) ( p+1) (p+2) (p+3)···( q-1 ) ) % q
= q - 1
所以,
( ( p ! ) ( p+1) (p+2) (p+3)···( q-1 ) ) ≡ q - 1 ( mod q)
p
!
=
1
(
p
+
1
)
(
p
+
2
)
(
p
+
3
)
⋅
⋅
⋅
(
q
−
2
)
(
m
o
d
p
)
p ! =\dfrac{1}{( p+1) (p+2) (p+3)···( q-2 )} ( mod p )
p!=(p+1)(p+2)(p+3)⋅⋅⋅(q−2)1(modp)
要计算这个式子,用到了我们的费马小定理
gcd(a,p)=1 a(p−1)≡1(mod p) (p is a prime)
则有,
(p + 1)(q−1)≡1(mod q)
即,
1
p
+
1
≡
(
p
+
1
)
q
−
2
(
m
o
d
q
)
\frac{1}{ p +1}≡(p+1)^{q-2} (mod q)
p+11≡(p+1)q−2(modq)
依次类推
1
p
+
2
≡
(
p
+
2
)
q
−
2
(
m
o
d
q
)
\frac{1}{ p +2}≡(p+2)^{q-2} (mod q)
p+21≡(p+2)q−2(modq)
⋅
⋅
⋅
⋅
⋅
⋅
······
⋅⋅⋅⋅⋅⋅
分别计算出这些结果,然后将这些结果乘起来即是答案,
在这里,我们会用到快速幂的方法求(p+2)q-2, 它们结果的成绩,我们会用快速乘的方法来避免内存超限和时间超限
代码
#include<bits/stdc++.h>
using namespace std;
long long mul(long long a,long long b,long long mod){//快速乘
long long ret=0;
while(b) {
if(b & 1) {
ret += a;
ret %= mod;
}
a <<= 1;
a %= mod;
b >>= 1;
}
return ret;
}
long long pow(long long a,long long b,long long mod) {//快速幂
long long ret = 1;
while(b) {
if(b & 1) ret = mul(ret,a,mod);
a = mul(a,a,mod);
b >>= 1;
}
return ret;
}
bool check(long long a,long long n){
long long x = n - 1;
int t = 0;
while((x & 1) == 0) {
x >>= 1;
t ++;
}
x = pow(a,x,n);
long long y;
for(int i=1;i<=t;i++) {
y = mul(x,x,n);
if(y == 1 && x != 1 && x != n - 1) return true;
x = y;
}
if(y != 1) return true;
return false;
}
bool Miller_Rabin(long long n) { //找出小于给出的质数的最大质数
if(n == 2) return true;
if(n == 1 || !(n & 1)) return false;
const int arr[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
for(int i = 0; i < 12; i++) {
if (arr[i] >= n) break;
if(check(arr[i], n)) return false;
}
return true;
}
int main() {
int t;long long n;scanf("%d",&t);
while(t--) {
scanf("%lld",&n);long long p=n-1;
while(!Miller_Rabin(p)) p--;
long long ans=1;
for(long long i=p+1;i<n-1;i++) ans=mul(ans,pow(i,n-2,n),n);
printf("%lld\n",ans);
}
}