codeforces 596 C. p-binary

Meaning of the questions: to give you a a n and p, let you use (2 k + the p-) decimal to represent n, to find out with a minimum of (2 k + the p-) to represent n.

 

Analysis: First, we see 2 k , at first thought the binary, we can list our formula, that is,

 

   (2x1 + p)+(2x+ p)+(2x+ p)+……+(2x+ p) == n

  

   We then converted to a 2 X . 1  +2 X +2 X . 3  + ...... + 2 X K == N- m * P

   Such problem is converted to a minimum value required number m.

   Let us analyze, since the data are given n-<10 . 9   , so that m <32.

   So that we can size from small to large enumeration m of violence.

   Finally, the determination condition is  binary (nm * p) in the number of 1 <&& m = m <= P * nm  , then m is the answer.

   Dir explain a first determination condition because binary, the upper 1 may be decomposed into two low a 1, so if (nm * p) the number of binary 1 <m, then (nm * p) of binary 1 in the high decomposition can continue until equal to m

   Under the second determination condition explain binary due, minimum is two 0 , i.e. a decimal, so that a maximum decomposition (nm * p) is a binary (nm * p) Items 2 0 , the m <= nm * p.

  

//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;
typedef double dou;
typedef long long ll;
typedef pair<int, int> pii;
typedef map<int, int> mii;

#define pai acos(-1.0)
#define M 1000005
#define inf 0x3f3f3f3f
#define mod 1000000007
#define IN inline
#define W(a) while(a)
#define lowbit(a) a&(-a)
#define left k<<1
#define right k<<1|1
#define lson L, mid, left
#define rson mid + 1, R, right
#define ms(a,b) memset(a,b,sizeof(a))
#define Abs(a) (a ^ (a >> 31)) - (a >> 31)
#define random(a,b) (rand()%(b+1-a)+a)
#define false_stdio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

int n, p;

int main() {
    false_stdio;
    cin >> n >> p;
    for (int i = 0; i < 32; I ++ ) {
         int tmp = n-- I * P;
         IF (__builtin_popcount (tmp) <&& I = I <= tmp) { // __builtin_popcount gcc built-in function is used for calculating the number of binary 1 
            cout < <I << endl;    
             return  0 ; 
        } 
    } 
    COUT << - . 1 << endl; 

    return  0 ; 
}

 

 

   

Guess you like

Origin www.cnblogs.com/caibingxu/p/11779499.html