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, 0);
  7.  
  8. void sieve(){
  9. for(int i = 2; i <= MAXN; i++){
  10. if(!spf[i]){
  11. for(int j = i; j <= MAXN; j += i){
  12. if(!spf[j]) 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 = 1;
  25. cin >> t;
  26. while(t--){
  27. int n;
  28. cin >> n;
  29.  
  30. vector<ll> b(n, -1), c(n, -1);
  31. for(int i = 0; i < n; i++){
  32. ll x;
  33. cin >> x;
  34.  
  35. ll v = x;
  36. ll p = spf[v];
  37. if(p == 0){
  38. b[i] = -1;
  39. c[i] = -1;
  40. continue;
  41. }
  42.  
  43. ll pw = 1;
  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.2s 42104KB
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