Advertisement
MasFlam

Mnożenie dziaa a nie dziaa

Dec 5th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define FROM(A,B) for(int i=A;i<B;++i)
  4.  
  5. using namespace std;
  6.  
  7. int findX(const long long&, const long long&, int, int);
  8. int findX(const long long&, const long long&);
  9.  
  10. int main()
  11. {
  12.     int z;
  13.     cin>>z;
  14.     long long p, q;
  15.     int x;
  16.    
  17.     FROM(0, z){
  18.         cin>>p;
  19.         cin>>q;
  20.        
  21.         int x = findX(p, q);
  22.         long long temp = (long long) x * x * x;
  23.         temp += p*x;
  24.         if(temp == q)
  25.             cout<<x<<endl;
  26.         else
  27.             cout<<x<<" NIE"<<endl;
  28.     }
  29.    
  30.     return 0;
  31. }
  32.  
  33. int findX(const long long& p, const long long& q, int start, int finish){
  34.     int x = finish/2;
  35.    
  36.     long long temp = p * x;
  37.     temp += (long long) x * x * x;
  38.    
  39.     if(start == finish-1 || start == finish)
  40.         return x;
  41.     if(temp == q)
  42.         return -1;
  43.     if(temp > q)
  44.         return findX(p, q, start, x-1);
  45.     if(temp < q)
  46.         return findX(p, q, x+1, finish);
  47. }
  48.  
  49. int findX(const long long& p, const long long& q){
  50.     int start = 0, finish = 1000000;
  51.     return findX(p, q, start, finish);
  52.     /*int x = finish/2;
  53.     long long temp = p * x;
  54.     temp += (long long) x * x * x;
  55.     if(start == finish || temp == q)
  56.         return x;
  57.     if(temp > q)
  58.         return findX(p, q, start, x);
  59.     if(temp < q)
  60.         return findX(p, q, x+1, finish);*/
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement