結果

問題 No.4 おもりと天秤
ユーザー darisdaris
提出日時 2022-04-30 12:39:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 4,011 bytes
コンパイル時間 2,270 ms
コンパイル使用メモリ 214,288 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-29 18:18:31
合計ジャッジ時間 2,712 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 3 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if !__INCLUDE_LEVEL__

#include __FILE__

template<class T>
int lower(V<T> &a, T &k) {
  int ok = a.size(), ng = -1;
  while(abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if(a[mid] >= k) ok = mid;
    else ng = mid;
  }
  return ok;
}

template<class T>
int upper(V<T> &a, T &k) {
  int ok = a.size(), ng = -1;
  while(abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if(a[mid] > k) ok = mid;
    else ng = mid;
  }
  return ok;
}

class FactorialMod {
  
  void calc_inverse() {
    inverse[0] = 0;
    inverse[1] = 1;
    rep(i, 2, max_num + 1) {
      inverse[i] = mod - ((mod / i) * inverse[mod % i] % mod);
    }
  }
    
  void calc_factorial_inverse() {
      factorial[0] = factorial_inverse[0] = 1;
      rep(i, 1, max_num + 1) {
        factorial[i] = (factorial[i - 1] * i) % mod;
        factorial_inverse[i] = (factorial_inverse[i - 1] * inverse[i]) % mod;
      }
    }
    
 public:  
    int max_num;
    int mod;
    vll inverse;
    vll factorial;
    vll factorial_inverse;
    
    FactorialMod(int _max_num, int _mod) {
      max_num = _max_num;
      mod = _mod;
      inverse = vll(max_num + 1);
      factorial = vll(max_num + 1);
      factorial_inverse = vll(max_num + 1);
      calc_inverse();
      calc_factorial_inverse();
    }
    
   ll conbination_mod(int n, int k) {
    if(min(n, k) < 0 || max(n, k) > max_num || k > n) return 0;
    return (((factorial[n] * factorial_inverse[k]) % mod) * factorial_inverse[n - k]) % mod;
  }
  
  ll multi_choose_mod(int n, int k) {
    return conbination_mod(n + k - 1, k);
  }
};

int main() {
  int n; cin >> n;
  vi w(n);
  int sum = 0;
  rep(i, 0, n) {
    cin >> w[i];
    sum += w[i];
  }
  if(sum % 2 == 1) {
    print("impossible");
    return 0;
  }
  vvi dp(n + 1, vi(sum / 2 + 1, 0));
  dp[0][0] = 1;
  rep(i, 0, n) {
    rep(j, 0, sum / 2 + 1) {
      if(dp[i][j] == 1) {
        dp[i + 1][j] = 1;
        if(j + w[i] <= sum / 2) {
          dp[i + 1][j + w[i]] = 1;
        }
      }
    }
  }
  print(dp[n][sum / 2] ? "possible" : "impossible");
  return 0;
}
/*

*/

#else
#include <bits/stdc++.h>
using namespace std;

#define _GLIBCXX_DEBUG 
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, j, n) for(int i = j; i < n ; i++)

template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using psi = pair<string, int>;
using pll = pair<ll, ll>;
using vi = V<int>;
using vd = V<double>;
using vc = V<char>;
using vs = V<string>;
using vll = V<ll>;
using vld = V<ld>;
using vvi = V<vi>;
using vvd = V<vd>;
using vvll = V<vll>;
using vvld = V<vld>;
using vvc = V<vc>;

//const ll mod = 998244353;
const ll mod = 1000000007;

template<typename T> bool chmax(T& a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<typename T> bool chmin(T& a, const T &b) { if (a > b) { a = b; return true; } return false; }
template<class... T> void input(T&... a) { (cin >> ... >> a); }
template<class T> void print(const T &a) { cout << a << '\n'; }
template<class T, class... Ts> void print(const T& a, const Ts&... b) { cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; }

ll power(ll n, ll k) { ll res = 1; while(k) { if(k & 1) res *= n; n *= n; k >>= 1; } return res; }
ll power_mod(ll n, ll k) { ll res = 1; while(k) { if(k & 1) res = res * n % mod; n = n * n % mod; k >>= 1; } return res; }
bool is_prime(ll n) { if(n == 1) return 0; for(ll i = 2; i * i <= n; i++) if(n % i == 0) return 0; return 1; }
V<ll> enum_divisors(ll n) { V<ll> res; for(ll i = 1; i * i <= n; i++)  if(n % i == 0) { res.push_back(i); if(n / i != i) res.push_back(n / i); }  sort(all(res)); return res; }
V<pll> prime_factorize(ll n) { V<pll> res; for(ll i = 2; i * i <= n; i++) { if(n % i != 0) continue; ll ex = 0; while(n % i == 0) { ex++; n /= i; } res.push_back({i, ex}); } if(n != 1) res.push_back({n, 1}); return res; }

const int inf = 1LL << 30;
const ll infl = 1LL << 60;
#endif
0