题目:Mark the Rope

题意就是给一个数,然后求这个数的所有因子中组成的最大的一个子集,其中1和本身除外,使得在这个子集中元素两两互素,求最大子集的元素个

数,并且求出和最大的值。

找规律就不难发现其实答案就是先大数分解n,例如,180=2^2*3^2*5,那么就输出3   18   ,这两个数分别是素因子的个数和2^2,3^2,5的和。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <algorithm>
  5. #include <iostream>
  6.  
  7. const int Times=10;
  8. const int N=550;
  9.  
  10. using namespace std;
  11. typedef unsigned __int64 LL;
  12.  
  13. LL ct,cnt;
  14. LL fac[N],num[N];
  15.  
  16. LL gcd(LL a,LL b)
  17. {
  18. return b? gcd(b,a%b):a;
  19. }
  20.  
  21. LL multi(LL a,LL b,LL m)
  22. {
  23. LL ans=0;
  24. while(b)
  25. {
  26. if(b&1)
  27. {
  28. ans=(ans+a)%m;
  29. b--;
  30. }
  31. b>>=1;
  32. a=(a+a)%m;
  33. }
  34. return ans;
  35. }
  36.  
  37. LL quick_mod(LL a,LL b,LL m)
  38. {
  39. LL ans=1;
  40. a%=m;
  41. while(b)
  42. {
  43. if(b&1)
  44. {
  45. ans=multi(ans,a,m);
  46. b--;
  47. }
  48. b>>=1;
  49. a=multi(a,a,m);
  50. }
  51. return ans;
  52. }
  53.  
  54. bool Miller_Rabin(LL n)
  55. {
  56. if(n==2) return true;
  57. if(n<2||!(n&1)) return false;
  58. LL a,m=n-1,x,y;
  59. int k=0;
  60. while((m&1)==0)
  61. {
  62. k++;
  63. m>>=1;
  64. }
  65. for(int i=0;i<Times;i++)
  66. {
  67. a=rand()%(n-1)+1;
  68. x=quick_mod(a,m,n);
  69. for(int j=0;j<k;j++)
  70. {
  71. y=multi(x,x,n);
  72. if(y==1&&x!=1&&x!=n-1) return false;
  73. x=y;
  74. }
  75. if(y!=1) return false;
  76. }
  77. return true;
  78. }
  79.  
  80. LL Pollard_rho(LL n,LL c)
  81. {
  82. LL x,y,d,i=1,k=2;
  83. y=x=rand()%(n-1)+1;
  84. while(true)
  85. {
  86. i++;
  87. x=(multi(x,x,n)+c)%n;
  88. d=gcd((y-x+n)%n,n);
  89. if(1<d&&d<n) return d;
  90. if(y==x) return n;
  91. if(i==k)
  92. {
  93. y=x;
  94. k<<=1;
  95. }
  96. }
  97. }
  98.  
  99. void find(LL n,int c)
  100. {
  101. if(n==1) return;
  102. if(Miller_Rabin(n))
  103. {
  104. fac[ct++]=n;
  105. return ;
  106. }
  107. LL p=n;
  108. LL k=c;
  109. while(p>=n) p=Pollard_rho(p,c--);
  110. find(p,k);
  111. find(n/p,k);
  112. }
  113.  
  114. int main()
  115. {
  116. int t;
  117. LL n,ans;
  118. scanf("%d",&t);
  119. while(t--)
  120. {
  121. scanf("%I64u",&n);
  122. ct=0;
  123. find(n,120);
  124. sort(fac,fac+ct);
  125. num[0]=1;
  126. int k=1;
  127. for(int i=1;i<ct;i++)
  128. {
  129. if(fac[i]==fac[i-1])
  130. ++num[k-1];
  131. else
  132. {
  133. num[k]=1;
  134. fac[k++]=fac[i];
  135. }
  136. }
  137. cnt=k;
  138. LL ret=0;
  139. for(int i=0;i<cnt;i++)
  140. {
  141. LL temp=1;
  142. for(int j=0;j<num[i];j++)
  143. temp*=fac[i];
  144. ret+=temp;
  145. }
  146. if(cnt==1) ret/=fac[0];
  147. printf("%I64u %I64u\n",cnt,ret);
  148. }
  149. return 0;
  150. }

HDU4344(大数分解)的更多相关文章

  1. poj1181 大数分解

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

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

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

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

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

  4. poj 1811 大数分解

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

  5. poj 2429 Pollard_rho大数分解

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

  6. 模板题Pollard_Rho大数分解 A - Prime Test POJ - 1811

    题意:是素数就输出Prime,不是就输出最小因子. #include <cstdio> #include<time.h> #include <algorithm> ...

  7. 【模板】SPOJ FACT0 大数分解 miller-rabin & pollard-rho

    http://www.spoj.com/problems/FACT0/en/ 给一个小于1e15的数,将他分解. miller-rabin & pollard-rho模板 #include & ...

  8. 数学--数论---P4718 Pollard-Rho算法 大数分解

    P4718 [模板]Pollard-Rho算法 题目描述 MillerRabin算法是一种高效的质数判断方法.虽然是一种不确定的质数判断法,但是在选择多种底数的情况下,正确率是可以接受的.Pollar ...

  9. poj2429 大数分解+dfs

    //Accepted 172 KB 172 ms //该程序为随机性算法,运行时间不定 #include <cstdio> #include <cstring> #includ ...

随机推荐

  1. 【Linux】环境变量设置

    在Windows中环境变量设置是非常easy的事情.例如以下图.仅仅要右键我的电脑->高级系统设置->环境变量,选择Path之后,点击"编辑"就能够输入你要加入的内容. ...

  2. [置顶] Java字节码文件剖析

    Java为什么能够支持跨平台,其实关键就是在于其*.class字节码文件,因为*.class字节码文件有一个统一标准的规范,里面是JVM运行的时需要的相关指令,各家的JVM必须能够解释编译执行标准字节 ...

  3. 万方数据知识平台 TFHpple +Xpath解析

    试了一下.基本上适合全部的检索结果. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loadi ...

  4. MFC 用gdi绘制填充多边形区域

    MFC 用gdi绘制填充多边形区域 这里的代码是实现一个三角形的绘制,并用刷子填充颜色 在OnPaint()函数里面 运用的是给定的三角形的三个点,很多个点可以绘制多边形 CBrush br(RGB( ...

  5. 测试关闭mojo utf-8

    [root@wx03 ~]# cat test.pl use Mojolicious::Lite; use JSON qw/encode_json decode_json/; use Encode; ...

  6. RobotFrameWork(十一)AutoItLibrary测试库在win7(64bit)下安装及简单使用

    最近安装AutoItLibrary,发现在win7 x64下无法安装成功,后来经过定位,发现是3rdPartyTools\AutoIt目录下面AutoItX3.dll的问题.因为AutoItX3.dl ...

  7. leetcode 最长连续序列 longest consecutive sequence

    转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...

  8. 使用gdb调试游戏服务器

    前言 谈论gdb重要性 一般来说.提gdb,命令用于调试."命令",用户是几乎相同的复杂话.而事实确实如此,实际的开发调试必须用到gdb. 如今.大多数Linux系统是存在于ser ...

  9. C语言中输入输出重定,freopen()妙用。

    使用的理由(范围):如果输入数据很庞大,需要一次又一次的重新输入和调试时可采用本函数. freopen ()函数: 1.格式 FILE * freopen ( const char * filenam ...

  10. 如何去掉List中的重复内容

    1.通过循环进行删除 public static void removeDuplicate(List list) { ; i < list.size() - ; i ++ ) { ; j > ...