Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define FROM(A,B) for(int i=A;i<B;++i)
- using namespace std;
- int findX(const long long&, const long long&, int, int);
- int findX(const long long&, const long long&);
- int main()
- {
- int z;
- cin>>z;
- long long p, q;
- int x;
- FROM(0, z){
- cin>>p;
- cin>>q;
- int x = findX(p, q);
- long long temp = (long long) x * x * x;
- temp += p*x;
- if(temp == q)
- cout<<x<<endl;
- else
- cout<<x<<" NIE"<<endl;
- }
- return 0;
- }
- int findX(const long long& p, const long long& q, int start, int finish){
- int x = finish/2;
- long long temp = p * x;
- temp += (long long) x * x * x;
- if(start == finish-1 || start == finish)
- return x;
- if(temp == q)
- return -1;
- if(temp > q)
- return findX(p, q, start, x-1);
- if(temp < q)
- return findX(p, q, x+1, finish);
- }
- int findX(const long long& p, const long long& q){
- int start = 0, finish = 1000000;
- return findX(p, q, start, finish);
- /*int x = finish/2;
- long long temp = p * x;
- temp += (long long) x * x * x;
- if(start == finish || temp == q)
- return x;
- if(temp > q)
- return findX(p, q, start, x);
- if(temp < q)
- return findX(p, q, x+1, finish);*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement