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> ...
随机推荐
- java final
final:(最终的)看不懂时有必要分析内存画图,不同方法的局部变量是相互独立的额不要被所起的名所困扰. 1)每个方法运行时jvm,都会为其开辟一片内存空间.内存空间是属于这个方法的, 同时,方法中的 ...
- DedeCMSV57数据库结构文档
表名:dede_addonarticle(ENGINE=MyISAM/CHARSET=gbk) 说明:Top 字段名 说明描述 具体参数 aid 文章ID mediumint(8) unsig ...
- 【转】 简单理解Socket
题外话 前几天和朋友聊天,朋友问我怎么最近不写博客了,一个是因为最近在忙着公司使用的一些控件的开发,浏览器兼容性搞死人:但主要是因为这段时间一直在看html5的东西,看到web socket时觉得很有 ...
- c++相关的类型推导
c++11和boost库增加许多关于类型推导(编译期)的关键字和类型, 用好这些机制, 对于编写项目的一些组件帮助颇大.正所谓工欲善其事,必先利其器. 1.初始化某种类型的变量 auto var = ...
- linux笔记:RPM软件包管理-yum在线管理
ip地址配置: 用ifconfig命令只能配置ip和子网掩码,这样只能访问内网:如果需要访问公网则还必须要网关和DNS. 使用setup工具配置ip: 网络yum源配置: 常用yum命令:查询 常用y ...
- Ubuntu下解决bash 没有那个文件或目录的方法
因为之前电脑硬盘坏掉,维修换了新硬盘,今天重新安装了ubuntu,装好之后就赶紧搭建工作环境,将备份的资料拷贝进来,搭建交叉编译环境,但是发现,修改bashrc中PATH绝对路径指向交叉编译器后,在命 ...
- C/C++源代码到可执行程序的过程详解
编译,编译程序读取源程序(字符流),对之进行词法和语法的分析,将高级语言指令转换为功能等效的汇编代码,再由汇编程序转换为机器语言,并且按照操作系统对可执行文件格式的要求链接生成可执行程序. 源代码-- ...
- 九度 题目1437:To Fill or Not to Fill
题目描述: With highways available, driving a car from Hangzhou to any other city is easy. But since the ...
- 使用WITH AS提高性能简化嵌套SQL(转载)
使用WITH AS提高性能简化嵌套SQL http://www.cnblogs.com/fygh/archive/2011/08/31/2160266.html
- linux apache 自动监护脚本
1 首先安装curl yum install curl 2 编写shell vi restart_apache.sh 写入一下内容 #!/bin/bashURL="http://127.0. ...