結果

問題 No.1243 約数加算
ユーザー phsplsphspls
提出日時 2023-02-05 19:16:05
言語 Rust
(1.77.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,432 bytes
コンパイル時間 1,856 ms
コンパイル使用メモリ 149,472 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-17 10:27:10
合計ジャッジ時間 1,924 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut t = String::new();
    std::io::stdin().read_line(&mut t).ok();
    let t: usize = t.trim().parse().unwrap();
    let queries = (0..t).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
            (temp[0], temp[1])
        })
        .collect::<Vec<_>>();

    for &(a, b) in queries.iter() {
        let bstr = format!("{:b}", b).chars().collect::<Vec<_>>();
        let astr = format!("{:b}", a);
        let astr = format!("{}{}", "0".repeat(bstr.len()-astr.len()), astr).chars().collect::<Vec<_>>();
        let n = bstr.len();
        let start = (0..n).filter(|&i| bstr[i] != astr[i]).nth(0).unwrap();
        let mut result = vec![];
        let mut aval = a;
        for i in (start+1..n).rev() {
            let idx = n - 1 - i;
            if (aval >> idx) & 1 == 1 {
                result.push(1usize << idx);
                aval += 1usize << idx;
            }
        }
        for i in 0..n {
            let idx = n - 1 - i;
            if ((aval >> idx) & 1) != ((b >> idx) & 1) {
                result.push(1usize << idx);
                aval += 1usize << idx;
            }
        }
        println!("{}", result.len());
        println!("{}", result.iter().map(|v| v.to_string()).collect::<Vec<_>>().join(" "));
    }
}
0