結果

問題 No.1711 Divide LCM
ユーザー chineristAC
提出日時 2021-08-13 04:12:54
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,549 ms / 4,000 ms
コード長 2,612 bytes
コンパイル時間 9,808 ms
コンパイル使用メモリ 208,900 KB
最終ジャッジ日時 2025-01-23 17:48:51
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#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";
}

int pow(int n,int k){
  int 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);

  int N;
  cin>>N;

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

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

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

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

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

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

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

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

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

  
}
0