首页 > 其他分享 >【模板】分解质因数 Pollard-Rho

【模板】分解质因数 Pollard-Rho

时间:2024-04-24 19:33:32浏览次数:17  
标签:return qmul int LL Pollard while Rho 质因数 r1

参见洛谷模板题题解,这里只有代码实现。一些强数据参考(输出了最大质因子)

7
9223372036854775783
Prime
9223371994482243049
3037000493
9223253290108583207
2097143
2147483648
2
2147483647
Prime
2147117569
46337
2141700569
1289

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
typedef long long LL;
mt19937_64 rng{random_device{}()};
LL qmul(LL a, LL b, LL n) { return (__int128)a * b % n; }
LL qpow(LL a, LL b, LL n) {
  LL r = 1;
  for (; b; b >>= 1, a = qmul(a, a, n)) {
    if (b & 1) r = qmul(r, a, n);
  }
  return r;
}
LL gcd(LL a, LL b) { return !b ? a : gcd(b, a % b); }
bool isprime(LL n) {
  if (n <= 1) return 0;
  auto MR = [&](LL a) -> bool {
    LL d = n - 1, r = 0;
    while (d % 2 == 0) d >>= 1, ++r;
    LL k = qpow(a, d, n);
    if (k == 1) return true;
    while (r--) {
      if (k == n - 1) return true;
      k = qmul(k, k, n);
    }
    return false;
  };
  constexpr int bases[] = {2, 3, 5, 7, 11, 13, 15, 17, 19, 23, 29, 31, 37};
  for (int b : bases) {
    if (n % b == 0) return n == b;
    if (!MR(b)) return false;
  }
  return true;
}
vector<LL> divide(LL n) {
  if (isprime(n)) return {n};
  if (n < 2) return {};
  static const auto find = [&](LL n) {
    auto f = [&, c = (LL)rng() % (n - 1) + 1](LL x) {
      return (qmul(x, x, n) + c) % n;
    };
    LL val = 1, s = 0, t = 0;
    for (int gal = 1;; gal <<= 1, s = t, val = 1) {
      for (int stp = 1; stp <= gal; stp++) {
        t = f(t);
        val = qmul(val, abs(t - s), n);
        if (stp % 127 == 0 || stp == gal) {
          LL d = gcd(val, n);
          if (d > 1) return d;
        }
      }
    }
    return n;
  };
  LL p = n;
  while (p == n) p = find(n);
  int cnt = 0;
  while (n % p == 0) n /= p, ++cnt;
  auto r1 = divide(n), r2 = divide(p);
  for (LL p : r2) r1.insert(r1.end(), cnt, p);
  return r1;
}

标签:return,qmul,int,LL,Pollard,while,Rho,质因数,r1
From: https://www.cnblogs.com/caijianhong/p/18156169/template-pr

相关文章

  • 52 Things: Number 35: Give the rough idea of Pollard rho, Pollard "kangaroo" and
    52Things:Number35:GivetheroughideaofPollardrho,Pollard"kangaroo"andparallelPollardrhoattacksonECDLP.52件事:第35件:大致了解Pollardrho、Pollard“袋鼠”和平行的Pollardrho对ECDLP的攻击。 Thisisthelatestinaseriesofblogpoststoadd......
  • [ABC254D] Together Square--分解质因数。
    [ABC254D]TogetherSquare-洛谷 #include<bits/stdc++.h>#defineintlonglong//(有超时风险)#definePIIpair<int,int>#defineendl'\n'usingnamespacestd;constintN=2e5+10,M=1e3+10;inta[N],pre[N];signedmain(){std::io......
  • 分解质因数
    1、算术基本定理(唯一分解定理)每个正整数都能够唯一的表示成它的质因数的乘积2、n中最多只有一个大于根号n的质因子因为如果有两个以上的话,乘积会大于n。因此只需要从2遍历到根号n即可。#include<iostream>usingnamespacestd;intmain(){ intn; cin>>n; for(int......
  • 二十六 3377. 约数的个数 (分解质因数)
    3377.约数的个数(分解质因数)略试除法importjava.util.*;publicclassMain{privatestaticintcalc(intx){intres=0;for(inti=1;i<=x/i;i++){if(x%i==0){res++;if(i......
  • 分解质因数
    描述编写一个把整数N分解为质因数乘积的程序。比如分解210,可以写成210=235*7,请按这个格式输出。输入描述一个整数N(2≤N≤10上角标9)。输出描述输出把N拆成几个质数相乘的形式,质数必须从小到大相乘。用例输入1 120用例输出1 120=2*2*2*3*5代码#include<......
  • 分解质因数
    这段代码是一个简单的C++程序,用于将输入的整数分解为质因数并输出其质因数分解形式。程序的主要流程如下:引入 <bits/stdc++.h> 头文件:这个头文件实际上包含了C++标准库中的几乎所有头文件,因此它可以简化代码,但通常不建议在生产环境中使用,因为它可能导致编译时间增加。......
  • P1075 [NOIP2012 普及组] 质因数分解
    P1075[NOIP2012普及组]质因数分解[NOIP2012普及组]质因数分解题目描述已知正整数\(n\)是两个不同的质数的乘积,试求出两者中较大的那个质数。输入格式输入一个正整数\(n\)。输出格式输出一个正整数\(p\),即较大的那个质数。样例#1样例输入#121样例输出#1......
  • 数论——Pollard-Rho 学习笔记
    数论——Pollard-Rho学习笔记非平凡因数:\(n\)除了\(1\)和\(n\)以外的因数。Pollard-Rho算法是一种用于快速分解非平凡因数的算法。Pollard-Rho能在期望复杂度\(\mathcalO(n^{1/4})\)的时间内找到\(n\)的最小的非平凡因数。而根据Pollard-Rho,我们可以用来加速质......
  • 大数因数分解Pollard_rho
    大数因数分解Pollard_rho#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<map>usingnamespacestd;constinttimes=50;intnumber=0;map<long......
  • 质数判断&质因数分解
    引入众所周知,素数的判断在longlong级别是不能\(O(\sqrt{n})\)硬上的。那怎么办呢??参考文献。ababab,先来最低效的。以下复杂度均考虑高精。1.试除法\(O(\sqrtn)\)枚举,\(n\le10^{14}\)。优化只枚举质数,无法优化预处理质数时间。2.Millar-Rabinlonglong:\(O(k\t......