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] n2k+[0/1], the number of possible x x x does not exceed 2 ⋅ log ⁡ 2 ( n ) 2\cdot \log_2(n) 2log2(n)

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

#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;
}

相关推荐

  1. ABC340(A-C)

    2024-02-12 05:50:03       53 阅读
  2. AT_abc348_c [ABC348C] Colorful Beans 题解

    2024-02-12 05:50:03       26 阅读
  3. 360 C++ 面试真题

    2024-02-12 05:50:03       49 阅读
  4. AtCoder Beginner Contest 340C - Divide and Divide

    2024-02-12 05:50:03       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-12 05:50:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-12 05:50:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-12 05:50:03       87 阅读
  4. Python语言-面向对象

    2024-02-12 05:50:03       96 阅读

热门阅读

  1. Flask基础学习2

    2024-02-12 05:50:03       53 阅读
  2. 6. 尚硅谷大数据111门技术+42个项目

    2024-02-12 05:50:03       55 阅读
  3. 刘谦春晚魔术解析Python

    2024-02-12 05:50:03       57 阅读
  4. 跟我一起学python 4.1 /20

    2024-02-12 05:50:03       53 阅读
  5. 从Unity到Three.js(画线组件line)

    2024-02-12 05:50:03       58 阅读
  6. 1103: 地盘划分(New Online Judge)

    2024-02-12 05:50:03       46 阅读