Fansblog

Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 3170 Accepted Submission(s): 671

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,找出小于P的最小素数Q,并计算Q的阶乘对P取模的结果

解决

从P-1开始进行素数检测,因为素数的分布是比较密集的,所以可以用试除法来判断素数。

在找到Q之后,由威尔逊定理可知:当P是素数的情况下,(P-1)! Ξ -1(mod P)

因为求的是Q! mod P,所以我们可以先将(P-1)! mod P求出来的,然后利用除法来计算Q! mod P

Code

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;
 6 const ll INF=0x3f3f3f3f3f3f3f3f;
 7 const int maxn=1e6+10;
 8 const int mod=1e9+7;
 9 const int maxm=1e3+10;
10 using namespace std;
11 bool check(ll n)
12 {
13     for(ll i=2;i*i<=n;i++)
14         if(n%i==0)
15             return false;
16     return true;
17 }
18 ll modmul(ll A,ll B,ll Mod)
19 {
20     return (A*B-(ll)((long double)A*B/Mod)*Mod+Mod)%Mod;
21 }
22 ll Pow(ll a,ll b,ll c)
23 {
24     ll ans=1;
25     while(b)
26     {
27         if(b&1)
28             ans=modmul(ans,a,c);
29         b>>=1;
30         a=modmul(a,a,c);
31     }
32     return ans;
33 }
34 ll inv(ll a,ll b)
35 {
36     return Pow(a,b-2,b);
37 }
38 int main(int argc, char const *argv[])
39 {
40     #ifndef ONLINE_JUDGE
41         freopen("in.txt", "r", stdin);
42         freopen("out.txt", "w", stdout);
43         srand((unsigned int)time(NULL));
44     #endif
45     ios::sync_with_stdio(false);
46     cin.tie(0);
47     int t;
48     ll p;
49     cin>>t;
50     while(t--)
51     {
52         cin>>p;
53         ll num;
54         for(ll i=p-1;;i--)
55             if(check(i))
56             {
57                 num=i;
58                 break;
59             }
60         ll ans=p-1;
61         for(ll i=num+1;i<=p-1;i++)
62             ans=modmul(ans,inv(i,p),p);
63         cout<<ans<<endl;
64     }
65     #ifndef ONLINE_JUDGE
66         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
67     #endif
68     return 0;
69 }

转载于:https://www.cnblogs.com/Friends-A/p/11267854.html

HDU 6608:Fansblog(威尔逊定理)相关推荐

  1. hdu 6608 Fansblog 威尔逊定理+大数阶乘取模

    传送门 题意:给出一个质数P,找出小于P的最大的质数N,求出N的阶乘模P.(P∈[1e10,1e14]) 思路:威尔逊定理:一个数n若是质数, 则有 (n−1) ! ≡ n−1mod n. 于是可以先 ...

  2. HDU 6608 FansBlog(粉丝博客)(MillerRabin算法+威尔逊算法)

    Farmer John keeps a website called 'FansBlog' .Everyday , there are many people visited this blog.On ...

  3. 「hdu6608」Fansblog 威尔逊定理

    「hdu6608」 Fansblog 今天回顾之前多校联赛的题目:Fansblog ,发现一件有趣的事情,快速积的时间复杂度,比普通*快多了,刚才一直Tle 在用了普通*,卧槽这也能T,当然也有乘法爆 ...

  4. HDU 6608 Fansblog——————大素数检测

    Fansblog 点击题目查看题面 点击这里也可以 Source 2019 Multi-University Training Contest 3 给你一个素数PPP 让给你求最接近P的素数QQQ 输 ...

  5. 威尔逊定理 ---- [hdu-6608] Fansblog 威尔逊定理 质数的密度分布 快速乘优化快速幂防止中间爆longlong

    题目链接 题目大意:就是给你一个质数P∈[2,1e14]P\in[2,1e14]P∈[2,1e14]求一个质数Q<PQ<PQ<P解出Q!modPQ!modPQ!modP 解题思路: ...

  6. HDU 6608 Fansblog(随机素数测试+思维)

    传送门 不得不说这种倒着除回去的想法真的是太绝了. 至于大数的素性测试,也是板子,没什么好说的. #include<bits/stdc++.h> #define int long long ...

  7. HDU 6608 [2019 Multi-University Training Contest 3]

    Fansblog Problem Description Farmer John keeps a website called 'FansBlog' .Everyday , there are man ...

  8. 三个重要的同余式——威尔逊定理、费马小定理、欧拉定理 + 求幂大法的证明

    一.威尔逊定理 若p为质数,则 p|(p-1)!+1 亦:(p-1)! ≡ p-1 ≡ -1(mod p) 例题: HDU 2973 YAPTCHA (威尔逊定理及其逆定理) 解题报告见http:// ...

  9. HDU多校第三场6608 Fansblog(米勒罗宾+威尔逊定理)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6608 题目说给你一个1e9−1e14的素数p,让你找到比p小的最大素数q,求q!mod  p的值题目说 ...

最新文章

  1. C#进阶系列——动态Lamada
  2. jquery 对 Json 的各种遍历
  3. draw.io创建自定义形状
  4. ProE二次开发之VS2005+ProE Wildfire 4.0开发环境配置
  5. WebService中文件传输
  6. UI交互设计关键词:情感化设计与心理
  7. xcode4.1自带SVN配置
  8. linux 黑酷命令行背景图片
  9. Partition分区的使用案例
  10. Think in Java ---Chapter 8 多态 [基础的混凝土大厦]
  11. vue基础知识点思维导图
  12. c语言关于内存编程,c语言内存
  13. linux下无sudo权限安装MatlabR2016
  14. win7计算机如何设置密码,电脑win7怎么设置开机密码
  15. Python之爬虫和数据小解析
  16. linux硬件之磁盘运行读写原理
  17. 如何实时计算日累计逐单资金流
  18. stm32正常运行流程图_stm32学习笔记之问题总结
  19. 关于网站恶意注册,访问
  20. 2022年最新河南建筑施工电工(建筑特种作业)模拟试题及答案

热门文章

  1. JAVA期末大作业之学生信息管理简洁版系统
  2. resultMap使用不当导致出现There is no WRITEABLE property named 'student_id' in class 'com.ssi.model.Stud
  3. 基于python的个人博客系统的设计开题报告_基于JavaSSM框架的个人博客系统设计与实现开题报告...
  4. ECMAScript(pink)
  5. 李嘉诚:无霸气才能成霸业
  6. 人工智能的定义与发展史
  7. breezy,dapper,edgy,feisty的含义
  8. 第 5 章 函数和代码复用
  9. MYSQL系列---默认隔离级别所引发的问题
  10. 【技术详解】阿里云AIoT物模型支撑设备规模已超亿级