Miller_Rabin、 Pollard_rho Template

Multiply and pow Function:

//计算 (a*b)%c.   a,b都是ll的数,直接相乘可能溢出的
//  a,b,c <2^63
ll mult_modq(ll a,ll b,ll c){
    a %= c;
    b %= c;
    ll ret = 0;
    while(b){
        if(b & 1){ret += a;ret %= c;}
        a <<= 1;
        if(a >= c)a %= c;
        b >>= 1;
    }
    return ret;
}

//计算  x^n %c
ll pow_mod(ll x,ll n,ll mod){
    if(n == 1)return x%mod;
    x %= mod;
    ll tmp = x;
    ll ret = 1;
    while(n){
        if(n & 1) ret = mult_mod(ret, tmp, mod);
        tmp = mult_mod(tmp, tmp, mod);
        n >>= 1;
    }
    return ret;
}


Miller_Rabin Prime Number test:

return TRUE when Prime Number (BE POSSIBLITY)

return FALSE when not a Prime Number

//以a为基,n-1=x*2^t      a^(n-1)=1(mod n)  验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(ll a,ll n,ll x,ll t){
    ll ret = pow_mod(a, x, n);
    ll last = ret;
    for(int i = 1; i <= t; ++i){
        ret = mult_mod(ret,ret,n);
        if(ret == 1 && last !=1 && last != n - 1) return true;//合数
        last = ret;
    }
    if(ret != 1) return true;
    return false;
}

// Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false;

bool Miller_Rabin(ll n){
    if(n < 2)   return false;
    if(n == 2)    return true;
    if((n & 1) == 0)    return false;//偶数
    ll x = n - 1;
    ll t = 0;
    while((x & 1) == 0){x >>= 1; ++t;}
    for(int i = 0; i <S ; ++i){
        ll a = rand() % (n - 1) + 1;
        if(check(a,n,x,t))
            return false;//合数
    }
    return true;
}


Pollard_rho Algorithm

The quality factor decomposition :

ll factor[100];//质因数分解结果(刚返回时是无序的)
int tol;//质因数的个数。数组小标从0开始

ll gcd(ll a,ll b){
    if(a == 0)  return 1;
    if(a < 0) return gcd(-a,b);
    while(b){
        ll t = a % b;
        a = b;
        b = t;
    }
    return a;
}

ll Pollard_rho(ll x,ll c){
    ll i = 1, k = 2;
    ll x0 = rand()%x;
    ll y = x0;
    while(1){
        ++i;
        x0 = (mult_mod(x0,x0,x)+c)%x;
        ll d = gcd(y-x0,x);
        if(d != 1 && d != x) return d;
        if(y == x0) return x;
        if(i == k){y = x0; k += k;}
    }
}
//对n进行素因子分解
void findfac(ll n){
    if(Miller_Rabin(n)){
        factor[tol++] = n;
        return;
    }
    ll p = n;
    while(p >= n)   p = Pollard_rho(p,rand()%(n-1)+1);
    findfac(p);
    findfac(n/p);
}
时间: 2024-08-08 15:09:17

Miller_Rabin、 Pollard_rho Template的相关文章

Kruscal 、 Prime Template

Kruscal  Template : 很裸的Kruscal Template(求最小生成树中最长路,即最短路中最长路) //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <climits> #include <cstring> #include <algori

[模板] Miller_Rabin和Pollard_Rho

Miller_Rabin 用途 快速($O(slogn)$,s为尝试次数)地判断一个数是否是质数 原理 首先有费马小定理$a^{p-1}=1 (mod\ p)$当p为质数时成立,所以可以随机选择a来以这个式子作为一定的判断依据,但并不是所有合数都不满足这个式子,甚至存在合数对所有的a都不满足这个式子 然后有二次探测定理$a^2=1 (mod p)$,p是奇质数成立当$a=1(mod p)$或$a=p-1(mod p)$ 证明:移项可得$(a-1)(a+1)=0 (mod p)$ 原文地址:htt

Centos 7 Docker、docker-compose、Registrator、Consul、Consul Template和Nginx实现高可扩展的Web框架

安装所需软件 Docker Docker-compose 配置docker-compose.yml文件内容如下: #load balancer will automatically update the config using consul-template lb: image: yeasy/nginx-consul-template:latest hostname: lb volumes: - /usr/soft/consul/logapi.conf:/etc/consul-template

AngularJS之延迟加载html template

当使用AngularJs中的routes/views模式建立大型网站或者应用的时候,把所有的自定义文件,如controllers和template等在初始化时全部加载进来,不是一个好的办法.最好的方式是,初始化时仅仅加载所需要的文件.这些文件可能会依赖一个连接或者多个文件,然而它们仅仅被特定的route所使用.当我们切换route时,未被加载的文件将会按需加载.这不但能提高初始化页面的速度,而且可以防止带宽浪费. 网上大部分文章都在讲通过$routeProvider以及第三方服务对control

WPF Template模版之DataTemplate与ControlTemplate的关系和应用【二】

1. DataTemplate和ControlTemplate的关系 学习过DataTemplate和ControlTemplate,你应该已经体会到,控件只是数据的行为和载体,是个抽象的概念,至于它本身长成什么样子(控件内部结构),它的数据会长成什么样子(数据显示结构)都是靠Template生成的.决定控件外观的是ControlTemplate,决定数据外观的是DataTemplate,它们正是Control类的Template和ContentTemplate两个属性值 凡是Template,

angularjs学习笔记--$http、数据渲染到表格、路由、依赖注入、provider

1—$http 可以从服务器中获取更大的数据集,这里使用angularjs的内置服务称为$http.使用angularjs的依赖注入为phoneList组件的控制器提供服务. 在我们的控制器中使用angularjs的$http服务,向我们的web服务器发出http请求,以获取文件中的数据. app/phone-list/phone-list.component1.js: angular.module('phoneList').component('phoneList',{ templateUrl

HTML5 template元素

前言 转自http://www.zhangxinxu.com/wordpress/2014/07/hello-html5-template-tag/ 在单页面应用,我们对页面的无刷新有了更高的要求,HTML不再由后端生成,后端只提供一个REST API,返回JSON数据,模版引擎可以大大方便我们渲染一个视图.而不是吃力的使用 jQeury 去拼接一个DOM. 在现在比较常见的 JS MVC Framework : backbone, emberjs, angularjs 中,模板是非常重要的一个

HTML5 &lt;template&gt;标签元素简介

一.HTML5 template元素初面 <template>元素,基本上可以确定是2013年才出现的.干嘛用的呢,顾名思意,就是用来声明是"模板元素". 目前,我们在HTML中嵌入模板HTML,往往是类似这样的写法: <script type="text/template"> // ... </script> 实际上,并不存在type="text/template"这样的标准写法,<template&g

STL学习笔记--2、空间配置器 allocator

2.1标准接口 allocator::value_type allocator::pointer allocator::const_pointer allocator::reference allocator::const_reference allocator::size_type allocator::difference_type allocator::rebind allocator::allocator()//默认构造函数 allocator::allocator(const allo