ACM常用模板合集

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int times = 10;
ll fast_mod(ll a,ll b,ll mod)//计算2^q的过程
{
ll res = 0;
while(b){
if(b & 1) res = res + a;
a <<= 1;
if(a >= mod) a -= mod;
if(res >= mod) res -= mod;
b >>= 1;
}
return res;
}
ll fast_pow_mod(ll a,ll b,ll mod)//快速幂算出a^m
{
ll res = 1;
while(b){
if(b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
bool check(ll a,ll m,ll p,ll n)//对于每次随机的a进行测试
{
ll temp = fast_pow_mod(a,m,n),ret = temp;
for(int i = 0;i < p;++i){
ret = fast_mod(temp,temp,n);
if(ret == 1 && temp != n - 1 && temp != 1) return true;
temp = ret;
}
return ret != 1;
}
bool Miller_Pabin(ll n)//Miller测试的主体结构
{
if(n < 2) return false;
if(n == 2) return true;
if(n & 1 == 0) return false;//对于偶数的优化
ll p = 0,x = n - 1;//p为Miller测试的q,x为Miller测试的m
while(x & 1 == 0){
x >>= 1;
p++;
}
srand(time(NULL));
for(int i = 0;i < times;++i){
ll o = rand() % (n - 1) + 1;//o就是Miller测试的底数a
if(check(o,x,p,n)) return false;
}
return true;
} int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
long long n;
cin >> n;
cout << (Miller_Pabin(n) ? "Prime" : "Not a Prime") << endl;
}
return 0;
}

数学--数论--Miller_Rabin判断素数的更多相关文章

  1. 数学--数论--Miller_Rabin判断一个大数是不是素数(随机算法)

    前提知识 1,费马定理:ap−1=1(mod p)a^{p-1}=1(mod\ p)ap−1=1(mod p)

  2. HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )

    How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  4. Golang并行判断素数

    ## Golang多核判断素数方式 package main import ( "bufio" "fmt" "os" "runti ...

  5. Miller_Rabin()算法素数判定 +ollard_rho 算法进行质因数分解

    //****************************************************************// Miller_Rabin 算法进行素数测试//速度快,而且可以 ...

  6. 快速判断素数 --Rabin-Miller算法

    以前我在判断素数上一直只会 sqrt(n) 复杂度的方法和所谓的试除法(预处理出sqrt(n)以内的素数,再用它们来除). (当然筛选法对于判断一个数是否是素数复杂度太高) 现在我发现其实还有一种方法 ...

  7. 2java判断素数

    package com.test; import java.math.*;import java.util.Scanner; public class test222 { /** * @param a ...

  8. filter运行出现 <filter object at 0x000001B68F052828> 判断素数

    刚接触filter时  运行总是出现<filter object at 0x000001B68F052828>  得不到想要的数据 后来发现是因为filter的结果是一个数组 需要 lis ...

  9. 【递归入门】组合+判断素数:dfs(递归)

    题目描述 已知 n 个整数b1,b2,…,bn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和. 例如当 n=4,k=3,4 个整数分别为 3,7,12, ...

随机推荐

  1. Tkinter布局管理器

    Layout management in Tkinter 原英文教程地址:zetcode.com In this part of the Tkinter tutorial, we introduce ...

  2. .NET MVC中登录过滤器拦截的两种方法

    今天给大家介绍两种ASP中过滤器拦截的两种方法. 一种是EF 的HtppModule,另一种则是灵活很多针对MVC的特性类 Attribute 具体什么是特性类可以参考着篇文章:https://www ...

  3. Java类锁和对象锁实践和内部私有锁关联

    Java类锁和对象锁实践 感谢[jiehao]同学的投稿,投稿可将文章发送到tengfei@ifeve.com 类锁和对象锁是否会冲突?对象锁和私有锁是否会冲突?通过实例来进行说明. 一.相关约定 为 ...

  4. zathura-vim风格轻量级pdf阅读器

    安装(arch/manjaro) yay -Sy zathura-pdf-poppler 0.2.9-1 使用 `快捷键` gg 行首 G 行尾 j/k/h/l 单行移动 J/K 或 Ctrl + f ...

  5. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Polymorphism

    Polymorphism is the third essential feature of an object-oriented programming language,after data ab ...

  6. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(六)之Initialization & Cleanup

    Two of these safety issues are initialization and cleanup. initialization -> bug cleanup -> ru ...

  7. ASE课程总结 by 冯晓云

    开始的开始,采访往届ASE班的blog:http://www.cnblogs.com/legs/p/4894362.html 和北航软工M1检查:http://www.cnblogs.com/legs ...

  8. Jingwen‘s update

    Bugs: The checkin button of the question answering page must be pressed twice to check in the result ...

  9. api_DZFPKJ & api_DZFPCX

    AES加密算法的网站:http://www.ssleye.com/aes_cipher.html """ AES加密(加解密算法/工作模式/填充方式:AES/ECB/PK ...

  10. [yii2] 实现所有action方法之前执行一段代码或者方法

    我做的是在执行任何方法之前,验证用户登陆状态! 其实就是在controller中写beforeaction()方法, 然后我的方案就是做一个基类,然后让你所有控制器继承你的基类, 如果控制器的基类用_ ...