結果

問題 No.2756 GCD Teleporter
ユーザー chro_96
提出日時 2024-05-10 22:31:27
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 326 ms / 2,000 ms
コード長 1,186 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 30,336 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-20 06:24:40
合計ジャッジ時間 7,053 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct tree {
  struct tree *p;
} tree;

tree *get_root (tree *t) {
  if (t == NULL || t->p == NULL) {
    return t;
  }
  
  t->p = get_root(t->p);
  return t->p;
}

int main () {
  int n = 0;
  int a = 0;
  
  int res = 0;
  
  int ans = 0;
  
  int cnt = 0;
  tree t[200000] = {};
  tree *tp[200001] = {};
  
  res = scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int tmp = 0;
    t[i].p = NULL;
    res = scanf("%d", &a);
    tmp = a;
    for (int p = 2; p*p <= a; p++) {
      if (tmp%p == 0) {
        if (tp[p] != NULL) {
          tree *rt = get_root(tp[p]);
          if (rt != t+i) {
            rt->p = t+i;
            cnt++;
          }
        }
        tp[p] = t+i;
        while (tmp%p == 0) {
          tmp /= p;
        }
      }
    }
    if (tmp > 1) {
      if (tp[tmp] != NULL) {
        tree *rt = get_root(tp[tmp]);
        if (rt != t+i) {
          rt->p = t+i;
          cnt++;
        }
      }  
      tp[tmp] = t+i;
    }
  }
  
  if (tp[2] != NULL) {
    ans = (n-cnt-1)*2;
  } else if (tp[3] != NULL && cnt == n-2) {
    ans = 3;
  } else if (cnt < n-1) {
    ans = (n-cnt)*2;
  }
  
  printf("%d\n", ans);
  return 0;
}
0