Advertisement
Zeinab_Hamdy

Pythagorean Pair // important

Oct 28th, 2022
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ull unsigned ll
  6. #define nl "\n"
  7. #define sz(x) x.size()
  8. #define NumOfDigit(w) log10(w) + 1
  9. #define fill(arr, val)  memset(arr, val , sizeof(arr))
  10. #define PI 3.141592654
  11. #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define fi first
  15. #define se second
  16. #define cin(v) for (auto&i:v) cin >> i;
  17. #define cout(v) for (auto&i:v) cout << i << " ";
  18. #define fixed(n) fixed << setprecision(n)
  19. #define MOD  1000000007
  20.  
  21.  
  22. void IO(){
  23.     ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  24. }
  25. void files(){
  26.     //freopen("filename.in" , "r" ,stdin);
  27.  
  28.             #ifndef ONLINE_JUDGE
  29.                      freopen("input.txt", "r", stdin);
  30.                      freopen("output.txt", "w", stdout);
  31.             #endif
  32. }
  33. /*
  34. #include <ext/pb_ds/assoc_container.hpp
  35. #include <ext/pb_ds/tree_policy.hpp>
  36. using namespace __gnu_pbds;
  37.  
  38. template <typename K, typename V, typename Comp = std::less<K>>
  39. using ordered_map = tree<K, V, Comp, rb_tree_tag, tree_order_statistics_node_update>;
  40. template <typename K, typename Comp = std::less<K>>
  41. using ordered_set = ordered_map<K, null_type, Comp>;
  42.  
  43. template <typename K, typename V, typename Comp = std::greater_equal<K>>
  44. using ordered_multimap = tree<K, V, Comp, rb_tree_tag, tree_order_statistics_node_update>;
  45. template <typename K, typename Comp = std::greater_equal<K>>
  46. using ordered_multiset = ordered_multimap<K, null_type, Comp>;
  47.  
  48. */
  49. bool prime(ll n){
  50.     if(n==2 || n==3) return true;
  51.     if(n<2 || n%2==0  ) return false;
  52.     for(int i=3;i<=sqrt(n);i+=2)   if(n%i==0) return false;
  53.     return true;
  54. }
  55.  
  56. ll BinEx(ll a , ll b ){
  57.     ll res = 1;
  58.     while (b){
  59.         if (b& 1) res *=a ;
  60.         a*= a;
  61.         b /=2 ;
  62.     }
  63.     return res;
  64. }
  65. void solve(){
  66. ll n , x; cin >> n ;
  67. x=n;
  68. int cnt2=0;
  69. while(n%2==0){
  70.     cnt2++;  // get how many times n is divisble by 2
  71.     n/=2 ;
  72. }
  73. // odd /
  74. if(cnt2&1) n*=2 , cnt2--; // to be perfect
  75. // 2^ ( even ) -> perfect square
  76. cnt2/=2 ;   // as we * twice taht
  77. ll ans=BinEx(2,cnt2);
  78.  
  79. for(ll i=0;i*i <=n; i++){ // n become smaller element after /2
  80.     ll tmp = n - (i*i);
  81.     ll sq=sqrt(tmp);
  82.      // perfect square * perfect square = perfect square
  83.     if(sq*sq + i*i == n )
  84.         return void (cout << i * ans  << " " <<
  85.         sq * ans  <<"\n" );
  86.    
  87. }
  88.  
  89. cout << -1 << nl;
  90.  
  91. }
  92. int main(){
  93.                  IO();          files();
  94.                  
  95.     int testCase=1;  // one test case
  96.        cin >> testCase ;      
  97.  while(testCase--)
  98.         solve();  // my code
  99.        
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement