fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<ll, ll> PII;
  6. typedef vector<int> VI;
  7. typedef vector<VI> VVI;
  8.  
  9. #define MAXN 200005
  10. #define pb push_back
  11. #define mp make_pair
  12. #define MOD (ll)1e9+7
  13. #define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  14. #define rep(i, a, b) for(int i = a; i < b; ++i)
  15.  
  16. #define SSTR( x ) static_cast< std::ostringstream & >( \
  17.   ( std::ostringstream() << std::dec << x ) ).str()
  18.  
  19.  
  20.  
  21. int main() {
  22. FASTIO
  23. int t;
  24. cin >> t;
  25. while(t--) {
  26. int n; cin >> n;
  27. vector<int> a(n);
  28. int sum = 0;
  29. rep(i, 0, n) {
  30. cin >> a[i];
  31. sum += a[i];
  32. }
  33. int ansIdx = -1;
  34. for(int i = 0; i < n; ++i) {
  35. int rem = sum - a[i];
  36. if(!(rem%a[i])) {
  37. ansIdx = i;
  38. break;
  39. }
  40. }
  41. if(ansIdx == -1) {
  42. int smol = *min_element(a.begin(), a.end());
  43. int rem = sum - smol;
  44. bool done = false;
  45. for(int i = 0; i < n; ++i) {
  46. if(!done and a[i] == smol) {
  47. done = true;
  48. cout << -rem;
  49. }
  50. else {
  51. cout << smol;
  52. }
  53. cout << " ";
  54. }
  55. }
  56. else {
  57. assert(ansIdx != -1 and abs(-(sum-a[ansIdx])/a[ansIdx]) <= 1000000000);
  58. int ss = 0;
  59. for(int i = 0; i < n; ++i) {
  60. int x = 0;
  61. if(i == ansIdx)
  62. x = -((sum-a[i])/a[i]);
  63. else
  64. x = 1;
  65. ss += x * a[i];
  66. }
  67. assert(ss == 0);
  68. for(int i = 0; i < n; ++i) {
  69. if(i == ansIdx)
  70. cout << -(sum-a[i])/a[i];
  71. else
  72. cout << 1;
  73. cout << " ";
  74. }
  75. }
  76.  
  77. cout << '\n';
  78. }
  79. return 0;
  80. }
Success #stdin #stdout 0.01s 5616KB
stdin
6
2
5 5
5
5 -2 10 -9 4
7
1 2 3 4 5 6 7
2
68 35
3
3 17 11
2
1 -1
stdout
-1 1 
1 5 1 1 1 
-27 1 1 1 1 1 1 
35 -68 
-28 3 3 
1 1