Project Euler 003 Largest prime factor

本文介绍了一种求解大整数最大质因子的方法,通过Miller-Rabin素性测试和Pollard's rho算法,实现了高效的质因数分解。适用于数学竞赛及加密算法等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:求 600851475143 的最大质因子。
分析:依旧考虑两种做法。
1. 直接暴力枚举质因子, O(n0.5)
2. pollard-rho, O(n0.25logn)

#include <bits/stdc++.h>

#define ll long long

#define pii std::pair<int,int>
#define mp std::make_pair
#define fi first
#define se second

#define SZ(x) (int)(x).size()
#define pb push_back

template<class T>inline void chkmax(T &x, const T &y) {if(x < y) x = y;}
template<class T>inline void chkmin(T &x, const T &y) {if(x > y) x = y;}

template<class T>
inline void read(T &x) {
    char c;int f = 1;x = 0;
    while(((c=getchar()) < '0' || c > '9') && c != '-');
    if(c == '-') f = -1;else x = c-'0';
    while((c=getchar()) >= '0' && c <= '9') x = x*10+c-'0';
    x *= f;
}
static int outn;
static char out[(int)2e7];
template<class T>
inline void write(T x) {
    if(x < 0) out[outn++] = '-', x = -x;
    if(x) {
        static int tmpn;
        static char tmp[20];
        tmpn = 0;
        while(x) tmp[tmpn++] = x%10+'0', x /= 10;
        while(tmpn) out[outn++] = tmp[--tmpn];
    }
    else out[outn++] = '0';
}

namespace prime {
    const int pn = 11, prime[11] = {2,3,5,7,11,13,17,19,23,29,31};
    ll pfac[50], pfacn;
    inline ll fmm(ll a, ll b, ll mod) {
        ll ret = 0;
        while(b) {
            if(b&1) (ret += a) %= mod;
            (a += a) %= mod, b >>= 1;
        }
        return ret;
    }
    inline ll fpm(ll a, ll b, ll mod) {
        ll ret = 1;
        while(b) {
            if(b&1) ret = fmm(ret, a, mod);
            a = fmm(a, a, mod), b >>= 1;
        }
        return ret;
    }
    inline bool check(int p, int s, ll t, ll n) {
        ll k = fpm(p, t, n);
        if(k == 1 || k == n-1) return true;
        for(int i = 1; i < s; ++i) {
            k = fmm(k, k, n);
            if(k == n-1) return true;
            if(k == 1) return false;
        }
        return false;
    }
    inline bool miller_rabin(ll n) {
        if(n == 1) return false;
        if(n == 2) return true;
        if(!(n&1)) return false;
        int s = 0;ll t = n-1;
        while(!(t&1)) t >>= 1, s++;
        for(int i = 0; i < pn && prime[i] < n; ++i)
            if(!check(prime[i], s, t, n))
                return false;
        return true;
    }
    ll gcd(ll a, ll b) {
        return b ? gcd(b, a%b) : a;
    }
    inline ll pollard_rho(ll n, ll c) {
        int i = 1, k = 2;
        ll x = rand()%(n-1)+1, y = x;
        while(true) {
            ++i;
            x = (fmm(x, x, n)+c)%n;
            ll d = gcd(x-y+n, n);
            if(d != 1 && d != n) return d;
            if(d == n) return n;
            if(i == k) {
                k <<= 1;
                y = x;
            }
        }
    }
    void find(ll n) {
        if(miller_rabin(n)) {
            pfac[++pfacn] = n;
            return ;
        }
        ll p = n;
        while(p == n)
            p = pollard_rho(p, rand()%n);
        find(p), find(n/p);
    }
    inline ll get_max_prime_factor(ll n) {
        pfacn = 0, find(n);
        return *std::max_element(pfac+1, pfac+pfacn+1);
    }
}

int main() {
    std::cout << prime::get_max_prime_factor(600851475143) << std::endl;
    return 0;
}
内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值