結果

問題 No.3044 よくあるカエルさん
ユーザー ArcAki
提出日時 2025-03-01 03:32:35
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 4,188 bytes
コンパイル時間 13,550 ms
コンパイル使用メモリ 405,264 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2025-03-10 05:01:43
合計ジャッジ時間 13,412 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::{
    cell::RefCell, convert::{Infallible, TryFrom, TryInto as _},
    fmt::{self, Debug, Display, Formatter,},
    fs::{File}, hash::{Hash, Hasher}, iter::{Product, Sum}, marker::PhantomData,
    ops::{Add, AddAssign, Sub, SubAssign, Div, DivAssign, Mul, MulAssign, Neg, },
    str::FromStr, sync::{atomic::{self, AtomicU32, AtomicU64}, Once}, collections::{*},
    mem::{swap}, cmp::{self, Reverse, Ordering, Eq, PartialEq, PartialOrd},
    thread::LocalKey, f64::consts::PI, time::Instant,
    io::{self, stdin, Read, read_to_string, BufWriter, BufReader, stdout, Write},
};
#[allow(unused_imports)]
use proconio::{input, input_interactive, marker::{*}};
#[allow(unused_imports)]
//use rand::{thread_rng, Rng, seq::SliceRandom};
#[allow(unused_imports)]
//use ac_library::{*};

#[allow(dead_code)]
const INF: i64 = 1<<60;
#[allow(dead_code)]
const MOD: i64 = 998244353;
#[allow(dead_code)]
const D: [(usize, usize); 4] = [(1, 0), (0, 1), (!0, 0), (0, !0)];

pub fn floor(a:i64, b:i64)->i64{let res=(a%b+b)%b;(a-res)/b}
pub fn extended_gcd(a:i64,b:i64)->(i64,i64,i64)
{if b==0{(a,1,0)}else{let(g,x,y)=extended_gcd(b,a%b);(g,y,x-floor(a,b)*y)}}
pub fn mod_inverse(a:i64,m:i64)->i64{let(_,x,_) =extended_gcd(a,m);(x%m+m)%m}
pub fn comb(a: i64, b: i64, f: &Vec<(i64, i64)>)->i64{
    if a<b{return 0;}if b==0 || a==b{ return 1; }
    else{let x=f[a as usize].0;
        let y=f[(a-b) as usize].1;let z=f[b as usize].1;return((x*y)%MOD)*z%MOD;}}
pub fn factorial(x: i64)->Vec<(i64, i64)>{
    let mut f=vec![(1i64,1i64),(1, 1)];let mut z = 1i64;
    let mut inv = vec![0; x as usize+10];inv[1] = 1;
    for i in 2..x+1{z=(z*i)%MOD;
        let w=(MOD-inv[(MOD%i)as usize]*(MOD/i)%MOD)%MOD;
        inv[i as usize] = w;
        f.push((z, (f[i as usize-1].1*w)%MOD));}return f;}
pub fn fast_mod_pow(x: i64,p: usize, m: i64)->i64{
    let mut res=1;let mut t=x;let mut z=p;while z > 0{
        if z%2==1{res = (res*t)%m;}t = (t*t)%m;z /= 2; }res}

#[derive(Clone)]
pub struct Matrix{
    r: usize,
    c: usize,
    v: Vec<Vec<i64>>,
}

impl Matrix {
    pub fn new(r: usize, c: usize, row: &Vec<i64>)->Self{
        let mut v = Vec::new();
        for i in 0..r{
            let mut nex = Vec::new();
            for j in 0..c{
                nex.push(row[i*c+j]);
            }
            v.push(nex.clone());
        }
        Matrix{
            r, c, v
        }
    }

    pub fn identity(n: usize)->Self{
        let mut res = Matrix::new(n, n, &vec![0; n*n]);
        for i in 0..n{
            res.v[i][i] = 1;
        }
        res
    }

    pub fn zero(n: usize)->Self{
        Matrix::new(n, n, &vec![0; n*n])
    }

    pub fn set(&mut self, u: usize, v: usize, x: i64){
        self.v[u][v] = x;
    }

    pub fn multiply(&mut self, other: &Matrix)->Matrix{
        let (n, m) = (self.r, other.c);
        let mut res = Matrix::new(n, m, &vec![0; n*m]);
        for i in 0..n{
            for j in 0..m{
                for k in 0..self.c{
                    res.v[i][j] = (res.v[i][j]+self.v[i][k]*other.v[k][j])%MOD;
                }
            }
        }
        res
    }

    pub fn pow(&mut self, p: usize)->Matrix{
        let mut res = Matrix::identity(self.r);
        let mut pow = self.clone();
        let mut x = p;
        while x > 0{
            if x%2==1{
                res = res.multiply(&pow);
            }
            x /= 2;
            let pre = pow.clone();
            pow = pow.multiply(&pre);
        }
        res
    }
}

//use proconio::fastout;
//#[fastout]
fn main(){
    input!{
        n: usize, t: usize,
        k: i64, l: i64,
    }
    let mut dp = vec![0; t];
    let div = mod_inverse(6, MOD);
    dp[0] = 1;
    dp[1] = (k-1)*div%MOD;
    for i in 2..t{
        dp[i] = (dp[i-1]*(k-1)*div%MOD+dp[i-2]*(l-k)*div%MOD)%MOD;
    }
    let mut matrix = Matrix::zero(t);
    for i in 0..t-1{
        matrix.set(i, i+1, 1);
    }
    matrix.set(t-1, 0, (7-l)*div%MOD);
    matrix.set(t-1, t-2, (l-k)*div%MOD);
    matrix.set(t-1, t-1, (k-1)*div%MOD);
    matrix = matrix.pow(n-1);
    let mut ans = 0;
    for i in 0..t{
        ans = (ans+matrix.v[0][i]*dp[i])%MOD;
    }
    println!("{}", ans);
}
0