• Codeforces 1247C. p-binary


    传送门

    首先 $n=sum_{i=1}^{ans}(2^{x_{ans}}+p)$ 可以变成 $n-ans cdot p=sum_{i=1}^{ans}2^{x_{ans}}$

    注意到如果 $n-ans cdot p$ 二进制下 $1$ 的个数等于 $ans$ ,那么一定有解

    (只要把 $x_{ans}$ 和 $n-ans cdot p$ 二进制下 $1$ 的位置一一对应即可)

    然后可以发现如果二进制下 $1$ 的个数小于 $ans$ 也有解,因为只要把某些比较大的 $x_{ans}$ 拆成两个 $x_{ans}-1$ 即可

    然后你就愉快地过了 $pretest$ ,于是就 $fst$ 了...

     $hack$ 数据: $ ext{9 4}$ ,答案是 $-1$ 但是输出 $2$

    因为没有注意到当 $n-ans cdot p$ 很小的时候(小于 $ans$),就算 $x_{ans}$ 全都是 $0$

    $sum_{i=1}^{ans}2^0>n-ans cdot p$ ,那么此时无解

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    inline int read()
    {
        int x=0,f=1; char ch=getchar();
        while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
        while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
        return x*f;
    }
    ll n,P;
    int main()
    {
        n=read(),P=read();
        for(int i=1;i<=32;i++)
        {
            n-=P;
            if(n<i) { cout<<-1<<endl; return 0; }
            int cnt=0; ll now=n;
            while(now) cnt+=now&1,now>>=1;
            if(cnt<=i) { cout<<i<<endl; return 0; }
        }
        cout<<-1<<endl;
        return 0;
    }
  • 相关阅读:
    Vue
    linux-----docker
    linux基础
    Flask基础
    websocket
    css
    Mysql数据库基础
    IO多路复用
    线程和协程
    sh_02_del关键字
  • 原文地址:https://www.cnblogs.com/LLTYYC/p/11748392.html
Copyright © 2020-2023  润新知