跳到正文

题解

题解:U522653 指数函数计算器 (官方)

FlowerAccepted约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
获赞
2

版本

  1. v1当前

题目传送门

解题思路

注意到,由于没有重复的 kik_i,所以 2ki2^{k_i} 正好对应 xx 二进制下的第 kik_i 且为 11 的位。

于是我们首先将 xx 转化为二进制,然后枚举它的第 ii 位,同时将答案加上对应二进制位的位权即可。

可以在转换进制时直接倒序操作更便捷,注意位权从 00 开始。

数据较大,对于 C++ 语言需要开 unsigned long long。

代码呈现

C++

C++
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring> // 不用万能头
using namespace std;

int main() {
    unsigned long long x, ans = 0; // 开ull
    cin >> x;
    for (int i = 0; x; i ++) { // 从 0 开始
        ans += i * (x % 2);
        x /= 2;
    }
    cout << ans;
    return 0;
}

Python

Python
x = int(input())
ans = i = 0

while x:
    ans += i * (x % 2)
    x //= 2
    i += 1

print(ans)

复杂度分析

程序的复杂度是 O(log⁡x)O(\log x)。

其他题解

如果你有其它解法,欢迎私信我,优秀的解法会在下方附上链接。欢迎投稿,私信时为方便,请按

lgchat
/commit sol U522653 <题解文章(建议)/剪贴板(不建议)链接>

的格式。

目前还没有其他题解,快来投稿哇!

原文