結果

問題 No.982 Add
ユーザー phspls
提出日時 2020-02-15 21:21:09
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 982 bytes
コンパイル時間 13,956 ms
コンパイル使用メモリ 378,080 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-06 14:05:44
合計ジャッジ時間 13,697 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::HashSet;
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 ab: String = String::new();
    std::io::stdin().read_line(&mut ab).ok();
    let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let a: usize = ab[0];
    let b: usize = ab[1];

    let gcd: usize = gcd(max(a, b), min(a, b));
    if gcd > 1 {
        println!("{}", -1);
    } else {
        let lcm: usize = a * b;
        let aa: Vec<usize> = (0..=b).map(|i| a*i).collect();
        let bb: Vec<usize> = (0..=a).map(|i| b*i).collect();
        let mut abs: HashSet<usize> = HashSet::new();
        for i in aa.iter() {
            for j in bb.iter() {
                if i + j <= lcm {
                    abs.insert(i+j);
                }
            }
        }
        let result: usize = lcm + 1 - abs.len();
        println!("{}", result);
    }
}
0