poj1181 大数分解
//Accepted 164 KB 422 ms
//类似poj2429 大数分解
#include <cstdio>
#include <cstring>
#include <ctime>
#include <time.h>
#include <iostream>
#include <algorithm>
using namespace std;
;
__int64 gcd(__int64 a,__int64 b)
{
) return a;
return gcd(b,a%b);
}
__int64 mult_mod(__int64 a,__int64 b,__int64 p)
{
__int64 res=,temp=a%p;
while (b)
{
)
{
res+=temp;
if (res>=p) res-=p;
}
temp<<=;
if (temp>=p) temp-=p;
b>>=;
}
return res;
}
__int64 exp_mod(__int64 a,__int64 b,__int64 p)
{
__int64 res=,exp=a%p;
)
{
)
res=mult_mod(res,exp,p);
exp=mult_mod(exp,exp,p);
b>>=;
}
return res;
}
bool miller_rabin(__int64 n,__int64 times)
{
) return true;
|| !(n&)) return false;
__int64 a,u=n-,x,y;
;
==)
{
t++;
u/=;
}
srand(time());
;i<times;i++)
{
a=rand()%(n-)+;
x=exp_mod(a,u,n);
;j<t;j++)
{
y=mult_mod(x,x,n);
&& x!= && x!=n-)
return false;
x=y;
}
) return false;
}
return true;
}
__int64 pollar_rho(__int64 n,int c)
{
__int64 x,y,d,i=,k=;
srand(time());
x=rand()%(n-)+;
y=x;
while (true)
{
i++;
x=(mult_mod(x,x,n)+c)%n;
d=gcd(y-x,n);
&& d<n) return d;
if (y==x) return n;
if (i==k)
{
y=x;
k<<=;
}
}
}
__int64 min_ans;
void findMinFactor(__int64 n,int c)
{
) return ;
))
{
if (n<min_ans) min_ans=n;
return ;
}
__int64 p=n;
while (p>=n)
p=pollar_rho(p,c--);
findMinFactor(p,c);
findMinFactor(n/p,c);
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
__int64 n;
scanf("%I64d",&n);
)==true)
{
printf("Prime\n");
}
else
{
min_ans=inf;
findMinFactor(n,);
printf("%I64d\n",min_ans);
}
}
;
}
poj1181 大数分解的更多相关文章
- HDU4344(大数分解)
题目:Mark the Rope 题意就是给一个数,然后求这个数的所有因子中组成的最大的一个子集,其中1和本身除外,使得在这个子集中元素两两互素,求最大子集的元素个 数,并且求出和最大的值. 找规律就 ...
- poj 1811 随机素数和大数分解(模板)
Sample Input 2 5 10 Sample Output Prime 2 模板学习: 判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解 miller : ...
- Pollard_Rho大数分解模板题 pku-2191
题意:给你一个数n, 定义m=2k-1, {k|1<=k<=n},并且 k为素数; 当m为合数时,求分解为质因数,输出格式如下:47 * 178481 = 8388607 = ( ...
- poj 1811 大数分解
模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...
- poj 2429 Pollard_rho大数分解
先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜 ...
- 模板题Pollard_Rho大数分解 A - Prime Test POJ - 1811
题意:是素数就输出Prime,不是就输出最小因子. #include <cstdio> #include<time.h> #include <algorithm> ...
- 【模板】SPOJ FACT0 大数分解 miller-rabin & pollard-rho
http://www.spoj.com/problems/FACT0/en/ 给一个小于1e15的数,将他分解. miller-rabin & pollard-rho模板 #include & ...
- 数学--数论---P4718 Pollard-Rho算法 大数分解
P4718 [模板]Pollard-Rho算法 题目描述 MillerRabin算法是一种高效的质数判断方法.虽然是一种不确定的质数判断法,但是在选择多种底数的情况下,正确率是可以接受的.Pollar ...
- poj2429 大数分解+dfs
//Accepted 172 KB 172 ms //该程序为随机性算法,运行时间不定 #include <cstdio> #include <cstring> #includ ...
随机推荐
- IOC Container(服务容器)的工作机制
IOC Container 是laravel的一个核心内容,有了IOC Container在Laravel的强大表现,我们可以在Laravel中实现很大程度的代码维护性.(文档我是看的懵逼懵逼的(*^ ...
- angular 模板 小例子
参考网站:https://docs.angularjs.org/tutorial/step_09 先看下目录结构 新建个空文件夹, 输入命令: express --view ejs cnpm inst ...
- 任性,新建对象不用new
先看最简单的一个例子: window.meng = window.meng || {}; (function () { /** * * @param {Number}width * @param {N ...
- canvas drawImage异步特性
先看代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- Sublime Text 3编辑器安装
一.安装Sublime Text 3 到Sublime Text 3官网下载最新版本:http://www.sublimetext.com/3 一路Next下去即可完成安装.安装完Sublime Te ...
- Excel中的隐藏函数
excel有一些隐藏函数,这些函数在帮助文件中找不到介绍,常用的有以下三个: 1.DATEDIF() 功能:计算两个日期的差值 语法:DATEDIF(start_date,end_date,unit) ...
- PHP中的CURL函数库
PHP中的CURL函数库(Client URL Library Function) curl_close — 关闭一个curl会话curl_copy_handle — 拷贝一个curl连接资源的所有内 ...
- Search for a Range [LeetCode]
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Spring Data Jpa配置
Spring Data JPA提供的接口,也是Spring Data JPA的核心概念: 1:Repository:最顶层的接口,是一个空的接口,目的是为了统一所有Repository的类型,且能让组 ...
- HDU 4123(树的直径+单调队列)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...