Advertisement
Guest User

Code

a guest
May 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #ifdef DEBUG
  4. #include <fstream>
  5. ifstream IN("input.txt");
  6. ofstream OUT("output.txt");
  7. #endif
  8.  
  9. #ifndef DEBUG
  10. #include <iostream>
  11. #define IN cin
  12. #define OUT cout
  13. #endif
  14.  
  15. bool prs[1000001];
  16. pair<int, int> nxt[1000001];
  17. bool was[1000001];
  18. int mas[1000001];
  19.  
  20. int n;
  21.  
  22. int main() {
  23. ios_base::sync_with_stdio(0);
  24. IN >> n;
  25. int x;
  26. int max = 0;
  27. for (int i = 0; i < n; ++i) {
  28. IN >> mas[i];
  29. if (mas[i] > max) max = mas[i];
  30. }
  31. for (int i = 2; i <= max; i++) {
  32. if (!was[i]) {
  33. was[i] = true;
  34. prs[i] = true;
  35. int k = 2 * i;
  36. int j = 2;
  37. for (; k <= max; k += i, ++j) {
  38. if (!was[k]) nxt[k] = { i, j };
  39. was[k] = true;
  40. prs[k] = false;
  41. }
  42. }
  43. }
  44. for (int i = 0; i < n; ++i) {
  45. if (prs[mas[i]]) OUT << mas[i];
  46. else {
  47. int x = mas[i];
  48. while (!prs[x]) {
  49. OUT << nxt[x].first << " ";
  50. x = nxt[x].second;
  51. }
  52. if (x > 1) {
  53. OUT << x;
  54. }
  55. }
  56. OUT << endl;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement