poj2429 大数分解+dfs
//Accepted 172 KB 172 ms
//该程序为随机性算法,运行时间不定
#include <cstdio>
#include <cstring>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
],factor_top=-;
//gcd
long long gcd(long long a,long long b)
{
) return a;
return gcd(b,a%b);
}
//a*b%n n<2^62
long long mult_mod(long long a,long long b,long long n)
{
;
while (b)
{
)
{
res+=exp;
if (res>n) res-=n;
}
exp<<=;
if (exp>n) exp-=n;
b>>=;
}
return res;
}
//return a^b%n
long long exp_mod(long long a,long long b,long long n)
{
,exp=a%n;
)
{
)
{
res=mult_mod(res,exp,n);
}
exp=mult_mod(exp,exp,n);
b>>=;
}
return res;
}
//miller_rabin 算法进行素数判定
//判断次数times次 一般取times=10
//return true 则n为素数
bool miller_rabin(long long n,long long times)
{
) return true;
|| !(n&)) return false;
,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; //not prime
x=y;
}
) return false;
}
return true;
}
//pollar_rho 求n的一个质因子
//c 为测试函数中的常数
long long pollard_rho(long long n,int c)
{
,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<<=;
}
}
}
//找出n的所用质因子
void findFactor(long long n,int c)
{
) return ;
))
{
factor[++factor_top]=n;
return ;
}
long long p=n;
while (p>=n)
{
p=pollard_rho(p,c--);
}
findFactor(p,c);
findFactor(n/p,c);
}
];
int m;
int cmp(long long a,long long b)
{
return a>b;
}
void slove()
{
sort(factor,factor+factor_top+,cmp);
m=;
a[]=factor[];
;i<factor_top;i++)
{
])
{
a[m]*=factor[i];
}
else
{
m++;
a[m]=factor[i+];
}
}
}
long long minx,ans;
void dfs(int s,long long num,long long t)
{
)
{
|| (num+t/num<minx))
{
minx=num+t/num;
ans=num;
}
return ;
}
dfs(s+,a[s]*num,t);
dfs(s+,num,t);
}
int main()
{
__int64 s,t,n;
while (scanf("%I64d%I64d",&s,&t)!=EOF)
{
n=t/s;
if (s==t)
{
printf("%I64d %I64d\n",s,t);
continue;
}
//printf("%I64d\n",gcd(t,s));
factor_top=-;
findFactor(n,);
//printf("findFactor()\n");
m=;
slove();
//printf("slove()\n");
minx=-;
dfs(,,n);
//printf("dfs()\n");
if (ans>n/ans) ans=n/ans;
printf("%I64d %I64d\n",ans*s,n/ans*s);
}
;
}
poj2429 大数分解+dfs的更多相关文章
- poj1181 大数分解
//Accepted 164 KB 422 ms //类似poj2429 大数分解 #include <cstdio> #include <cstring> #include ...
- 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 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd lcm/gcd=a/gcd*b/gcd 可知a/gc ...
- poj 2429 Pollard_rho大数分解
先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜 ...
- Light OJ 1341 Aladdin and the Flying Carpet Pollard_rho整数分解+DFS
进入a b 多少努力p, q 使p*q == a && p < q && p >= b 直接大整数分解 然后dfs所有可能的解决方案劫持 #include ...
- 1400 序列分解(dfs)
1400 序列分解 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 小刀和大刀是双胞胎兄弟.今天他们玩一个有意思的游戏. 大刀给小刀准备了一个长度为n的整数序列.小 ...
- poj 1811 大数分解
模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...
随机推荐
- ActionErrors 使用说明 struts1 validate 处理流程 详细教程(转)
转自(http://blog.csdn.net/wyx100/article/details/8736445). struts1 处理流程是 jsp --> ActionForm 中的A ...
- 在腾讯云上创建您的SQL Cluster(1)
版权声明:本文由李斯达原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/247 来源:腾云阁 https://www.qclo ...
- webpages框架中使用Html.TextArea()在前台显示多行信息时,如何进行大小、样式的设置
环境:vs2015 webpages框架+razor语法: 目的:服务器进行数据更新操作后,在前台显示更新的相关信息: 后台代码:将更新条数等相关信息存储在一个变量中: @{ var serverIn ...
- OneProxy与其它数据库中间件的对比
OneProxy 优点 性能 缺点 闭源,被商业公司掌控,到时候随别人蹂躏 可维护性极差,缺乏友好的出错信息,光维护这个环节就被他人掌控 定价不明 有没有这样的公司? 大到10wtps,但是没人能理解 ...
- 【BZOJ 3295】动态逆序对 - 分块+树状数组
题目描述 给定一个1~n的序列,然后m次删除元素,每次删除之前询问逆序对的个数. 分析:分块+树状数组 (PS:本题的CDQ分治解法见下一篇) 首先将序列分成T块,每一块开一个树状数组,并且先把最初的 ...
- Date的那一大堆事儿--1
String perfTimeStr = "";// 统一设置日历格式 Calendar calendar = Calendar.getInstance(); calendar.s ...
- css3属性 transition transform
1.transition 译:过渡,转变 可以设置过渡属性 transition: property duration timing-function delay; transition-proper ...
- Java_Ant 详解
1,什么是antant是构建工具2,什么是构建概念到处可查到,形象来说,你要把代码从某个地方拿来,编译,再拷贝到某个地方去等等操作,当然不仅与此,但是主要用来干这个3,ant的好处跨平台 --因为 ...
- 第二周 WBS、NABCD查阅
WBS WBS:工作分解结构(Work Breakdown Structure) 创建WBS:创建WBS是把项目可交付成果和项目工作分解成较小的,更易于管理的组成部分的过程. WBS是项目管理重要的专 ...
- 关闭“JDK自动更新”提示