結果

問題 No.186 中華風 (Easy)
ユーザー tonyu0
提出日時 2020-04-26 14:33:16
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,221 bytes
コンパイル時間 28,878 ms
コンパイル使用メモリ 377,820 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-17 06:14:18
合計ジャッジ時間 14,557 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

fn gcd(a: u64, b: u64) -> u64 {
    if b == 0 {
        return a;
    }
    gcd(b, a % b)
}
fn lcm(a: u64, b: u64) -> u64 {
    a / gcd(a, b) * b
}

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let mut x = vec![0u64; 3];
    let mut y = vec![0u64; 3];
    for i in 0..3 {
        x[i] = itr.next().unwrap().parse().unwrap();
        y[i] = itr.next().unwrap().parse().unwrap();
    }

    // 0 と 1を両方満たすもの
    // n = y[0] * i + x[0]
    // このiを0 ~ y[1] - 1まで試す。(周期がy[1]なので)
    //
    let mut n = 0;
    let mut found = false;
    for i in 0..y[1] {
        n = y[0] * i + x[0];
        if n % y[1] == x[1] {
            found = true;
            break;
        }
    }
    if found {
        let y01 = lcm(y[0], y[1]);
        let mut m = 0;
        found = false;
        for i in 0..y[2] {
            m = n + y01 * i;
            if m % y[2] == x[2] {
                found = true;
                break;
            }
        }
        if found && m != 0 {
            println!("{}", m);
            return;
        }
    }
    println!("-1");
}
0