結果

問題 No.2734 Addition and Multiplication in yukicoder (Hard)
ユーザー naut3naut3
提出日時 2024-04-19 22:47:05
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 827 ms / 5,000 ms
コード長 6,258 bytes
コンパイル時間 15,103 ms
コンパイル使用メモリ 378,488 KB
実行使用メモリ 18,560 KB
最終ジャッジ日時 2024-10-11 16:37:57
合計ジャッジ時間 29,482 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 1 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 1 ms
5,248 KB
testcase_10 AC 1 ms
5,248 KB
testcase_11 AC 1 ms
5,248 KB
testcase_12 AC 1 ms
5,248 KB
testcase_13 AC 1 ms
5,248 KB
testcase_14 AC 1 ms
5,248 KB
testcase_15 AC 1 ms
5,248 KB
testcase_16 AC 1 ms
5,248 KB
testcase_17 AC 675 ms
16,064 KB
testcase_18 AC 678 ms
16,128 KB
testcase_19 AC 1 ms
5,248 KB
testcase_20 AC 1 ms
5,248 KB
testcase_21 AC 2 ms
5,248 KB
testcase_22 AC 826 ms
18,560 KB
testcase_23 AC 824 ms
18,360 KB
testcase_24 AC 827 ms
18,560 KB
testcase_25 AC 824 ms
18,432 KB
testcase_26 AC 824 ms
18,432 KB
testcase_27 AC 805 ms
17,536 KB
testcase_28 AC 814 ms
18,016 KB
testcase_29 AC 813 ms
18,048 KB
testcase_30 AC 783 ms
17,028 KB
testcase_31 AC 824 ms
18,244 KB
testcase_32 AC 767 ms
17,152 KB
testcase_33 AC 538 ms
13,208 KB
testcase_34 AC 691 ms
15,596 KB
testcase_35 AC 357 ms
9,472 KB
testcase_36 AC 389 ms
10,112 KB
testcase_37 AC 564 ms
13,824 KB
testcase_38 AC 550 ms
13,440 KB
権限があれば一括ダウンロードができます

ソースコード

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 mut A = input!(String, N);

    A.sort_by(|a1, a2| {
        let mut s1 = a1.clone();
        let mut s2 = a2.clone();

        s1 += &s2.clone();
        s2 += &s1.clone();

        s1.cmp(&s2)
    });

    let mut X = MInt998244353::new();

    for a in A {
        let n: usize = a.parse().unwrap();
        let p = MInt998244353::from_raw(10).pow(a.len() as u64);
        X *= p;
        X += n.into();
    }

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

pub type MInt998244353 = ModInt<998244353>;
pub type MInt1000000007 = ModInt<1_000_000_007>;

#[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 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(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(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 };
    }
}

macro_rules! int_from_impl {
    ($($t: ty), *) => {
        $(
            #[allow(unused_comparisons)]
            impl<const P: u32> From<$t> for ModInt<P> {
                fn from(value: $t) -> Self {
                    if value >= 0 {
                        Self {
                            value: (value % P as $t) as u32,
                        }
                    } else {
                        let rem = P as $t + value % P as $t;
                        Self { value: rem as u32 }
                    }
                }
            }
        ) *
    };
}

int_from_impl!(usize, isize, u64, i64, u32, i32, u128, i128);

impl<const P: u32> Default for ModInt<P> {
    fn default() -> Self {
        Self { value: 0 }
    }
}

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