/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 283ms 3.504 MiB
#2 Accepted 291ms 3.5 MiB
#3 Accepted 280ms 3.504 MiB
#4 Accepted 456ms 3.57 MiB
#5 Accepted 444ms 3.676 MiB

Code

const M: usize = 200_001; // Adjusted to Rust syntax
const MOD: i64 = 1_000_000_007;

fn precalculate() -> Vec<i64> {
    let mut dp = vec![MOD; M]; // Initialize the dp array
    dp[0] = 1; // Base case

    for i in 1.. {
        let l = i * i;
        if l >= M {
            break; // Stop if square exceeds limit
        }

        let mut temp = vec![MOD; M]; // Temporary dp array
        for j in l..M {
            if dp[j - l] != MOD && dp[j - l] > 0 {
                temp[j] = temp[j].min(dp[j - l] + 1);
            }
            temp[j] = temp[j].min(dp[j]);
        }
        for j in l..M {
            dp[j] = dp[j].min(temp[j]);
        }
    }

    for i in 1..M {
        if dp[i] == MOD {
            dp[i] = 0; // Mark as unreachable
        }
    }

    dp
}

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

    let dp = precalculate(); // Precalculate dp values

    for _ in 0..t {
        let x: usize = {
            input.clear();
            std::io::stdin().read_line(&mut input).unwrap();
            input.trim().parse().unwrap()
        };
        println!("{}", dp[x] - 1); // Output the result
    }
}

Information

Submit By
Type
Submission
Problem
P1051 Square Sum
Language
Rust 2021 (Rust 1.75.0)
Submit At
2024-10-21 20:19:48
Judged At
2024-11-11 02:36:07
Judged By
Score
100
Total Time
456ms
Peak Memory
3.676 MiB