結果

問題 No.2625 Bouns Ai
ユーザー nautnaut
提出日時 2024-02-09 23:12:07
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 9,326 bytes
コンパイル時間 1,314 ms
コンパイル使用メモリ 194,280 KB
実行使用メモリ 13,480 KB
最終ジャッジ日時 2024-02-09 23:12:21
合計ジャッジ時間 4,793 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
13,480 KB
testcase_01 AC 7 ms
6,676 KB
testcase_02 AC 149 ms
6,676 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let N = input!(usize);
    let A = input!(usize, N);

    let mut dp = SegmentTree::new(100_001, |&x, &y| x + y, ModInt::<998244353>::new());

    for i in 0..=100_000 {
        if A[0] <= i {
            dp.insert(i, ModInt::<998244353>::from_raw(1));
        }
    }

    for i in 1..N {
        let mut nxtdp = SegmentTree::new(100_001, |&x, &y| x + y, ModInt::<998244353>::new());

        for j in 0..=100_000 {
            let mut c = j as isize + A[i - 1] as isize - A[i] as isize;

            if c > 100_000 {
                c = 100_000;
            } else if c < 0 {
                continue;
            }

            let c = c as usize;

            nxtdp.insert(j, dp.prod(..=std::cmp::min(c, j)));
        }

        dp = nxtdp;
    }

    let ans = dp.prod(..);

    writeln!(out, "{}", ans);
}

pub struct SegmentTree<M> {
    size: usize,
    tree: Vec<M>,
    op: fn(&M, &M) -> M,
    id: M,
}

impl<M: Copy + PartialEq> SegmentTree<M> {
    /// self.tree = [id; size], self.op = op, self.id = id
    pub fn new(size: usize, op: fn(&M, &M) -> M, id: M) -> Self {
        return Self {
            size: size,
            tree: vec![id; 2 * size],
            op: op,
            id: id,
        };
    }

    /// self.tree = arr, self.op = op, self.id = id
    pub fn from(arr: &[M], op: fn(&M, &M) -> M, id: M) -> Self {
        let size = arr.len();
        let mut tree = vec![id; 2 * size];

        for i in 0..size {
            tree[i + size] = arr[i];

            assert!(
                op(&id, &arr[i]) == arr[i],
                "id is not the identity element of given operator"
            );
        }

        for i in (1..size).rev() {
            tree[i] = op(&tree[i << 1], &tree[i << 1 | 1]);
        }

        return Self {
            size: size,
            tree: tree,
            op: op,
            id: id,
        };
    }

    /// self.tree[pos] <- value
    pub fn insert(&mut self, mut pos: usize, value: M) -> () {
        pos += self.size;
        self.tree[pos] = value;

        while pos > 1 {
            pos >>= 1;
            self.tree[pos] = (self.op)(&self.tree[pos << 1], &self.tree[pos << 1 | 1]);
        }
    }

    /// return self.tree[pos] (syntax sugar: self[pos])
    pub fn get_point(&self, pos: usize) -> M {
        return self[pos];
    }

    /// return Π_{i ∈ [left, right)} self.tree[i]
    pub fn get(&self, left: usize, right: usize) -> M {
        let (mut l, mut r) = (left + self.size, right + self.size);
        let (mut vl, mut vr) = (self.id, self.id);

        while l < r {
            if l & 1 == 1 {
                vl = (self.op)(&vl, &self.tree[l]);
                l += 1;
            }

            if r & 1 == 1 {
                r -= 1;
                vr = (self.op)(&self.tree[r], &vr);
            }

            l >>= 1;
            r >>= 1;
        }

        return (self.op)(&vl, &vr);
    }

    /// return Π_{i ∈ range} self.tree[i]
    pub fn prod<R: std::ops::RangeBounds<usize>>(&self, range: R) -> M {
        let left = match range.start_bound() {
            std::ops::Bound::Included(&l) => l,
            std::ops::Bound::Excluded(&l) => l + 1,
            std::ops::Bound::Unbounded => 0,
        };

        let right = match range.end_bound() {
            std::ops::Bound::Included(&r) => r + 1,
            std::ops::Bound::Excluded(&r) => r,
            std::ops::Bound::Unbounded => self.size,
        };

        return self.get(left, right);
    }
}

impl<M> std::ops::Index<usize> for SegmentTree<M> {
    type Output = M;
    fn index(&self, index: usize) -> &Self::Output {
        &self.tree[index + self.size]
    }
}

impl<M: std::fmt::Display> std::fmt::Display for SegmentTree<M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            self.tree[self.size..]
                .iter()
                .map(|x| x.to_string())
                .collect::<Vec<_>>()
                .join(" ")
        )
    }
}

#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub struct ModInt<const P: u32> {
    value: u32,
}

impl<const P: u32> ModInt<P> {
    pub fn value(&self) -> u32 {
        assert!(self.value < P);
        return self.value;
    }

    pub fn new() -> Self {
        return Self { value: 0 };
    }

    pub fn from_raw(x: u32) -> Self {
        return Self { value: x };
    }

    pub fn from_usize(x: usize) -> Self {
        return Self {
            value: (x % P as usize) as u32,
        };
    }

    pub fn from_isize(x: isize) -> Self {
        let mut value = x % P as isize;
        if value < 0 {
            value += P as isize;
        }

        return Self {
            value: value as u32,
        };
    }

    pub fn inv(&self) -> Self {
        pub fn ext_gcd(a: isize, b: isize) -> (isize, isize) {
            let mut a_k = a;
            let mut b_k = b;

            let mut q_k = a_k / b_k;
            let mut r_k = a_k % b_k;

            let mut x_k = 0;
            let mut y_k = 1;
            let mut z_k = 1;
            let mut w_k = -q_k;

            a_k = b_k;
            b_k = r_k;

            while r_k != 0 {
                q_k = a_k / b_k;
                r_k = a_k % b_k;

                a_k = b_k;
                b_k = r_k;

                let nx = z_k;
                let ny = w_k;
                let nz = x_k - q_k * z_k;
                let nw = y_k - q_k * w_k;

                x_k = nx;
                y_k = ny;
                z_k = nz;
                w_k = nw;
            }

            (x_k, y_k)
        }

        let val = self.value() as isize;

        let ret = ext_gcd(val, P as isize).0;

        return Self::from_isize(ret);
    }

    pub fn pow(&self, mut x: u64) -> Self {
        let mut ret = ModInt::from_raw(1);
        let mut a = self.clone();

        while x > 0 {
            if (x & 1) == 1 {
                ret = ret * a;
            }

            a *= a;
            x >>= 1;
        }

        return ret;
    }
}

impl<const P: u32> std::ops::Add for ModInt<P> {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        let mut ret = self.value() + rhs.value();

        if ret >= P {
            ret -= P;
        }

        return Self::from_raw(ret);
    }
}

impl<const P: u32> std::ops::AddAssign for ModInt<P> {
    fn add_assign(&mut self, rhs: Self) {
        self.value = (self.clone() + rhs).value();
    }
}

impl<const P: u32> std::ops::Sub for ModInt<P> {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        if self.value() >= rhs.value() {
            return Self::from_raw(self.value() - rhs.value());
        } else {
            return Self::from_raw(P + self.value() - rhs.value());
        }
    }
}

impl<const P: u32> std::ops::SubAssign for ModInt<P> {
    fn sub_assign(&mut self, rhs: Self) {
        self.value = (self.clone() - rhs).value();
    }
}

impl<const P: u32> std::ops::Mul for ModInt<P> {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        let ret = self.value() as usize * rhs.value() as usize;
        return Self::from_usize(ret);
    }
}

impl<const P: u32> std::ops::MulAssign for ModInt<P> {
    fn mul_assign(&mut self, rhs: Self) {
        self.value = (self.clone() * rhs).value();
    }
}

impl<const P: u32> std::ops::Div for ModInt<P> {
    type Output = Self;
    fn div(self, rhs: Self) -> Self::Output {
        self * rhs.inv()
    }
}

impl<const P: u32> std::ops::DivAssign for ModInt<P> {
    fn div_assign(&mut self, rhs: Self) {
        self.value = (self.clone() / rhs).value();
    }
}

impl<const P: u32> std::ops::Neg for ModInt<P> {
    type Output = Self;
    fn neg(self) -> Self::Output {
        let value = self.value();
        return Self { value: P - value };
    }
}

impl<const P: u32> std::fmt::Display for ModInt<P> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.value)
    }
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0