fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. const int MAXN = 10000000;
  6. vector<int> spf(MAXN + 1);
  7.  
  8. void sieve() {
  9. for(int i = 2; i <= MAXN; i++) {
  10. if(spf[i] == 0) {
  11. for(int j = i; j <= MAXN; j += i) {
  12. if(spf[j] == 0) spf[j] = i;
  13. }
  14. }
  15. }
  16. }
  17.  
  18. int main() {
  19. ios::sync_with_stdio(false);
  20. cin.tie(nullptr);
  21.  
  22. sieve();
  23.  
  24. int t;
  25. cin >> t;
  26. while(t--) {
  27. int n;
  28. cin >> n;
  29.  
  30. vector<ll> b(n), c(n);
  31. for(int i = 0; i < n; i++) {
  32. ll x;
  33. cin >> x;
  34.  
  35. ll p = spf[x];
  36. if(p == 0) {
  37. b[i] = -1;
  38. c[i] = -1;
  39. continue;
  40. }
  41.  
  42. ll pw = 1;
  43. ll v = x;
  44. while(v % p == 0) {
  45. v /= p;
  46. pw *= p;
  47. }
  48.  
  49. if(v == 1) {
  50. b[i] = -1;
  51. c[i] = -1;
  52. } else {
  53. b[i] = pw;
  54. c[i] = x / pw;
  55. }
  56. }
  57.  
  58. for(int i = 0; i < n; i++) cout << b[i] << ' ';
  59. cout << '\n';
  60. for(int i = 0; i < n; i++) cout << c[i] << ' ';
  61. cout << '\n';
  62. }
  63.  
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0.22s 42164KB
stdin
10
2 3 4 5 6 7 8 9 10 24
stdout
-1 -1 
-1 -1 
2 -1 -1 -1 2 
3 -1 -1 -1 5 
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5