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. 洛谷 P3919 可持久化线段树 题解

    题面 这题好水的说~很明显就是主席树的大板子 然而我交了3遍才调完所有的BUG,开好足够的数组,卡掉大大的常数: 针对与每次操作,change()会创建新节点,而ask()虽然也会更新左右儿子的节点编 ...

  2. Mysql 事件记录 | performance_schema全方位介绍

    Mysql 事件记录 | performance_schema全方位介绍 | 导语 在上一篇 初相识|performance_schema全方位介绍 中,我们详细介绍了performance_sche ...

  3. sql server 获取随机数函数RAND()和RAND(x)

    --RAND(x)返回一个随机浮点值v,范围在0~1之间(即0<=v<=1.0) --若指定一个整数参数x,则它被用作种子值,使用相同的种子数将产生重复序列.如果同一种子值多次调用RAND ...

  4. IDEA配置远程git仓库

    一,在项目创建本地仓储 2,选择当前文件夹为本地仓储 3, 将代码添加到本地仓库 4,提交到本地仓库 5,提交到本地仓库 配置远程账号和地址 输入你自己的远程仓库----码云或者github,创建的 ...

  5. 8.jQuery之上下滑动效果

    上下滑动:slideDown   slideUp  slideToggle <style> div { width: 150px; height: 300px; background-co ...

  6. intellij 编译 springmvc+hibernate+spring+maven 找不到hbm.xml映射文件

    1. 错误信息 Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException ...

  7. last, lastb - 显示最近登录的用户列表

    总览 last [-R] [-num] [ -n num ] [-adiox] [ -f file ] [name...] [tty...] lastb [-R] [-num] [ -n num ] ...

  8. AIX用户和组管理

    1.用户登录和初始化

  9. 网页图片失效自动替换图片地址js代码

    当你网页中的图片失效之后它会显示你预先设定好的默认图片,而不是显示为一个大大的红叉叉. js脚本如下: <script language="javascript"> v ...

  10. Codeforces 899 1-N两非空集合最小差 末尾最多9对数计算 pair/链表加优先队列最少次数清空

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...