Advertisement
EWTD

Untitled

Oct 26th, 2019
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. //#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
  2. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
  3. #include <iostream>
  4. #include <vector>
  5. #include <set>
  6. #include <array>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <map>
  10. using namespace std;
  11. #define MAX_INT 2147483647
  12. #define ll long long
  13. int main(){
  14. int n, p;
  15. cin >> n >> p;
  16. if (p >= n){
  17. cout << -1 << '\n';
  18. return 0;
  19. }
  20. if(p == 0){
  21. int ans = 0;
  22. while(n>0){
  23. ans += (int)n%2;
  24. n/=2;
  25. }
  26. cout << ans << '\n';
  27. }else{
  28. for(int i = 1; n - p*i > 0 && n-p*i < 1'000'000'000 ; ++i){
  29. int temp = n - p*i;
  30. int cnt_min = 0;
  31. int cnt_max = 0;
  32. int bin = 0;
  33. while(temp > 0){
  34. cnt_min += (int)temp % 2;
  35. if(temp%2 == 0){
  36. cnt_max += bin;
  37. bin = 0;
  38. }else {
  39. bin = (bin * 2) + 1;
  40. }
  41. temp/=2;
  42. }
  43. cnt_max+= bin;
  44. if(cnt_min <= i && i <= cnt_max){
  45. cout << i << '\n';
  46. return 0;
  47. }
  48. }
  49. cout << -1 << '\n';
  50. }
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement