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

Solution:

通过严谨的计算(打表)可以发现,对于一个质数\(p\),有\((p-1)!\,mod\,p=p-1\)

并且我们知道对于一个质数\(p\),在\(log \,p\)次内可以找到比它小的最大的质数

那么我们每次只要判断当前数是不是质数,再把它除掉,也就是乘上它的逆元,就行了

Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int isprime(int x){
if(x<2) return 0;
for(int i=2;i*i*1ll<=x;i++)
if(x%i==0) return 0;
return 1;
}
int dqy(int a,int b,int p){
int ans=0;
while(b){
if(b&1)ans=(ans+a)%p;
a=(a+a)%p;
b>>=1;
}
return ans;
}
int quick(int a,int b,int p){
int re=1;
while(b){
if(b&1) re=dqy(re,a,p);
a=dqy(a,a,p);b>>=1;
}return re%p;
}
void solve(){
int p=read(),ans=p-1;
for(int x=p-1;x;x--){
if(isprime(x)) break;
ans=dqy(ans,quick(x,p-2,p),p);
}printf("%lld\n",ans);
}
signed main(){
int t=read();
while(t--) solve();
return 0;
}

2019hdu多校 Fansblog的更多相关文章

  1. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  2. HDU6579 2019HDU多校训练赛第一场1002 (线性基)

    HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...

  3. 2019HDU多校第一场1001 BLANK (DP)(HDU6578)

    2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...

  4. 2019HDU多校第三场F Fansblog——威尔逊定理&&素数密度

    题意 给定一个整数 $P$($10^9 \leq p\leq 1^{14}$),设其前一个质数为 $Q$,求 $Q!  \ \% P$. 分析 暴力...说不定好的板子能过. 根据威尔逊定理,如果 $ ...

  5. 2019HDU多校训练第三场 Planting Trees 暴力 + 单调队列优化

    题意:有一个n * n的网格,每个网格中间有一颗树,你知道每棵树的高,你可以选择一个矩形区域把里面的树都围起来,但是矩形区域里面任意两棵树的高度差的绝对值不超过m,问这个矩形的最大面积是多少? 思路: ...

  6. 2019HDU多校第九场 Rikka with Quicksort —— 数学推导&&分段打表

    题意 设 $$g_m(n)=\begin{cases}& g_m(i) = 0,     \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ...

  7. 2019HDU多校第7场——构造

    题意 假设现在你在准备考试,明天的考试有 $n$ 道题目,对于分值为 $i$ 的题目至少复习 $i+1$ 小时才能做对,已知总分为$m$,求确保完成 $k$ 道题的最少时间. 分析 手动尝试一下,发现 ...

  8. 2019HDU多校第六场1009 Three Investigators——杨表

    题意 给定一个 n 个元素的数列,从前 k 个元素中取5次不下降子序列,求取得的和的最大值(k从1至n) 分析 考虑将数字 a[i] 拆成 a[i] 个 a[i],比如 “4,1,2”→“4,4,4, ...

  9. 2019HDU多校第六场 6641 TDL——乱搞&&思维题

    题意 设 $f(n, m)$ 为大于 $n$ 且与 $n$ 互质的数中第 $m$ 小的数,求满足 $(f(n, m) - n) \oplus n = k$ 的最小正整数 $n$ 分析 因为 $m \l ...

随机推荐

  1. datav轮播表使用事例

    官方事例地址: http://datav.jiaminghi.com/guide/scrollBoard.html 安装: npm install @jiaminghi/data-view 局部引入: ...

  2. python 并发编程 多线程 目录

    线程理论 python 并发编程 多线程 开启线程的两种方式 python 并发编程 多线程与多进程的区别 python 并发编程 多线程 Thread对象的其他属性或方法 python 并发编程 多 ...

  3. spring boot-3.原理探究

    新建的项目结构如下图: 1.POM 文件 项目会默认依赖 spring-boot-starter-parent 项目 <parent> <groupId>org.springf ...

  4. BZOJ 1257 余数之和 题解

    题面 这道题是一道整除分块的模板题: 首先,知道分块的人应该知道,n/i最多有2*sqrt(n)种数,但这和余数有什么关系呢? 注意,只要n/i的值和n/(i+d)的值一样,那么n%i到n%(i+d) ...

  5. OLT设置vlan透传及ONU端口划vlan

    CZ-PX-ShangShi-FTTH-OLT-A#show runn System current configuration:!Software Version ISCOM5800E-SMCB_1 ...

  6. js自执行函数

    5.1对于函数表达式,在后面加括号即可以让函数立即执行:例如下面这个函数,至于为什么加了括号就可以立即执行,我们可以这么理解,就是像fn1():这样写的话,函数 可以立即执行是没问题的,我们在经常会用 ...

  7. thinkphp5服务器部署遇到的问题

    candir() has been disabled for security reasons 解决办法: 进入到php的配置目录,编辑php.ini cd /usr/local/php/etcvi ...

  8. 初探CSS -3 语法

    CSS 语法 实例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  9. 2019-11-29-git无法pull仓库refusing-to-merge-unrelated-histories

    title author date CreateTime categories git无法pull仓库refusing to merge unrelated histories lindexi 201 ...

  10. 2019-11-29-C#-序列类为-xml-可以使用的特性大全

    title author date CreateTime categories C# 序列类为 xml 可以使用的特性大全 lindexi 2019-11-29 8:59:2 +0800 2018-6 ...