C. p-binary

Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer pp (which may be positive, negative, or zero). To combine their tastes, they invented pp-binary numbers of the form 2x+p2x+p, where xx is a non-negative integer.

For example, some 9−9-binary ("minus nine" binary) numbers are: 8−8 (minus eight), 77 and 10151015 (8=209−8=20−9, 7=2497=24−9, 1015=21091015=210−9).

The boys now use pp-binary numbers to represent everything. They now face a problem: given a positive integer nn, what's the smallest number of pp-binary numbers (not necessarily distinct) they need to represent nn as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.

For example, if p=0p=0 we can represent 77 as 20+21+2220+21+22.

And if p=9p=−9 we can represent 77 as one number (249)(24−9).

Note that negative pp-binary numbers are allowed to be in the sum (see the Notes section for an example).

Input

The only line contains two integers nn and pp (1n1091≤n≤109, 1000p1000−1000≤p≤1000).

Output

If it is impossible to represent nn as the sum of any number of pp-binary numbers, print a single integer 1−1. Otherwise, print the smallest possible number of summands.

Examples
input
Copy
24 0
output
Copy
2
input
Copy
24 1
output
Copy
3
input
Copy
24 -1
output
Copy
4
input
Copy
4 -7
output
Copy
2
input
Copy
1 1
output
Copy
-1
Note

00-binary numbers are just regular binary powers, thus in the first sample case we can represent 24=(24+0)+(23+0)24=(24+0)+(23+0).

In the second sample case, we can represent 24=(24+1)+(22+1)+(20+1)24=(24+1)+(22+1)+(20+1).

In the third sample case, we can represent 24=(241)+(221)+(221)+(221)24=(24−1)+(22−1)+(22−1)+(22−1). Note that repeated summands are allowed.

In the fourth sample case, we can represent 4=(247)+(217)4=(24−7)+(21−7). Note that the second summand is negative, which is allowed.

In the fifth sample case, no representation is possible.

 

 拆分二进制, 尝试的个数 小于 等于全为1所需要的个数 大于单纯拆分最少的个数 这个尝试个数有效

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod = 998244353;
const int N = 1e6 + 5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}

int main()
{
    ll n, p;
    cin >> n >> p;
    for (int i = 0; i <= 1e6; i++)
    {
        int cnt=0,tmp = n - i*p;
        while (tmp)
        {
            cnt += tmp % 2;
            tmp /= 2;
        }
          if (cnt <= i && i <= n - i * p)
        {
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}

 

posted @ 2020-06-03 15:22  DeaL57  阅读(140)  评论(0编辑  收藏  举报