跳到正文

个人记录

P1582 倒水

#SUSPENDED#约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月16日
篇幅
约 1 分钟

版本

  1. v1当前

错误原因

进位只判了一次,二进制数组只遍历了一遍。

思路

将 nn 转化为二进制数,每次将位置最前的 11 加上一,代价是 2i2^i ,重复操作知道 11 的数量不超过 kk .

代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k;
int a[51];
bool check(){
	int cnt=0;
	for(int i=0;i<=32;i++){
		if(a[i]==1){
			cnt++;
		}
	}
	return cnt<=k;
}
int ans;
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>k;
	a[0]=n;
	for(int i=0;i<=32;i++){
		a[i+1]+=a[i]/2;
		a[i]%=2;
	}
	while(!check()){
		for(int i=0;i<=32;i++){
			if(a[i]==1){
				ans+=pow(2,i);
				a[i]++;
				for(int j=0;j<=32;j++){
					a[j+1]+=a[j]/2;
					a[j]%=2;
				}
				break;
			}
		}
	}
	cout<<ans;
	return 0;
}
原文