結果
| 問題 |
No.1059 素敵な集合
|
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2020-05-23 00:55:46 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 14 ms / 2,000 ms |
| コード長 | 1,756 bytes |
| コンパイル時間 | 12,135 ms |
| コンパイル使用メモリ | 403,360 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-23 09:36:01 |
| 合計ジャッジ時間 | 13,226 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
//---------- begin union_find ----------
#[allow(dead_code)]
mod union_find {
pub struct UF {
p: Vec<i32>,
}
impl UF {
pub fn new(n: usize) -> UF {
UF {p: vec![-1; n] }
}
pub fn init(&mut self) {
for p in self.p.iter_mut() {
*p = -1;
}
}
pub fn root(&mut self, mut x: usize) -> usize {
while self.p[x] >= 0 {
let p = self.p[x] as usize;
if self.p[p] < 0 {
return p;
}
let q = self.p[p] as usize;
self.p[x] = q as i32;
x = q;
}
x
}
pub fn unite(&mut self, mut x: usize, mut y: usize) -> Option<(usize, usize)> {
x = self.root(x);
y = self.root(y);
if x == y {
return None;
}
if self.p[x] > self.p[y] {
let s = x;
x = y;
y = s;
}
self.p[x] += self.p[y];
self.p[y] = x as i32;
Some((x, y))
}
}
}
//---------- end union_find ----------
fn run() {
let mut s = String::new();
std::io::stdin().read_line(&mut s).unwrap();
let mut it = s.trim().split_whitespace();
let l: usize = it.next().unwrap().parse().unwrap();
let r: usize = it.next().unwrap().parse().unwrap();
let mut ans = r - l;
let mut u = union_find::UF::new(r + 1);
for i in l..=r {
let mut j = 2 * i;
while j <= r {
if u.unite(i, j).is_some() {
ans -= 1;
}
j += i;
}
}
println!("{}", ans);
}
fn main() {
run();
}
akakimidori