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