Advertisement
a53

jocprim

a53
Mar 25th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #include <cmath>
  4. #define N 10000001
  5. using namespace std;
  6. int F[N],nr;
  7.  
  8. class InParser
  9. {
  10. private:
  11. FILE *fin;
  12. char *buff;
  13. int sp;
  14.  
  15. char read_ch()
  16. {
  17. ++sp;
  18. if(sp==4096)
  19. {
  20. sp=0;
  21. fread(buff,1,4096,fin);
  22. }
  23. return buff[sp];
  24. }
  25.  
  26. public:
  27. InParser(const char* nume)
  28. {
  29. fin=fopen(nume,"r");
  30. buff=new char[4096]();
  31. sp=4095;
  32. }
  33.  
  34. InParser& operator >> (int &n)
  35. {
  36. char c;
  37. while(!isdigit(c=read_ch())&&c!='-');
  38. int sgn=1;
  39. if (c=='-')
  40. {
  41. n=0;
  42. sgn=-1;
  43. }
  44. else
  45. {
  46. n=c-'0';
  47. }
  48. while(isdigit(c=read_ch()))
  49. {
  50. n=10*n+c-'0';
  51. }
  52. n*=sgn;
  53. return *this;
  54. }
  55.  
  56. InParser& operator >> (long long &n)
  57. {
  58. char c;
  59. n=0;
  60. while(!isdigit(c=read_ch())&&c!='-');
  61. long long sgn=1;
  62. if(c=='-')
  63. {
  64. n=0;
  65. sgn=-1;
  66. }
  67. else
  68. {
  69. n=c-'0';
  70. }
  71. while(isdigit(c=read_ch()))
  72. {
  73. n=10*n+c-'0';
  74. }
  75. n*=sgn;
  76. return *this;
  77. }
  78. };
  79.  
  80. class OutParser
  81. {
  82. private:
  83. FILE *fout;
  84. char *buff;
  85. int sp;
  86.  
  87. void write_ch(char ch)
  88. {
  89. if(sp==50000)
  90. {
  91. fwrite(buff,1,50000,fout);
  92. sp=0;
  93. buff[sp++]=ch;
  94. }
  95. else
  96. {
  97. buff[sp++]=ch;
  98. }
  99. }
  100.  
  101. public:
  102. OutParser(const char* name)
  103. {
  104. fout=fopen(name,"w");
  105. buff=new char[50000]();
  106. sp=0;
  107. }
  108. ~OutParser()
  109. {
  110. fwrite(buff,1,sp,fout);
  111. fclose(fout);
  112. }
  113.  
  114. OutParser& operator <<(int vu32)
  115. {
  116. if(vu32<=9)
  117. {
  118. write_ch(vu32+'0');
  119. }
  120. else
  121. {
  122. (*this) <<(vu32/10);
  123. write_ch(vu32%10+'0');
  124. }
  125. return *this;
  126. }
  127.  
  128. OutParser& operator <<(long long vu64)
  129. {
  130. if(vu64<=9)
  131. {
  132. write_ch(vu64+'0');
  133. }
  134. else
  135. {
  136. (*this) <<(vu64/10);
  137. write_ch(vu64%10+'0');
  138. }
  139. return *this;
  140. }
  141.  
  142. OutParser& operator <<(char ch)
  143. {
  144. write_ch(ch);
  145. return *this;
  146. }
  147. OutParser& operator <<(const char *ch)
  148. {
  149. while(*ch)
  150. {
  151. write_ch(*ch);
  152. ++ch;
  153. }
  154. return *this;
  155. }
  156. };
  157.  
  158. void FactorprimMax(int n)
  159. {
  160. int d=2;
  161. bool ok=true;
  162. while(n>1)
  163. {
  164. if(n%d==0)
  165. while(n%d==0)
  166. n/=d;
  167. ++d;
  168. if(n>1&&d*d>n)
  169. {
  170. if(F[n]==0)
  171. ++nr;
  172. ++F[n],n=1,ok=false;
  173. }
  174. }
  175. if(ok)
  176. {
  177. if(F[d-1]==0)
  178. ++nr;
  179. ++F[d-1];
  180. }
  181. }
  182.  
  183. int main()
  184. {
  185. int n;
  186. InParser f("jocprim.in");
  187. f>>n;
  188. int x;
  189. while(n--)
  190. {
  191. f>>x;
  192. if(x>1)
  193. FactorprimMax(x);
  194. }
  195. OutParser g("jocprim.out");
  196. g<<nr<<'\n';
  197. for(int i=1;i<=10000000;++i)
  198. if(F[i])
  199. g<<i<<' '<<F[i]<<'\n';
  200. return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement