結果

問題 No.1303 Inconvenient Kingdom
ユーザー LayCurseLayCurse
提出日時 2020-11-27 22:14:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 11,574 bytes
コンパイル時間 3,106 ms
コンパイル使用メモリ 219,768 KB
実行使用メモリ 8,752 KB
最終ジャッジ日時 2023-10-01 06:23:41
合計ジャッジ時間 7,839 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,752 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define MD (998244353U)
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
  walloc1d(arr, x2-x1, mem);
  (*arr) -= x1;
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
  sort(a, a+N);
}
struct Modint{
  unsigned val;
  Modint(){
    val=0;
  }
  Modint(int a){
    val = ord(a);
  }
  Modint(unsigned a){
    val = ord(a);
  }
  Modint(long long a){
    val = ord(a);
  }
  Modint(unsigned long long a){
    val = ord(a);
  }
  inline unsigned ord(unsigned a){
    return a%MD;
  }
  inline unsigned ord(int a){
    a %= (int)MD;
    if(a < 0){
      a += MD;
    }
    return a;
  }
  inline unsigned ord(unsigned long long a){
    return a%MD;
  }
  inline unsigned ord(long long a){
    a %= (int)MD;
    if(a < 0){
      a += MD;
    }
    return a;
  }
  inline unsigned get(){
    return val;
  }
  inline Modint &operator+=(Modint a){
    val += a.val;
    if(val >= MD){
      val -= MD;
    }
    return *this;
  }
  inline Modint &operator-=(Modint a){
    if(val < a.val){
      val = val + MD - a.val;
    }
    else{
      val -= a.val;
    }
    return *this;
  }
  inline Modint &operator*=(Modint a){
    val = ((unsigned long long)val*a.val)%MD;
    return *this;
  }
  inline Modint &operator/=(Modint a){
    return *this *= a.inverse();
  }
  inline Modint operator+(Modint a){
    return Modint(*this)+=a;
  }
  inline Modint operator-(Modint a){
    return Modint(*this)-=a;
  }
  inline Modint operator*(Modint a){
    return Modint(*this)*=a;
  }
  inline Modint operator/(Modint a){
    return Modint(*this)/=a;
  }
  inline Modint operator+(int a){
    return Modint(*this)+=Modint(a);
  }
  inline Modint operator-(int a){
    return Modint(*this)-=Modint(a);
  }
  inline Modint operator*(int a){
    return Modint(*this)*=Modint(a);
  }
  inline Modint operator/(int a){
    return Modint(*this)/=Modint(a);
  }
  inline Modint operator+(long long a){
    return Modint(*this)+=Modint(a);
  }
  inline Modint operator-(long long a){
    return Modint(*this)-=Modint(a);
  }
  inline Modint operator*(long long a){
    return Modint(*this)*=Modint(a);
  }
  inline Modint operator/(long long a){
    return Modint(*this)/=Modint(a);
  }
  inline Modint operator-(void){
    Modint res;
    if(val){
      res.val=MD-val;
    }
    else{
      res.val=0;
    }
    return res;
  }
  inline operator bool(void){
    return val!=0;
  }
  inline operator int(void){
    return get();
  }
  inline operator long long(void){
    return get();
  }
  inline Modint inverse(){
    int a = val;
    int b = MD;
    int u = 1;
    int v = 0;
    int t;
    Modint res;
    while(b){
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    if(u < 0){
      u += MD;
    }
    res.val = u;
    return res;
  }
  inline Modint pw(unsigned long long b){
    Modint a(*this);
    Modint res;
    res.val = 1;
    while(b){
      if(b&1){
        res *= a;
      }
      b >>= 1;
      a *= a;
    }
    return res;
  }
  inline bool operator==(int a){
    return ord(a)==val;
  }
  inline bool operator!=(int a){
    return ord(a)!=val;
  }
}
;
inline Modint operator+(int a, Modint b){
  return Modint(a)+=b;
}
inline Modint operator-(int a, Modint b){
  return Modint(a)-=b;
}
inline Modint operator*(int a, Modint b){
  return Modint(a)*=b;
}
inline Modint operator/(int a, Modint b){
  return Modint(a)/=b;
}
inline Modint operator+(long long a, Modint b){
  return Modint(a)+=b;
}
inline Modint operator-(long long a, Modint b){
  return Modint(a)-=b;
}
inline Modint operator*(long long a, Modint b){
  return Modint(a)*=b;
}
inline Modint operator/(long long a, Modint b){
  return Modint(a)/=b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline int rd_int(void){
  int x;
  rd(x);
  return x;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(Modint x){
  int i;
  i = (int)x;
  wt_L(i);
}
struct unionFind{
  int*d;
  int N;
  int M;
  inline void malloc(const int n){
    d = (int*)std::malloc(n*sizeof(int));
    M = n;
  }
  inline void malloc(const int n, const int fg){
    d = (int*)std::malloc(n*sizeof(int));
    M = n;
    if(fg){
      init(n);
    }
  }
  inline void free(void){
    std::free(d);
  }
  inline void walloc(const int n, void **mem=&wmem){
    walloc1d(&d, n, mem);
    M = n;
  }
  inline void walloc(const int n, const int fg, void **mem=&wmem){
    walloc1d(&d, n, mem);
    M = n;
    if(fg){
      init(n);
    }
  }
  inline void init(const int n){
    int i;
    N = n;
    for(i=(0);i<(n);i++){
      d[i] = -1;
    }
  }
  inline void init(void){
    init(M);
  }
  inline int get(int a){
    int t = a;
    int k;
    while(d[t]>=0){
      t=d[t];
    }
    while(d[a]>=0){
      k=d[a];
      d[a]=t;
      a=k;
    }
    return a;
  }
  inline int connect(int a, int b){
    if(d[a]>=0){
      a=get(a);
    }
    if(d[b]>=0){
      b=get(b);
    }
    if(a==b){
      return 0;
    }
    if(d[a] < d[b]){
      d[a] += d[b];
      d[b] = a;
    }
    else{
      d[b] += d[a];
      d[a] = b;
    }
    return 1;
  }
  inline int operator()(int a){
    return get(a);
  }
  inline int operator()(int a, int b){
    return connect(a,b);
  }
  inline int& operator[](const int a){
    return d[a];
  }
  inline int size(int a){
    a = get(a);
    return -d[a];
  }
  inline int sizeList(int res[]){
    int i;
    int sz=0;
    for(i=(0);i<(N);i++){
      if(d[i]<0){
        res[sz++] = -d[i];
      }
    }
    return sz;
  }
}
;
int N;
int mat[100][100];
int n;
Modint m[100][100];
int sz;
int lis[100];
Modint calc_det(void){
  int i;
  int j;
  int k;
  Modint res = 1;
  Modint t;
  for(k=(0);k<(n);k++){
    for(i=(k);i<(n);i++){
      if(m[i][k] != 0){
        break;
      }
    }
    if(i == n){
      return 0;
    }
    if(i != k){
      for(j=(k);j<(n);j++){
        swap(m[k][j], m[i][j]);
      }
    }
    for(i=(k+1);i<(n);i++){
      t = m[i][k] / m[k][k];
      for(j=(k);j<(n);j++){
        m[i][j] -= t * m[k][j];
      }
    }
  }
  for(i=(0);i<(n);i++){
    res *= m[i][i];
  }
  return res;
}
int main(){
  int k, tU__gIr_;
  wmem = memarr;
  int i;
  int j;
  int x;
  int y;
  long long c;
  unionFind uf;
  long long res1 = 0;
  Modint res2 = 1;
  Modint tmp;
  rd(N);
  uf.walloc(N,1);
  int a2conNHc = rd_int();
  for(tU__gIr_=(0);tU__gIr_<(a2conNHc);tU__gIr_++){
    rd(i);i += (-1);
    rd(j);j += (-1);
    mat[i][j] = mat[j][i] = 1;
    uf(i,j);
  }
  for(k=(0);k<(N);k++){
    n = 0;
    for(i=(0);i<(N);i++){
      if(uf(i) == k){
        lis[n++] = i;
      }
    }
    if(n<=1){
      continue;
    }
    for(i=(0);i<(n);i++){
      for(j=(0);j<(n);j++){
        m[i][j] = -mat[lis[i]][lis[j]];
      }
    }
    for(i=(0);i<(n);i++){
      for(j=(0);j<(n);j++){
        if(j!=i){
          m[i][i] -= m[i][j];
        }
      }
    }
    n--;
    res2 *= calc_det();
  }
  sz = uf.sizeList(lis);
  sortA_L(sz, lis);
  if(sz >= 2){
    x = lis[sz-2];
    y = lis[sz-1];
    c = 0;
    for(i=(0);i<(sz);i++){
      for(j=(i+1);j<(sz);j++){
        if(lis[i]==x && lis[j]==y){
          c++;
        }
      }
    }
    res2 *= c *x * y;
    lis[sz-2] = x + y;
    sz--;
  }
  else{
    tmp = res2;
    n = N;
    for(i=(0);i<(n);i++){
      for(j=(i+1);j<(n);j++){
        if(mat[i][j]==0){
          for(x=(0);x<(n);x++){
            for(y=(0);y<(n);y++){
              m[x][y] = -mat[x][y];
            }
          }
          m[i][j] = -1;
          for(x=(0);x<(n);x++){
            for(y=(0);y<(n);y++){
              if(x!=y){
                m[x][x] -= m[x][y];
              }
            }
          }
          n--;
          res2 += calc_det() - tmp;
          n++;
        }
      }
    }
  }
  for(i=(0);i<(sz);i++){
    res1 += (N - lis[i]) * lis[i];
  }
  wt_L(res1);
  wt_L('\n');
  wt_L(res2);
  wt_L('\n');
  return 0;
}
// cLay version 20201123-1

// --- original code ---
// #define MD 998244353
// int N, mat[100][100];
// 
// int n;
// Modint m[100][100];
// 
// int sz, lis[100];
// 
// Modint calc_det(void){
//   int i, j, k;
//   Modint res = 1, t;
// 
//   // wt("---");
//   // wt(m(n,n));
// 
//   rep(k,n){
//     rep(i,k,n) if(m[i][k] != 0) break;
//     if(i == n) return 0;
//     if(i != k){
//       rep(j,k,n) swap(m[k][j], m[i][j]);
//     }
//     rep(i,k+1,n){
//       t = m[i][k] / m[k][k];
//       rep(j,k,n) m[i][j] -= t * m[k][j];
//     }
//   }
// 
//   // wt("+++");
//   // wt(m(n,n));
//   rep(i,n) res *= m[i][i];
//   // wt("res",res);
//   return res;
// }
// 
// {
//   int i, j, x, y;
//   ll c;
//   unionFind uf;
//   ll res1 = 0;
//   Modint res2 = 1, tmp;
// 
//   rd(N);
//   uf.walloc(N,1);
//   REP(rd_int()){
//     rd(i--, j--);
//     mat[i][j] = mat[j][i] = 1;
//     uf(i,j);
//   }
// 
//   rep(k,N){
//     n = 0;
//     rep(i,N) if(uf(i) == k) lis[n++] = i;
//     if(n<=1) continue;
//     rep(i,n) rep(j,n) m[i][j] = -mat[lis[i]][lis[j]];
//     rep(i,n) rep(j,n) if(j!=i) m[i][i] -= m[i][j];
//     n--;
//     res2 *= calc_det();
//   }
// 
//   sz = uf.sizeList(lis);
//   sortA(sz, lis);
//   if(sz >= 2){
//     x = lis[sz-2];
//     y = lis[sz-1];
//     c = 0;
//     rep(i,sz) rep(j,i+1,sz) if(lis[i]==x && lis[j]==y) c++;
//     res2 *= c *x * y;
// 
//     lis[sz-2] = x + y;
//     sz--;
//   } else {
//     tmp = res2;
//     n = N;
//     rep(i,n) rep(j,i+1,n) if(mat[i][j]==0){
//       rep(x,n) rep(y,n) m[x][y] = -mat[x][y];
//       m[i][j] = -1;
//       rep(x,n) rep(y,n) if(x!=y) m[x][x] -= m[x][y];
//       n--;
//       res2 += calc_det() - tmp;
//       n++;
//     }
//   }
//   rep(i,sz) res1 += (N - lis[i]) * lis[i];
// 
//   wtLn(res1, res2);
// }
0