結果

問題 No.2767 Add to Divide
ユーザー あさくちあさくち
提出日時 2024-06-01 15:52:40
言語 Rust
(1.77.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 931 bytes
コンパイル時間 15,350 ms
コンパイル使用メモリ 393,412 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-01 15:53:02
合計ジャッジ時間 16,516 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 12 ms
6,816 KB
testcase_04 AC 1 ms
6,816 KB
testcase_05 AC 8 ms
6,940 KB
testcase_06 AC 7 ms
6,940 KB
testcase_07 AC 7 ms
6,944 KB
testcase_08 AC 10 ms
6,944 KB
testcase_09 AC 9 ms
6,940 KB
testcase_10 AC 9 ms
6,940 KB
testcase_11 AC 10 ms
6,940 KB
testcase_12 AC 10 ms
6,940 KB
testcase_13 AC 9 ms
6,940 KB
testcase_14 AC 9 ms
6,944 KB
testcase_15 AC 9 ms
6,940 KB
testcase_16 AC 10 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::input;

#[proconio::fastout]
fn main() {
    input! {
        t: usize,
    }

    for _ in 0..t {
        input! {
            a: usize,
            b: usize,
        }

        if let Some(result) = solve(a, b) {
            println!("{}", result);
        } else {
            println!("-1");
        }
    }
}

fn solve(a: usize, b: usize) -> Option<usize> {
    if a == b {
        return Some(0);
    }

    let list = divisors(b - a);

    for divisor in list {
        if divisor >= a {
            return Some(divisor - a);
        }
    }

    None
}

fn divisors(n: usize) -> Vec<usize> {
    let mut list = Vec::new();

    {
        let mut i = 1;

        while i * i <= n {
            if n % i == 0 {
                list.push(i);

                if n / i != i {
                    list.push(n / i);
                }
            }

            i += 1;
        }
    }

    list.sort();

    list
}
0