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

参见洛谷模板题题解,这里只有代码实现。

文末有一组测试数据。

注意要特判 n=4,不知道为什么。

Miller Rabin 取前 12 个质数。

1

#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;
}
2
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
#define gcd __gcd
using LL = long long;
LL qmul(LL a, LL b, LL n) { return (LL)((__int128)a * b % n); }
LL qpow(LL a, LL b, LL n) {
  LL r = 1;
  while (b) {
    if (b & 1) r = qmul(r, a, n);
    if (b >>= 1) a = qmul(a, a, n);
  }
  return r;
}
bool isprime(LL n) {
  if (n <= 2) return n == 2;
  LL d = n - 1, r = __builtin_ctzll(d);
  d >>= r;
  for (LL a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
    if (n % a == 0) return n == a;
    LL k = qpow(a, d, n);
    if (k == 1) continue;
    bool flag = false;
    for (int i = 1; i <= r && !flag; i++, k = qmul(k, k, n)) flag |= k == n - 1;
    if (!flag) return false;
  }
  return true;
}
mt19937_64 rng64{random_device{}()};
map<LL, int> divide(LL n) {
  if (n <= 1) return {};
  if (n == 4) return {{2, 2}}; // !!!
  if (isprime(n)) return {{n, 1}};
  LL d = 1;
  while (d <= 1) {
    auto f = [n, c = rng64() % (n - 1) + 1](LL x) { return (qmul(x, x, n) + c) % n; };
    LL l = f(0), r = f(l);
    do {
      LL p = 1;
      for (int i = 0; i < 127 && l != r; i++) {
        LL q = qmul(p, abs(l - r), n);
        if (!q) break;
        p = q, l = f(l), r = f(f(r));
      }
      d = gcd(p, n);
      if (d > 1) break;
    } while (l != r);
  }
  int c = 0;
  while (n % d == 0) n /= d, ++c;
  auto res = divide(n);
  for (auto e : divide(d)) res[e.first] += e.second * c;
  return res;
}
int main() {
#ifndef LOCAL
  cin.tie(nullptr)->sync_with_stdio(false);
#endif
  LL n;
  for (scanf("%*d"); ~scanf("%lld", &n);) {
    if (isprime(n))
      puts("Prime");
    else {
      auto ret = divide(n);
      printf("%lld\n", max_element(ret.begin(), ret.end())->first);
    }
  }
  return 0;
}
测试数据输入
357
9223372036854775783
9223371994482243049
9223253290108583207
2147483648
2147483647
2147117569
2141700569
998773295310793741
998723688531410213
999094140199356937
998907669487445233
999168967862173543
998267762332461011
999836026703613263
998770205723489723
999364322016519691
998927866202197537
998818223715809621
998633994686928001
999035960763148213
999022094067788293
998577328190053993
999077687784040289
999070186104230833
999634273237012949
999435057656514977
998884170196766489
999201476201994199
998509849046279033
999154109566823311
998582979717556511
999425747909774687
999536485519408763
999031853109629177
998750549771077421
999002295786401677
999652485342805837
998933265771249973
998832069957141493
998999741827917307
998665606135526041
998238190329006953
999725202731063153
998871422053952443
998315293145872363
999102440629580633
999336465424402673
999523805852933971
998862635261680663
999105277537581593
998672827824776653
999177051287046559
999254016709058687
998547570296399957
999221426830955701
998649620392409653
998374718669926457
998542090267899493
999362837667082687
998313827772143437
999310714784917831
999206982455514967
998691099091838077
998360277908522929
999064081615748363
998861034667971031
998180201861510659
998216201608780109
999382670049083093
998654085012449113
999321806121147599
998950282042643107
999443251318376399
998353787860652341
998827272059467297
998259429275324927
998475764636596481
999235032037288847
998767105463504603
998262559129910369
998734342654285651
999632651321806231
998590160017863409
999917721674258159
999365444185455673
998328354259053047
998820247903354379
998987318197540279
998197274067839731
998911181089269839
998497899546923179
999479703810420769
999801476237090269
999010213062802429
999066069800610683
998576716554922541
998888903542904471
999793456856055629
998616295836670681
998463122949294533
999116291318350691
998741686585956223
998893318877767759
998892728692235227
998814637916294119
998716464025779833
999311901217632007
999576649206177683
998989311698250077
998387810233346431
998714229064537907
998975677849993681
999104121236417093
998289151414148057
999059810040872689
998772524173267807
998551486165985803
998838633134292383
998854252791979091
999149928524719519
999821938792689517
999166035403895867
998298515512823839
999349818939371957
998628618441123727
998699479044384539
998616357158996357
998405749905566747
998542941134666329
999186539375988289
998865324605472413
999183979809787879
998933277758624023
999919924632033523
999014168359753583
998609149987228181
998521569817246133
998988621964907237
999234505554132983
999657012958163921
999154322615322637
999205314199432187
999000618493903187
999304081092476443
999294924648777593
998287557654622943
998690078944948043
999328649559179257
998669566134958373
998435752928661391
999943522053133597
999352534064553281
999094734421230697
998931449940614009
999144352608144019
998211315707625563
999438884656856519
999764651174554879
999229862769934781
998491012784290657
999418902727042529
999211828730332849
999348189757575071
999547382284483763
998381366797573813
999374540571506419
999270149197621183
999072521902270897
999203815980634417
999183728904019693
998662945481008901
999111445368322151
999150526616465947
999623641377671267
999125432857417693
999140138631585793
999352995906224251
999006346817612053
999567973230404587
999552925776160139
999691726404262369
999347278795025123
999307693640144419
998586013288107763
998280189982597481
998493160306208857
998879839925904133
999110939807875061
999116164461237071
999204198354660439
998856487168009087
999054376317401321
999567709879413721
999061118020939651
999299595264556133
999219788229641057
998872391648290109
998522695786892261
999720044971160153
998614973409002473
999558459550000087
998254270832421349
998353997986693063
999189998474339597
999343259344587961
998457587850565267
998656681249245383
999225091888980043
998606703327006797
998821169494624481
998808694384532971
998895610396365887
998975586938402659
999485963492145433
999107748882006047
998595429373995353
998816135175222379
999128940913278823
999061540782354151
999141584532085339
998569928049364277
998860594834277213
999011698440589423
998596800639349621
999033209320163957
999781257043419229
999478208515624637
999195008088342607
999657651507061021
998687449581444743
998218748097778189
999676520424402479
999082517299417381
999544229151578273
998329410972991883
999043329676264693
998813593293872221
998461578454713887
998980605503433547
998945759405181209
999596059757038231
999184812175864667
999752007407726401
998523180382853057
999331021632647581
998874380823997177
998919803974669021
998823156388339861
999375233690077183
999484858233584851
999269670710272459
999076045283861947
999173788714552481
998981283100616149
999815574118088353
999109455288974437
998609311593174221
999119269438952203
998773811066103031
999091347396461287
998802267734072617
999027590849119201
998987953818218137
998525614317315481
999464688543204229
998992961123566309
999046767327373307
998704538909375999
999501533699523769
998315404803121709
999553905753438743
998928867970560011
999124699503536237
999025576341251099
999754695077682287
998608352135639621
999446879611954141
999174589415956699
999465786699466139
998956843309625917
999523306415650349
999206278018877431
999296832768598531
998760525949744399
999302367238557113
999590920738087813
999193598635657499
998581465381187357
998754433321956263
998505987742339369
998351603294362897
999073108484637397
998854159585554667
998447830125052289
999113463421861951
999157994453161091
998763202309077379
999065731943993309
998933679175681637
998682495805474783
999557538170337673
999218750964609743
998893831940874049
998818253644344803
999604790681112149
998910904276094123
998974651519822489
999049475414866861
998397979919732107
998095416688383469
998364556506178123
998755777787620543
999242916139976383
998872925630281913
998531027625846479
999390496534470653
999494700944084161
999177203045303453
999192898286814757
998619064504143001
999232198222378759
998931016024968227
999111697576616383
999690464146286779
998494264279682081
998749439181282587
999041051690442727
998652806509501681
999012117236528611
999153976793806579
998834604332233817
999548302006309841
999554300529880577
998828501919244223
998641099781664613
998235971654601217
999408110232458321
999835726462618831
998528190571666273
999180666295545823
999616890633138229
999742146470027501
998143612198077409
998547659513441839
998103216222126643
999426142771172317
998643422608563439
998987528336589199
999653168584863083
999626369702072677
998856434005815457
999299015845480633
999219548584262821
999062917838309327
998459453265355813
999278747210040811
999285934715832221
测试数据输出
Prime
3037000493
2097143
2
Prime
46337
1289
999538481
999631723
999698731
999605221
999728819
999155401
999981403
999477667
999879677
999887227
999469249
999581519
999593293
999812767
999516761
999781567
999845017
999918151
999729977
999527033
999739463
999455851
999840367
999342457
999793789
999878683
999943969
999625541
999624253
999909013
999696371
999488353
999869081
999617681
999137371
999908957
999739729
999271319
999918343
999969307
999937513
999700043
999972871
999552511
999837607
999899809
999413033
999774241
999423533
999202901
999488657
999949427
999303551
999845111
999754477
999679189
999337279
999678443
999790633
999172169
999131893
999958217
999646391
999990851
999549679
999978869
999190733
999751009
999157273
999423629
999904331
999580243
999209621
999444869
999936391
999471547
999963131
999983521
999293879
999425939
999865411
999182579
999849689
999495493
999857641
999988007
999926843
999801893
999303583
999516823
999985097
999341869
999276001
999667589
999601903
999654983
999710729
999744091
999677513
999877673
999987323
999536987
999202517
999633449
999807979
999823051
999281867
999877699
999530027
999328021
999435109
999845953
999803341
999995431
999798647
999243019
999743977
999523913
999517027
999560449
999336713
999273941
999869069
999721337
999755381
999617687
999991123
999732157
999430651
999365483
999979859
999941203
999908843
999856789
999728039
999788809
999941843
999879071
999152813
999506987
999949141
999386249
999356333
999999043
999704003
999932671
999637777
999965147
999134509
999853847
999934031
999889733
999382679
999887489
999807397
999884959
999955163
999242551
999972277
999639649
999659629
999949037
999859603
999392441
999565709
999957083
999889519
999885779
999815491
999770143
999735703
999997133
999867007
999998683
999851761
999868769
999397153
999149237
999322187
999863927
999696371
999878771
999607801
999606719
999658733
999866587
999880367
999920687
999771083
999578857
999276017
999885017
999419009
999924803
999168281
999331211
999682183
999774121
999367619
999489877
999862033
999368893
999500503
999429247
999697997
999855809
999912007
999903379
999432253
999682469
999745613
999729373
999815149
999324607
999556249
999866071
999446263
999679139
999920953
999778673
999641911
999917117
999416963
999217673
999879953
999945173
999992977
999222017
999680389
999598429
999305873
999840031
999929449
999912211
999669929
999975841
999346549
999682417
999669607
999654373
999694303
999776573
999987601
999961387
999680153
999590309
999816791
999927427
999801721
999453527
999583367
999457757
999710197
999631393
999881977
999649933
999506477
999902933
999546701
999819797
999482093
999984043
999263423
999779699
999661777
999794863
999546571
999966601
999405749
999939947
999618509
999758653
999561191
999781799
999801893
999824083
999729827
999871283
999879737
999597563
999296483
999480817
999279031
999311683
999988547
999618817
999305137
999671297
999929417
999399971
999873869
999551743
999390361
999847903
999921829
999880477
999724651
999998269
999784537
999908423
999860111
999299687
999079421
999351329
999537269
999678019
999631519
999355993
999995813
999777379
999817129
999916717
999412079
999688223
999507893
999895999
999985969
999360643
999438059
999820909
999582109
999832807
999655757
999695303
999912007
999861637
999653587
999488159
999212329
999999607
999934723
999395629
999868799
999816617
999883483
999083471
999501967
999060047
999998653
999553333
999583439
999958829
999978103
999767303
999919699
999841523
999966997
999445039
999693043
999930647
posted @   caijianhong  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示