大数分解,大数判定综合模板
通过poj1811整理这些算法的模板
所有代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | #include <iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; #define LL long long const int maxs = 10000000+5; //对于1要外加特判,否则会运行错误 //用小素数表做随机种子 __int64 pri[25] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; __int64 ans[maxs],flag; //ans数组记录分解质因数的结果(没有顺序),flag记录质因数的个数,相同的质因数不合并! //最大公约数 __int64 gcd( __int64 a, __int64 b) { while (b!=0) { __int64 r = a%b; a=b; b=r; } return a; } //乘法快速幂a*b%n,不写连乘是防止相乘时溢出 __int64 multi( __int64 a, __int64 b, __int64 n) { __int64 temp = 0; while (b!=0) { if (b%2==1) { temp+=a; if (temp>=n) temp-=n; } a*=2; if (a>=n) a-=n; b/=2; } return temp; } //乘方快速幂a^n%m __int64 multimod( __int64 a, __int64 n, __int64 m) { __int64 ans=1; while (n!=0) { if (n%2==1) ans=multi(ans,a,m); a=multi(a,a,m); n/=2; } return ans; } //判断大数是否为素数(米勒罗宾算法) //调用multimod(),multi()函数 bool miller_rabin( __int64 n) { if (n<2) return false ; if (n==2) return true ; if (n%2==0) return false ; __int64 k = 0,m,a,i,j; m=n-1; while (m%2==0) { m=m/2; k++; } for (i=0;i<10;i++) { if (pri[i]>=n) return true ; a = multimod(pri[i],m,n); if (a==1) continue ; for (j=0;j<k;j++) { if (a==n-1) break ; a = multi(a,a,n); } if (j==k) return false ; } return true ; } //寻找因数 __int64 pollard_rho( __int64 c, __int64 n) { __int64 i,x,y,k,d; i=1; x = y = rand ()%n; k=2; do { i++; d = gcd(n+y-x,n); if (d>1&&d<n) return d; if (i==k) { y=x; k=k*2; } x = (multi(x,x,n)+n-c)%n; } while (y!=x); return n; } //递归分解,把大整数n分解保存到ans全局数组中,保存的是质因数 //且这些质因数没有保证顺序,也没有排重 void rhoAll( __int64 n) { if (miller_rabin(n)) { ans[flag]=n; flag++; return ; } __int64 t = n; while (t>=n) { //随机取出一个因子,不一定是素数,也不确定大小 t = pollard_rho( rand ()%(n-1)+1,n); } rhoAll(t); rhoAll(n/t); return ; } //返回大整数n最小的质因数 __int64 rho( __int64 n) { if (miller_rabin(n)) return n; __int64 t = n; while (t>=n) { t = pollard_rho( rand ()%(n-1)+1,n); } __int64 a = rho(t); __int64 b = rho(n/t); return a<b?a:b; } int main() { //freopen("in.txt","r",stdin); int T; scanf ( "%d" ,&T); while (T--) { flag = 0; __int64 n; scanf ( "%I64d" ,&n); if (miller_rabin(n)) //是素数 printf ( "Prime\n" ); else { rhoAll(n); //把大数n分解成质因数 printf ( "%I64d\n" ,rho(n)); //最小的质因数 } for ( int i=0;i<flag;i++) //输出这些质因数 printf ( "%I64d\n" ,ans[i]); } return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构