【HDOJ6608】Fansblog(威尔逊定理)
题意:给定质数p,求q!模p的值,其中q为小于p的最大质数
1e9<=p<=1e14
思路:根据质数密度近似分布可以暴力找q并检查
找到q后根据威尔逊定理:
把q+1到p-1这一段的逆元移过去
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
#define N 110000
#define M 1100000
#define fi first
#define se second
#define MP make_pair
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const ll MOD=,inv2=(MOD+)/;
double eps=1e-;
ll INF=1e14; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} int prime(ll x)
{
ll t=sqrt(x);
for(ll i=;i<=t;i++)
if(x%i==) return ;
return ;
} ll mult(ll a,ll b,ll p)
{
ll t=(a*b-ll((long double)a/p*b+1e-)*p)%p;
return t<?t+p:t;
} ll pw(ll x,ll y,ll p)
{
ll t=;
while(y)
{
if(y&) t=mult(t,x,p);
x=mult(x,x,p);
y>>=;
}
return t;
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int cas=read();
while(cas--)
{
ll n;
scanf("%I64d",&n);
ll k=n-;
if(n==) k=;
else
{
while(k&&!prime(k)) k-=;
}
ll ans=n-;
//printf("k=%I64d\n",k);
for(ll i=k+;i<=n-;i++)
{
//printf("i=%I64d\n",i);
ll t=pw(i,n-,n);
//printf("inv%I64d=%I64d\n",i,t);
ans=mult(ans,t,n);
} printf("%I64d\n",ans);
}
return ;
}
【HDOJ6608】Fansblog(威尔逊定理)的更多相关文章
- 2019杭电多校第三场hdu6608 Fansblog(威尔逊定理)
Fansblog 题目传送门 解题思路 Q! % P = (P-1)!/(P-1)...(Q-1) % P. 因为P是质数,根据威尔逊定理,(P-1)!%P=P-1.所以答案就是(P-1)((P-1) ...
- 2019HDU多校第三场F Fansblog——威尔逊定理&&素数密度
题意 给定一个整数 $P$($10^9 \leq p\leq 1^{14}$),设其前一个质数为 $Q$,求 $Q! \ \% P$. 分析 暴力...说不定好的板子能过. 根据威尔逊定理,如果 $ ...
- HDU 6608:Fansblog(威尔逊定理)
Fansblog Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- HDU-6608 Fansblog(威尔逊定理+素数间隔+逆元)
参考博客:https://blog.csdn.net/birdmanqin/article/details/97750844 题目链接:链接:http://acm.hdu.edu.cn/showpro ...
- HDU6608-Fansblog(Miller_Rabbin素数判定,威尔逊定理应用,乘法逆元)
Problem Description Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people ...
- hdu5391 Zball in Tina Town(威尔逊定理)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Zball in Tina Town Time Limit: 3000/1500 ...
- HDU2973(威尔逊定理)
YAPTCHA Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 2973"YAPTCHA"(威尔逊定理)
传送门 题意: 给出自然数 n,计算出 Sn 的值,其中 [ x ]表示不大于 x 的最大整数. 题解: 根据威尔逊定理,如果 p 为素数,那么 (p-1)! ≡ -1(mod p),即 (p-1)! ...
- hdu2973 YAPTCHA【威尔逊定理】
<题目链接> 题目大意: The task that is presented to anyone visiting the start page of the math departme ...
随机推荐
- Python中import的使用方法
源文出处: "import"的本质参照: Python中import机制 python导入自定义模块和包
- git 时 出现 Permission denied (publickey).
https://blog.csdn.net/awp0011/article/details/73368481 第一次使用github.com在本地 执行 git clone git@github.co ...
- upc 组队赛18 STRENGTH【贪心模拟】
STRENGTH 题目链接 题目描述 Strength gives you the confidence within yourself to overcome any fears, challeng ...
- [Linux] 027 RPM 包与 源码包的区别
1. 区别 安装之前的区别: 概念上的区别 安装之后的区别: 安装位置不同 2. RPM 包安装位置 安装在默认位置中 RPM 包默认安装路径 明细 /ect 配置文件安装目录 /usr/bin/ 可 ...
- [Linux] 003 分区
1. 磁盘分区 使用分区编辑器再磁盘上划分几个逻辑部分 不用类的目录与文件可以存储进不同的分区 2. 分区类型 主分区 最多只能有 4 个 扩展分区 最多只能有 1 个 主分区加扩展分区最多为 4 个 ...
- vue 全局filter的坑
下面连段代码的filter放在不同的位子会有不同的效果, 1.filter放在new vue之后,居然不起作用 <script> new Vue({ el: '#app', data: { ...
- 耗时近一个月,终于录完了VUE.JS2.0前端视频教程!
这次课录制的比较辛苦,圣诞节时原本已经快录制完成了,偶然的一次,播放了一下,感觉不满意,好几篇推倒重来,所以今天才结束. vue.js2.0是Vue.JS的最新版本,视频教程还不多,如果你看到了,学到 ...
- Shell脚本语言学习总结
Shell 是一种脚本程序,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了. 编写第一个Shell 程序 在linux命令行执行 [root@iz2zeexr9tk4ckr7dp ...
- CodeChef A String Game(SG)
A String Game Problem code: ASTRGAME Submit All Submissions All submissions for this problem a ...
- poj2385 Apple Catching (线性dp)
题目传送门 Apple Catching Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 154 ...