Let life be beautiful li|

Ginger_he

园龄:3年1个月粉丝:5关注:5

洛谷P2558题解

本文同步更新于洛谷博客

题目描述

求由多个互不相等的 k 的方幂之和构成的第 p 小的数。

题解

因为题目要求选择互不相等的 k 的方幂之和,所以每种方幂只有选或不选两种情况。我们用 ai 表示是否选 ki,则 2ai=p。因此我们在二进制下枚举 p 的每一位即可,记得用 __int128高精度

Code

#include<bits/stdc++.h>
using namespace std;
#define int __int128
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;
}
void write(int x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}
int k,p,res,ans;
signed main()
{
    k=read(),p=read();
    for(int i=0;i<=11;i++)
    {
        if(!i) res=1;
        else res*=k;
        if(p&(1<<i))
            ans+=res;
    }
    write(ans);
    return 0;
}

本文作者:Ginger_he

本文链接:https://www.cnblogs.com/Gingerhe/p/15924894.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ginger_he  阅读(66)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起