結果

問題 No.941 商とあまり
ユーザー pekempeypekempey
提出日時 2019-12-04 22:43:33
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,133 ms / 2,000 ms
コード長 4,495 bytes
コンパイル時間 3,276 ms
コンパイル使用メモリ 196,516 KB
実行使用メモリ 10,312 KB
最終ジャッジ日時 2024-12-14 13:30:50
合計ジャッジ時間 21,881 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 104
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
 
#define rep(i, n)      for (int i = 0; i < (n); i++)
#define repr(i, n)     for (int i = (n) - 1; i >= 0; i--)
#define repe(i, l, r)  for (int i = (l); i < (r); i++)
#define reper(i, l, r) for (int i = (r) - 1; i >= (l); i--)
#define repi(i, l, r)  for (int i = (l); i <= (r); i++)
#define repir(i, l, r) for (int i = (r); i >= (l); i--)
#define range(a) a.begin(), a.end()
void initio() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }

bool can[500001];

set< vector<ll> > st;

bitset<500001> dp[1 << 11];
bitset<500001> checked[1 << 11];
int N;
ll A[500010];
ll prod[1 << 11];
int root[1 << 11];
int cnt;
ll X;

clock_t beg;

void solve2() {
  vector<bitset<500001>> bs(N);
  rep(i, N) {
    for (int j = 0; j <= X; j += A[i]) bs[i][j] = true;
  }
  int U = (1 << N) - 1;
  repe(i, 1, 1 << N) {
    if (root[i] != i) continue;
    if ((i & i - 1) == 0) {
      dp[i][prod[i] - 1] = true;
      continue;
    }
    rep(j, N) if (i >> j & 1) {
      if (root[i ^ 1 << j] != (i ^ 1 << j)) continue;
      for (ll k = prod[i ^ 1 << j] - 1; (k + 1) * prod[U ^ i ^ (1 << j)] - 1 <= X && k < prod[i ^ 1 << j] - 1 + 20*A[j]; k++) if (dp[i ^ 1 << j][k]) {
        dp[i] |= bs[j] << ((k + 1) * (A[j] + 1) - 1);
        // for (ll l = (k + 1) * (A[j] + 1) - 1; (l + 1) * prod[U ^ i] - 1 <= X; l += A[j]) {
        //    dp[i][l] = true;
        // }
      }
    }
  }
  string ans(X, '0');
  for (int i = 1; i <= X; i++) {
    ans[i - 1] = dp[U][i] ? '1' : '0';
  }
  cerr << count(range(ans), '1') << endl;
  cout << ans << endl;
}

void solve() {
  vector<bitset<500001>> bs(N);
  rep(i, N) {
    for (int j = 0; j <= X; j += A[i]) bs[i][j] = true;
  }
  int U = (1 << N) - 1;
  repe(i, 1, 1 << N) {
    if (root[i] != i) continue;
    if ((i & i - 1) == 0) {
      dp[i][prod[i] - 1] = true;
      continue;
    }
   
    rep(j, N) if (i >> j & 1) {
      if (root[i ^ 1 << j] != (i ^ 1 << j)) continue;
      for (ll k = prod[i ^ 1 << j] - 1; (k + 1) * prod[U ^ i ^ (1 << j)] - 1 <= X; k++) if (dp[i ^ 1 << j][k]) {
         if ((double)(clock() - beg) / CLOCKS_PER_SEC >= 1.1) {
          solve2();
          exit(0);
        }
        dp[i] |= bs[j] << ((k + 1) * (A[j] + 1) - 1);
        // for (ll l = (k + 1) * (A[j] + 1) - 1; (l + 1) * prod[U ^ i] - 1 <= X; l += A[j]) {
        //    dp[i][l] = true;
        // }
      }
    }
  }
  string ans(X, '0');
  for (int i = 1; i <= X; i++) {
    ans[i - 1] = dp[U][i] ? '1' : '0';
  }
  cerr << count(range(ans), '1') << endl;
  cout << ans << endl;
}

bool dfs(int x, int u) {
  if ((double)(clock() - beg) / CLOCKS_PER_SEC >= 0.5) {
    solve();
    exit(0);
  }
  u = root[u];
  if (checked[u][x]) return dp[u][x];
  cnt++;
  checked[u][x] = true;
  if (prod[u] - 1 > x) return dp[u][x] = false;
  if ((u & u - 1) == 0) return dp[u][x] = prod[u] - 1 == x;
  rep(i, N) if (u >> i & 1) {
    if ((x - (prod[u] - 1)) % A[i] == 0) return dp[u][x] = true;
  }
  for (int i = (u - 1) & u; i != 0; i = (i - 1) & u) {
    if (root[i] != i) continue;
    int j = u ^ i;
    for (int k = prod[j]; k <= x && prod[i] - 1 <= x / k; k++) if (x % k != 0) {
      if (dfs(x % k, j) && dfs(x / k, i)) {
        return dp[u][x] = true;
      }
    }
  }
  return dp[u][x] = false;
}



int main() {
  beg = clock();
  cin >> N >> X;
  rep(i, N) cin >> A[i];
  sort(A, A + N);
  ll p = 1;
  rep(i, N) {
    p *= A[i] + 1;
    if (p - 1 > X) {
      cout << string(X, '0') << endl;
      return 0;
    }
  }
  ll g = 0;
  rep(i, N) g = __gcd(g, A[i]);
  if (count(A, A + N, 1) > 0 && N >= 2) {
    string ans(X, '0');
    for (int i = p - 1; i <= X; i++) {
      ans[i - 1] = '1';
    }
    cout << ans << endl;
    return 0;
  }
  rep(i, 1 << N) prod[i] = 1;
  rep(i, 1 << N) rep(j, N) if (i >> j & 1) {
    prod[i] *= A[j] + 1;
  }
  rep(i, 1 << N) {
    vector<ll> a;
    rep(j, N) if (i >> j & 1) a.push_back(A[j]);
    int mask = 0;
    int k = 0;
    rep(j, N) {
      if (k < a.size() && a[k] == A[j]) {
        mask |= 1 << j;
        k++;
      }
    }
    root[i] = mask;
  }
  cerr << "here" << endl;
  string ans(X, '0');
  int cont = 0;
  for (int i = p - 1; i <= X; i++) {
    if ((i - (p - 1)) % g != 0) continue;
    if (cont >= 100 || dfs(i, (1 << N) - 1)) {
      ans[i - 1] = '1';
      cont++;
    } else {
      cont = 0;
    }
  }
  cout << ans << endl;
  cerr << cnt << endl;
}
0