hdu2138 How many prime numbers(Miller_Rabbin随机素数测试)
来源:互联网 发布:kmeans聚类算法java 编辑:程序博客网 时间:2024/05/23 01:22
给定几个数,问其中素数的个数。
由于数量很大,打表肯定过不了,所以必须用Miller_Rabbin随机素数测试的方法,下面是我的C++ Miller_Rabbin模板
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;typedef long long ll;ll exp_mod(ll a,ll b,ll mod){ll res = 1;while(b){if(b & 1) res = (res * a) % mod;a = (a * a) % mod;b >>= 1;}return res;}bool Miller_Rabbin(ll n){ll a,s = (n-1) >> 2,i = 1, t = n-1;s = min(s,(ll)50);do{a = rand() % (t) + 1;if(exp_mod(a,t,n) != 1) return false;}while(i++ < s);return true;}int main(){int n,ans;ll num;while(scanf("%d",&n)!=EOF){ans = 0;for(int i = 0;i < n;i++){scanf("%I64d",&num);if(Miller_Rabbin(num))ans++;}printf("%d\n",ans);}return 0;}
- hdu2138 How many prime numbers(Miller_Rabbin随机素数测试)
- hdu2138 How many prime numbers (素数测试)
- hdu2138 How many prime numbers
- HDU2138 How many prime numbers
- HDU2138--How many prime numbers
- HDU2138 How many prime numbers【水题】
- HDu 2138 How many prime numbers 高效Miller素数测试
- 判断素数How many prime numbers
- 素数——How many prime numbers
- hdu 2138 How many prime numbers (随即素数测试模版)
- HDU 2138 How many prime numbers(米勒拉宾素数测试算法)
- HDU-2138-How many prime numbers(米勒拉宾素数测试)
- How many prime numbers
- How many prime numbers
- How many prime numbers
- How many prime numbers
- How many prime numbers
- How many prime numbers
- HDOJ_ 1133 Buy the Ticket
- POJ1651
- Inline Virtual Functions
- 文件系统小结
- ZOJ 1039 Number Game (状态压缩,记忆化搜索)
- hdu2138 How many prime numbers(Miller_Rabbin随机素数测试)
- 关于JavaScript的两个建议
- 进制学习
- Android系统中设置TextView的行间距(非行高)
- OleDB简单用例
- sgmllib Introduction
- jQuery之checkbox全选
- POJ 2594 允许有相交点的最小路径覆盖
- strrep()函数的实现