結果

問題 No.2995 The Ruler Sequence Concatenation
ユーザー akakimidoriakakimidori
提出日時 2024-12-20 20:36:43
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 6,673 bytes
コンパイル時間 14,902 ms
コンパイル使用メモリ 388,500 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-20 20:37:00
合計ジャッジ時間 15,788 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,820 KB
testcase_04 AC 1 ms
6,820 KB
testcase_05 AC 1 ms
6,820 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 1 ms
6,816 KB
testcase_09 AC 1 ms
6,816 KB
testcase_10 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    input!(n: usize);
    println!("{}", fast(n));
}

type M = ModInt<998244353>;
type E = ModInt<998244352>;

fn fast(n: usize) -> M {
    if n < 10 {
        return naive(n).0;
    }
    let mut now = 9;
    let (mut a, mut b) = naive(now);
    while now < n {
        let up = n.min((now + 1) * 10 - 1);
        let p = up - now;
        let d = M::from((now + 1) * 10);
        let r = d * b;
        let mut na = M::zero();
        na += a * (r.pow(E::new(2).pow(p).0 as usize) - M::one()) / (r - M::one());
        let mut add = M::from(now + 1);
        let mut map = std::collections::BTreeMap::new();
        let mut i = 0;
        while i <= p {
            let e = E::new(2).pow(i).0;
            let po = map.entry(e).or_insert((i, M::zero()));
            if po.0 != i && p - i > 200 {
                let len = i - po.0;
                let q = (p - i) / len;
                for j in i..(i + len) {
                    let e = E::new(2).pow(j).0;
                    map.entry(e).or_insert((i, M::zero())).1 += M::from(q);
                }
                i += len * q;
            } else {
                po.1 += add;
                i += 1;
            }
            add = M::one();
        }
        let base = d.inv();
        for (e, (_, q)) in map {
            let re = r.pow(e as usize);
            na += base * q * (re - r.pow(E::new(2).pow(p).0 as usize)) / (M::one() - re);
        }
        a = na;
        let ep = E::new(2).pow(p);
        b = b.pow(ep.0 as usize) * d.pow((ep - E::one()).0 as usize);
        now = up;
    }
    a
}

fn naive(n: usize) -> (M, M) {
    let mut v = M::zero();
    let mut d = M::one();
    let mut k = 1;
    for i in 1..=n {
        if k <= i {
            k *= 10;
        }
        v = v * (M::one() + d * M::from(k)) + d * M::from(i);
        d = d * d * M::from(k);
    }
    (v, d)
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
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)*}
    };
}

#[macro_export]
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)*}
    };
}

#[macro_export]
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, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------

use std::ops::*;

pub const fn is_prime(n: u32) -> bool {
    if n <= 1 {
        return false;
    }
    let mut d = 2;
    while d * d <= n {
        if n % d == 0 {
            return false;
        }
        d += 1;
    }
    true
}

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct ModInt<const M: u32>(u32);

impl<const M: u32> ModInt<{ M }> {
    const IS_PRIME: bool = is_prime(M);
    pub const fn new(v: u32) -> Self {
        Self(v % M)
    }
    pub const fn const_mul(self, rhs: Self) -> Self {
        Self((self.0 as u64 * rhs.0 as u64 % M as u64) as u32)
    }
    pub const fn pow(&self, mut n: usize) -> Self {
        let mut t = Self::new(1);
        let mut r = *self;
        while n > 0 {
            if n & 1 == 1 {
                t = t.const_mul(r);
            }
            r = r.const_mul(r);
            n >>= 1;
        }
        t
    }
    pub const fn inv(&self) -> Self {
        assert!(Self::IS_PRIME);
        assert!(self.0 != 0);
        self.pow(M as usize - 2)
    }
    pub const fn zero() -> Self {
        Self::new(0)
    }
    pub const fn one() -> Self {
        Self::new(1)
    }
}

impl<const M: u32> Add for ModInt<{ M }> {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        let mut v = self.0 + rhs.0;
        if v >= M {
            v -= M;
        }
        Self(v)
    }
}

impl<const M: u32> Sub for ModInt<{ M }> {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        let mut v = self.0 - rhs.0;
        if self.0 < rhs.0 {
            v += M;
        }
        Self(v)
    }
}

impl<const M: u32> Mul for ModInt<{ M }> {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        self.const_mul(rhs)
    }
}

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

impl<const M: u32> AddAssign for ModInt<{ M }> {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl<const M: u32> SubAssign for ModInt<{ M }> {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl<const M: u32> MulAssign for ModInt<{ M }> {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl<const M: u32> DivAssign for ModInt<{ M }> {
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

impl<const M: u32> Neg for ModInt<{ M }> {
    type Output = Self;
    fn neg(self) -> Self::Output {
        if self.0 == 0 {
            self
        } else {
            Self(M - self.0)
        }
    }
}

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

impl<const M: u32> std::fmt::Debug for ModInt<{ M }> {
    fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<const M: u32> std::str::FromStr for ModInt<{ M }> {
    type Err = std::num::ParseIntError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let val = s.parse::<u32>()?;
        Ok(ModInt::new(val))
    }
}

impl<const M: u32> From<usize> for ModInt<{ M }> {
    fn from(val: usize) -> ModInt<{ M }> {
        ModInt::new((val % M as usize) as u32)
    }
}
0