이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define vec vector
#define sz(x) ( int)x.size()
#define m_p make_pair
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
typedef long long ll;
typedef pair<ll, int> pli;
typedef pair<int,ll> pil;
typedef pair<int,int> pii;
typedef unsigned long long ull;
auto rng=bind(uniform_int_distribution<int>(1,1000),mt19937(time(0)));
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
const int Mx=1e3;
const int N=20;
//vec<int> w[Mx];
int dp[21][1<<N];
int a[N],b[N],n,m;
vec<int>z[1<<N];
void rec(int i,int x,int mask){
if(i==n){
cout<<"YES";
exit(0);
}
if(x==0){
return rec(i+1,a[i+1],mask);
}
if(dp[i][mask]) return;
dp[i][mask]=1;
for(auto &j : z[mask]){
if(b[j]<=x){
rec(i,x-b[j],mask|(1<<j));
}
}
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// int n,m;
// n=20;m=20;
cin>>n>>m;
for(int i=0;i<n;i++) cin>>a[i];
for(int j=0;j<m;j++){
cin>>b[j];
}
for(int i=0;i<(1<<m);i++){
for(int j=0;j<m;j++){
if(i&(1<<j)) continue;
z[i].pb(j);
}
}
rec(0,a[0],0);
cout<<"NO";
return 0;
}
/*
1 5
8
4 2 5 1 3
2 4
4 5
2 2 1 4
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |