AtCoder Beginner Contest 340C - Divide and Divide

problem link

Naively, a brute force recursion solution be implemented with O ( n ) \mathcal O (n) O(n) complexity.

复制代码
int work(int x)
{
	if(x==1)return 0;
	return x+work(x>>1)+work((x>>1)+(x&1))
}

However, since all possible x x x can be represented as n ⋅ 2 − k + [ 0 / 1 ] n\cdot 2^{-k}+[0/1] n⋅2−k+[0/1], the number of possible x x x does not exceed 2 ⋅ log ⁡ 2 ( n ) 2\cdot \log_2(n) 2⋅log2(n)

Then, we can intuitively implement a memorization search with map.

cpp 复制代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
map <long long,long long> f;
long long n,ans;
long long work(long long x)
{
	if(x==1)return 0;
	if(f[x])return f[x];
	return f[x]=x+work(x>>1)+work((x>>1)+(x&1));
}
int main()
{
	cin>>n;
	cout<<work(n)<<endl;
	return 0;
}
相关推荐
CHANG_THE_WORLD1 小时前
金字塔降低采样
算法·金字塔采样
不知天地为何吴女士3 小时前
Day32| 509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯
算法
小坏坏的大世界3 小时前
C++ STL常用容器总结(vector, deque, list, map, set)
c++·算法
励志要当大牛的小白菜6 小时前
ART配对软件使用
开发语言·c++·qt·算法
qq_513970446 小时前
力扣 hot100 Day56
算法·leetcode
PAK向日葵7 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
爱喝矿泉水的猛男9 小时前
非定长滑动窗口(持续更新)
算法·leetcode·职场和发展
YuTaoShao9 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先
YouQian77210 小时前
Traffic Lights set的使用
算法
go546315846511 小时前
基于深度学习的食管癌右喉返神经旁淋巴结预测系统研究
图像处理·人工智能·深度学习·神经网络·算法