結果

問題 No.1711 Divide LCM
ユーザー chineristACchineristAC
提出日時 2021-08-13 13:14:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,155 ms / 4,000 ms
コード長 2,487 bytes
コンパイル時間 1,991 ms
コンパイル使用メモリ 157,924 KB
実行使用メモリ 42,468 KB
最終ジャッジ日時 2023-10-17 19:50:57
合計ジャッジ時間 16,319 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 7 ms
4,372 KB
testcase_05 AC 28 ms
4,372 KB
testcase_06 AC 9 ms
4,372 KB
testcase_07 AC 21 ms
4,372 KB
testcase_08 AC 10 ms
4,372 KB
testcase_09 AC 58 ms
4,372 KB
testcase_10 AC 12 ms
4,372 KB
testcase_11 AC 90 ms
4,480 KB
testcase_12 AC 13 ms
4,372 KB
testcase_13 AC 2 ms
4,372 KB
testcase_14 AC 5 ms
4,372 KB
testcase_15 AC 7 ms
4,372 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 5 ms
4,372 KB
testcase_18 AC 588 ms
27,432 KB
testcase_19 AC 585 ms
27,364 KB
testcase_20 AC 603 ms
27,388 KB
testcase_21 AC 166 ms
10,404 KB
testcase_22 AC 2,155 ms
29,444 KB
testcase_23 AC 1,661 ms
42,468 KB
testcase_24 AC 173 ms
7,496 KB
testcase_25 AC 175 ms
7,436 KB
testcase_26 AC 164 ms
7,116 KB
testcase_27 AC 161 ms
7,324 KB
testcase_28 AC 155 ms
6,992 KB
testcase_29 AC 3 ms
4,372 KB
testcase_30 AC 190 ms
9,672 KB
testcase_31 AC 3 ms
4,372 KB
testcase_32 AC 194 ms
10,716 KB
testcase_33 AC 185 ms
10,716 KB
testcase_34 AC 12 ms
4,372 KB
testcase_35 AC 235 ms
7,884 KB
testcase_36 AC 249 ms
8,384 KB
testcase_37 AC 252 ms
8,396 KB
testcase_38 AC 210 ms
16,476 KB
testcase_39 AC 188 ms
10,952 KB
testcase_40 AC 5 ms
4,372 KB
testcase_41 AC 4 ms
4,372 KB
testcase_42 AC 4 ms
4,372 KB
testcase_43 AC 5 ms
4,372 KB
testcase_44 AC 5 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <random>
#include <stdio.h>
#include <fstream>
#include <functional>
#include <cassert>

using namespace std;

#define rep(i,n,c) for (int i=0;i<n;i+=c)
#define append push_back
#define all(x) (x).begin(), (x).end()

template<class T>
using vec = vector<T>;
template<class T>
using vvec = vec<vec<T>>;
template<class T>
using vvvec = vec<vvec<T>>;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;


template<class T>
bool chmin(T &a, T b){
  if (a>b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmax(T &a, T b){
  if (a<b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
T sum(vec<T> x){
  T res=0;
  for (auto e:x){
    res += e;
  }
  return res;
}

template<class T>
void printv(vec<T> x){
  for (auto e:x){
    cout<<e<<" ";
  }
  cout<<"\n";
}

ll pow(ll n,ll k){
  ll res = 1;
  while (k){
    if (k&1){
      res *= n;
    }
    n *= n;
    k >>= 1;
  }
  return res;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll N;
  cin>>N;

  map<ll,ll> LCM;
  vec<ll> A(N,1);
  set<ll> all_divisors;

  ll m,p,e,t;
  for (ll i=0;i<N;i++){
    cin>>m;
    vec<ll> D = {1};
    for (ll x=0;x<m;x++){
      cin>>p>>e;
      t = pow(p,e);
      A[i] *= t;
      LCM[p];
      chmax(LCM[p],t);

      vec<ll> nD;
      for (auto d:D){
        nD.append(d);
        nD.append(d*t);
      }
      D = nD;
    }
    for (auto d:D){
      all_divisors.insert(d);
    }
  }

  vec<ll> P;
  for (auto [p,pe]:LCM){
    P.append(pe);
  }
  ll n = P.size();

  deque<pair<ll,ll>> deq = {{1,-1}};
  ll prod = -1;
  while (!deq.empty()){
    auto [v,k] = deq.front();
    deq.pop_front();
    for (ll i=k+1;i<n;i++){
      if (!all_divisors.count(v*P[i])){
        prod = v * P[i];
        deq = {};
        break;
      }
      deq.append({v*P[i],i});
    }
  }

  if (prod==-1){
    cout<<-1<<endl;
    return 0;
  }

  vec<ll> selected_P;
  for (ll i=0;i<n;i++){
    if (prod%P[i]==0){
      selected_P.append(P[i]);
    }
  }
  ll k = selected_P.size();

  vec<vec<ll>> S(k,vec<ll>(0));
  for (ll i=0;i<N;i++){
    for (ll j=0;j<k;j++){
      if (A[i]%selected_P[j]){
        S[j].append(A[i]);
        break;
      }
    }
  }

  cout<<k<<endl;
  for (ll i=0;i<k;i++){
    cout<<S[i].size()<<" ";
    printv(S[i]);
  }

  
}
0