結果

問題 No.186 中華風 (Easy)
ユーザー tonyu0tonyu0
提出日時 2020-04-26 14:33:16
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,221 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 141,564 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 20:42:15
合計ジャッジ時間 2,919 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
4,380 KB
testcase_01 AC 4 ms
4,380 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 10 ms
4,384 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 13 ms
4,376 KB
testcase_07 AC 10 ms
4,380 KB
testcase_08 AC 16 ms
4,380 KB
testcase_09 AC 15 ms
4,380 KB
testcase_10 AC 13 ms
4,380 KB
testcase_11 AC 20 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 10 ms
4,376 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 9 ms
4,380 KB
testcase_22 AC 10 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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