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. 韩顺平老师java视频全套-java视频教程下载

    解压压缩包会有一个种子文件.直接迅雷下载即可,包含了韩顺平老师的java入门视频,jdbc,jsp,servlet,oracle,hibermate,spring,SHH框架,struct,linux ...

  2. 目标检测中的bounding box regression

    目标检测中的bounding box regression 理解:与传统算法的最大不同就是并不是去滑窗检测,而是生成了一些候选区域与GT做回归.

  3. Vue.js not detected

    安装vue devtools工具,在chrome中一直是灰色,title是Vue.js not detected ① F12关闭开发者模式 ② 刷新 ③ 然后再按F12就好了 网上看到的,居然真的有用 ...

  4. [ARIA] Create an Accessible Tooltip on a Text Input

    Here we use HTML and CSS to create a stylish yet semantic tooltip on a form input. I am using aria-d ...

  5. learning scala akka tell pattern(二)

    package com.example import akka.actor._ object Tutorial_02_Tell_Pattern extends App { println(" ...

  6. PostGraphile 4.4 发布,支持real time 查询

    在4.4 之前,real time 是通过插件完成处理的,4.4 直接内置了,还是很方便的功能,总算 和其他类似graphql 平台看齐了,使用上还是挺方便的. 参考资料 https://www.gr ...

  7. REdis主从复制之repl_backlog

    目录 目录 1 1. 前言 1 2. 配置项 1 3. redisServer 2 4. feedReplicationBacklog-写repl_backlog 3 5. addReplyRepli ...

  8. 【JZOJ6225】【20190618】计数

    题目 对于一个01串,定义\(f(s)\)为\(f(s) = \sum_{i=0}^{\lfloor \frac{|s|}{2} \rfloor -1 }[s_i=s_{|s|-1-i}]\) 定义\ ...

  9. 干货 | 10分钟教你用column generation求解vehicle routing problems

    OUTLINE 前言 VRPTW description column generation Illustration code reference 00 前言 此前向大家介绍了列生成算法的详细过程, ...

  10. SPA和MVVM设计思想

    Vue基础篇设计模式SPAMVVMVue简介Vue的页面基本使用Vue的全局环境配置基本交互 插值表达式基础指令 v-text v-html v-pre v-once v-cloak v-on MVV ...