2019 Multi-University Training Contest 3 T6 - Fansblog
Fansblog
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 374 Accepted Submission(s): 107
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
题意
给出一个质数p,每一次询问\(s!\%p,(s\text{为小于p的最大质数})\)。
题解
定理:\((p-1)!\equiv p-1 \space(\mod p)\),p 为质数。
并且,质数以ln分配。
所以,$ans \sum_{i=s+1}^{p-1}i\equiv p-1(\mod p) $
所以,$ ans\equiv p-1\sum_{i=s+1}{p-1}i{-1}(\mod p) $
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int prime[10]={2,3,5,7,11,13,19,61,2333,24251};
long long M;
int Quick_Multiply(int a,int b,int c)
{
long long ans=0,res=a;
while(b)
{
if(b&1)
ans=(ans+res)%c;
res=(res+res)%c;
b>>=1;
}
return (int)ans;
}
int Quick_Power(int a,int b,int c)
{
int ans=1,res=a;
while(b)
{
if(b&1)
ans=Quick_Multiply(ans,res,c);
res=Quick_Multiply(res,res,c);
b>>=1;
}
return ans;
}
bool Miller_Rabin(int x)
{
int i,j,k;
int s=0,t=x-1;
if(x==2) return true;
if(x<2||!(x&1)) return false;
while(!(t&1))
{
s++;
t>>=1;
}
for(i=0;i<10&&prime[i]<x;++i)
{
int a=prime[i];
int b=Quick_Power(a,t,x);
for(j=1;j<=s;++j)
{
k=Quick_Multiply(b,b,x);
if(k==1&&b!=1&&b!=x-1)
return false;
b=k;
}
if(b!=1) return false;
}
return true;
}
signed main()
{
int T;
cin >> T;
while (T--){
int x;
int ans;
scanf("%lld",&x);
ans = x - 1;
int M = x;
while (Miller_Rabin(x-1) == 0) x--, ans = Quick_Multiply(ans, Quick_Power(x,M-2,M),M);
cout << ans << endl;
}
return 0;
}
2019 Multi-University Training Contest 3 T6 - Fansblog的更多相关文章
- 2019 Multi-University Training Contest 3 - 1006 - Fansblog - 打表 - 暴力
http://acm.hdu.edu.cn/showproblem.php?pid=6608 题意:给一个比较大的质数P(1e14以内),求比它小的最大的质数Q(貌似保证存在的样子,反正我没判不存在) ...
- 2019 Nowcoder Multi-University Training Contest 4 E Explorer
线段树分治. 把size看成时间,相当于时间 $l$ 加入这条边,时间 $r+1$ 删除这条边. 注意把左右端点的关系. #include <bits/stdc++.h> ; int X[ ...
- 2019 Nowcoder Multi-University Training Contest 1 H-XOR
由于每个元素贡献是线性的,那么等价于求每个元素出现在多少个异或和为$0$的子集内.因为是任意元素可以去异或,那么自然想到线性基.先对整个集合A求一遍线性基,设为$R$,假设$R$中元素个数为$r$,那 ...
- HDU校赛 | 2019 Multi-University Training Contest 3
2019 Multi-University Training Contest 3 http://acm.hdu.edu.cn/contests/contest_show.php?cid=850 100 ...
- 2019 Multi-University Training Contest 8
2019 Multi-University Training Contest 8 C. Acesrc and Good Numbers 题意 \(f(d,n)\) 表示 1 到 n 中,d 出现的次数 ...
- 2019 Multi-University Training Contest 7
2019 Multi-University Training Contest 7 A. A + B = C 题意 给出 \(a,b,c\) 解方程 \(a10^x+b10^y=c10^z\). tri ...
- 2019 Multi-University Training Contest 1
2019 Multi-University Training Contest 1 A. Blank upsolved by F0_0H 题意 给序列染色,使得 \([l_i,r_i]\) 区间内恰出现 ...
- 2019 Multi-University Training Contest 2
2019 Multi-University Training Contest 2 A. Another Chess Problem B. Beauty Of Unimodal Sequence 题意 ...
- 2019 Multi-University Training Contest 5
2019 Multi-University Training Contest 5 A. fraction upsolved 题意 输入 \(x,p\),输出最小的 \(b\) 使得 \(bx\%p&l ...
随机推荐
- 原生js实现选中所有的checkbox
<div class="con"> <input name='多选项名称' type='checkbox' value='' id="all" ...
- shell下的 awk/sed/grep/seq/tr
转自:实例手册 https://github.com/liquanzhou/ops_doc/blob/master/shell%E5%AE%9E%E4%BE%8B%E6%89%8B%E5%86%8C. ...
- 094、Swarm 中最重要的概念(Swarm01)
参考https://www.cnblogs.com/CloudMan6/p/7845365.html 从主机层面来看,Docker Swarm 管理的是 Docker Host 集群.所以先来讨论 ...
- Ubuntu安装openssh安装ssh、 免密登录、 创建新用户并免密登录
一.安装openssh sudo apt-get install openssh-server ssh localhost 二.免密登录 cd ~/.ssh ssh-keygen ...
- 6.纯css绘制叮当猫
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 纯CSS绘制3D立方体
本篇记录的是使用CSS3绘制3D立方体,并旋转起来. 我的思路: 1️⃣ 首先,用div元素画6个正方形摞在一起放在画布中间.为了区分,分别给每个div选择了不同的颜色,并且设置为半透明方便透视. 2 ...
- js事件冒泡、阻止事件冒泡以及阻止默认行为
事件冒泡 当事件发生后,这个事件就要开始传播(从里到外或者从外向里).为什么要传播呢?因为事件源本身(可能)并没有处理事件的能力,即处理事件的函数(方法)并未绑定在该事件源上.例如我们点击一个按钮时, ...
- 日语能力考试N2级核心词汇必备—形容词
日语能力考试N2级核心词汇必备—形容词 ありがたい·有難い 难得的,值得感谢的,真高兴的あかい·赤い 红色的,左翼的,共产主义的あさい·浅い 浅的,事物的程度等小的,色淡的,浅薄的,肤浅的あつい· 1 ...
- redis数据库如何用Django框架缓存数据
---恢复内容开始--- 一.python 使用redis 1.1 安装 pip install redis 测试有一些基本的数据类型 import redis # redis 是一个缓存数据库 # ...
- Delphi 保留字