51nod 1162 质因子分解
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1162
数据范围大约是2^97,需要高精度计算
可以使用pollard-rho算法,期望需要$O((logn)^{1/4})$次整数操作
需要采用 蒙哥马利约化(Montgomery reduction)避免大量取模,并每隔一段时间检查一次gcd。
#include<bits/stdc++.h>
typedef __int128 num;
typedef long long i64;
const int B=,K=;
bool np[B+];
int ps[B],pp=,pps[B],fp=,qp=;
num fs[],P,qs[];
num read(){
num x=;
int c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))x=x*+c-,c=getchar();
return x;
}
void pr(num x){
if(x<)putchar('-'),x=-x;
int ss[],sp=;
do ss[++sp]=x%;while(x/=);
while(sp)putchar(+ss[sp--]);
putchar();
}
num rnd(){
static std::mt19937 mt(time());
num z=mt();
z=z<<^mt();
z=z<<^mt();
return z%(P-)+;
}
const unsigned X=(<<)-;
inline num fix1(num a){return a>=P?a-P:a;}
inline num fix2(num a){return a<?a+P:a;}
inline num mm(num a,num b,num C=){
unsigned b0=b&X;b>>=;
unsigned b1=b&X;b>>=;
unsigned b2=b&X;b>>=;
unsigned b3=b&X;
num c=a*b3%P;
c=((c<<)+a*b2)%P;
c=((c<<)+a*b1)%P;
c=((c<<)+a*b0+C)%P;
return c;
}
num iP,RR,iR;
#define HI(x) u64((x)>>52)
#define LO(x) u64(x&((1ll<<52)-1))
typedef unsigned __int128 u128;
typedef unsigned long long u64;
const num R=num()<<,R1=R-;
__attribute__((optimize("Ofast")))
inline u128 mul(u128 a,u128 b){
u128 m=a*b*iP&R1;
u128 t=(
(u128)HI(a)*HI(b)+
(u128)HI(m)*HI(P)
);
u128 t1=(
(u128)HI(a)*LO(b)+
(u128)LO(a)*HI(b)+
(u128)HI(m)*LO(P)+
(u128)LO(m)*HI(P)
);
u128 t0=(
(u128)LO(a)*LO(b)+
(u128)LO(m)*LO(P)
);
t+=HI(HI(t0)+LO(t1))+HI(t1);
return fix1(t);
}
inline u128 mulRR(u128 a){return mul(a,RR);}
num exgcd(num a,num b,num&x,num&y){
if(!b){x=,y=;return a;}
num g=exgcd(b,a%b,y,x);
y-=a/b*x;
return g;
}
void setP(num x){
P=x;
exgcd(P,R,iP,iR);
iP=R1&-iP;
iR=fix2(iR);
RR=mm(R%P,R%P);
}
num pw(num a,num n){
num v=;
for(;n;n>>=,a=mm(a,a))if(n&)v=mm(v,a);
return v;
}
bool mr(){
num s=P-;
int r=;
for(;~s&;s>>=,++r);
num a=pw(rnd(),s);
for(;;){
if(a==||a==P-)return ;
if(!--r)return ;
a=mm(a,a);
}
}
bool isp(){
if(P<=B)return !np[P];
if(~P&)return ;
for(int i=;i<;++i)if(!mr())return ;
return ;
}
num gcd(num a,num b){return b?gcd(b,a%b):a;}
num abs(num x){return x>?x:-x;}
void pollard_rho(num n){
setP(n);
if(isp()){
fs[fp++]=n;
return;
}
const int mod=;
for(;;){
num a,b,c,prod=mulRR();
a=b=mulRR(rnd());
c=mulRR(rnd());
for(i64 ii=,k=;;){
a=fix1(mul(a,a)+c);
prod=mul(prod,abs(a-b));
if(!prod){
prod=abs(a-b);
num g=gcd(P,mul(prod,));
if(g==n)break;
if(g!=){
qs[qp++]=g;
qs[qp++]=n/g;
return;
}
}
if(++ii==k)k<<=,b=a;
if(!(ii&mod)){
num g=gcd(P,mul(prod,));
if(g==n)break;
if(g!=){
qs[qp++]=g;
qs[qp++]=n/g;
return;
}
}
}
}
}
void factor(num x){
for(int i=;i<pp;++i){
for(int p=ps[i];x%p==;fs[fp++]=p,x/=p);
}
if(x>)qs[qp++]=x;
while(qp)pollard_rho(qs[--qp]);
std::sort(fs,fs+fp);
for(int i=;i<fp;++i)pr(fs[i]);
}
void init(){
for(int i=;i<=B;++i){
if(!np[i])ps[pp++]=i;
for(int j=;j<pp&&i*ps[j]<=B;++j){
np[i*ps[j]]=;
if(i%ps[j]==)break;
}
}
for(int i=;i<pp;++i){
int p=ps[i],s=;
while(s<=B/p)s*=p;
pps[i]=s;
}
}
int main(){
init();
factor(read());
return ;
}
51nod 1162 质因子分解的更多相关文章
- BZOJ 1485: [HNOI2009]有趣的数列 [Catalan数 质因子分解]
1485: [HNOI2009]有趣的数列 Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所 ...
- A 洛谷 P3601 签到题 [欧拉函数 质因子分解]
题目背景 这是一道签到题! 建议做题之前仔细阅读数据范围! 题目描述 我们定义一个函数:qiandao(x)为小于等于x的数中与x不互质的数的个数. 这题作为签到题,给出l和r,要求求. 输入输出格式 ...
- P2043 质因子分解
P2043 质因子分解 题目描述 对N!进行质因子分解. 输入输出格式 输入格式: 输入数据仅有一行包含一个正整数N,N<=10000. 输出格式: 输出数据包含若干行,每行两个正整数p,a,中 ...
- POJ1845:Sumdiv(求因子和+逆元+质因子分解)好题
题目链接:http://poj.org/problem?id=1845 定义: 满足a*k≡1 (mod p)的k值就是a关于p的乘法逆元. 为什么要有乘法逆元呢? 当我们要求(a/b) mod p的 ...
- P2043 质因子分解(阶乘的质因数分解)
P2043 质因子分解 对$n!$进行质因数分解的一种高效算法 首先,筛出$<=n$的素数 蓝后,对$n$反复除以$prime$,同时$cnt+=n/prime$ $n!$中含有该$prime$ ...
- Lightoj-1356 Prime Independence(质因子分解)(Hopcroft-Karp优化的最大匹配)
题意: 找出一个集合中的最大独立集,任意两数字之间不能是素数倍数的关系. 思路: 最大独立集,必然是二分图. 最大数字50w,考虑对每个数质因子分解,然后枚举所有除去一个质因子后的数是否存在,存在则建 ...
- luogu P2043 质因子分解
题目描述 对N!进行质因子分解. 输入输出格式 输入格式: 输入数据仅有一行包含一个正整数N,N<=10000. 输出格式: 输出数据包含若干行,每行两个正整数p,a,中间用一个空格隔开.表示N ...
- LightOJ1138 —— 阶乘末尾0、质因子分解
题目链接:https://vjudge.net/problem/LightOJ-1138 1138 - Trailing Zeroes (III) PDF (English) Statistic ...
- LightOJ1336 Sigma Function —— 质因子分解、约数和为偶数
题目链接:https://vjudge.net/problem/LightOJ-1336 1336 - Sigma Function PDF (English) Statistics Forum ...
随机推荐
- WeX5学习笔记-02
1.安装wex5,下载地址 http://wex5.com WeX5是绿色免安装的,解压即可直接使用.注意:不能解压到含中文.空格和特殊字符 的目录下, 例如:Program Files,是不可以的, ...
- c++利用类进行单链表的插入,删除,清空操作
#if 1 #include <iostream> #include <stdlib.h> #include <time.h> #include <fstre ...
- Archlinux安装总结
Archlinux安装总结 一.引导 1.BIOS与UEFI root@archiso ~ # ls /sys/firmware/efi/efivars ls: cannot access '/sys ...
- 06_java基础知识——break/continue和标签的配合使用
package com.huawei.test.java03; /** * This is Description * * @author * @date 2018/08/29 */ public c ...
- CSS3扁平化Loading动画特效
效果预览:http://hovertree.com/texiao/css3/42/ 代码如下: <!doctype html> <html> <head> < ...
- C#创建、读写、增加、删除XML操作
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 暗影精灵2pro——使用一年多后电池鼓包,传说中的更新BIOS问题(惠普15ax-226tx)
17年春天的时候正好赶上自己在东北一所省会城市的211大学读硕毕业,为了更新下自己的装备,同时也是为了纪念在母校读书7年的经历,遂够了一款暗影精灵2pro,如上图右侧所示,是一款颜值较高的很轻薄的笔记 ...
- postman Could not get any response。
浏览器输入地址可以返回结果,但是由于返回的json没有格式,看起来比较麻烦,用postman却报错Could not get any response. 可以注意到下面写了可能的情况:比如服务器无响应 ...
- java-14习题
.使用TreeSet集合生成一个不重复随机数组,该数组包含10个100以内的随机整数.输出该随机数组. import java.util.Iterator; import java.util.Tree ...
- Spring(转载二)
在网上看到一篇文章,感觉写得挺不错的,转载一下,本文转载自:http://blog.csdn.net/m13666368773/article/details/7802126 一. IoC理论的背景 ...