2019 多校3 Fansblog

Fansblog

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


 

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

 

 

我可真是个菜鸡。

__int128运算比long long 慢大概一倍,溢出只有在乘法曹祖东阿时候会有溢出,所以只需要把乘法重载一下防止溢出就行了。

另外,这里用MR检验比根号n的检验方法快了很多,还没有学习,只是粘了个模板...这就去看了(我可真是菜,而且还很懒)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<stdlib.h>

using namespace std;
typedef long long ll;

ll num[]= {2,3,5,7,11,13,17,19};

__int128 _a,_b,_m;
ll mul(ll a,ll b,ll m) {
    _a=a,_b=b,_m=m;
    return (ll)(_a*_b%_m);
}

inline ll qpow(ll a,ll b,ll p)
{
    ll r=1;
    while(b)
    {
        if(b&1) r=mul(r,a,p);
        a=mul(a,a,p);
        b>>=1;
    }
    return r;
}

inline bool Miller_Rabin(ll n)
{
    if (n==2) return 1;
    if((n&1)==0||n==1) return false;
    for (ll i=0;i<8;i++) if(n==num[i]) return 1;
    for (ll i=0;i<8;i++) if(n%num[i] == 0) return false;

    ll temp=n-1,t=0,nxt;
    while((temp&1)==0) temp>>=1,t++;

    for(ll i=0;i<4;i++)
    {
        ll a=num[i];
        ll now=qpow(a,temp,n);
        nxt=now;
        for(ll j=1;j<=t;j++)
        {
            nxt=mul(now,now,n);
            if(nxt==1&&now!=n-1&&now!=1) return false;
            now=nxt;
        }
        if(now!=1) return false;
    }
    return true;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll p,q,ans;
        scanf("%lld",&p);
        ans=p-1;
        for(ll i=p-1;;i--)
        {
            if(Miller_Rabin(i)) break;
            ans=mul(ans,qpow(i,p-2,p),p);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值