結果

問題 No.25 有限小数
ユーザー phsplsphspls
提出日時 2022-09-04 21:38:54
言語 Rust
(1.72.1)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,290 bytes
コンパイル時間 995 ms
コンパイル使用メモリ 145,692 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-12 06:37:52
合計ジャッジ時間 2,628 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,384 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn gcd(n: usize, m: usize) -> usize {
    if m == 0 { return n; }
    gcd(m, n % m)
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut m = String::new();
    std::io::stdin().read_line(&mut m).ok();
    let m: usize = m.trim().parse().unwrap();

    let gcdval = gcd(n, m);
    let n = n / gcdval;
    let m = m / gcdval;
    let mut tempm = m;
    let mut cnt2 = 0usize;
    let mut cnt5 = 0usize;
    while tempm % 2 == 0 { tempm /= 2; cnt2 += 1; }
    while tempm % 5 == 0 { tempm /= 5; cnt5 += 1; }
    if tempm > 1 {
        println!("-1");
    } else if m == 1 {
        println!("{}", n.to_string().chars().rev().filter(|&c| c != '0').nth(0).unwrap());
    } else {
        if cnt5 == cnt2 {
            println!("{}", n.to_string().chars().rev().filter(|&c| c != '0').nth(0).unwrap());
        } else if cnt5 > cnt2 {
            let starts = vec![0,2,2,6,4,1,6,4,8,8];
            let mapping = vec![2,4,8,6];
            if n % 2 == 1 {
                cnt5 -= 1;
            }
            let idx = (0..4).filter(|&v| mapping[v] == starts[n%10]).nth(0).unwrap();
            println!("{}", mapping[(idx+cnt5-cnt2)%4]);
        } else {
            println!("5");
        }
    }
}
0