T3000

Untitled

Jan 16th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     long long t; // number of test cases
  8.     cin >> t;
  9.     while (t-- > 0)
  10.     {
  11.         long long x, z2;  // number
  12.         long long a = -1; // a + a+1 + a+2 +.....+ a+n = x
  13.         long long s1 = 1; // 1+2+3+...+n
  14.        
  15.         long long sh = 2; // help at calculating s1
  16.         cin >> x;
  17.         for (long long i = 2; s1 <= x; i++) // looking for < a >
  18.         {
  19.             s1 += i;
  20.             if ((x - s1) % i == 0)
  21.             {
  22.                 z2 = i;
  23.                 a = (x - s1) / i;
  24.                 break;
  25.             }
  26.         }
  27.  
  28.         if (a == -1)
  29.         {
  30.             cout << "IMPOSSIBLE" << '\n';
  31.         }
  32.         else
  33.         {
  34.             a += 1;
  35.             cout << x << " "
  36.                  << "="
  37.                  << " ";
  38.             for (int i = 1; i < z2; i++)
  39.             {
  40.                 cout << a << " "
  41.                      << "+"
  42.                      << " ";
  43.                 a += 1;
  44.             }
  45.             cout << a << '\n';
  46.         }
  47.     }
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment