2019hdu多校 Fansblog
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的更多相关文章
- HDU6578 2019HDU多校训练赛第一场 1001 (dp)
HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...
- HDU6579 2019HDU多校训练赛第一场1002 (线性基)
HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...
- 2019HDU多校第一场1001 BLANK (DP)(HDU6578)
2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...
- 2019HDU多校第三场F Fansblog——威尔逊定理&&素数密度
题意 给定一个整数 $P$($10^9 \leq p\leq 1^{14}$),设其前一个质数为 $Q$,求 $Q! \ \% P$. 分析 暴力...说不定好的板子能过. 根据威尔逊定理,如果 $ ...
- 2019HDU多校训练第三场 Planting Trees 暴力 + 单调队列优化
题意:有一个n * n的网格,每个网格中间有一颗树,你知道每棵树的高,你可以选择一个矩形区域把里面的树都围起来,但是矩形区域里面任意两棵树的高度差的绝对值不超过m,问这个矩形的最大面积是多少? 思路: ...
- 2019HDU多校第九场 Rikka with Quicksort —— 数学推导&&分段打表
题意 设 $$g_m(n)=\begin{cases}& g_m(i) = 0, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ...
- 2019HDU多校第7场——构造
题意 假设现在你在准备考试,明天的考试有 $n$ 道题目,对于分值为 $i$ 的题目至少复习 $i+1$ 小时才能做对,已知总分为$m$,求确保完成 $k$ 道题的最少时间. 分析 手动尝试一下,发现 ...
- 2019HDU多校第六场1009 Three Investigators——杨表
题意 给定一个 n 个元素的数列,从前 k 个元素中取5次不下降子序列,求取得的和的最大值(k从1至n) 分析 考虑将数字 a[i] 拆成 a[i] 个 a[i],比如 “4,1,2”→“4,4,4, ...
- 2019HDU多校第六场 6641 TDL——乱搞&&思维题
题意 设 $f(n, m)$ 为大于 $n$ 且与 $n$ 互质的数中第 $m$ 小的数,求满足 $(f(n, m) - n) \oplus n = k$ 的最小正整数 $n$ 分析 因为 $m \l ...
随机推荐
- [转帖]中国AI芯“觉醒”的五年
中国AI芯“觉醒”的五年 https://www.cnbeta.com/articles/tech/857863.htm 原来 海思的营收已经超过了按摩店(AMD) 没想到.. 十多款芯片问世,多起并 ...
- [转帖]2017年新闻: 中国CPU还在“群雄割据” ,印度已确定了国家指令集
中国CPU还在“群雄割据” ,印度已确定了国家指令集 时间:2017-12-21 作者:观察者网 https://www.eet-china.com/news/201712210610.html ...
- Java基础(四)
概述 常用快捷键 数组 概述 数组是一种引用类型.变量只可以存放一个数据,数组则可以存放多个类型统一的数据,可以存放基本类型,也可以存放引用类型. 如果需要存储的数据很多,那么定义多个变量很麻烦: I ...
- [BZOJ 1535] [Luogu 3426]SZA-Template (KMP+fail树+双向链表)
[BZOJ 1535] [Luogu 3426]SZA-Template (KMP+fail树+双向链表) 题面 Byteasar 想在墙上涂一段很长的字符,他为了做这件事从字符的前面一段中截取了一段 ...
- 非关系型数据库MongoDB初级使用教程
安装:官网 安装难度不大,依序即可 1.新建存储文件 完成后,打开MongoDBx下载路径,新建名为data的文件夹,在此新建名为db的文件夹,db文件夹即用于存储数据 2.配置文件 在b ...
- 如何用item pipeline(管道)清洗数据
版权声明:本文为博主原创文章,转载请注明出处:如果博客中有错误之处抑或有可以改进的地方,欢迎在评论区留言. https://blog.csdn.net/f156207495/article/detai ...
- 组合&多态&封装
目录 组合&多态&封装 一.组合 1.1什么是组合 1.2 为什么要用组合 1.3 如何使用组合 1.4 继承和组合都在什么时候用 二.多态与多态性 2.1 什么是多态 2.2 如何用 ...
- 剑指offer-字符串的排列-数组-递归-动态规划-python
题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: 输 ...
- vue video.js使用
插件github地址:https://github.com/videojs/video.js 这边做一个切换视频vue url的解决记录 this.$nextTick(() => { let u ...
- vue项目报错1 Vue is a constructor and should be called with the `new` keyword && jquery.js?eedf:3850 Uncaught TypeError: this._init is not a function...
Vue is a constructor and should be called with the `new` keyword Uncaught TypeError: this._init is n ...