洛谷P2320 [HNOI2006]鬼谷子的钱袋

本文详细解析了洛谷2320题的解题思路,通过数学思维和分治思想,将复杂的问题简化为一系列更小的子问题,最终得出解决方案。代码实现简洁高效,适合算法初学者和进阶者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.luogu.org/problem/show?pid=2320#sub

题目描述全是图

 

数学思维,分治思想

假设总数为n

从n/2+1到n的数都可以用1~n的数+n/2表示出来

1~n/2的数也可以这样拆分成两份。

一路拆下去即可。

例如n=12时:

{1 2 3 4 5 6}+6={7,8,9,10,11,12}

{1,2,3}+3={4,5,6}

{1,2}+3={4,5}

{1}+2={3}

所以只需要1 2 3 6

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 const int mxn=100010;
10 int a[mxn];
11 int n;
12 int main(){
13     cin>>n;
14     int cnt=0;
15     while(n){
16         a[++cnt]=(n+1)/2;
17         n/=2;
18     }
19     printf("%d\n",cnt);
20     for(int i=cnt;i;i--)printf("%d ",a[i]);
21     return 0;
22 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/6040002.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值