子集和
题目大意
你有 \(n\) 个正整数 \(a_1,a_2,\cdots,a_n\), 它们的和是 \(m\)。你想对它们的每个子集 \(S\), 求出它们的和。
现在你得到了 \(2^n\) 个 \([0,m]\) 之间的和, 其中数字 \(i\) 出现了 \(b_i\) 次。
现在给你数组 \(b_i\), 请还原 \(a_1,a_2,\cdots,a_n\) 这些数。
题目分析
我们不妨用一个辅助数组 \(t\) 来辅助计数,我们的目标是 \(\forall i\in[1,m],b_i=t_i\)。
若 \(t_i\ge b_i\),则将 \(i\) 加入当前的 \(a\) 序列;
否则若 \(t_i\lt b_i\),那么我们不停地更新:\(t_{i+j}=t_{i+j}+t_j\)。
代码
#include <iostream>
#include <cstdio>
#include <climits>//need "INT_MAX","INT_MIN"
#include <cstring>//need "memset"
#include <numeric>
#include <algorithm>
#include <cmath>
#define enter putchar(10)
#define debug(c,que) std::cerr << #c << " = " << c << que
#define cek(c) puts(c)
#define blow(arr,st,ed,w) for(register int i = (st);i <= (ed); ++ i) std::cout << arr[i] << w;
#define speed_up() std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0)
#define mst(a,k) memset(a,k,sizeof(a))
#define stop return(0)
const int mod = 1e9 + 7;
inline int MOD(int x) {
if(x < 0) x += mod;
return x % mod;
}
namespace Newstd {
char buf[1 << 21],*p1 = buf,*p2 = buf;
inline int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1 << 21,stdin),p1 == p2) ? EOF : *p1 ++;
}
inline int read() {
int ret = 0,f = 0;char ch = getc();
while (!isdigit(ch)) {
if(ch == '-') f = 1;
ch = getc();
}
while (isdigit(ch)) {
ret = (ret << 3) + (ret << 1) + ch - 48;
ch = getc();
}
return f ? -ret : ret;
}
inline double double_read() {
long long ret = 0,w = 1,aft = 0,dot = 0,num = 0;
char ch = getc();
while (!isdigit(ch)) {
if (ch == '-') w = -1;
ch = getc();
}
while (isdigit(ch) || ch == '.') {
if (ch == '.') {
dot = 1;
} else if (dot == 0) {
ret = (ret << 3) + (ret << 1) + ch - 48;
} else {
aft = (aft << 3) + (aft << 1) + ch - '0';
num ++;
}
ch = getc();
}
return (pow(0.1,num) * aft + ret) * w;
}
inline void write(int x) {
if(x < 0) {
putchar('-');
x = -x;
}
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
}
using namespace Newstd;
const int N = 10005;
int a[N],b[N],cnt[N];
int n,m,idx;
int main(void) {
n = read(),m = read();
for (register int i = 0;i <= m; ++ i) b[i] = read();
cnt[0] = 1;
for (register int i = 1;i <= m; ++ i) {
while (b[i] > cnt[i]) {
a[++ idx] = i;
if (idx == n) break;
for (register int j = m;j >= i; -- j) cnt[j] += cnt[j - i];
}
if (idx == n) break;
}
for (register int i = 1;i <= n; ++ i) printf("%d%c",a[i]," \n"[i == n]);
return 0;
}

浙公网安备 33010602011771号