Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #include <fstream>
  2. using namespace std;
  3. ifstream fin("arma1.in");
  4. ofstream fout("arma1.out");
  5. int xlan(int x, int n)
  6. {
  7. int p=1;
  8. while(n>0)
  9. {
  10. p=p*x;
  11. n--;
  12. }
  13. return p;
  14. }
  15. int prim(int n)
  16. {
  17. if(n<2) return 0;
  18. if(n%2==0 && n!=2) return 0;
  19. for(int d=3;d*d<=n;d=d+2)
  20. if(n%d==0) return 0;
  21. return 1;
  22. }
  23. int cmmdc(int a, int b)
  24. {
  25. while(b>0)
  26. {
  27. int r=a%b;
  28. a=b;
  29. b=r;
  30. }
  31. return a;
  32. }
  33. int A[10001],D[10001],E[10001],l;
  34. bool Er[50001];
  35. int P[50001],np;
  36. int main()
  37. {
  38. int max=50000;
  39. for(int i=2;i<=max;i++)
  40. Er[i]=1;
  41. for(int i=2;i*i<=max;i++)
  42. if(Er[i]==1)
  43. for(int j=i*i;j<=max;j=j+i)
  44. Er[j]=0;
  45. for(int i=2;i<=max;i++)
  46. if(Er[i]==1) P[++np]=i;
  47. /*for(int i=1;i<=np;i++) fout<<P[i]<<" ";*/
  48. int p,n;
  49. fin>>p>>n;
  50. for(int i=1;i<=n;i++)
  51. fin>>A[i];
  52. if(p==1)
  53. {
  54. long long s=0;
  55. for(int i=1;i<=n;i++)
  56. {
  57. l=0;
  58. int x=A[i];
  59. if(prim(x) || x==0) s=s+x;
  60. else
  61. {
  62. int d=1; //descompunere numar
  63. while(x>1)
  64. {
  65. if(x%P[d]==0)
  66. {
  67. int e=0;
  68. while(x%P[d]==0)
  69. {
  70. e++;
  71. x=x/P[d];
  72. }
  73. l++;
  74. E[l]=e; //retin exponetul
  75. D[l]=P[d]; //retin baza
  76. }
  77. else d++;
  78. if(P[d]*P[d]>x && x!=1)
  79. {
  80. l++;
  81. D[l]=x;
  82. E[l]=1;
  83. x=1;
  84. }
  85. }
  86. int cmmdc1=0,w=E[1]; //calculez cmmdc-ul exponentilor
  87. for(int q=1;q<=l;q++)
  88. if(E[q]==1)
  89. {
  90. cmmdc1=1;
  91. break;
  92. }
  93. else
  94. {
  95. int y=E[q];
  96. cmmdc1=cmmdc(y,w);
  97. w=cmmdc1;
  98. }
  99. if(cmmdc1==1) s=s+A[i];
  100. else
  101. {
  102. int r=1;
  103. for(int q=1;q<=l;q++)
  104. {
  105. E[q]=E[q]/cmmdc1;
  106. r=r*xlan(D[q],E[q]);
  107. }
  108. s=s+r;
  109. }
  110. }
  111. }
  112. fout<<s;
  113. }
  114. else
  115. {
  116. for(int i=1;i<=n;i++)
  117. {
  118. l=0;
  119. int x=A[i];
  120. if(prim(x) || x==1) fout<<1<<"\n";
  121. else
  122. {
  123. int d=1; //descompunere numar
  124. while(x>1)
  125. {
  126. if(x%P[d]==0)
  127. {
  128. int e=0;
  129. while(x%P[d]==0)
  130. {
  131. e++;
  132. x=x/P[d];
  133. }
  134. l++;
  135. E[l]=e; //retin exponetul
  136. D[l]=P[d]; //retin baza
  137. }
  138. else d++;
  139. if(P[d]*P[d]>x && x!=1)
  140. {
  141. l++;
  142. D[l]=x;
  143. E[l]=1;
  144. x=1;
  145. }
  146. }
  147. int cmmdc1=0,w=E[1]; //calculez cmmdc-ul exponentilor
  148. for(int q=1;q<=l;q++)
  149. if(E[q]==1)
  150. {
  151. cmmdc1=1;
  152. break;
  153. }
  154. else
  155. {
  156. int y=E[q];
  157. cmmdc1=cmmdc(y,w);
  158. w=cmmdc1;
  159. }
  160. fout<<cmmdc1<<"\n";
  161. }
  162. }
  163. }
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement