题意:是素数就输出Prime,不是就输出最小因子.

#include <cstdio>
#include<time.h>
#include <algorithm>
#include<set>
using namespace std; typedef long long llt; int const Repeat = ;
set<llt>sss;
//利用二进制计算a*b%mod
llt multiMod(llt a, llt b, llt mod){
llt ret = 0LL;
a %= mod;
while (b){
if (b & 1LL) ret = (ret + a) % mod, --b;
b >>= 1LL;
a = (a + a) % mod;
}
return ret;
} //计算a^b%mod
llt powerMod(llt a, llt b, llt mod){
llt ret = 1LL;
a %= mod;
while (b){
if (b & 1LL) ret = multiMod(ret, a, mod), --b;
b >>= 1LL;
a = multiMod(a, a, mod);
}
return ret;
} //Miller-Rabin测试,测试n是否为素数
bool Miller_Rabin(llt n, int repeat){
if (2LL == n || 3LL == n) return true;
if (!(n & 1LL)) return false; //将n分解为2^s*d
llt d = n - 1LL;
int s = ;
while (!(d & 1LL)) ++s, d >>= 1LL; //srand((unsigned)time(0));
for (int i = ; i<repeat; ++i){//重复repeat次
llt a = rand() % (n - ) + ;//取一个随机数,[2,n-1)
llt x = powerMod(a, d, n);
llt y = 0LL;
for (int j = ; j<s; ++j){
y = multiMod(x, x, n);
if (1LL == y && 1LL != x && n - 1LL != x) return false;
x = y;
}
if (1LL != y) return false;
}
return true;
} llt Fac[];//质因数分解结果(刚返回时是无序的)
int FCnt;//质因数的个数。数组小标从0开始 llt gcd(llt a, llt b){
if (0L == a || 0L == b) return ;
if (a < ) a = -a;
if (b < ) b = -b;
while (b){
llt t = a % b;
a = b;
b = t;
}
return a;
}
llt Pollard_Rho(llt n, llt c){
llt i = , k = ;
llt x = rand() % n;
llt y = x;
while (){
++i;
x = (multiMod(x, x, n) + c) % n;
llt d = gcd(y - x, n);
if (d != 1LL && d != n) return d;
if (y == x) return n;
if (i == k) y = x, k <<= ;
}
} void find(llt n){
if (4LL == n){
Fac[] = Fac[] = 2LL;
FCnt = ;
return;
}
if (Miller_Rabin(n, Repeat)){
Fac[FCnt++] = n;
return;
} llt p;
while ((p = Pollard_Rho(n, rand() % (n - ) + )) == n); find(p);
find(n / p);
} int main(){
int kase;
scanf("%d", &kase);
while (kase--){
llt n;
scanf("%lld", &n); FCnt = ;
if (Miller_Rabin(n, )){ printf("Prime\n"); }
else{
find(n);
llt ans = Fac[];
for (int i = ; i < FCnt;++i)
if (ans>Fac[i])ans = Fac[i];
printf("%lld\n", ans);
}
}
return ;
}

最新文章

  1. Visual Studio SetSite failed for package [JavaScriptWebExtensionsPackage] 错误解决方案一则
  2. ThinkPHP常用配置路径
  3. 【代码笔记】iOS-截屏功能
  4. servlet定义
  5. HBase在单Column和多Column情况下批量Put的性能对比分析
  6. HTML5[6]:多行文本显示省略号
  7. 用python简单处理图片(1):打开\显示\保存图像
  8. SQL Server文本和图像函数
  9. linux tar.gz zip 解压缩 压缩命令
  10. bzoj1954 poj3764
  11. C# Interface显式实现和隐式实现
  12. 【openstack N版】——块存储服务cinder
  13. [SinGuLaRiTy] 复习模板-图论
  14. 在Ubuntu中部署并测试HyperLedger Fabric 0.6
  15. Java大数统计-hdu1316
  16. Listen error 错误和 limit of inotify watches was reached
  17. SpringBoot 启动错误搜集
  18. LinkedBlockingQueue源码解析(3)
  19. 自定义Git【转】
  20. 提高 GitHub 网页访问速度 以及 Git Clone 速度 的小技巧

热门文章

  1. ExtJS4中设置tabpanel的tab高度问题
  2. IIS7 开发与 管理 编程 之 Microsoft.Web.Administration
  3. Python知识梳理
  4. WebFrom 【文件上传】
  5. SqlServer--用代码创建和删除数据库和表
  6. 【RabbitMQ】2、心得总结,资料汇总
  7. 【高并发解决方案】7、一致性hash解读
  8. Mybatis关联查询之一对多和多对一XML配置详解
  9. 设计模式-享元模式(FlyWeight)
  10. C#设计模式——简单工厂模式、工厂模式和抽象工厂模式