Advertisement
a53

spp

a53
Feb 19th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #include <fstream>
  2. using namespace std;
  3. long long int p;
  4. int X;
  5.  
  6. long long int SPP(int x,int y) /// Intoarce suma x^2+(x+1)^2+...+y^2
  7. {
  8. --x;
  9. unsigned long long int S1=1ULL*x*(x+1)*(2*x+1)/6,S2=1ULL*y*(y+1)*(2*y+1)/6;
  10. return (long long int)(S2-S1);
  11. }
  12.  
  13. int cb(int x,int y) /// Cautare binara a lui y
  14. {
  15. int m=(x+y)/2;
  16. if(SPP(X,m)>=p&&SPP(X,m-1)<p)
  17. return m;
  18. else
  19. {
  20. if(SPP(X,m)<p)
  21. return cb(m+1,y);
  22. if(SPP(X,m)>p)
  23. return cb(x,m-1);
  24. }
  25. }
  26.  
  27. int main()
  28. {
  29. int q;
  30. ifstream f("spp.in");
  31. f>>q;
  32. ofstream g("spp.out");
  33. while(q--)
  34. {
  35. f>>X>>p;
  36. p*=p;
  37. g<<cb(X,1500000)<<'\n';
  38. }
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement