Submission #16402611


Source Code Expand

Copy
// ${url}
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

//===

fn main() {
    input! {
        n: usize, M: i64
    };

    let mut dp = dvec!(0; 3*n+1, n+1);
    dp[0][0] = 1;
    for i in 0..3*n {
        for j in 0..=n {
            let left_numbers = (3*n-i) as i64;
            let base = dp[i][j];
            if base == 0 {
                continue;
            }

            // Type 1: place the largest number
            dp[i+1][j] += base;
            dp[i+1][j] %= M;

            // Type 2: place 2 numbers so that the largest number is at the head
            if i+2 <= 3*n && j < n {
                dp[i+2][j+1] += base * (left_numbers - 1) % M;
                dp[i+2][j+1] %= M;
            }

            // Type 3: place 3 numbers so that the largest number is at the head
            if i+3 <= 3*n && j < n {
                dp[i+3][j+1] += base * (left_numbers - 1) * (left_numbers - 2) % M;
                dp[i+3][j+1] %= M;
            }
        }
    }

    let ans = (0..=n).fold(0, |acc, idx| acc+dp[3*n][idx]);
    println!("{}", ans % M);
}

Submission Info

Submission Time
Task D - Merge Triplets
User hamadu
Language Rust (1.42.0)
Score 1200
Code Size 4419 Byte
Status AC
Exec Time 419 ms
Memory 95992 KB

Compile Error

warning: variable `M` should have a snake case name
   --> src/main.rs:146:19
    |
146 |         n: usize, M: i64
    |                   ^ help: convert the identifier to snake case: `m`
    |
    = note: `#[warn(non_snake_case)]` on by default

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1200 / 1200
Status
AC × 3
AC × 15
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt
Case Name Status Exec Time Memory
0_000.txt AC 7 ms 2084 KB
0_001.txt AC 2 ms 2064 KB
0_002.txt AC 24 ms 4380 KB
1_003.txt AC 1 ms 2052 KB
1_004.txt AC 3 ms 2324 KB
1_005.txt AC 115 ms 25688 KB
1_006.txt AC 9 ms 2240 KB
1_007.txt AC 101 ms 21296 KB
1_008.txt AC 35 ms 7224 KB
1_009.txt AC 414 ms 94872 KB
1_010.txt AC 416 ms 95980 KB
1_011.txt AC 419 ms 95980 KB
1_012.txt AC 411 ms 95992 KB
1_013.txt AC 412 ms 95976 KB
1_014.txt AC 415 ms 95920 KB