Advertisement
a53

FactorulX

a53
May 7th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #define LL long long int
  4. using namespace std;
  5. int N,Fact[500500];
  6.  
  7. LL CMMDC(int a,int b)
  8. {
  9. int r;
  10. if(a==0&&b==0)
  11. return -1;
  12. else
  13. {
  14. if(a<b)
  15. swap(a,b);
  16. if(b==0)
  17. return a;
  18. else
  19. {
  20. r=a%b;
  21. while(r)
  22. a=b,b=r,r=a%b;
  23. return b;
  24. }
  25. }
  26. }
  27.  
  28. bool cautbin(int x)
  29. {
  30. int st=0,dr=N;
  31. while(st<=dr)
  32. {
  33. int m=(st+dr)/2;
  34. if(Fact[m]==x)
  35. return false;
  36. if(Fact[m]>x)
  37. dr=m-1;
  38. if(Fact[m]<x)
  39. st=m+1;
  40. }
  41. return true;
  42. }
  43.  
  44. int factorx(int n)
  45. {
  46. int factx=1;
  47. if(n%2==0)
  48. {
  49. factx*=2;
  50. while(n%2==0)
  51. n/=2;
  52. }
  53. int d=3;
  54. while(n>1)
  55. {
  56. if(n%d==0)
  57. {
  58. factx*=d;
  59. while(n%d==0)
  60. n/=d;
  61. }
  62. else
  63. d+=2;
  64. if(n>1&&d*d>n)
  65. {
  66. factx*=n;
  67. break;
  68. }
  69. }
  70. return factx;
  71. }
  72.  
  73. int main()
  74. {
  75. int n;
  76. ifstream f("factorulx.in");
  77. f>>n;
  78. int a[n];
  79. for(int i=0;i<n;++i)
  80. f>>a[i];
  81. for(int i=0;i<n-1;++i)
  82. for(int j=i+1;j<n;++j)
  83. {
  84. LL cmmdc=CMMDC(a[i],a[j]);
  85. bool ok=false;
  86. if(cmmdc==1LL*a[i]*a[j])
  87. {
  88. if(cautbin(1))
  89. ok=true,Fact[N++]=1;
  90. }
  91. else
  92. {
  93. int factx=factorx(cmmdc);
  94. if(cautbin(factx))
  95. ok=true,Fact[N++]=factx;
  96. }
  97. if(ok)
  98. sort(Fact,Fact+N);
  99. }
  100. ofstream g("factorulx.out");
  101. g<<N<<'\n';
  102. for(int i=0;i<N;++i)
  103. g<<Fact[i]<<' ';
  104. g<<'\n';
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement