結果
| 問題 |
No.916 Encounter On A Tree
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-02-06 23:14:43 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,448 bytes |
| コンパイル時間 | 15,419 ms |
| コンパイル使用メモリ | 383,116 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-25 06:57:15 |
| 合計ジャッジ時間 | 15,424 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 5 |
| other | RE * 56 |
ソースコード
use std::io::Read;
use std::collections::{HashMap, BinaryHeap};
use std::cmp::{max, min};
fn gcd(a: usize, b: usize) -> usize {
if a % b == 0 {
return b;
}
gcd(b, a % b)
}
fn main() {
let mut all_data = String::new();
std::io::stdin().read_to_string(&mut all_data).ok();
let all_data: Vec<&str> = all_data.trim().split('\n').map(|s| s.trim()).collect();
let a: Vec<usize> = all_data.iter().skip(1).next().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect();
let mut keys: BinaryHeap<isize> = BinaryHeap::new();
let mut gcd2count: HashMap<usize, usize> = HashMap::new();
for val in a {
let mut temp_keys = keys.clone().to_owned();
for _ in 0..keys.len() {
let key = keys.pop().map(|s| (s as isize) * -1).unwrap() as usize;
let count = *gcd2count.get(&key).unwrap();
let next_key = gcd(max(key, val), min(key, val));
if let Some(x) = gcd2count.get_mut(&next_key) {
*x += count;
} else {
gcd2count.insert(next_key, count);
temp_keys.push(-(next_key as isize));
}
}
if let Some(x) = gcd2count.get_mut(&val) {
*x += 1;
} else {
gcd2count.insert(val, 1);
temp_keys.push(-(val as isize));
}
keys = temp_keys;
}
println!("{}", gcd2count.get(&1usize).unwrap_or(&0usize));
}