论O(1)快速乘和O(logn)快速乘的差距….

//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
ll shai[10]={2,3,5,7,11,13,17,19,23,29};
ll mul(ll a,ll b,ll p){
ll d=((long double)a/p*b+1e-8);
ll res=a*b-d*p;
res=res<0?res+p:res;
return res;
}
ll pow(ll x,ll y,ll mod){
x%=mod;ll res=1;
while(y){
if(y&1)res=mul(res,x,mod);
x=mul(x,x,mod),y>>=1;
}return res;
}
bool check(ll a,ll n,ll r,int s){
ll x=pow(a,r,n),pre=x;
for(int i=1;i<=s;i++){
x=mul(x,x,n);
if(x==1&&pre!=1&&pre!=n-1)return 0;
pre=x;
}return x==1;
}
bool miller_rabin(ll n){
if(n<=1)return 0;
ll r=n-1,s=0;
while(!(r&1))r>>=1,s++;
for(int i=0;i<10;i++){
if(shai[i]==n)return 1;
if(!check(shai[i],n,r,s))return 0;
}return 1;
}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
ll prime_factor(ll n,ll c){
ll k=2,x=rand()%n,y=x,p=1;
for(int i=1;p==1;i++){
x=(mul(x,x,n)+c)%n;
p=gcd(abs(x-y),n);
if(i==k)y=x,k<<=1;
}return p;
}
ll ans,xx;int cases;
void pollard_rho(ll n){
if(n==1)return;
if(miller_rabin(n)){ans=max(ans,n);return;}
ll p=n;
while(p==n)p=prime_factor(n,rand()%(n-1));
pollard_rho(p),pollard_rho(n/p);
}
int main(){
scanf("%d",&cases);
while(cases--){
ans=0,scanf("%lld",&xx),pollard_rho(xx);
if(ans!=xx)printf("%lld\n",ans);
else puts("Prime");
}
}

//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
typedef unsigned long long ll;
ll shai[10]={2,3,5,7,11,13,17,19,23,29};
ll mul(ll x,ll y,ll mod){
x%=mod;ll res=0;
while(y){
if(y&1)res=(res+x)%mod;
x=(x+x)%mod;
y>>=1;
}return res;
}
ll pow(ll x,ll y,ll mod){
x%=mod;ll res=1;
while(y){
if(y&1)res=mul(res,x,mod);
x=mul(x,x,mod),y>>=1;
}return res;
}
bool check(ll a,ll n,ll r,int s){
ll x=pow(a,r,n),pre=x;
for(int i=1;i<=s;i++){
x=mul(x,x,n);
if(x==1&&pre!=1&&pre!=n-1)return 0;
pre=x;
}return x==1;
}
bool miller_rabin(ll n){
if(n<=1)return 0;
ll r=n-1,s=0;
while(!(r&1))r>>=1,s++;
for(int i=0;i<10;i++){
if(shai[i]==n)return 1;
if(!check(shai[i],n,r,s))return 0;
}return 1;
}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
ll prime_factor(ll n,ll c){
ll k=2,x=rand()%n,y=x,p=1;
for(int i=1;p==1;i++){
x=(mul(x,x,n)+c)%n;
p=gcd(abs((long long)x-(long long)y),(long long)n);
if(i==k)y=x,k<<=1;
}return p;
}
ll ans,xx;int cases;
void pollard_rho(ll n){
if(n==1)return;
if(miller_rabin(n)){ans=max(ans,n);return;}
ll p=n;
while(p==n)p=prime_factor(n,rand()%(n-1));
pollard_rho(p),pollard_rho(n/p);
}
int main(){
scanf("%d",&cases);
while(cases--){
ans=0,scanf("%llu",&xx),pollard_rho(xx);
if(ans!=xx)printf("%llu\n",ans);
else puts("Prime");
}
}

最新文章

  1. Bootstrap3系列:下拉菜单
  2. itertools模块
  3. 简易c语言文法
  4. Windows Phone 8.1 新特性 - 常用的启动器
  5. 关于 app测试工具
  6. HDFS小文件处理——Mapper处理
  7. 正则表达式中的\n
  8. iphone开发中数据持久化之——属性列表序列化(一)
  9. BootStrap详解之(二)
  10. xBIM IFC 墙壁案例
  11. 升级adb注意事项
  12. C#中的 隐式与显式接口实现
  13. leetcode — binary-tree-level-order-traversal
  14. 2017-2018-2 20165237 实验四《Android开发基础》实验报告
  15. javascript——10章 DOM
  16. iOS时间显示今天昨天
  17. 2018秋寒假作业4- -PTA编程总结1
  18. python全栈 流程控制;while 循环 格式化输出 运算符 及编码
  19. 《C++标准程序库》笔记之三
  20. Tocmat 启动错误 Port 8005 required by tomcat v7.0 server at localhost is already in use

热门文章

  1. hiho一下 第172周
  2. background-attachment css3属性
  3. cms初步构想
  4. img-responsive class图片响应式
  5. Java中的常量
  6. MySQL_视图/触发器/事务/存储过程/函数
  7. JS for循环的应用: 打印三角形
  8. 【JavaScript框架封装】数据类型检测模块功能封装
  9. javascript的带操作符的赋值运算
  10. Nginx 的安装 与 启动