//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的更多相关文章

  1. poj1181 大数分解

    //Accepted 164 KB 422 ms //类似poj2429 大数分解 #include <cstdio> #include <cstring> #include ...

  2. HDU4344(大数分解)

    题目:Mark the Rope 题意就是给一个数,然后求这个数的所有因子中组成的最大的一个子集,其中1和本身除外,使得在这个子集中元素两两互素,求最大子集的元素个 数,并且求出和最大的值. 找规律就 ...

  3. poj 1811 随机素数和大数分解(模板)

    Sample Input 2 5 10 Sample Output Prime 2 模板学习: 判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解 miller : ...

  4. Pollard_Rho大数分解模板题 pku-2191

    题意:给你一个数n,  定义m=2k-1,   {k|1<=k<=n},并且 k为素数;  当m为合数时,求分解为质因数,输出格式如下:47 * 178481 = 8388607 = ( ...

  5. 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 ...

  6. poj 2429 Pollard_rho大数分解

    先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜 ...

  7. Light OJ 1341 Aladdin and the Flying Carpet Pollard_rho整数分解+DFS

    进入a b 多少努力p, q 使p*q == a && p < q && p >= b 直接大整数分解 然后dfs所有可能的解决方案劫持 #include ...

  8. 1400 序列分解(dfs)

    1400 序列分解 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 小刀和大刀是双胞胎兄弟.今天他们玩一个有意思的游戏. 大刀给小刀准备了一个长度为n的整数序列.小 ...

  9. poj 1811 大数分解

    模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...

随机推荐

  1. sql CHARINDEX函数

    CHARINDEX函数返回字符或者字符串在另一个字符串中的起始位置.CHARINDEX函数调用方法如下: CHARINDEX ( expression1 , expression2 [ , start ...

  2. grep使用方法

    linux grep命令详解 简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来 ...

  3. JavaScript ——闭包理解

    昨天晚上听别人谈起闭包这个东西,虽然对js有一点了解但却丝毫没有印象,今天也没什么事就顺便研究了一下满足好奇宝宝.整合于网上的理解,记录一下. 一.闭包的作用域 要理解闭包,首先必须理解Javascr ...

  4. Excel中的表单控件和active控件

    EXCEL中有两种控件:表单控件和active控件 表单控件是excel5和excel95开始使用的,从excel97开始,active控件开始出现 关于表单控件和active控件的区别和使用范围,网 ...

  5. 《javascript高级程序设计》第四章 Variables,scope,and memory

    4.1 基本类型和引用类型的值 primitive and reference values 4.1.1 动态的属性 dynamic properties 4.1.2 复制变量值 copying va ...

  6. 数据库中User和Schema的关系

    如果我们想了解数据库中的User和Schema到底什么关系,那么让我们首先来了解一下数据库中User和Schema到底是什么概念.        在SQL Server2000中,由于架构的原因,Us ...

  7. 小例子(三)、winform控件的移动

    程序:Do You Love Me ? 说明:就是鼠标移动到“不爱”按钮上按钮就会移动到其他地方 代码: //鼠标进入控件表面的事件MouseEnter //this.ClientSize.Width ...

  8. Runner之记计账项目的典型用户分析

  9. OC self和super

    在OC中 1 self是一个指针,在每一个方法中都有一个self指针 2 self可以出现在所有的方法中(对象方法和类方法),不能在函数中 3 self指向调用者.(谁调用它就指向谁) 4 可以使用s ...

  10. 修改Linux系统时区

    修改配置文件来修改时区1.修改/etc/sysconfig/clock         ZONE=Asia/Shanghai 2.rm /etc/localtime3.链接到上海时区文件 ln -sf ...