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 PPP , 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 PPP.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∗1n: n! = n * (n-1) * (n-2) * (n-3) *… * 3 * 2 * 1n:n!=n∗(n−1)∗(n−2)∗(n−3)∗…∗3∗2∗1. For example, 4!=4∗3∗2∗1=244! = 4 * 3 * 2 * 1 = 244!=4∗3∗2∗1=24)

Input

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

Output

For each testcase, output an integer representing the factorial of QQQ modulo PPP.

Sample Input

1
1000000007

Sample Output

328400734

Source

2019 Multi-University Training Contest 3

题意

  • 给你一个109−101410^9-10^{14}109−1014内的质数ppp,求小于ppp的最大质数的阶乘取模ppp

题解

  • 威尔逊定理+Miller_RabinMiller\_RabinMiller_Rabin素数测试
  • 威尔逊定理就是对于任意的正质数kkk,有
    ((k−1)!)%k=k−1((k-1)!)\%k=k-1 ((k−1)!)%k=k−1
    然后对于本题先用Miller_RabinMiller\_RabinMiller_Rabin找到小于ppp的最大质数qqq,然后用威尔逊定理推一下式子:
    ((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} ((p−1)!)%p​=q!×(q+1)×(q+2)×...×(p−1)%p=p−1​
    q!≡1(q+1)×(q+2)×...×(p−2)(modp)\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)×...×(p−2)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);}
}

「hdu6608」Fansblog【Miller_Rabin+威尔逊定理】相关推荐

  1. 「hdu6608」Fansblog 威尔逊定理

    「hdu6608」 Fansblog 今天回顾之前多校联赛的题目:Fansblog ,发现一件有趣的事情,快速积的时间复杂度,比普通*快多了,刚才一直Tle 在用了普通*,卧槽这也能T,当然也有乘法爆 ...

  2. 2019多校第三场 HDU6608 Fansblog(威尔逊定理,逆元,质数间隔)

    链接:HDU6608 Fansblog 题意: 给出一个质数PPP(109≤P≤101410^9\le P\le 10^{14}109≤P≤1014),找出最大的质数QQQ(Q<PQ \lt P ...

  3. hdu6608 Fansblog(威尔逊定理)

    题意: 给定一个大质数P,要求找到一个小于P的最大质数Q, 输出Q的阶乘对P取模的结果. 数据范围:1e9<=P<=1e14 解法: Q和P之间的距离挺小的(好像比1e5还小很多,但是网上 ...

  4. 杭电2019多校第三场 HDU-6608 Fansblog(威尔逊定理+素数间隔+逆元)

    威尔逊定理:在初等数论中,威尔逊定理给出了判定一个自然数是否为素数的充分必要条件.即:当且仅当p为素数时:( p -1 )! ≡ -1 ( mod p ),但是由于阶乘是呈爆炸增长的,其结论对于实际操 ...

  5. 杭电2019多校第三场 HDU-6608 Fansblog(威尔逊定理+大数判素数+逆元)

    威尔逊定理:在初等数论中,威尔逊定理给出了判定一个自然数是否为素数的充分必要条件.即:当且仅当p为素数时:(p−1)!≡−1(modp)( p -1 )! ≡ -1 ( mod p )(p−1)!≡− ...

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

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

  7. 【HDU 6608】Fansblog(威尔逊定理+逆元+快速乘+快速幂)

    题目:点击打开题目链接 题意:输入一个素数 P,找出 P 的前一个素数,并求出 ! mod P的值.(1e9≤ P ≤1e14) 思路: 1.首先找出Q.因为自然数是由素数.合数.1和0组成的,并且数 ...

  8. 2019杭电多校第三场 6608 Fansblog(威尔逊定理+miller_rabin素性测试)

    Problem Description 传送门 Farmer John keeps a website called 'FansBlog' .Everyday , there are many peo ...

  9. HDU6608 Fansblog【Miller_Rabin素性测试算法+威尔逊定理】

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

最新文章

  1. 黎明觉醒火种测试服务器维护,黎明觉醒火种测试什么时候上线 黎明觉醒火种测试资格获取方式(图文)...
  2. JavaScript数据结构与算法——集合
  3. 学习总结之数据挖掘三大类六分项
  4. CCNA-第十五篇-DHCP配置+SDN介绍(最后一章)
  5. 面试官系统精讲Java源码及大厂真题 - 26 惊叹面试官:由浅入深手写队列
  6. 【面向对象】第四单元总结——UML
  7. SQL Server 判定时间是否在某个时间区间内
  8. 独家|对决金融云:备案将至,谁得先手
  9. 算法:两个数之和为目标数Two Sum II - Input array is sorted
  10. PPT快速成长经验,我将其免费告诉你
  11. Linux libaio 实例,linux libaio安装
  12. CSRF跨站请求伪造漏洞修复方案
  13. adb shell dunpsys SurfaceFlinger
  14. cf虚拟服务器设置方法,CF和GA免费顶级域名申请注册及修改DNS域名解析方法
  15. 微信小程序picker选择器(下拉框)以及传值问题
  16. Java多线程系列--“JUC锁”03之 公平锁(一) (r)
  17. 如何将本地图片转换成链接
  18. 时间序列-异常检测(Anomaly Detection)(四):深度学习方法
  19. recover database
  20. SpriteKit中应用TexturePacker

热门文章

  1. linux如何识别3t硬盘分区,linux下,给3T硬盘分区,parted命令出现问题,无法分区!!!!急!!...
  2. JAVA多线程基础篇-关键字synchronized
  3. 非门,NOT Gate
  4. 安卓图像处理(四)保存以及删除图片
  5. python+undetected_chromedriver+chrome绕过五秒盾(cloudflare)
  6. 基于ESP8266+华为云数据监控模块
  7. ubuntu下启动wifi
  8. 易基因|3文一览:简化甲基化测序(RRBS)技术优势及研究成果(医学+物种保护+农学)
  9. Java核心技术卷Ⅰ-第四章对象和类
  10. maya 白天室内灯光_Maya Vray室内灯光渲染教程