結果

問題 No.826 連絡網
ユーザー akakimidoriakakimidori
提出日時 2019-05-04 20:41:48
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 1,569 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 30,572 KB
実行使用メモリ 5,748 KB
最終ジャッジ日時 2023-08-15 18:28:05
合計ジャッジ時間 1,727 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 20 ms
4,628 KB
testcase_13 AC 8 ms
4,376 KB
testcase_14 AC 15 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 10 ms
4,376 KB
testcase_17 AC 8 ms
4,376 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 23 ms
4,972 KB
testcase_20 AC 23 ms
4,932 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 8 ms
4,380 KB
testcase_23 AC 10 ms
4,376 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 27 ms
5,724 KB
testcase_26 AC 5 ms
4,376 KB
testcase_27 AC 22 ms
4,840 KB
testcase_28 AC 16 ms
4,380 KB
testcase_29 AC 8 ms
4,380 KB
testcase_30 AC 28 ms
5,748 KB
testcase_31 AC 9 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef struct union_find {
  int32_t *parent;
  int32_t size;
} union_find;

void init_union_find (union_find * const u) {
  for (int32_t i = 0; i < u->size; ++i){
    u->parent[i] = -1;
  }
}

union_find* new_union_find (const int32_t size) {
  union_find *u = (union_find *) calloc (1, sizeof (union_find));
  u->parent = (int32_t *) calloc (size, sizeof (int32_t));
  u->size = size;
  init_union_find (u);
  return u;
}

void free_union_find (union_find * const u) {
  free (u->parent);
  free (u);
}

int32_t root (union_find * const u, int32_t x) {
  int32_t index[32];
  int32_t top = 0;
  while (u->parent[x] >= 0) {
    index[top++] = x;
    x = u->parent[x];
  }
  while (top > 0) {
    u->parent[index[--top]] = x;
  }
  return x;
}

int32_t get_size (union_find * const u, const int32_t x) {
  return - (u->parent[root (u, x)]);
}

void unite (union_find * const u, int32_t x, int32_t y) {
  x = root (u, x);
  y = root (u, y);
  if (x == y) return;
  if (u->parent[x] > u->parent[y]) {
    const int32_t swap = x;
    x = y;
    y = swap;
  }
  u->parent[x] += u->parent[y];
  u->parent[y] = x;
}

typedef int32_t i32;

void run (void) {
  i32 n, p;
  scanf ("%" SCNi32 "%" SCNi32, &n, &p);
  union_find *u = new_union_find (n + 1);
  for (i32 i = 2; 2 * i <= n; ++i) {
    if (get_size (u, i) > 1) continue;
    for (i32 j = 2 * i; j <= n; j += i) {
      unite (u, j, j - i);
    }
  }
  printf ("%" PRIi32 "\n", get_size (u, p));
}

int main (void) {
  run();
  return 0;
}
0