「hdu6608」Fansblog【Miller_Rabin+威尔逊定理】

Fansblog

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

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 P 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 &lt; P ) Q ( Q &lt; P ) Q(Q<P) ,and get the answer of Q ! Q! Q! Module P P P.But he is too busy to find out the answer. So he ask you for help. ( Q ! Q! 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 n: n! = n * (n-1) * (n-2) * (n-3) *… * 3 * 2 * 1 n:n!=n(n1)(n2)(n3)321. For example, 4 ! = 4 ∗ 3 ∗ 2 ∗ 1 = 24 4! = 4 * 3 * 2 * 1 = 24 4!=4321=24)

Input

First line contains an number # T ( 1 ≤ T ≤ 10 ) T(1\leq T\leq 10) T(1T10) indicating the number of testcases.
Then T T T line follows, each contains a positive prime number P ( 1 0 9 ≤ p ≤ 1 0 14 ) P (10^9\leq p\leq10^{14}) P(109p1014)

Output

For each testcase, output an integer representing the factorial of Q Q Q modulo P P P.

Sample Input

1
1000000007

Sample Output

328400734

Source

2019 Multi-University Training Contest 3

题意

  • 给你一个 1 0 9 − 1 0 14 10^9-10^{14} 1091014内的质数 p p p,求小于 p p p的最大质数的阶乘取模 p p p

题解

  • 威尔逊定理+ M i l l e r _ R a b i n Miller\_Rabin Miller_Rabin素数测试
  • 威尔逊定理就是对于任意的正质数 k k k,有
    ( ( k − 1 ) ! ) % k = k − 1 ((k-1)!)\%k=k-1 ((k1)!)%k=k1
    然后对于本题先用 M i l l e r _ R a b i n Miller\_Rabin Miller_Rabin找到小于 p p p的最大质数 q q q,然后用威尔逊定理推一下式子:
    ( ( p − 1 ) ! ) % p = q ! × ( q + 1 ) × ( q + 2 ) × . . . × ( p − 1 ) % p = p − 1 \begin{aligned}((p-1)!)\%p &amp;= q!\times(q+1)\times(q+2)\times...\times(p-1)\%p \\ &amp; =p-1 \\ \end{aligned} ((p1)!)%p=q!×(q+1)×(q+2)×...×(p1)%p=p1
    q ! ≡ 1 ( q + 1 ) × ( q + 2 ) × . . . × ( p − 2 ) ( m o d   p ) \begin{aligned}q! &amp;\equiv \frac{1}{(q+1)\times(q+2)\times...\times(p-2)}(mod\ p) \end{aligned} q!(q+1)×(q+2)×...×(p2)1(mod p)

代码

#include <cstdio>
#include <cstdlib>
#include <map>
using namespace std;
long long gcd(long long a,long long b) {
    if (b == 0) return a;
    return gcd(b,a%b);
}
long long mul(long long a,long long b,long long mod){
    long long ret=0;
    while(b) {
        if(b & 1) ret=(ret+a)%mod;
        a=(a+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+1<n;i++) ans=mul(ans,pow(i,n-2,n),n);
        printf("%lld\n",ans);
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值