結果

問題 No.1172 Add Recursive Sequence
ユーザー noshi91noshi91
提出日時 2020-05-09 22:50:17
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 263 ms / 4,000 ms
コード長 3,929 bytes
コンパイル時間 13,897 ms
コンパイル使用メモリ 378,564 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2024-10-08 20:15:12
合計ジャッジ時間 15,856 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 8 ms
5,248 KB
testcase_11 AC 7 ms
5,248 KB
testcase_12 AC 6 ms
5,248 KB
testcase_13 AC 6 ms
5,248 KB
testcase_14 AC 70 ms
13,440 KB
testcase_15 AC 34 ms
10,240 KB
testcase_16 AC 263 ms
13,440 KB
testcase_17 AC 216 ms
10,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{self, Read as _, Write as _};

struct Scanner<'a>(std::str::SplitWhitespace<'a>);

impl<'a> Scanner<'a> {
    fn new(s: &'a str) -> Self {
        Self(s.split_whitespace())
    }

    fn next<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        let s = self.0.next().expect("found EOF");
        match s.parse() {
            Ok(v) => v,
            Err(msg) => {
                println!(
                    "parse error. T = {}, s = \"{}\": {:?}",
                    std::any::type_name::<T>(),
                    s,
                    msg
                );
                panic!()
            }
        }
    }
}

mod fp {

    use std::convert::From;
    use std::ops;

    const P: u32 = 1000000007;

    #[derive(Copy, Clone)]
    pub struct Fp(pub u32);

    impl From<i64> for Fp {
        fn from(mut x: i64) -> Fp {
            x %= P as i64;
            if x < 0 {
                x += P as i64;
            }
            Fp(x as u32)
        }
    }

    impl ops::Add<Fp> for Fp {
        type Output = Fp;
        fn add(mut self, rhs: Fp) -> Fp {
            self += rhs;
            self
        }
    }

    impl ops::AddAssign<Fp> for Fp {
        fn add_assign(&mut self, rhs: Fp) {
            self.0 += rhs.0;
            if self.0 >= P {
                self.0 -= P;
            }
        }
    }

    impl ops::Mul<Fp> for Fp {
        type Output = Fp;
        fn mul(mut self, rhs: Fp) -> Fp {
            self *= rhs;
            self
        }
    }

    impl ops::MulAssign<Fp> for Fp {
        fn mul_assign(&mut self, rhs: Fp) {
            self.0 = (self.0 as u64 * rhs.0 as u64 % P as u64) as u32;
        }
    }

    impl ops::Neg for Fp {
        type Output = Fp;
        fn neg(self) -> Fp {
            Fp(match self.0 {
                0 => 0,
                s => P - s,
            })
        }
    }

    impl ops::Sub<Fp> for Fp {
        type Output = Fp;
        fn sub(mut self, rhs: Fp) -> Fp {
            self -= rhs;
            self
        }
    }

    impl ops::SubAssign<Fp> for Fp {
        fn sub_assign(&mut self, rhs: Fp) {
            if self.0 < rhs.0 {
                self.0 += P;
            }
            self.0 -= rhs.0;
        }
    }

    impl std::iter::Sum for Fp {
        fn sum<I>(iter: I) -> Fp
        where
            I: Iterator<Item = Fp>,
        {
            iter.fold(Fp(0), |s, x| s + x)
        }
    }
}

use fp::Fp;

fn main() {
    let mut stdin = String::new();
    std::io::stdin().read_to_string(&mut stdin).unwrap();
    let mut sc = Scanner::new(&stdin);
    let stdout = io::stdout();
    let mut stdout = io::BufWriter::new(stdout.lock());

    let k = sc.next();
    let n = sc.next();
    let m = sc.next();
    let mut a = (0..k).map(|_| Fp(sc.next())).collect::<Vec<_>>();
    let c = (0..k)
        .map(|_| Fp(sc.next()))
        .collect::<Vec<_>>()
        .into_iter()
        .rev()
        .collect::<Vec<_>>();

    let mut ls = vec![Vec::new(); n + 1];
    let mut rs = vec![Vec::new(); n + 1];
    for _ in 0..m {
        let (l, r): (usize, usize) = (sc.next(), sc.next());
        ls[l].push(0);
        rs[r].push(r - l);
    }

    a.reserve_exact(n);
    for i in 0..n {
        a.push(c.iter().zip(&a[i..]).map(|(&c, &a)| c * a).sum());
    }

    let mut state = vec![Fp(0); k];

    for (ls, rs) in ls.into_iter().zip(rs).take(n) {
        {
            let next = c.iter().zip(&state).map(|(&c, &s)| c * s).sum();
            state.remove(0);
            state.push(next);
        }

        for l in ls {
            for (s, &a) in state.iter_mut().zip(&a[l..]) {
                *s += a;
            }
        }

        for l in rs {
            for (s, &a) in state.iter_mut().zip(&a[l..]) {
                *s -= a;
            }
        }

        writeln!(stdout, "{}", state[0].0).unwrap();
    }

    stdout.flush().unwrap();
}
0