結果
| 問題 | No.826 連絡網 |
| コンテスト | |
| ユーザー |
neko_the_shadow
|
| 提出日時 | 2019-05-07 11:49:25 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,880 bytes |
| 記録 | |
| コンパイル時間 | 2,291 ms |
| コンパイル使用メモリ | 79,172 KB |
| 実行使用メモリ | 744,216 KB |
| 最終ジャッジ日時 | 2024-07-01 23:33:26 |
| 合計ジャッジ時間 | 27,957 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 7 TLE * 3 MLE * 10 -- * 10 |
ソースコード
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int n = stdin.nextInt();
int p = stdin.nextInt();
Main main = new Main(n, p);
System.out.println(main.solve());
}
private int n;
private int p;
private int[][] cache;
public Main(int n, int p) {
this.n = n;
this.p = p;
this.cache = new int[n + 1][n + 1];
}
public int solve() {
UnionFind uf = new UnionFind(n);
for (int x = 0; x < n; x++) {
for (int y = x + 1; y < n; y++) {
if (gcd(x + 1, y + 1) != 1) {
uf.union(x, y);
}
}
}
return uf.size(p - 1);
}
public int gcd(int x, int y) {
if (cache[x][y] != 0) {
return cache[x][y];
}
cache[x][y] = (x < y) ? gcd(y, x) :
(x % y == 0) ? y :
gcd(y, x % y);
return cache[x][y];
}
}
class UnionFind {
private int[] parents;
private int[] sizes;
public UnionFind(int n) {
parents = IntStream.range(0, n).toArray();
sizes = new int[n];
Arrays.fill(sizes, 1);
}
public void union(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
sizes[y] += sizes[x];
parents[x] = y;
}
}
public int find(int x) {
if (parents[x] == x) {
return x;
} else {
parents[x] = find(parents[x]);
return parents[x];
}
}
public int size(int x) {
return sizes[find(x)];
}
}
neko_the_shadow