Fansblog

Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2801 Accepted Submission(s): 1158

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

Source
2019 Multi-University Training Contest 3

问题链接:HDU6608 Fansblog
问题简述:给定一个109−1014内的质数p,求小于p的最大质数的阶乘取模p。
问题分析
    这个问题的解决需要用到Miller_Rabin素性测试算法和威尔逊定理,这里不做解释。
程序说明
    快速模幂计算过程中需要做乘法,有可能会产生溢出,所以需要用一个函数来实现。还有一种方法是使用128位整数计算。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU6608 Fansblog */#include <bits/stdc++.h>using namespace std;typedef unsigned long long ULL;const int TIME = 5;ULL gcd(ULL a, ULL b) {return b == 0 ? a : gcd(b, a % b);
}// 快速乘积,计算a*b
ULL mulmod(ULL a, ULL b, ULL m)
{ULL ret = 0;while(b) {if(b & 1) {ret += a; ret %= m;}a = (a << 1) % m;b >>= 1;}return ret;
}// 快速模幂计算
ULL powmod(ULL a, ULL b, ULL mod)
{ULL ret = 1;while(b) {if(b & 1) ret = mulmod(ret, a, mod);a = mulmod(a, a, mod);b >>= 1;}return ret;
}ULL random(ULL n)
{return (ULL)((double) rand() / RAND_MAX * n + 0.5);
}bool check(ULL a, ULL n)
{ULL m = n - 1;int t = 0;while((m & 1) == 0) {t++; m >>= 1;}ULL x = powmod(a, m, n);if(x == 1 || x == n - 1)return false;while(t--) {x = mulmod(x, x, n);if(x == n - 1)return false;}return true;
}bool Miller_Rabin(ULL n)
{if(n < 2) return false;if(n == 2) return true;if((n & 1) == 0) return false;ULL x = n - 1;ULL t = 0;while((x&1) == 0) {x >>= 1;t++;}for(int i = 0; i <= TIME; i++){ULL a = random(n - 2) + 1;if(check(a, n)) return false;}return true;
}int main()
{int t;ULL n;scanf("%d", &t);while(t--) {scanf("%llu", &n);ULL p = n - 1;while(!Miller_Rabin(p)) p--;ULL ans = 1;for(ULL i = p + 1; i < n - 1; i++)ans = mulmod(ans, powmod(i, n - 2, n), n);printf("%llu\n", ans);}return 0;
}

AC的C++语言程序如下:

/* HDU6608 Fansblog */#include <bits/stdc++.h>using namespace std;typedef unsigned long long ULL;ULL gcd(ULL a,ULL b) {return b == 0 ? a : gcd(b, a % b);
}// 快速乘积,计算a*b
ULL mulmod(ULL a, ULL b, ULL m)
{ULL ret = 0;while(b) {if(b & 1) {ret += a; ret %= m;}a = (a << 1) % m;b >>= 1;}return ret;
}// 快速模幂计算
ULL powmod(ULL a, ULL b, ULL mod)
{ULL ret = 1;while(b) {if(b & 1) ret = mulmod(ret, a, mod);a = mulmod(a, a, mod);b >>= 1;}return ret;
}bool check(ULL a, ULL n)
{ULL m = n - 1;int t = 0;while((m & 1) == 0) {t++; m >>= 1;}ULL x = powmod(a, m, n);if(x == 1 || x == n - 1)return false;while(t--) {x = mulmod(x, x, n);if(x == n - 1)return false;}return true;
}bool Miller_Rabin(ULL n)
{if(n == 2) return true;if(n == 1 || !(n & 1)) return false;const ULL prime[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};for(int i = 0; i < 12; i++) {if (prime[i] >= n) break;if(check(prime[i], n)) return false;}return true;
}int main()
{int t;ULL n;scanf("%d", &t);while(t--) {scanf("%llu", &n);ULL p = n - 1;while(!Miller_Rabin(p)) p--;ULL ans = 1;for(ULL i = p + 1; i < n - 1; i++)ans = mulmod(ans, powmod(i, n - 2, n), n);printf("%llu\n", ans);}return 0;
}

HDU6608 Fansblog【Miller_Rabin素性测试算法+威尔逊定理】相关推荐

  1. 米勒-拉宾(MillerRabbin)素性测试算法

    原创滴博客~https://www.cnblogs.com/precious-ZPF/p/9481599.html 小编赶紧摘过来的,多看几遍向银家多学习学习QAQ 首先,在了解米勒-拉宾素性测试之前 ...

  2. 2019杭电多校第三场 6608 Fansblog(威尔逊定理+miller_rabin素性测试)

    Problem Description 传送门 Farmer John keeps a website called 'FansBlog' .Everyday , there are many peo ...

  3. miller_rabin_素性测试

    摘自:http://blog.csdn.net/pi9nc/article/details/27209455 看了好久没看懂,最后在这篇博客中看明白了. 费马定理的应用,加上二次探测定理. Ferma ...

  4. 素数与素性测试(Miller-Rabin测试)(目前为止我见过最好的博客)

    原地址(原地址比我好了100倍):https://www.cnblogs.com/Norlan/p/5350243.html 以下是原文: 转载自Matrix大牛的博客 把代码翻译成C++ http: ...

  5. 素性测试AKS算法程序

    AKS算法,是三位印度人发明的,AKS是他们的姓氏首字母.ASK算法是确定算法,其时间复杂度相当于多项式的,属于可计算的算法. 另外需要了解的是Miller-Rabin素性测试算法.该算法不是确定算法 ...

  6. 「hdu6608」Fansblog【Miller_Rabin+威尔逊定理】

    Fansblog Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  7. 「hdu6608」Fansblog 威尔逊定理

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

  8. 2019多校第三场 HDU6608 Fansblog(威尔逊定理,逆元,质数间隔)

    链接:HDU6608 Fansblog 题意: 给出一个质数PPP(109≤P≤101410^9\le P\le 10^{14}109≤P≤1014),找出最大的质数QQQ(Q<PQ \lt P ...

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

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

最新文章

  1. XP与Windows 7(Win7)等操作系统Ghost备份
  2. STC单片机自动下载调试器设计
  3. JavaScript中的地图与对象
  4. 无人车飞速狂飙,黑科技如何为其加油打气?
  5. PackageManager.hasSystemFeature Android SystemServer裁剪
  6. 赛事+内容IP齐发力,汽车之家打破Z世代次元壁
  7. aria2和motrix的使用
  8. 五、ELK设置用户密码登陆
  9. 用PS抠图做电子签名
  10. FSA(有限状态自动机)python代码实现 自然语言处理作业
  11. 电瓶车罚款不交后果如何
  12. iphone应用隐私政策_如何在iPhone上“隐藏”联系人以获得更大的隐私
  13. Selenium学习 - ActionChains接口
  14. 单点登录cas-4.0.0 只是简单的同时登入,同时登出功能
  15. 社群运营:如何招募高质量种子用户?
  16. Springboot 启动命令中–spring.config.location不生效问题
  17. 多多情报通:拼多多蓝海词是什么意思?拼多多蓝海词怎么找?
  18. 决策树随机森林GBDTXGBoost学习笔记以及代码实现
  19. 优秀网址导航国内篇(更新于20170408)
  20. iphone 计算机知道密码忘了,苹果笔记本密码忘了怎么办_苹果笔记本密码忘记如何解决-win7之家...

热门文章

  1. 在移动设备中创建ArcGIS API for JavaScript地图应用程序
  2. Go 1.9中值得关注的几个变化
  3. 在C#中获取如PHP函数time()一样的时间戳
  4. 蛙蛙推荐:在c#使用IOCP(完成端口)的简单示例
  5. 达梦数据库 函数操作
  6. eclipse使用git合并_IntelliJ IDEA完整使用教学:从小白到大牛的必经之路!
  7. HDFS读流程,写流程,放置策略
  8. java语言中实现键盘输入_Java程序设计中的键盘输入数据的方法分析
  9. java解析pom.xml_从pom.xml java获取变量
  10. jdk jre jvm的关系