题目
https://acm.ecnu.edu.cn/contest/292/problem/E/思路参考
以2 2 3 3 3为例
两个集合 每个集合分别拿 0 1 2个数然后进行组合即可
很容易想到
1 2 4
1 3 9 27
然后有12种组合
但是要注意顺序 要求相邻的差一个质数
这时候就可以把前面结果用ans数组存起来
构造过程:
1
1 2
1 2 4
1 2 4 12
1 2 4 12 6
1 2 4 12 6 3
1 2 4 12 6 3 9
1 2 4 12 6 3 9 18
1 2 4 12 6 3 9 18 36
从ans的末尾依次乘以相应的pi
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <stack>
#include <math.h>
#include <vector>
using namespace std;
using LL = long long;
#define debug(x) cout<<#x<<": "<<(x)<<endl;
#define out(x) cout<<#x<<": "<<(x)<<" ";
int p[16];
int k[16];
int main() {
int n;
scanf("%d",&n);
LL maxL=1;
for(int i=0;i<n;i++){
scanf("%d", &p[i]);
scanf("%d", &k[i]);
maxL *= k[i] + 1;
}
vector<LL> ans;
ans.reserve(maxL);
ans.push_back(1);
for(int i = 0;i < k[0];i ++){
ans.push_back( ans[ans.size()-1] * p[0]);
}
for(int i=1;i<n;i++){
int alen = ans.size();
for(int j=0;j < k[i];j++){
int aend = ans.size() -1;
for(int k = 0; k < alen; k ++ ){
ans.push_back( ans[aend-k] * p[i]);
}
}
}
for(LL a:ans){
cout<<a<<endl;
}
return 0;
}
官方题解给了类似格雷码的构造过程,可能有异曲同工之妙?