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

题意:

找出Q,Q为比P小的数中的最大素数,求Q!

题解:

用Miller_Rabbin素数检测快速找出Q,用威尔逊定理 ,求出(P-1)!

根据乘法逆元,用除的模求出Q!

Code:

 #include <iostream>
#include <algorithm>
#include <time.h>
using namespace std;
/**
Miller_Rabin 算法进行素数测试
快速判断一个<2^63的数是不是素数,主要是根据费马小定理
*/
#define ll __int128
const int S=; ///随机化算法判定次数
ll MOD;
///计算ret=(a*b)%c a,b,c<2^63
ll mult_mod(ll a,ll b,ll c)
{
a%=c;
b%=c;
ll ret=;
ll temp=a;
while(b)
{
if(b&)
{
ret+=temp;
if(ret>c)
ret-=c;//直接取模慢很多
}
temp<<=;
if(temp>c)
temp-=c;
b>>=;
}
return ret;
} ///计算ret=(a^n)%mod
ll pow_mod(ll a,ll n,ll mod)
{
ll ret=;
ll temp=a%mod;
while(n)
{
if(n&)
ret=mult_mod(ret,temp,mod);
temp=mult_mod(temp,temp,mod);
n>>=;
}
return ret;
} ///通过费马小定理 a^(n-1)=1(mod n)来判断n是否为素数
///中间使用了二次判断,令n-1=x*2^t
///是合数返回true,不一定是合数返回false
bool check(ll a,ll n,ll x,ll t)
{
ll ret=pow_mod(a,x,n);
ll last=ret;//记录上一次的x
for(int i=;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-)
return true;//二次判断为是合数
last=ret;
}
if(ret!=)
return true;//是合数,费马小定理
return false;
} ///Miller_Rabbin算法
///是素数返回true(可能是伪素数),否则返回false
bool Miller_Rabbin(ll n)
{
if(n<) return false;
if(n==) return true;
if((n&)==) return false;//偶数
ll x=n-;
ll t=;
while((x&)==)
{
x>>=;
t++;
}
srand(time(NULL));
for(int i=;i<S;i++)
{
ll a=rand()%(n-)+; // 生成随机数 0<a<=n-1 去试试
if(check(a,n,x,t))
return false;
}
return true;
} //------------------------------------------------------------------------求素数
inline ll pow(const ll n, const ll k) {
ll ans = ;
for (ll num=n,t=k;t;num=num*num%MOD,t>>=) if(t&) ans=ans*num%MOD;
return ans%MOD;
} inline ll inv(const ll num) {
return pow(num, MOD - );
}
//求乘法逆元 int main(){
ll ans;
int T;
long long a;
cin>>T;
while(T--){
cin>>a;
MOD=a;
a--;
while(!Miller_Rabbin(a)) a--;
ans=MOD-;
for(ll i=a+;i<=MOD-;i++){
ans=(ans%MOD*inv(i)%MOD)%MOD;
}
a=ans;
cout<<a<<'\n';
}
}

HDU6608-Fansblog(Miller_Rabbin素数判定,威尔逊定理应用,乘法逆元)的更多相关文章

  1. 简记乘法逆元(费马小定理+扩展Euclid)

    乘法逆元 什么是乘法逆元? 若整数 \(b,m\) 互质,并且\(b|a\) ,则存在一个整数\(x\) ,使得 \(\frac{a}{b}\equiv ax\mod m\) . 称\(x\) 是\( ...

  2. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

  3. HDU-6608 Fansblog(威尔逊定理+素数间隔+逆元)

    参考博客:https://blog.csdn.net/birdmanqin/article/details/97750844 题目链接:链接:http://acm.hdu.edu.cn/showpro ...

  4. 2019HDU多校第三场F Fansblog——威尔逊定理&&素数密度

    题意 给定一个整数 $P$($10^9 \leq p\leq 1^{14}$),设其前一个质数为 $Q$,求 $Q!  \ \% P$. 分析 暴力...说不定好的板子能过. 根据威尔逊定理,如果 $ ...

  5. 2019杭电多校第三场hdu6608 Fansblog(威尔逊定理)

    Fansblog 题目传送门 解题思路 Q! % P = (P-1)!/(P-1)...(Q-1) % P. 因为P是质数,根据威尔逊定理,(P-1)!%P=P-1.所以答案就是(P-1)((P-1) ...

  6. HDU 6608:Fansblog(威尔逊定理)

    Fansblog Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  7. Codevs 1702 素数判定 2(Fermat定理)

    1702 素数判定 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 一个数,他是素数么? 设他为P满足(P< ...

  8. hdu 2012 素数判定 Miller_Rabbin

    素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. 【HDOJ6608】Fansblog(威尔逊定理)

    题意:给定质数p,求q!模p的值,其中q为小于p的最大质数 1e9<=p<=1e14 思路:根据质数密度近似分布可以暴力找q并检查 找到q后根据威尔逊定理: 把q+1到p-1这一段的逆元移 ...

随机推荐

  1. ActiveMQ-启动服务异常

    如果报这种异常: Caused by: java.io.IOException: Failed to bind to server socket: tcp://0.0.0.0:61616?maximu ...

  2. Xms Xmx PermSize MaxPermSize的含义

    参数的含义 -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M -vmargs 说明后面是VM的参数,所以后面的其实都是JV ...

  3. make 命令出现:"make:*** No targets specified and no makefile found.Stop."

    我们在Linux 安装包的时候,使用make 命令出现:"make:*** No targets specified and no makefile found.Stop."这样的 ...

  4. BeEF 获取同局域网内用户浏览器信息

    1.将kali网络适配器改为桥接模式 打开网络适配器,获取权限 修改桥接模式,进行应用 重启网卡 /etc/init.d/networking restart 查看IP地址 查看网络通不通 2.开启B ...

  5. learning java 处理流的用法

    有点重定向使用的味道 import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.Pri ...

  6. WinDbg常用命令系列---内存查看d*

    d*命令显示给定范围内的内存内容. d{a|b|c|d|D|f|p|q|u|w|W} [Options] [Range] dy{b|d} [Options] [Range] d [Options] [ ...

  7. tomcat 配置域名证书

    tomcat 配置域名证书 示例: <!--" protocol="HTTP/1.1" connectionTimeout=" redirectPort= ...

  8. 【csp模拟赛4】基站建设 (station.cpp)

    [题目描述] 小 Z 的爸爸是一位通信工程师,他所在的通信公司最近接到了一个新的通 信工程建设任务,他们需要在 C 城建设一批新的基站. C 城的城市规划做得非常好,整个城市被规整地划分为 8 行 8 ...

  9. Programming a robot

    题目链接:Gym - 101492H 自己的纯暴力做法: /* */ # include <iostream> # include <cstdio> # include < ...

  10. UVA 12299 RMQ with shifts

    就是线段树的单点修改和区间查询. 然而输入打了一个小时才弄清楚. #include<iostream> #include<cstdio> #include<cstring ...