HDU多校--6608Fansblog--威尔逊定理推公式

Fansblog

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2579    Accepted Submission(s): 1068


 

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

 

 

Source

2019 Multi-University Training Contest 3

 

 

Recommend

chendu

 

 

给出一个质数Q,求小于Q的最大的素数P的阶乘。对于素数P

P-1\equiv (P-1)!modP

P小于Q,那么P!*(P+1)*...*(Q-1)=(Q-1)!modQ\equiv Q-1

那么移项,P!=(Q-1)/((P+1)*...*(Q-1))

暴力枚举P是多少,然后用快速乘,注意逆元。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
ll qmul(ll a, ll b, ll mod)
{
    ll ans = 0;
    while (b)
    {
        if (b & 1)
            ans = (ans + a) % mod;
        a = (a << 1) % mod;
        b >>= 1;
    }
    return ans;
}
ll qpow(ll x, ll n, ll mod)
{
    ll ans = 1;
    while (n)
    {
        if (n & 1)
            ans = qmul(ans, x, mod);
        x = qmul(x, x, mod);
        n >>= 1;
    }
    return ans;
}
bool Miller_Rabin(ll x)   //判断素数
{
    srand(unsigned(time(NULL)));
    ll s = 0, t = x - 1;
    if (x == 2)
        return true;   //2是素数
    if (x < 2 || !(x & 1))
        return false;     //如果x是偶数或者是0,1,那它不是素数
    while (!(t & 1))    //将x分解成(2^s)*t的样子
    {
        s++;
        t >>= 1;
    }
    for (int i = 0; i < 10; ++i)        //随便选10个数进行测试
    {
        ll a = rand() % (x - 1) + 1;
        ll b = qpow(a, t, x), k;      //先算出a^t
        for (int j = 1; j <= s; ++j)      //然后进行s次平方
        {
            k = qmul(b, b, x);   //求b的平方
            if (k == 1 && b != 1 && b != x - 1)     //用二次探测判断
                return false;
            b = k;
        }
        if (b != 1)
            return false;   //用费马小定律判断
    }
    return true;   //如果进行多次测试都是对的,那么x就很有可能是素数
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        ll p, q;
        scanf("%lld", &p);
        q = p - 1;
        while (!Miller_Rabin(q))
            --q;//挨个判断素数
        ll ans = p - 1;
        for (ll i = p - 1; i > q; --i)
            ans = qmul(ans, qpow(i, p - 2, p), p);
        printf("%lld\n", ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值