HDU-6608 Fansblog(威尔逊定理+素数间隔+逆元)
即:当且仅当p为素数时:( p -1 )! ≡ -1 ( mod p ),但是由于阶乘是呈爆炸增长的,其结论对于实际操作意义不大。
(Q!)*(Q+1)*(Q+2)*...*(P-1)=(p-1)!
得到Q!(mod P)=(((P-1)!)/(Q+1)*(Q=2)*(Q+3)*...*(P-1))(mod P)
Q!(mod P)=((P-1)/(Q+1)*(Q=2)*(Q+3)*...*(P-1))(mod P)
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 using namespace std;
6 typedef long long ll;
7 const int maxn=1e7+10;
8 ll mod;
9 int prime[maxn+10],cnt;
10 int vis[maxn+10];
11 void get_prime()
12 {
13 cnt=0;
14 for(int i=2;i<=maxn;++i)
15 {
16 if(!vis[i])
17 prime[cnt++]=i;
18 for(int j=0;j<cnt&&(ll)i*prime[j]<=maxn;j++)
19 {
20 vis[i*prime[j]]=1;
21 if(i%prime[j]==0) break;
22 }
23 }
24 }
25 bool is_prime(ll x)
26 {
27 for(int i=0;i<cnt&&(ll)prime[i]*prime[i]<=x;++i)
28 {
29 if(x%prime[i]==0)
30 return 0;
31 }
32 return 1;
33 }
34 ll mul(ll a,ll b)
35 {
36 ll res=0;
37 while(b)
38 {
39 if(b&1) res=(res+a)%mod;
40 a=(a+a)%mod;
41 b>>=1;
42 }
43 return res%mod;
44 }
45 ll poww(ll a,ll b)
46 {
47 ll res=1;
48 while(b)
49 {
50 if(b&1)
51 res=mul(res,a);
52 a=mul(a,a);
53 b>>=1;
54 }
55 return res;
56 }
57 int main()
58 {
59 int t;
60 ll p,q;
61 get_prime();
62 scanf("%d",&t);
63 while(t--)
64 {
65 scanf("%lld",&p);
66 mod=p;
67 q=p-1;
68 while(!is_prime(q)) q--;
69 ll ans=p-1;
70 for(ll i=q+1;i<=p-1;++i)
71 {
72 ans=mul(ans,poww(i,mod-2));
73 }
74 printf("%lld\n",ans);
75 }
76 return 0;
77 }

HDU-6608 Fansblog(威尔逊定理+素数间隔+逆元)的更多相关文章
- 2019HDU多校第三场F Fansblog——威尔逊定理&&素数密度
题意 给定一个整数 $P$($10^9 \leq p\leq 1^{14}$),设其前一个质数为 $Q$,求 $Q! \ \% P$. 分析 暴力...说不定好的板子能过. 根据威尔逊定理,如果 $ ...
- 2019杭电多校第三场hdu6608 Fansblog(威尔逊定理)
Fansblog 题目传送门 解题思路 Q! % P = (P-1)!/(P-1)...(Q-1) % P. 因为P是质数,根据威尔逊定理,(P-1)!%P=P-1.所以答案就是(P-1)((P-1) ...
- hdu 2973"YAPTCHA"(威尔逊定理)
传送门 题意: 给出自然数 n,计算出 Sn 的值,其中 [ x ]表示不大于 x 的最大整数. 题解: 根据威尔逊定理,如果 p 为素数,那么 (p-1)! ≡ -1(mod p),即 (p-1)! ...
- hdu 2582(数论相关定理+素数筛选+整数分解)
f(n) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 6608:Fansblog(威尔逊定理)
Fansblog Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- HDU6608-Fansblog(Miller_Rabbin素数判定,威尔逊定理应用,乘法逆元)
Problem Description Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people ...
- HDU 5391 Zball in Tina Town【威尔逊定理】
<题目链接> Zball in Tina Town Problem Description Tina Town is a friendly place. People there care ...
- HDU - 2973:YAPTCHA (威尔逊定理)
The math department has been having problems lately. Due to immense amount of unsolicited automated ...
- HDU 2973 YAPTCHA (威尔逊定理)
YAPTCHA Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- (二)数据源处理1-configparser读取.ini配置文件
import osimport configparsercurrent_path =os.path.dirname(__file__)#获取config当前文件路径config_file_path = ...
- 【Java】标识符
一.标识符 文章目录 一.标识符 1.标识符的命名规则 2.关键字.保留字.特殊值 3.code Java 对各种变量.方法和类等要素命名时使用的字符序列称为标识符.简单的说,凡是程序员自己命名的部分 ...
- 2019 Eclipse的下载与安装教程
Eclipse 是一个开放源代码的.基于Java的可扩展开发平台,可以免费下载使用. 首先我们先进入这个软件的官网:https://www.eclipse.org/ 点击这个网页download下载: ...
- React中的合成事件
React中的合成事件 React自己实现了一套高效的事件注册.存储.分发和重用逻辑,在DOM事件体系基础上做了很大改进,减少了内存消耗,简化了事件逻辑,并最大程度地解决了IE等浏览器的不兼容问题. ...
- linux下安装nacos
一.安装 1.下载安装包: https://github.com/alibaba/nacos/releases 2.解压 : tar -xzvf nacos-server-1.2.1.tar.gz 3 ...
- Redisson 分布式锁实战与 watch dog 机制解读
Redisson 分布式锁实战与 watch dog 机制解读 目录 Redisson 分布式锁实战与 watch dog 机制解读 背景 普通的 Redis 分布式锁的缺陷 Redisson 提供的 ...
- HTML部分
1.说一下<label>标签的用法 label标签主要是方便鼠标点击使用,扩大可点击的范围,增强用户操作体验 2.说一下事件代理? 事件委托是指将事件绑定到目标元素的父元素上,利用冒泡机制 ...
- C++ unordered_map/unordered_set 自定义键类型
1. unordered_map 和 unordered_set template < class Key, // unordered_map::key_type class T, // uno ...
- Architecture and design 洋葱 中间件 装饰器
Go kit - Frequently asked questions https://gokit.io/faq/ Architecture and design Introduction - Und ...
- java-数据类型复习
java中共有8种基本的数据类型,分别为 字节型byte(8字节,32位),短整型short(16字节),整型int(32字节),长整型long(64字节), 字符型char(16字节),浮点型flo ...