結果
問題 | No.917 Make One With GCD |
ユーザー |
|
提出日時 | 2020-02-06 22:43:33 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,448 bytes |
コンパイル時間 | 14,186 ms |
コンパイル使用メモリ | 384,332 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-24 11:14:07 |
合計ジャッジ時間 | 14,388 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 32 |
ソースコード
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)); }