Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
- #include <iostream>
- #include <vector>
- #include <set>
- #include <array>
- #include <cmath>
- #include <algorithm>
- #include <map>
- using namespace std;
- #define MAX_INT 2147483647
- #define ll long long
- int main(){
- int n, p;
- cin >> n >> p;
- if (p >= n){
- cout << -1 << '\n';
- return 0;
- }
- if(p == 0){
- int ans = 0;
- while(n>0){
- ans += (int)n%2;
- n/=2;
- }
- cout << ans << '\n';
- }else{
- for(int i = 1; n - p*i > 0 && n-p*i < 1'000'000'000 ; ++i){
- int temp = n - p*i;
- int cnt_min = 0;
- int cnt_max = 0;
- int bin = 0;
- while(temp > 0){
- cnt_min += (int)temp % 2;
- if(temp%2 == 0){
- cnt_max += bin;
- bin = 0;
- }else {
- bin = (bin * 2) + 1;
- }
- temp/=2;
- }
- cnt_max+= bin;
- if(cnt_min <= i && i <= cnt_max){
- cout << i << '\n';
- return 0;
- }
- }
- cout << -1 << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement