Codeforces Round #596 C

The meaning of problems

for p p , you have a number of 2 x + p 2^x +p
Please number with a minimum number of composition n n

answer

First, we can not simply judge this number more than 30,000
first n 1 e 9 n \ leq 1e9 , so 2 x k 2^x*k does not exceed 1 e 9 1e9 , when x x large, k k
will remain no more than 1 e 5 1e5 , when the x x is small, a small portion may be replaced by the larger part ( 2 2 times)
so far is no more than 3 e 5 3e5

We enumerate the number, resulting in n i p n-i*p

We binary decomposition, get a few numbers, but the number may not equal the enumeration of us.
How to do it
first. One 2 x 2^x certainly equal 2 x 2^x months 2 0 2^0 , and each of us put a 1 1 down adjustment will be more of a two, a minus removed, one by one every time is added up, that is to say: If you start fewer bits, but the ratio of the total number of enumeration large, is that you can achieve.

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define sf(x) scanf("%d",&x)
using namespace std;
 
typedef long long ll;
const int maxn = 300000;
const ll mod = 1e9+7;
 
int n,p;
int num[maxn+100],a[maxn+100];
map<ll,int>mp;
 
int main(){
    cin>>n>>p;
    FOR(i,1,maxn){
        int m=n-i*p,tmp=m;
        if(m<0)break;
        int num=0;
        while(m){
            if(m%2)num++;
            m/=2;
        }
        if(num<=i&&i<=tmp){
            cout<<i<<endl;
            return 0;
        }
    }
    puts("-1");
}
Published 203 original articles · won praise 17 · views 20000 +

Guess you like

Origin blog.csdn.net/mxYlulu/article/details/104072279