結果

問題 No.1621 Sequence Inversions
ユーザー fukafukatanifukafukatani
提出日時 2021-07-23 11:22:08
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 6,766 bytes
コンパイル時間 2,872 ms
コンパイル使用メモリ 164,256 KB
実行使用メモリ 209,364 KB
最終ジャッジ日時 2023-09-25 09:28:11
合計ジャッジ時間 15,909 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 312 ms
209,164 KB
testcase_01 AC 329 ms
209,240 KB
testcase_02 AC 323 ms
209,252 KB
testcase_03 AC 323 ms
209,112 KB
testcase_04 AC 329 ms
209,132 KB
testcase_05 AC 328 ms
209,148 KB
testcase_06 AC 327 ms
209,200 KB
testcase_07 AC 322 ms
209,180 KB
testcase_08 AC 362 ms
209,160 KB
testcase_09 AC 356 ms
209,148 KB
testcase_10 RE -
testcase_11 AC 368 ms
209,176 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 362 ms
209,188 KB
testcase_15 AC 362 ms
209,164 KB
testcase_16 AC 965 ms
209,172 KB
testcase_17 RE -
testcase_18 AC 910 ms
209,164 KB
testcase_19 AC 315 ms
209,164 KB
testcase_20 AC 621 ms
209,172 KB
testcase_21 RE -
testcase_22 AC 474 ms
209,180 KB
testcase_23 RE -
testcase_24 AC 313 ms
209,276 KB
testcase_25 AC 316 ms
209,248 KB
testcase_26 AC 313 ms
209,196 KB
testcase_27 AC 311 ms
209,164 KB
testcase_28 AC 314 ms
209,120 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: associated function `set_modulus` is never used
  --> Main.rs:87:8
   |
87 |     fn set_modulus(m: i64) {
   |        ^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: associated function `pow` is never used
   --> Main.rs:105:8
    |
105 |     fn pow(self, p: i64) -> Modulo {
    |        ^^^

warning: associated function `inv` is never used
   --> Main.rs:117:8
    |
117 |     fn inv(self) -> Modulo {
    |        ^^^

warning: 3 warnings emitted

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, k) = (v[0], v[1]);

    let mut a = read_vec::<i64>();
    a.sort();

    let mut array = vec![1];
    for i in 1..n {
        if a[i] == a[i - 1] {
            let last = array.len() - 1;
            array[last] += 1;
        } else {
            array.push(1);
        }
    }

    let mut f = vec![vec![vec![Modulo(0); 100 + 1]; 2500 + 1]; 100 + 1]; // n,m,k
    for ki in 0..=100 {
        f[0][0][ki] = Modulo(1);
    }
    for ni in 1..=100 {
        for ki in 0..=100 {
            for mi in 0..=2500 {
                if mi > ni * ki {
                    break;
                }
                f[ni][mi][ki] = f[ni - 1][mi][ki];
                if mi >= ni && ki > 0 {
                    let val = f[ni][mi - ni][ki - 1];
                    f[ni][mi][ki] += val;
                }
            }
        }
    }

    let mut placed = 0;
    let mut dp = vec![Modulo(0); k + 1];
    dp[0] += 1;
    for num in array {
        let mut next = vec![Modulo(0); k + 1];
        for prev in 0..=k {
            for cur_i in 0..=k - prev {
                next[cur_i + prev] += dp[prev] * f[num][cur_i][placed];
            }
        }
        placed += num;
        dp = next;
    }

    println!("{}", dp[k]);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct Modulo(i64);
static mut MODULUS: i64 = 998244353;
impl Modulo {
    fn set_modulus(m: i64) {
        unsafe {
            MODULUS = m;
        }
    }
    fn get_modulus() -> i64 {
        unsafe { MODULUS }
    }
    fn new(x: i64) -> Modulo {
        let m = Modulo::get_modulus();
        if x < 0 {
            Modulo(x % m + m)
        } else if x < m {
            Modulo(x)
        } else {
            Modulo(x % m)
        }
    }
    fn pow(self, p: i64) -> Modulo {
        if p == 0 {
            Modulo(1)
        } else {
            let mut t = self.pow(p / 2);
            t *= t;
            if p & 1 == 1 {
                t *= self;
            }
            t
        }
    }
    fn inv(self) -> Modulo {
        self.pow(Modulo::get_modulus() - 2)
    }
}
impl std::fmt::Display for Modulo {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.0.fmt(f)
    }
}
impl std::ops::AddAssign for Modulo {
    fn add_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 += other.0;
        if self.0 >= m {
            self.0 -= m;
        }
    }
}
impl std::ops::MulAssign for Modulo {
    fn mul_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 *= other.0;
        self.0 %= m;
    }
}
impl std::ops::SubAssign for Modulo {
    fn sub_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 += m - other.0;
        if self.0 >= m {
            self.0 -= m;
        }
    }
}
macro_rules! impl_modulo_ops {
    ($imp:ident, $method:ident, $assign_imp:ident, $assign_method:ident) => {
        impl<'a> std::ops::$assign_imp<&'a Modulo> for Modulo {
            fn $assign_method(&mut self, other: &'a Modulo) {
                std::ops::$assign_imp::$assign_method(self, *other);
            }
        }
        impl std::ops::$imp for Modulo {
            type Output = Modulo;
            fn $method(self, other: Modulo) -> Modulo {
                let mut x = self;
                std::ops::$assign_imp::$assign_method(&mut x, other);
                x
            }
        }
        impl<'a> std::ops::$imp<Modulo> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: Modulo) -> Modulo {
                std::ops::$imp::$method(*self, other)
            }
        }
        impl<'a> std::ops::$imp<&'a Modulo> for Modulo {
            type Output = Modulo;
            fn $method(self, other: &'a Modulo) -> Modulo {
                std::ops::$imp::$method(self, *other)
            }
        }
        impl<'a, 'b> std::ops::$imp<&'b Modulo> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: &'b Modulo) -> Modulo {
                std::ops::$imp::$method(*self, *other)
            }
        }
        impl std::ops::$assign_imp<i64> for Modulo {
            fn $assign_method(&mut self, other: i64) {
                std::ops::$assign_imp::$assign_method(self, Modulo::new(other));
            }
        }
        impl<'a> std::ops::$assign_imp<&'a i64> for Modulo {
            fn $assign_method(&mut self, other: &'a i64) {
                std::ops::$assign_imp::$assign_method(self, *other);
            }
        }
        impl std::ops::$imp<i64> for Modulo {
            type Output = Modulo;
            fn $method(self, other: i64) -> Modulo {
                let mut x = self;
                std::ops::$assign_imp::$assign_method(&mut x, other);
                x
            }
        }
        impl<'a> std::ops::$imp<&'a i64> for Modulo {
            type Output = Modulo;
            fn $method(self, other: &'a i64) -> Modulo {
                std::ops::$imp::$method(self, *other)
            }
        }
        impl<'a> std::ops::$imp<i64> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: i64) -> Modulo {
                std::ops::$imp::$method(*self, other)
            }
        }
        impl<'a, 'b> std::ops::$imp<&'b i64> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: &'b i64) -> Modulo {
                std::ops::$imp::$method(*self, *other)
            }
        }
    };
}
impl_modulo_ops!(Add, add, AddAssign, add_assign);
impl_modulo_ops!(Mul, mul, MulAssign, mul_assign);
impl_modulo_ops!(Sub, sub, SubAssign, sub_assign);

use std::iter::Sum;
impl Sum for Modulo {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = Modulo>,
    {
        iter.fold(Modulo(0), |a, b| a + b)
    }
}

impl<'a> Sum<&'a Modulo> for Modulo {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = &'a Self>,
    {
        iter.fold(Modulo(0), |a, b| a + b)
    }
}
0