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 )
农民约翰开了一个网站叫粉丝博客,每天,都有很多人访问这个博客。一天,他发现访问量达到了P,P是个质数。他认为这是个很有趣的现象。他记得他的访问量之前达到了另一个质数。他尝试去找到最大的质数Q,并得到Q的阶乘值,在mod P环境下。但是他太忙了,没法算出答案,所以他叫你帮忙。
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
显然,一个1e9的数字是没法很快判定质数的,如果用普通的遍历算法的话。但是用MillerRabin就可以在很短时间内判断一个大数的素性。MillerRabin由三个模块组成:快速/慢速防溢出乘,快速幂,主算法模块,之后说明。
同样的,一个大数的阶乘更是难以算出,所以用威尔逊定理把它转化一下:(P-1)!=-1 mod(P)=P-1(mod P),也就是把(P-1)!在取余P的情况下变成了P-1,使得问题简化了许多。而求Q!的过程就变成了(P-1)!/((P-2)*(P-3)*...*(P-Q)这样的一个过程,以P-1代替(P-1)!,再多次乘逆元就可以顺利解出Q!。
//#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
const int INF = 0x3f3f3f3f;
using namespace std;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 2000;
inline ll lldcin()
{
ll tmp = 0, si = 1;
char c;
c = getchar();
while (c > '9' || c < '0')
{
if (c == '-')
si = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
tmp = tmp * 10 + c - '0';
c = getchar();
}
return si * tmp;
}
///Untersee Boot IXD2(1942)
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
/**Last Remote**/
const int LIM=10;
ll TestCases[LIM] = { 2,3,5,7,11,13,17,23,37,211};
ll QuickMultiply(ll a, ll b, ll c)
{
return ((a*b - ll(ld(a) / c * b+0.005)*c) % c + c) % c;
}
ll QuickPower(ll a, ll b, ll c)
{
ll ans = 1;
a %= c;
while (b)
{
if (b & 1)
ans = QuickMultiply(ans, a, c);
b >>= 1;
a = QuickMultiply(a, a, c);
}
return ans;
}
bool MillerRabin(ll num)
{
if (num % 2 == 0)
return false;
ll power = num - 1,limit=0;
while (!(power & 1))
{
limit++;
power >>= 1;
}
for (int i = 0; i < LIM; i++)
{
if (TestCases[i] == num)
return true;
ll solution = QuickPower(TestCases[i], power, num);
ll tmpsolution = solution;
for (int j = 1; j <= limit; j++)
{
solution = QuickMultiply(solution, solution, num);
if (solution == 1 && tmpsolution != 1 && tmpsolution != num - 1)
return false;
tmpsolution = solution;
}
if (tmpsolution != 1)
return false;
}
return true;
}
int DETERMINATION()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
ll t;
cin >> t;
while (t--)
{
ll n;
cin >> n;
ll ans = n - 1;
ll q = n - 1;
while (MillerRabin(q) == false)
{
ans = QuickMultiply(ans , (QuickPower(q, n - 2, n)), n);
q--;
}
cout << ans << endl;
}
return 0;
}