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

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<stdlib.h>
#include<time.h>
using namespace std;typedef long long ll;ll qmul(ll a,ll b,ll mod){ll res=0;while(b>0){if(b&1)res=(res+a)%mod;a=(a+a)%mod;b>>=1;}return res;
}ll mod_pow(ll x,ll n,ll mod){ll res=1;while(n>0){if(n&1)res=qmul(res,x,mod);x=qmul(x,x,mod);n>>=1;}return res;
}ll random(ll n){return (double)rand()/RAND_MAX*n;
}bool miller_rabin(ll n){if(n==2)return true;if(n<2||n%2==0)return false;ll p=n-1;int k=0;while(p%2==0){p/=2;k++;}for(int i=0;i<10;i++){ll a=random(n-2)+1;ll x=mod_pow(a,p,n),y;for(int j=0;j<k;j++){y=qmul(x,x,n);if(y==1&&x!=1&&x!=n-1)return false;x=y;}if(y!=1)return false;}return true;
}ll Q,P;int main(){int T;scanf("%d",&T);while(T--){scanf("%lld",&P);for(ll i=P-1;;i--){if(miller_rabin(i)){Q=i;break;}}ll res=1;for(ll i=Q+1;i<=P-2;i++){res=qmul(res,i,P);}res=mod_pow(res,P-2,P);printf("%lld\n",res);}return 0;
}

2019杭电多校第三场 6608 Fansblog(威尔逊定理+miller_rabin素性测试)相关推荐

  1. 2019 杭电 多校第3场 1006 Fansblog (HDU 6608)

    题目链接 题解: 用威尔逊定理变换,然后求逆元. 代码: #include <bits/stdc++.h> using namespace std; typedef long long l ...

  2. 2019.7.29 杭电多校第三场小结

    index > 杭电多校第三场 题号 标题 AC 做法 状态 6603 Azshara's deep sea (51/150)34.00% 6604 Blow up the city (213/ ...

  3. 2019杭电多校第9场1002 Rikka with Cake HDU6681

    2019杭电多校第9场1002 Rikka with Cake HDU6681 题意:给你若干个点按上下左右切割平面,最后问平面内有几块被切割开来. 解法1:红黑树+思维+贪心 A:根据欧拉定理可以得 ...

  4. 2019杭电多校 第七场 Kejin Player 6656(求期望值)

    2019杭电多校 第七场 Kejin Player 6656(求期望值) 题目 http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意 给你n,q.表示有n ...

  5. 杭电多校第三场-H-Game

    题目描述 Again Alice and Bob is playing a game with stones. There are N piles of stones labelled from 1 ...

  6. 【2019.08.21】2019杭电多校第十场

    补题地址:http://acm.hdu.edu.cn/listproblem.php?vol=58 题号:6691-6701 1001: 1002: 1003:✅ 1004: 1005:✅ 1006: ...

  7. 2019 杭电多校第六场 题解

    比赛记录 注意随机数据 ,1-n排列这种,一般都有啥暴力重构之类的方法,期望重构次数很少之类的 1005也是这样,因为n^2但只有n个值有数,所以就可以n^2logn 题解 1001 Salty Fi ...

  8. hdu 6656 2019杭电多校第7场 期望题

    设f[i]为从i升级到i+1期望需要的金钱,由于每级都是能倒退或者升级到i+1,所以询问从l,r的期望金钱可以直接前缀和,那么推导每一级升级需要的期望钱也可以用前缀和推导 设sum[i]=f[1]+f ...

  9. 2019杭电多校第7场 K Kejin Player HDU 6656(数学推导)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题目大意:对于每一个等级,可以花ai元,有pi概率升级,如果升级失败就退到xi级,问从li级升到 ...

最新文章

  1. python列表切片口诀-python学习之“切片操作从入门到精通”
  2. superset设置自动刷新
  3. C# 中的占位符本质
  4. c ++查找字符串_C ++类和对象| 查找输出程序| 套装5
  5. 运行第一个 docker image 并在浏览器中查看
  6. Mac安装nmap及常用命令【亲测有用】
  7. python tkinter库四则运算_python tkinter 编写心理学试验程序干扰任务之四则运算 psychopy...
  8. Educoder Basemap和Seaborn 第一关:Seaborn
  9. dls 深度受限搜索java_JAVA深入学习(栈和队列)之栈
  10. Oracle高级查询之GROUP BY
  11. 解密:fotona4d有什么功效,欧洲之星做一次能保持多久
  12. Python 读写文件操作: with open() as f,pickle,pmml,数据库
  13. 计算机win10无法打开小键盘,win10系统下小键盘数字键不能用怎么办_win10电脑小键盘数字键不能用如何解决...
  14. Vue 不睡觉教程3 - 来点实在的:自动计算剩余时间的任务列表
  15. 速览 关于网络安全专业学生的未来职业规划
  16. 陶瓷纤维毯行业调研报告 - 市场现状分析与发展前景预测
  17. Kubernates(k8s)工作负载之工作负载资源
  18. Linux 系统和安全
  19. 计算机四级网络工程师考点总结
  20. 电感器饱和的简单说明

热门文章

  1. 15 个常用的python 开源框架
  2. 神经网络——最易懂最清晰的一篇文章
  3. hadoop-ykt(自定义key)
  4. poco for android,POCO桌面(com.mi.android.globallauncher) - 2.6.8.25 - 应用 - 酷安
  5. 写给Krpano小白们的最最最入门级教程(一)
  6. poi导出excel无法打开文件
  7. 用JAVA写一个画图小程序(JAVA 大作业)
  8. sphinx配置文件解析
  9. 科学解释:为什么越是无知的人越自信?
  10. 中止执行后超过2年_中止两年终结本次执行吗