首页 > 其他分享 >Codeforces Round #596 C. p-binary

Codeforces Round #596 C. p-binary

时间:2023-02-03 10:03:57浏览次数:51  
标签:binary typedef 596 int ll Codeforces long ans include

Codeforces Round #596 C. p-binary_其他

Codeforces Round #596 C. p-binary_ios_02

给定N和p,让你找到满足2^x+p最少有多少不同的项。

就把N转成二进制然后枚举P的个数就是答案,昨天特判没写好,今天早上起来发现被卡掉了。rank又出1000了。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<set>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF = 1e9;
int main()
{
    ll n,p,ans=1;
    cin>>n>>p;
    while(n-ans*p>0)
    {
        if(__builtin_popcountll(n-ans*p)<=ans)//统计二进制中1的个数
        {
            if(n-ans*p>=ans)
            {
                cout<<ans<<endl;
                return 0;
            }
        }
        ans++;
    }
    cout<<-1<<endl;
    return 0;
}

 

标签:binary,typedef,596,int,ll,Codeforces,long,ans,include
From: https://blog.51cto.com/u_15952369/6034927

相关文章

  • Educational Codeforces Round 75 D. Salary Changing(二分)
    题意就是:给n个人发工资,总钱数为s。每个人有一个工资范围。要求一个发工资方案,使得工资中位数最大,求这个中位数。考虑到中位数最大,于是我们可以二分。但是每个人的工资......
  • Educational Codeforces Round 75 C Minimize The Integer
    这道题的意思就是给出一个由数字组成的字符串,相邻的数字可以互换位置,但是如果相邻的为同奇同偶这样就不能交换。让我们求交换任意次数可以产生的最小数。这条限制就是说......
  • codeforces 595 C2. Good Numbers (hard version)
    给出Q组查询,每组给出一个N找到一个>=n的m,m可以分解成不同的3的幂次相加。可以看题意解释,就是转化为3^0,3^1,...,3^m,每个只能出现最多一次,但是可以不同组合,输出符合条件最......
  • codeforces 595 B2 Books Exchange (hard version)
    这道题的意思就是有n本书,每本书都有自己的编号,每次可以移动一本书,把这个本书移动到当前编号对应的位置,求移动几次可以使得编号和位置对应起来。比如样例32312第一......
  • [LeetCode] 1145. Binary Tree Coloring Game
    Twoplayersplayaturnbasedgameonabinarytree.Wearegiventhe root ofthisbinarytree,andthenumberofnodes n inthetree. n isodd,andeach......
  • Luogu5435 基于值域预处理的快速 GCD & Leetcode2543 - binary GCD -
    题目链接:https://www.luogu.com.cn/problem/P5435请忽略题目名称学到一个科技:binaryGCD,能够快速求出两个数GCD(从这道题来看已经接近\(O(1)\)了)代码://bySkyRainW......
  • Codeforces Round #848 (Div. 2)(持续更新)
    Preface这场打的可以说发挥了水平但没有完全发挥的说ABC都是一眼一遍过,结果D是我最怕的期望题,大概推了个DP的式子但感觉成环了初始条件又不够(flag)就弃了本来当时看E只有......
  • Codeforces Round #845 (Div. 2) and ByteRace 2023(A,B,C)
    CodeforcesRound#845(Div.2)andByteRace2023(A,B,C)AA这个题目大意是有n个数,我们需要让ai和ai+1的奇偶性不一样我们可以让相邻的两个数变成一个数,那个数就是这两个......
  • 2.2 vp Codeforces Round #848 (Div. 2)
    A-FlipFlopSum题意给出长度为n的序列a,a中只包括1和-1,你必须操作一次,让相邻两个元素由1变-1或由-1变1,问操作后数组总和最大多少思路暴力即可voidsolve(){ intn......
  • Codeforces Round #848 (Div. 2)
    题目链接A核心思路按照题目模拟就好了//Problem:A.FlipFlopSum//Contest:Codeforces-CodeforcesRound#848(Div.2)//URL:https://codeforces.com/cont......