//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. 实现 像网易云音乐 播放列表那样的弹出型Dialog

    如图 所示是点击Test之后的 弹出的Dialog (请无视我工程的命名) 20161017/**加入点击回调,假设dialog里放了一个TextView*/ 得先写一个点击回调 public int ...

  2. 【BZOJ 3295】动态逆序对 - 分块+树状数组

    题目描述 给定一个1~n的序列,然后m次删除元素,每次删除之前询问逆序对的个数. 分析:分块+树状数组 (PS:本题的CDQ分治解法见下一篇) 首先将序列分成T块,每一块开一个树状数组,并且先把最初的 ...

  3. jmeter 使用URL重写处理用户会话

    如果您的web应用程序使用URL重写而不是cookie保存会话信息, 那么你需要做一些额外的工作来测试你的网站. 正确应对URL重写,JMeter需要解析HTML 接收从服务器和检索独特的会话ID,使 ...

  4. 在AngularJS应用中实现认证授权

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAokAAAFwCAIAAABbwHY6AAAgAElEQVR4nOy9+XtcxbX3+/4H9z73jP ...

  5. [Eclipse] 详细设置护眼背景色和字体颜色并导出

    http://jingyan.baidu.com/article/d5a880eb6c4f7813f147ccef.html Eclipse是一款码农们喜闻乐见的集成开发平台,但是其默认的主题和惨白的 ...

  6. python 使用*args 和**kwargs

    def fun_var_args(farg, *args): print "arg:", farg for value in args: print "another a ...

  7. jquery 常用函数

    过滤选择器 $("li:eq(2)").css("background-color", "#60F"); 索引 li:contains('土 ...

  8. DES加密算法

    DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开 ...

  9. hdu----(2084)数塔(dp)

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  10. 聚类算法:K-means 算法(k均值算法)

    k-means算法:      第一步:选$K$个初始聚类中心,$z_1(1),z_2(1),\cdots,z_k(1)$,其中括号内的序号为寻找聚类中心的迭代运算的次序号. 聚类中心的向量值可任意设 ...