結果

問題 No.2568 列辞書順列列
ユーザー Kyo_s_sKyo_s_s
提出日時 2023-11-27 17:14:29
言語 Rust
(1.77.0)
結果
AC  
実行時間 256 ms / 3,000 ms
コード長 2,788 bytes
コンパイル時間 1,506 ms
コンパイル使用メモリ 188,300 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2023-11-27 17:14:43
合計ジャッジ時間 13,762 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 159 ms
6,676 KB
testcase_03 AC 158 ms
6,676 KB
testcase_04 AC 197 ms
9,728 KB
testcase_05 AC 199 ms
9,728 KB
testcase_06 AC 198 ms
9,728 KB
testcase_07 AC 225 ms
9,728 KB
testcase_08 AC 199 ms
9,728 KB
testcase_09 AC 217 ms
9,728 KB
testcase_10 AC 242 ms
9,728 KB
testcase_11 AC 215 ms
9,728 KB
testcase_12 AC 228 ms
9,728 KB
testcase_13 AC 255 ms
9,728 KB
testcase_14 AC 229 ms
9,728 KB
testcase_15 AC 229 ms
9,728 KB
testcase_16 AC 256 ms
9,728 KB
testcase_17 AC 212 ms
9,728 KB
testcase_18 AC 217 ms
9,728 KB
testcase_19 AC 238 ms
9,728 KB
testcase_20 AC 210 ms
9,728 KB
testcase_21 AC 210 ms
9,728 KB
testcase_22 AC 232 ms
9,728 KB
testcase_23 AC 211 ms
9,728 KB
testcase_24 AC 212 ms
9,728 KB
testcase_25 AC 238 ms
9,728 KB
testcase_26 AC 212 ms
9,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::vec;

macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

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

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

#[derive(Debug, Clone, Copy)]
struct ModInt {
    value: usize,
}

impl ModInt {
    fn modulo() -> usize {
        998244353
    }

    fn new(value: usize) -> Self {
        Self {
            value: value % Self::modulo(),
        }
    }

    fn pow(self, n: usize) -> Self {
        let mut res = Self::new(1);
        let mut x = self;
        let mut n = n;
        while n > 0 {
            if n & 1 == 1 {
                res = res * x;
            }
            x = x * x;
            n >>= 1;
        }
        res
    }
}

impl std::ops::Add for ModInt {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Self::new(self.value + rhs.value)
    }
}

impl std::ops::Sub for ModInt {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        Self::new(self.value + Self::modulo() - rhs.value)
    }
}

impl std::ops::Mul for ModInt {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self {
        Self::new(self.value * rhs.value)
    }
}

impl std::ops::Div for ModInt {
    type Output = Self;

    fn div(self, rhs: Self) -> Self {
        self * rhs.pow(Self::modulo() - 2)
    }
}

fn main() {
    input! {
        n: usize,
        m: usize,
        q: usize,
        a: [usize; n],
        query: [(usize, usize); q],
    }

    let m = ModInt::new(m);
    let m_pow = (0..n).map(|i| m.pow(i)).collect::<Vec<_>>();
    let b = (0..n)
        .map(|i| ModInt::new(a[i] - 1) * m_pow[n - i - 1])
        .collect::<Vec<_>>();
    let cs = {
        let mut cs = vec![ModInt::new(0); 1];
        for &b in &b {
            cs.push(cs[cs.len() - 1] + b);
        }
        cs
    };

    for (l, r) in query {
        let l = l - 1;
        let sum = cs[r] - cs[l];
        let ans = sum / m_pow[n - r] + ModInt::new(1);
        println!("{}", ans.value);
    }
}
0