結果

問題 No.1711 Divide LCM
ユーザー SSRSSSRS
提出日時 2021-10-15 23:12:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,469 ms / 4,000 ms
コード長 3,664 bytes
コンパイル時間 3,671 ms
コンパイル使用メモリ 207,316 KB
実行使用メモリ 76,120 KB
最終ジャッジ日時 2023-10-17 21:00:20
合計ジャッジ時間 15,872 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 10 ms
4,428 KB
testcase_05 AC 42 ms
9,444 KB
testcase_06 AC 13 ms
4,836 KB
testcase_07 AC 33 ms
6,472 KB
testcase_08 AC 13 ms
4,612 KB
testcase_09 AC 73 ms
15,472 KB
testcase_10 AC 16 ms
4,864 KB
testcase_11 AC 106 ms
16,084 KB
testcase_12 AC 16 ms
4,876 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 6 ms
4,348 KB
testcase_15 AC 9 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 145 ms
15,760 KB
testcase_19 AC 146 ms
15,780 KB
testcase_20 AC 146 ms
15,768 KB
testcase_21 AC 197 ms
25,932 KB
testcase_22 AC 715 ms
76,120 KB
testcase_23 AC 461 ms
43,936 KB
testcase_24 AC 185 ms
21,936 KB
testcase_25 AC 186 ms
21,936 KB
testcase_26 AC 179 ms
21,880 KB
testcase_27 AC 180 ms
23,412 KB
testcase_28 AC 176 ms
22,848 KB
testcase_29 AC 5 ms
4,348 KB
testcase_30 AC 186 ms
24,036 KB
testcase_31 AC 4 ms
4,348 KB
testcase_32 AC 222 ms
25,872 KB
testcase_33 AC 223 ms
25,876 KB
testcase_34 AC 12 ms
4,868 KB
testcase_35 AC 210 ms
28,792 KB
testcase_36 AC 221 ms
28,732 KB
testcase_37 AC 220 ms
28,736 KB
testcase_38 AC 1,469 ms
28,192 KB
testcase_39 AC 568 ms
25,948 KB
testcase_40 AC 4 ms
4,348 KB
testcase_41 AC 4 ms
4,348 KB
testcase_42 AC 4 ms
4,348 KB
testcase_43 AC 4 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<int> &ans, bool &ok, int &cnt, vector<unsigned long long> &hash, vector<unsigned long long> &ng, unsigned long long ch, int csz, int sz, int p){
  if (p == cnt){
    if (csz == sz){
      auto itr = lower_bound(ng.begin(), ng.end(), ch);
      if (itr == ng.end()){
        ok = true;
      } else if (*itr != ch){
        ok = true;
      }
    }
  } else {
    if (csz < sz){
      dfs(ans, ok, cnt, hash, ng, ch ^ hash[p], csz + 1, sz, p + 1);
      if (ok){
        ans.push_back(p);
        return;
      }
    }
    dfs(ans, ok, cnt, hash, ng, ch, csz, sz, p + 1);
  }
}
int main(){
  mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
  int N;
  cin >> N;
  vector<int> m(N);
  vector<vector<int>> p(N), e(N);
  for (int i = 0; i < N; i++){
    cin >> m[i];
    p[i] = vector<int>(m[i]);
    e[i] = vector<int>(m[i]);
    for (int j = 0; j < m[i]; j++){
      cin >> p[i][j] >> e[i][j];
    }
  }
  vector<int> P;
  for (int i = 0; i < N; i++){
    for (int j = 0; j < m[i]; j++){
      P.push_back(p[i][j]);
    }
  }
  sort(P.begin(), P.end());
  P.erase(unique(P.begin(), P.end()), P.end());
  int cnt = P.size();
  for (int i = 0; i < N; i++){
    for (int j = 0; j < m[i]; j++){
      p[i][j] = lower_bound(P.begin(), P.end(), p[i][j]) - P.begin();
    }
  }
  vector<int> mx(cnt, -1);
  for (int i = 0; i < N; i++){
    for (int j = 0; j < m[i]; j++){
      mx[p[i][j]] = max(mx[p[i][j]], e[i][j]);
    }
  }
  vector<vector<int>> s(N);
  for (int i = 0; i < N; i++){
    for (int j = 0; j < m[i]; j++){
      if (mx[p[i][j]] == e[i][j]){
        s[i].push_back(p[i][j]);
      }
    }
  }
  bool ok = true;
  for (int i = 0; i < N; i++){
    if (s[i].size() == cnt){
      ok = false;
    }
  }
  if (!ok){
    cout << -1 << endl;
  } else {
    vector<unsigned long long> hash(cnt, 0);
    for (int i = 0; i < cnt; i++){
      for (int j = 0; j < 64; j++){
        hash[i] *= 2;
        hash[i] += mt() % 2;
      }
    }
    vector<unsigned long long> ng;
    for (int i = 0; i < N; i++){
      int cnt2 = s[i].size();
      for (int j = 0; j < (1 << cnt2); j++){
        int sz = __builtin_popcount(j);
        unsigned long long S = 0;
        for (int k = 0; k < cnt2; k++){
          if ((j >> k & 1) == 1){
            S ^= hash[s[i][k]];
          }
        }
        ng.push_back(S);
      }
    }
    sort(ng.begin(), ng.end());
    ng.erase(unique(ng.begin(), ng.end()), ng.end());
    vector<int> ans;
    for (int i = 2; i <= 9; i++){
      unsigned long long chh = 0;
      bool ok2 = false;
      dfs(ans, ok2, cnt, hash, ng, chh, 0, i, 0);
      if (ok2){
        break;
      }
      if (i == 2 && cnt >= 10000){
        cout << -1 << endl;
        return 0;
      }
    }
    vector<int> A(N, 1);
    for (int i = 0; i < N; i++){
      for (int j = 0; j < m[i]; j++){
        for (int k = 0; k < e[i][j]; k++){
          A[i] *= P[p[i][j]];
        }
      }
    }
    int k = ans.size();
    vector<vector<int>> S(k);
    for (int i = 0; i < N; i++){
      for (int j = 0; j < k; j++){
        bool ok2 = true;
        int cnt2 = s[i].size();
        for (int l = 0; l < cnt2; l++){
          if (s[i][l] == ans[j]){
            ok2 = false;
          }
        }
        if (ok2){
          S[j].push_back(A[i]);
          break;
        }
      }
    }
    cout << k << endl;
    for (int i = 0; i < k; i++){
      int n = S[i].size();
      cout << n;
      for (int j = 0; j < n; j++){
        cout << ' ' << S[i][j];
      }
      cout << endl;
    }
  }
}
0