結果

問題 No.1022 Power Equation
ユーザー akakimidoriakakimidori
提出日時 2020-04-10 22:20:06
言語 Rust
(1.72.1)
結果
AC  
実行時間 338 ms / 2,000 ms
コード長 1,282 bytes
コンパイル時間 1,727 ms
コンパイル使用メモリ 148,968 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 01:04:17
合計ジャッジ時間 4,201 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

use std::io::Read;
use std::io::Write;

fn gcd(a: u64, b: u64) -> u64 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}

fn lcm(a: u64, b: u64) -> u64 {
    a / gcd(a, b) * b
}

fn run() {
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    it.next();
    for s in it {
        let n: u64 = s.parse().unwrap();
        let mut ans = n * n;// a = c = 1
        if n > 1 {
            ans += (n - 1) * n;// a = c > 1
        }
        let mut set = std::collections::BTreeSet::new();
        // a > c > 1
        for r in 2u64.. {
            if r.pow(2) > n {
                break;
            }
            for a in 2u64.. {
                if r.pow(a as u32) > n {
                    break;
                }
                for c in 1u64..a {
                    let (p, q) = (r.pow(a as u32), r.pow(c as u32));
                    if set.insert((p, q)) {
                        let x = lcm(a, c);
                        ans += 2 * (n / (x / c));
                    }
                }
            }
        }
        writeln!(out, "{}", ans).ok();
    }
}

fn main() {
    run();
}
0