結果

問題 No.2510 Six Cube Sum Counting
ユーザー LyricalMaestro
提出日時 2025-01-02 23:20:26
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1,378 ms / 4,000 ms
コード長 2,849 bytes
コンパイル時間 12,772 ms
コンパイル使用メモリ 385,228 KB
実行使用メモリ 342,268 KB
最終ジャッジ日時 2025-01-02 23:21:21
合計ジャッジ時間 44,480 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: variable `X` should have a snake case name
 --> src/main.rs:8:9
  |
8 |     let X: i64 = lines
  |         ^ help: convert the identifier to snake case (notice the capitalization): `x`
  |
  = note: `#[warn(non_snake_case)]` on by default

ソースコード

diff #

use std::collections::HashMap;
use std::io::{self, BufRead};

fn main() {
    // 標準入力から X を読み取る
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();
    let X: i64 = lines
        .next()
        .expect("No input found")
        .expect("Failed to read line")
        .trim()
        .parse()
        .expect("Input is not an integer");

    const ITEM_MAX_VALUE: usize = 300;

    // c_array[c] は「キー: 和(a^3 + b^3 + c^3), 値: 出現回数」を保持する HashMap
    // 大きさ ITEM_MAX_VALUE+1 のベクタを生成
    let mut c_array: Vec<HashMap<i64, i64>> = (0..=ITEM_MAX_VALUE)
        .map(|_| HashMap::new())
        .collect();

    // a <= b <= c となる組み合わせで a^3 + b^3 + c^3 を計算
    for a in 0..=ITEM_MAX_VALUE {
        let a3 = a as i64 * a as i64 * a as i64;
        if a3 > X {
            break;
        }

        for b in a..=ITEM_MAX_VALUE {
            let b3 = b as i64 * b as i64 * b as i64;
            if a3 + b3 > X {
                break;
            }

            for c in b..=ITEM_MAX_VALUE {
                let c3 = c as i64 * c as i64 * c as i64;
                let sum_abc = a3 + b3 + c3;
                if sum_abc > X {
                    break;
                }

                // c_array[c][sum_abc] のカウントをインクリメント
                let entry = c_array[c].entry(sum_abc).or_insert(0);
                *entry += 1;
            }
        }
    }

    let mut answer: i64 = 0;
    let mut value_map: HashMap<i64, i64> = HashMap::new();

    // d >= e >= f となる組み合わせで d^3 + e^3 + f^3 を計算
    // Pythonコードの reversed(range()) 相当 ⇒ (0..=ITEM_MAX_VALUE).rev()
    for d in (0..=ITEM_MAX_VALUE).rev() {
        let d3 = d as i64 * d as i64 * d as i64;
        if d3 > X {
            continue;
        }

        for e in d..=ITEM_MAX_VALUE {
            let e3 = e as i64 * e as i64 * e as i64;
            if d3 + e3 > X {
                break;
            }

            for f in e..=ITEM_MAX_VALUE {
                let f3 = f as i64 * f as i64 * f as i64;
                let sum_def = d3 + e3 + f3;
                if sum_def > X {
                    break;
                }

                // value_map[sum_def] のカウントをインクリメント
                let entry = value_map.entry(sum_def).or_insert(0);
                *entry += 1;
            }
        }

        // c_array[d] にある (key, count) に対して、
        // X - key を value_map で探す
        // c_array[d][key] * value_map[X - key] を answer に加算
        for (key, &val) in c_array[d].iter() {
            let x = X - key;
            if let Some(&v) = value_map.get(&x) {
                answer += val * v;
            }
        }
    }

    println!("{}", answer);
}
0