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

题意:T组样例,每组样例,给出一个素数P(1e9≤P≤1e14),Q是P的前一个素数求Q!%P。

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

所以:由威尔迅定理得:((p−1)!)%p​=q!×(q+1)×(q+2)×...×(p−1)%p=p−1​

q!​≡(q+1)×(q+2)×...×(p−2)1​(mod p)​

AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e7+10;
ll mod;
int prime[N+10],cnt;
bool vis[N+10];
bool is_prime(ll x)
{for(int i=0;i<cnt&&(ll)prime[i]*prime[i]<=x;i++){if(x%prime[i]==0)return 0;}return 1;
}
void get_prime()
{for(int i=2;i<=N;i++){if(!vis[i])prime[cnt++]=i;for(int j=0;j<cnt&&(ll)i*prime[j]<=N;j++){vis[i*prime[j]]=1;if(i%prime[j]==0) break;    }    }
}
ll mul(ll a,ll b)
{ll res=0;while(b){if(b&1) res=(res+a)%mod;a=(a+a)%mod;b>>=1;}return res%mod;
}
ll poww(ll a,ll b)
{ll res=1;while(b){if(b&1) res=mul(res,a);a=mul(a,a);b>>=1;}return res;
}
int main(void)
{int t;get_prime(); ll p,q;scanf("%d",&t);while(t--){scanf("%lld",&p);mod=p;q=p-1;while(!is_prime(q)) q--;ll ans=p-1;for(ll i=q+1;i<=p-1;i++)ans=mul(ans,poww(i,mod-2));printf("%lld\n",ans);}return 0;
}

Fansblog (HDU - 6608)(威尔迅定理+费马小定理)相关推荐

  1. 卢卡斯定理 费马小定理

    Lucas定理是用来求 c(n,m) mod p的值,p是素数(从n取m组合,模上p) 描述为: Lucas(n,m,p)=combine(n%p,m%p)* Lucas(n/p,m/p,p) Luc ...

  2. 夜深人静写算法(三十二)- 费马小定理

    文章目录 一.前言 二.费马小定理 1.费马小定理定义 2.费马小定理证明 三.素数判定和伪素数 1.素数判定 2.伪素数 四.费马小定理的应用 1.二分快速幂降幂 2.模 p 逆元 3.Rabin- ...

  3. BUUCTF [NCTF2019]childRSA(费马小定理)

    [NCTF2019]childRSA(费马小定理) 题目 from random import choice from Crypto.Util.number import isPrime, sieve ...

  4. 关于逆元(费马小定理,exgcd)

    1.逆元是个什么东西 首先这里有个式子: ((( ab\frac{a}{b}ba​ ))) modmodmod ppp,这个式子的答案怎么求呢?? A 直接求不行吗? Q别想了不能.我们发现一个分数 ...

  5. 密码学基础——辗转相除法,费马小定理,欧拉定理,裴蜀定理,中国剩余定理

    文章主要根据百度百科和维基百科相关相关知识点整理而成! 辗转相除法 辗转相除法, 又名欧几里德算法(Euclidean algorithm),是求最大公约数的一种方法.它的具体做法是:用较小数除较大数 ...

  6. hdu 4549 M斐波那契数列(费马小定理 + 二分快速幂 + 矩阵快速幂)

    M斐波那契数列                                                                           Time Limit: 3000/1 ...

  7. HDU - 5667 Sequence(矩阵快速幂+费马小定理降幂)

    题目链接:点击查看 题目大意:给出函数f(x): 现给出n,a,b,c,mod,求f(n)对mod取模后的结果 题目分析:这个题目相对于前几个题来说稍微加大了点难度,但还是挺水的一个题,首先我们可以对 ...

  8. HDU 4704 Sum (费马小定理)

    题意 给定一个N,设S(K)表示x1 + x2 + -- + xk = N的解的个数(xi ∈ Z+),求SUM( S(i), 0 <= i <= N )  mod  109+7. 思路 ...

  9. Fansblog  HDU-6608(费马小定理、威尔逊定理)

    1e14以内的素数间隔最大不超过700(我也不会证明,网上看的) 威尔逊定理:https://blog.csdn.net/m0_50623076/article/details/109894993 p ...

最新文章

  1. 【RS】Improving Implicit Recommender Systems with View Data - 使用浏览数据提升隐式推荐系统...
  2. INFO:InstallShield InstallScript工程中自定义界面文本输入控件的两个注意事项
  3. 使用form上传文件到application server的另一种办法
  4. 文本查询TextQuery类文件编写
  5. 编写高质量代码的十个秘诀(转)
  6. 软件设计模式(持续更新)
  7. ubuntu安装l2tp/ipsec
  8. 路飞项目整体路程(六)
  9. CRAY超级计算机发展,Cray推出专为企业设计的Shasta超级计算机
  10. 最美应用-从Android研发工程师的角度之[厨房故事]
  11. 不用 IDE,仅用命令行 + SDK 开发 Android App
  12. python编程一级考试报名_2021年的第一场考试!全国青少年编程等级考试开始报名!...
  13. 大类资产配置(一)均值方差模型MOV
  14. python怎么打开图片_新手教学——python怎么打开图片
  15. 行进中换轮胎——万字长文解析美团和大众点评两大数据平台是怎么融合的
  16. 京东出王炸,地下物流要来了!以后快递直接从管道送到家
  17. 6月刊推荐:轻松理解复合事件处理
  18. Photoshop切图----学习笔记
  19. 如何给WordPress博客网站换个漂亮的字体
  20. android 群控 网络 adb,安卓群控.sln · 罗金方/结合mini和adb命令,和adb socket实现安卓群控 - Gitee.com...

热门文章

  1. Dubbo的Api+Provider+Customer示例(IDEA+Maven+Springboot+dubbo)
  2. 一般试卷的纸张大小是多少_试卷,考试试卷是多大的纸
  3. ppt制作:加总页数首页不显示页码
  4. 显示器与计算机主机无线连接,如何让手机、电脑和显示器无线连接
  5. 把手机当作电脑显示器指导参考
  6. AI 加持实时互动|ZegoAvatar 面部表情随动技术解析
  7. 越是领军人才,越要看基本素质
  8. Android平板app开发布局文件,Android平板开发相关
  9. 用vue2写的开发者在线简历导出
  10. 微信小程序图片404时显示默认图片