Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int spr(long long p, long long q, long long x) {
  5. if (x > 1000000) return -1;
  6. long long a = x * x * x + p * x;
  7. if (a == q) return 0;
  8. if (a < q) return 1;
  9. else return -1;
  10. }
  11.  
  12. void rozwiaz(long long p, long long q){
  13. long long poczatek = 0;
  14. long long srodek;
  15. int wartosc_spr;
  16. long long koniec = q / p + 1; //bo dzielenie zaokragla w dol wiec trzeba dodac
  17. while(poczatek < koniec){
  18. srodek = (poczatek + koniec) / 2;
  19. wartosc_spr = spr(p, q, srodek);
  20. if(wartosc_spr == 0){
  21. cout << srodek << endl;
  22. return;
  23. }
  24. else if(wartosc_spr == -1) koniec = srodek;
  25. else poczatek = srodek + 1;
  26. }
  27. cout << "NIE" << endl;
  28. }
  29.  
  30. int main() {
  31. long long a;
  32. cin >> a;
  33. long long p, q;
  34. while(a--){
  35. cin >> p >> q;
  36. rozwiaz(p, q);
  37. }
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement