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 ...
随机推荐
- [转]Android 5.0——Material Design详解(动画篇)
Material Design:Google推出的一个全新的设计语言,它的特点就是拟物扁平化. Material Design包含了很多内容,今天跟大家分享一下Material新增的动画: 在Andr ...
- VC++调试说明
目录 第1章调试说明 1 1.1 调试设置 1 1.2 跟踪代码 2 1.3 断点 2 第2章模块生命周期 4 2.1 exe模块 4 2.2 dll模块 5 ...
- 在ubuntu下真机调试android程序出现设备没有访问权限
今天把android的开发环境从windows平台切换到了ubuntu上. java jdk android-adt android-ndk都下好,环境变量都配好之后, 在调试程序的时候,出现设备没有 ...
- 将自定义的 service provider 绑定到 IOC 容器
首先要有一些类,可以自己自定义一些类放在app/目录下的自己新建的文件夹,在类里面实现代码逻辑 然后通过命令生成serviceprovider (php artisan make:provider ...
- Android虚拟机常见错误及解决办法
第一个: [2012-11-09 13:15:14 - Tesa] Android Launch! [2012-11-09 13:15:14 - Tesa] The connection to adb ...
- (04)odoo视图操作
-----------------更新时间19:04 2016-09-29 星期四11:17 2016-09-18 星期日18:13 2016-04-05 星期二15:05 2016-03-14 星期 ...
- jsp基础知识(基本的语法及原理)
jsp 语法分为三种不同的类型: (1) 编译器指令: 类如: <%@ page import="java.io.*"%> (2) 脚本语法: 指定的是java代码: ...
- 198. 213. 337. House Robber -- 不取相邻值的最大值
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- Q: How could I use MATLAB interface for parameter selection?
Q: How could I use MATLAB interface for parameter selection? One can do this by a simple loop. See t ...
- linux tar 增量备份命令
tar --newer-mtime "2013-09-17 00:00:00" -zcvf /var/www/good.tar.gz spider/