跳到正文

题解

题解:AT_arc182_b [ARC182B] |{floor(A_i/2^k)}|

gw2204约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年8月12日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 2 分钟

版本

  1. v1当前

这题一看很复杂,可以从最终的答案下手,由于0是必然有的,所以不用判断。

可以从1开始,看得出来,它的贡献是0和1,让它乘2得到2,贡献就有0,1和2,再乘2,得到4,发现它的贡献是0,1,2,和4……这么下去,不难发现,每次的贡献都包含前一个的贡献,所以越大贡献就越多,要控制在 2K−12^K-1以内。

因为在做1的时候,2,4,8,这些数都被遍历了,为了避免重复,我们只需要从奇数开始乘2,要从小到大遍历奇数,因为这样贡献的因数就会尽可能多。如果还有多余的位置,都输出1就行了。

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, k;
int read() {
	int sum = 0, d = 1;
	char c;
	c = getchar();
	while(!isdigit(c)) d = c == '-' ? -d : d, c = getchar();
	while(isdigit(c)) sum = (sum << 3) + (sum << 1) + c - 48, c = getchar();
	return sum * d;
}
void write(int x) {
	if (x / 10 == 0) {
		if (x < 0) putchar('-');
		putchar(char(abs(x) % 10 + 48));
	}else {
		write(x / 10);
		putchar(char(abs(x) % 10 + 48));
	}
}
void work() {
	n = read(), k = read();
	int maxx = (1 << k) - 1, now = 0;
	for (int i = 1; now < n && i <= maxx; i += 2) {
		int j = i;
		while (j * 2 <= maxx) {
			j *= 2;
		}
		write(j);
		now++;
		putchar(' ');
	}
	while (now < n) {
		write(1);
		putchar(' ');
		now++;
	}
}
main() {
	int Time = read();
	while (Time--) {
		work();
		putchar('\n');
	}
	return 0;
} 
原文