首页 > 其他 > 详细

CodeForces - 1225C p-binary(思维)

时间:2019-11-09 21:48:29      阅读:94      评论:0      收藏:0      [点我收藏+]

题目链接: http://codeforces.com/problemset/problem/1225/C

思路

把所有的p移回左边得到一个数, 此时它应该有多个\({2^k}\) 组成 然后我们就可以数出这个数的二进制及有多少个1 判断它是不是符合题意

Code

#include <bits/stdc++.h>
 
using namespace std;
#define LL long long
#define popcount __builtin_popcount
 
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int n, p;
    cin >> n >> p;
    for (int i = 1; i < 100000000; ++i) {
        int num = n - p * i;
        if(num < i) break;
        if (i >= popcount(num)) {
            cout << i << endl;
            return 0;
        }
    }
    cout << "-1" << endl;
    return 0;
}

CodeForces - 1225C p-binary(思维)

原文:https://www.cnblogs.com/YY666/p/11827845.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!