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

p前面的第一个素数m与p的间隔大概在log p,m较大不能直接求m!,但我们可以通过威尔逊定理求得(p-1)!再除以m+1到p-1之间的数求得m!。

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<time.h>
using namespace std;
const int maxn=1e7+10;
const double exc=1e-7;
long long mod;
int cnt;
bool vis[maxn+10];
long long prime[maxn+10];
void getprime()
{for(int i=2; i<=maxn; i++){if(!vis[i])prime[cnt++]=i;for(int j=0; j<cnt&&i*prime[j]<=maxn; j++){vis[i*prime[j]]=1;if(i%prime[j]==0)break;}}
}
bool isprime(long long n)
{for(int i=0; i<cnt&&prime[i]*prime[i]<=n; i++){if(n%prime[i]==0){return false;}}return true;
}
long long fastmul(long long a,long long b)
{long long ans=0;while(b){if(b&1)ans=(ans+a)%mod;a=(a+a)%mod;b>>=1;}return ans%mod;
}
long long fastpow(long long a,long long b)
{long long ans=1;while(b){if(b&1)ans=fastmul(ans,a);a=fastmul(a,a);b>>=1;}return ans;
}
int main()
{int t;getprime();scanf("%d",&t);while(t--){long long p,m;scanf("%lld",&p);mod=p;m=p-1;while(!isprime(m)) m--; long long ans=p-1;for(long long i=p-1; i>=m+1; i--)ans=fastmul(ans,fastpow(i,p-2));printf("%lld\n",ans);}return 0;
}

Fansblog HDU - 6608相关推荐

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

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

  2. HDU 6608 [2019 Multi-University Training Contest 3]

    Fansblog Problem Description Farmer John keeps a website called 'FansBlog' .Everyday , there are man ...

  3. HDU 6608 Fansblog——————大素数检测

    Fansblog 点击题目查看题面 点击这里也可以 Source 2019 Multi-University Training Contest 3 给你一个素数PPP 让给你求最接近P的素数QQQ 输 ...

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

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

  5. HDU 6608 FansBlog(粉丝博客)(MillerRabin算法+威尔逊算法)

    Farmer John keeps a website called 'FansBlog' .Everyday , there are many people visited this blog.On ...

  6. Fansblog (HDU - 6608)(威尔迅定理+费马小定理)

    Farmer John keeps a website called 'FansBlog' .Everyday , there are many people visited this blog.On ...

  7. hdu 6608 Fansblog 威尔逊定理+大数阶乘取模

    传送门 题意:给出一个质数P,找出小于P的最大的质数N,求出N的阶乘模P.(P∈[1e10,1e14]) 思路:威尔逊定理:一个数n若是质数, 则有 (n−1) ! ≡ n−1mod n. 于是可以先 ...

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

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

  9. HDU 6608 Fansblog(随机素数测试+思维)

    传送门 不得不说这种倒着除回去的想法真的是太绝了. 至于大数的素性测试,也是板子,没什么好说的. #include<bits/stdc++.h> #define int long long ...

最新文章

  1. 互联网1分钟 |1204
  2. 西南往事回忆录—工作点滴
  3. java外围设计_Java 编程(23 种设计模式)
  4. 纯jsp实现评论功能_自己实现的java手写tomcat
  5. c语言多种选,教你轻松学会C语言系列之——一种更简洁、更经典的选择结构
  6. jQuery实现左移右移
  7. Angular实现多标签页效果(路由重用)
  8. java 实验报告模板_java实验报告模板
  9. c语言 什么是指针变量,c语言指针详解:什么是指针?
  10. hibernate四种状态
  11. 4.29 笔记+day7作业
  12. c语言一本书的页码从自然数1开始顺序编码,算法设计与分析 1-1 统计数字问题(C语言版)...
  13. 企业联合体的形式_企业联合体与垄断的初探
  14. [PyTorch]手动实现logistic回归(只借助Tensor和Numpy相关的库)
  15. 安卓的NFC开发,简单入门
  16. 适合学生不想上学的请假理由(17个)
  17. 武汉伯钧成科技有限公司之行的郁闷感受
  18. 【小安翻唱】启程去明天——oblivious
  19. 华为私有云的搭建方案_网盘限速太坑爹,用它小白也能搭建私有云
  20. 标题:如何通过python或者云函数定时刷运动步数

热门文章

  1. 杰里之104X之输出 3 路 PMW【篇】
  2. TIOBE 3 月编程语言排行榜刚刚出炉
  3. Instruction Tuning(FLAN、instructGPT、chatGPT)
  4. android 百度云语音,手机安卓百度云AI智能之语音合成
  5. winUSB设备上位机驱动开发环境的搭建
  6. win7系统64位MSCOMCTL.OCX丢失或无效解决办法
  7. 【Python刷题篇】——Python入门 011面向对象(二)
  8. 在移动硬盘里移动视频文件到移动硬盘 另外一个文件夹 显示正在计算_稳定可靠的数据之仓 柯达X200 SSD固态移动硬盘体验评测...
  9. 利用QProcess::finished信号(signal)来保持目标程序始终运行
  10. Android 军刀级神器:Magisk