AT_abc340_c 题解

· · 题解

简简单单记忆化搜索而已。在这题中,因为 数据大,不能直接用数组记忆化,所以我们可以用 map。

代码

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
map <ll, ll> f;
ll dfs (ll x) {
    if (f[x])
        return f[x];
    if (x < 2)
        return 0;
    return f[x] = x + dfs (x >> 1) + dfs (x + 1 >> 1);
}
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> n;
    cout << dfs (n);
    return 0;
}