https://www.luogu.com.cn/record/83499080
https://www.luogu.com.cn/record/83499132
RT,实在不理解O2原理(P1021),代码如下:
#include <bits/stdc++.h>
using namespace std;
int n,k,a[17],ans,fangan[17],con;
bool pan[1005],pan2[17][1005];
void dfs2(int t,int s){
if(pan2[t][s]) return;
pan[s]=1;
pan2[t][s]=1;
// cout<<t<<" "<<s<<'\n';
if(t==n+1) return;
for(int i=1;i<=k;i++){
dfs2(t+1,s+a[i]);
}
}
int check(){
fill(pan,pan+1005+1,0);
fill(pan2[0],pan2[0]+17*1005,0);
dfs2(1,0);
int cnt=0;
while(pan[cnt+1]){
cnt++;
}
return cnt;
}
void dfs(int t,int now){
int x=check();
con++;
if((ans>0&&now>ans)||(t>1&&x==0)) return;
// cout<<t<<" "<<now<<" "<<x<<'\n';
if(t==k+1){
// for(int i=1;i<=k;i++){
// cout<<a[i]<<" ";
// }
// cout<<'\n';
if(x>ans){
ans=x;
for(int i=1;i<=k;i++){
fangan[i]=a[i];
}
}
return;
}
a[t]=now;
dfs(t+1,now+1);
dfs(t,now+1);
}
int main(){
cin>>n>>k;
dfs(1,1);
// cout<<con<<'\n';
for(int i=1;i<=k;i++){
cout<<fangan[i]<<" ";
}
cout<<"\nMAX="<<ans;
return 0;
}