跳到正文

题解

题解:P13701 [NWERC 2023] Brickwork

q1uple约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年11月14日
收录
2026年11月18日
更新
2026年11月21日
篇幅
约 6 分钟
获赞
1
题目
P13701

版本

  1. v1当前

先判定可行性。定义 fif_i 表示是否能表示出 ii,这可以用多重背包,并用 bitset 优化,时间复杂度为 O(nmω)O\left(\frac{nm}{\omega}\right)。

接下来,从小到大枚举 aia_i,直接强行构造方案,然后将该方案反转。

考虑是否会出现重叠情况。若存在前缀相等,则不如将较长的前缀替换为较短的,因为我们并非从大到小构造。所以不合法当且仅当构造出的序列只有一个数。

如果 bb 中所有数相同,则再从大到小执行一遍该过程,记找到的序列为 cc。

  • 如果 cc 中不是所有数相同,则正、反各输出一次即可。
  • 如果 cc 中所有数也相同,并且与 bb 相等,则无解。
  • 剩下的情况不妨钦定 b>cb > c,此时显然有 b∣mb \mid m 且 c∣mc \mid m。

然后按如下方式构造:

  • 序列上方全部放 bb;
  • 下方第一个放 cc;
  • 接下来放 m−lcm⁡(b,c)b\frac{m - \operatorname{lcm}(b, c)}{b} 个 bb;
  • 最后放 lcm⁡(b,c)c−1\frac{\operatorname{lcm}(b, c)}{c} - 1 个 cc。
C++
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5,mod=998244353,INF=2e9;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
#define qmi(a,b) a=min(a,b)
#define qma(a,b) a=max(a,b)
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define atrep(i,l,r) for(int i=(r);i>=(l);i--)
#define vec vector<int>
#define pb push_back
namespace fast_IO {

} using namespace fast_IO;
int a[N],cnt[N];
int v[N*20],tot=0;
bitset<N>dp;
vector<int>S,T;
void solve(){
	int n,m;
	cin>>n>>m;
	rep(i,0,m)	dp[i]=0;
	tot=0;
	S.clear(),T.clear();
	map<int,int>mp;
	rep(i,1,n){
		cin>>a[i];
		if(!mp[a[i]]) cnt[i]=m/a[i];
		mp[a[i]]=1;
	}
	rep(i,1,n){
		if(a[i]==m){
			cout<<"possible"<<endl;
			cout<<1<<endl;
			cout<<a[i]<<endl;
			cout<<1<<endl;
			cout<<a[i]<<endl;
			return ;
		}
	}
	rep(i,1,n){
		for(int j=1;j<=cnt[i];j*=2){
			v[++tot]=a[i]*j;
			cnt[i]-=j;
		}
		if(cnt[i]){
			v[++tot]=a[i]*cnt[i];
		}
	}
	dp[0]=1;
	rep(i,1,tot){
		dp|=dp<<v[i];
	}
	if(dp[m]==0){
		cout<<"impossible"<<endl;
	}else{
		sort(a+1,a+n+1);
		int tmpm=m;
		int cnt=0;
		rep(i,1,n){
			int fl=0;
			while(tmpm-a[i]>=0&&dp[tmpm-a[i]]){
				fl=1;
				tmpm-=a[i];
				S.push_back(a[i]);
			}
			cnt+=fl;
		}
		if(cnt>1){
			T=S;reverse(T.begin(),T.end());
			cout<<"possible"<<endl;
			cout<<S.size()<<endl;
			for(auto it:S){
				cout<<it<<" ";
			}
			cout<<endl;
			cout<<T.size()<<endl;
			for(auto it:T){
				cout<<it<<" ";
			}
			cout<<endl;
			return;
		}else{
			int cnt=0;
			int tmpm=m;
			atrep(i,1,n){
				int fl=0;
				while(tmpm-a[i]>=0&&dp[tmpm-a[i]]){
					fl=1;
					tmpm-=a[i];
					T.push_back(a[i]);
				}
				cnt+=fl;
			}
			if(cnt>1){
				vector<int> F=T;reverse(F.begin(),F.end());
				cout<<"possible"<<endl;
				cout<<T.size()<<endl;
				for(auto it:T){
					cout<<it<<" ";
				}
				cout<<endl;
				cout<<F.size()<<endl;
				for(auto it:F){
					cout<<it<<" ";
				}
				cout<<endl;
				return ;
			}
			if(S[0]==T[0]){
				cout<<"impossible"<<endl;
				return;
			}
			int b=S[0],c=T[0];
			int lc=b/__gcd(b,c)*c;
			if(b<c)	swap(b,c),swap(S,T);
			cout<<"possible"<<endl;
			cout<<S.size()<<endl;
			for(auto it:S){
				cout<<it<<" ";
			}
			cout<<endl;
			T.clear();
			T.push_back(c);
			rep(i,1,(m-lc)/b)	T.push_back(b);
			rep(i,1,lc/c-1)	T.push_back(c);
			cout<<T.size()<<endl;
			for(auto it:T){
				cout<<it<<" ";
			}
			cout<<endl;
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	int Tz=1;
	// cin>>Tz;
	rep(i,1,Tz){
		solve();
	}
}
原文