이 제출은 이전 버전의 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 = 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];
void calc(int goal, int cur, int sum, int hs){
if(sum == goal){
dp[hs] = true;
return;
}
if(cur == m) return;
if(sum + b[cur] <= goal){
calc(goal, cur + 1, sum + b[cur], hs + (1 << cur));
}
calc(goal, cur + 1, sum, hs);
}
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++){
calc(a[i], 0, 0, 0);
for(int j = 0; j < (1 << m); j++){
for(int mask = 0; mask < (1 << m); mask++){
if(!dp[j] || !base[mask]) continue;
if((mask ^ j) == (mask | j)){
// cout << j << " "<< mask << endl;
nw[j | mask] = true;
}
}
}
for(int j = 0; j < (1 << m); j++){
base[j] = nw[j];
nw[j] = false;
dp[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(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... |