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. 性能:Output层面

    将数据保存到MySQL中 import java.sql.DriverManager import org.apache.spark.storage.StorageLevel import org.a ...

  2. shell部分面试题

    1.用Shell编程,判断一文件是不是块或字符设备文件,如果是将其拷贝到 /dev 目录下. #!/bin/bash#1.sh#判断一文件是不是字符或块设备文件,如果是将其拷贝到 /dev 目录下#f ...

  3. django-发送文件

    客户端授权密码”,勾选“开启”,弹出新窗口填写手机验证码. settings.py配置 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBac ...

  4. 浏览器报400-Bad Request异常

    今天在使用ie浏览器在测试程序的时候,报这个错误,后台日志打印出来显示的是:连接一个远程主机失败 解决Invalid character found in the request target. Th ...

  5. [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)

    题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0​(n) be the number of positive diviso ...

  6. 使用vue+mintui 开发省市区功能

    做移动端的都知道 经常会有省市区这种三级联动的功能 今天研究了一下午~ 1.准备工作 vue+mintui+省市区的json数据 下载地址:https://github.com/chzm/addres ...

  7. 什么是 Kafka Rebalance 以及关于 Rebalance Kafka-Python 社区客户端应该关注的地方

    什么是 Rebalance? Rebalance 为什么会发生?Rebalance 的情况下 consumer 是否还能正确消费消息呢? 记得之前在一段时间密集面试的时候总会问候选人这些问题. 重平衡 ...

  8. mysql e的n次幂exp()

    mysql> ); +-------------------+ | exp() | +-------------------+ | 2.718281828459045 | +---------- ...

  9. 怎么样使element ui 的table某列变色

    第一步.在el-table里面加上:row-style="rowClass" <el-table :data="targetCarList" border ...

  10. 软件工程 “校园汇” 个人IDEA竞选分析与总结

    IDEA竞选 19/10/8软件工程课上举行了一次IDEA竞选: 我的竞选IDEA是"校友汇",大学生的在线活动中心. 投票结果: 可以看到,校友会(汇)IDEA竞选结果十分惨淡, ...