跳到正文

个人记录

P9532 [YsOI2023] 前缀和 题解

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

文章信息

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

版本

  1. v1当前

Solution

设数组的第一个数为 mm,因为除了其中第一个数字以外其它数字都等于前面所有数字的和,所以第二个数就为 mm,第三个数为 m+m=2mm+m=2m,依此类推,从第二个数起,第 ii 个数为 2i−2m2^{i-2}m。 因为我们要让 ana_n 最小,所以要让 xx 在这个数组中的位置最靠后。于是就可以让 xx 不断除以 22,直到不是偶数为止,放在第一个。

Code

C++
#include<bits/stdc++.h>
#define int long long//记得开long long

using namespace std;

const int inf = 0x3f3f3f3f,mod = 1e9 + 7;
const int maxm = 20 + 7;

int t,n,x;

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> t;
	while(t--){
		cin >> n >> x;
		int cnt = 2,y = x;//第一个数和第二个数一样,cnt可以从2开始
		while(y % 2 == 0 && cnt <= n)//x中2的个数不能超过n
			y /= 2,cnt++;
		while(cnt < n)
			x *= 2,cnt++;
		cout << x << endl;
    }

	return 0;
}
原文