이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
using namespace std;
#define F first
#define S second
typedef long long ll;
#define pii pair <int, int>
#define pll pair <ll, ll>
typedef long double ld;
const ll N = 1e6 + 1e5 + 10;
const ll mod = 998244353;
ll um(ll a, ll b){
return ((1LL * a * b) % mod + mod) % mod;
}
ll subr(ll a, ll b){
return ((1LL * a - b) % mod + mod) % mod;
}
int a[N], b[N], n, m;
bool dp[N], base[N], nw[N], rl[N];
void find(int mask, int index, int sum){
if(index == m){
if(dp[sum] == false) return;
if((mask ^ sum) == (mask | sum)) nw[sum | mask] = true;
return;
}
if((mask & (1 << index)) == 0){
find(mask, index + 1, sum + (1 << index));
}
find(mask, index + 1, sum);
}
void subtask1(){
dp[0] = true;
for(int i = 0; i < m; i++){
for(int j = a[0]; j >= b[i]; j--){
dp[j] |= dp[j - b[i]];
}
}
if(dp[a[0]]) cout << "YES";
else cout << "NO";
}
void subtask2(){
base[0] = 1;
for(int i = 0; i < n; i++){
for(int mask = 0; mask < (1 << m); mask++){
int hs = 0;
for(int dig = 0; dig < m; dig++){
if((mask & (1 << dig)) > 0) hs += b[dig];
}
if(hs == a[i]) dp[mask] = true;
}
for(int mask = 0; mask < (1 << m); mask++){
if(!base[mask]) continue;
find(mask, 0, 0);
}
for(int mask = 0; mask < (1 << m); mask++){
for(int dig = 0; dig < m; dig++){
if((mask & (1 << dig)) > 0){
rl[mask] |= rl[mask ^ (1 << dig)];
if(nw[mask] && rl[mask ^ (1 << dig)]) nw[mask] = false;
}
}
if(nw[mask]) rl[mask] = true;
}
for(int j = 0; j < (1 << m); j++){
base[j] = nw[j];
nw[j] = false;
dp[j] = false;
rl[j] = false;
}
}
bool can = false;
for(int i = 0; i < (1 << m); i++){
if(base[i]) can = true;
}
if(can) cout << "YES\n";
else cout << "NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
//freopen("bank.in", "r", stdin);
//freopen("bank.out", "w", stdout);
cin >> n >> m;
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < m; i++){
cin >> b[i];
}
if(m < n){
cout << "NO";
return 0;
}
if(n == 1){
subtask1();
} else{
subtask2();
}
return 0;
}
# | 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... |