当前位置:网站首页>大整数分解 浅析
大整数分解 浅析
2022-07-15 21:06:00 【STJqwq】
大整数分解 浅析
解决:质因数分解大整数 n n n 。 1 ≤ n ≤ 1 0 18 1\le n\le 10^{18} 1≤n≤1018 。
1.试除法
枚举 [ 2 , n ] [2,\sqrt n] [2,n] 的所有质数,判断是否整除。除完之后只剩一个 质数 或者 1 1 1 了。时间复杂度 O ( n ln n ) O(\dfrac{\sqrt n}{\ln n}) O(lnnn) 。
这是一个笨方法,但是它告诉我们一些性质:
- 对于 n n n ,最小的质因子不会大于 n \sqrt n n 。(质数除外)
2.玄学法
玄学多好啊(
2.1 不靠谱的玄学法
先判断 n n n 是不是质数,然后在 [ 2 , n ] [2,\sqrt n] [2,n] 里面随机选取一个整数 x x x,判断是不是 gcd ( n , x ) ≠ 1 \gcd(n,x)\neq1 gcd(n,x)=1,是的话把 n / x n/x n/x 和 x x x 递归下去。
时间复杂度 O ( T log 2 n ) O(T\log^2 n) O(Tlog2n) 到 O ( T n log n ) O(T\sqrt n\log n) O(Tnlogn) ,无法保障效率。
2.2 稍微靠谱一点的做法
假设 n n n 的一个最小的素因子为 p p p ,那么随机选择两个数 ( i , j ) (i,j) (i,j) 使得 i ≡ j ( m o d p ) i\equiv j\pmod p i≡j(modp) 。则 p ∣ gcd ( i , j ) p|\gcd(i,j) p∣gcd(i,j) ,则 gcd ( ∣ i − j ∣ , n ) ≠ 1 \gcd(|i-j|,n)\neq 1 gcd(∣i−j∣,n)=1 。
但是,随机枚举两个数判断模 p p p 意义相等,时间复杂度不允许,怎么办?
2.3 引入生日悖论
50 50 50 多个人的班里面至少有 97 % 97\% 97% 的概率能有两个人同一天生日。
为什么呢?因为乘法原理。
一开始,一个人的时候,第一个人能选 365 365 365 天生日;
两个人的时候,因为不能和第一个人重复,所以第二个人能选 364 364 364 天生日;概率就是 364 365 \dfrac{364}{365} 365364 。
第三个人,不能跟前两个重复,所以第三个人只能选 363 363 363 天生日,到了他时候,不重复的概率就是 363 365 \dfrac{363}{365} 365363 。
把不重复的概率乘起来,则前三个人不重复生日的概率是 365 × 364 × 363 36 5 3 \dfrac{365\times 364\times 363}{365^3} 3653365×364×363 。
以此类推,前 n n n 个人不重复生日的概率就是 n ! 36 5 n ( 365 − n + 1 ) ! \dfrac{n!}{365^n(365-n+1)!} 365n(365−n+1)!n! ,越乘越小。
所以,有 23 23 23 个人就能有 50 % 50\% 50% 的概率能有两个人同一天生日。
生日悖论告诉我们另外一个性质: n n n 个数中大约选 n \sqrt n n 次就能达到 50 % 50\% 50% 的概率。
这有什么用途呢?比如 2.1 的随机算法,枚举第一次碰不到因子的概率为 P 1 P_1 P1 ,第二次碰不到因子的概率为 P 2 P_2 P2 ……总共碰不到因子的概率就是 P 1 P 2 P 3 . . . P_1P_2P_3... P1P2P3... 。可知碰了很多次之后,这个概率大大下降。
但是效率和正确性堪忧。
2.4 pollard_rho
2.2 的改进。
因为一个个随机选太玄学,我们就更玄学地生成随机数列,判断相邻两个数是否符合条件。我们只会存储两个数。
有章法的生成方法:上一个数为 x x x ,那么下一个数就是 ( x 2 + a ) % n (x^2+a)\% n (x2+a)%n 。
回到 2.2 的证明,假设 p p p 为 n n n 的最小质因子,那么
但是,这个生成方法有问题,到了最后的时候会形成一个环,出不去了。如图:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Cd29sehM-1657701752031)(C:\Users\Admin\AppData\Roaming\Typora\typora-user-images\image-20220514102638547.png)]
那么我们怎么出这个圈呢?
出不去了。如图:[外链图片转存中…(img-Cd29sehM-1657701752031)]
那么我们怎么出这个圈呢?
联想一下小学的追及问题。甲和乙
边栏推荐
- in_array的第3个参数实例分析
- Code change and invariance
- 服务器进程在用户会话期间所要完成的工作有哪些。
- User experience from the perspective of error pages
- Package type
- EasyMock、EasyMock Class Extension 和 PowerMock
- OSCache 框架源码解析
- 2022年上半年新晋独角兽40家,30%都被红杉中国投过了
- Idea import jar package operation and XML file configuration file
- Security access of UDS common services 0x27
猜你喜欢

HCIP --- OSPF的选录原则和OSPF的防环

面试难题:怎么不用定时任务实现关闭订单?

手动上传表单数据+图片文件功能

Huawei switching technology: BGP basic experiment

The application of digital twins in cities

npm install安装报错:gyp info it worked if it ends with ok如何解决

In the first half of 2022, 40 new unicorns, 30% of which have been invested by Sequoia China

nodeJS中npm简介与使用方法

接口测试实战 - 学生信息管理系统

Logic Pro 使用教程之 Quick Sampler(非常详细)
随机推荐
nodeJS中npm简介与使用方法
【直播回顾】OpenHarmony知识赋能六期第三课—OpenHarmony智能家居项目之控制面板功能实现
Digital twins equip the reservoir with "smart brain"
Idea import jar package operation and XML file configuration file
mysql: command not found 找不到mysql命令
Package type
(board) acwing 143 Maximum XOR pair
(board) trie tree template acwing835 Trie string statistics
Is golden sun Guosen Securities safe? Can I open an account on it?
阿佛的信念
【物联网】WiFi基础知识 (二)【看评论区领取资料】
包装类型
微服务架构 | 服务隔离 - [Gateway]
10 webapis that open the door to my new world
[Internet of things] basic knowledge of WiFi (II) [see the comment area to get information]
JUC joint contracting - cyclicbarrier
Comment publier votre propre paquet NPM
C语言_字符串拼接函数strcat使用及实现
Mysql基础学习Day05
2022-7-15 Leetcode 151. Reverse the words in the string - [split from back to front]