結果
問題 | No.1847 Good Sequence |
ユーザー | koba-e964 |
提出日時 | 2022-02-20 13:59:43 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 397 ms / 3,000 ms |
コード長 | 4,634 bytes |
コンパイル時間 | 13,787 ms |
コンパイル使用メモリ | 388,744 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-29 10:48:31 |
合計ジャッジ時間 | 17,683 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 186 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 5 ms
5,376 KB |
testcase_09 | AC | 7 ms
5,376 KB |
testcase_10 | AC | 1 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 1 ms
5,376 KB |
testcase_13 | AC | 1 ms
5,376 KB |
testcase_14 | AC | 3 ms
5,376 KB |
testcase_15 | AC | 6 ms
5,376 KB |
testcase_16 | AC | 13 ms
5,376 KB |
testcase_17 | AC | 29 ms
5,376 KB |
testcase_18 | AC | 45 ms
5,376 KB |
testcase_19 | AC | 88 ms
5,376 KB |
testcase_20 | AC | 1 ms
5,376 KB |
testcase_21 | AC | 385 ms
5,376 KB |
testcase_22 | AC | 59 ms
5,376 KB |
testcase_23 | AC | 1 ms
5,376 KB |
testcase_24 | AC | 1 ms
5,376 KB |
testcase_25 | AC | 110 ms
5,376 KB |
testcase_26 | AC | 1 ms
5,376 KB |
testcase_27 | AC | 10 ms
5,376 KB |
testcase_28 | AC | 118 ms
5,376 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 10 ms
5,376 KB |
testcase_31 | AC | 219 ms
5,376 KB |
testcase_32 | AC | 53 ms
5,376 KB |
testcase_33 | AC | 206 ms
5,376 KB |
testcase_34 | AC | 1 ms
5,376 KB |
testcase_35 | AC | 1 ms
5,376 KB |
testcase_36 | AC | 397 ms
5,376 KB |
testcase_37 | AC | 115 ms
5,376 KB |
testcase_38 | AC | 1 ms
5,376 KB |
testcase_39 | AC | 1 ms
5,376 KB |
testcase_40 | AC | 364 ms
5,376 KB |
ソースコード
use std::cmp::*; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 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 ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } // Registry. #[derive(Clone, Debug)] struct Reg<T> { a: Vec<T>, inv: std::collections::HashMap<T, usize>, } impl<T: std::hash::Hash + Eq + Clone> Reg<T> { fn get(&mut self, t: &T) -> usize { if !self.inv.contains_key(t) { let idx = self.a.len(); self.a.push(t.clone()); self.inv.insert(t.clone(), idx); } self.inv[t] } // init must have distinct elements. fn init<F>(&mut self, init: &[T], f: F) -> Vec<Vec<i64>> where F: Fn(T) -> Vec<T> { let mut que = std::collections::VecDeque::new(); for t in init { let idx = self.get(t); que.push_back(idx); } let mut n = self.a.len(); let mut vis = vec![false; n]; let mut to = vec![vec![]; n]; while let Some(v) = que.pop_front() { if vis[v] { continue; } let ans = f(self.a[v].clone()); let mut entries = vec![]; for elem in ans { let idx = self.get(&elem); entries.push(idx); if n <= idx { // A newly created entry. n = self.a.len(); vis.resize(n, false); to.resize(n, vec![]); que.push_back(idx); } } vis[v] = true; to[v] = entries; } let mut ans = vec![vec![0; n]; n]; for i in 0..n { for &e in &to[i] { ans[i][e] += 1; } } ans } pub fn find_index(&self, t: &T) -> usize { self.inv[t] } // init must have distinct elements. pub fn new<F>(init: &[T], f: F) -> (Self, Vec<Vec<i64>>) where F: Fn(T) -> Vec<T> { let mut me = Reg { a: vec![], inv: std::collections::HashMap::default() }; let res = me.init(init, f); (me, res) } } fn squmul(a: &[Vec<i64>], b: &[Vec<i64>], mo: i64) -> Vec<Vec<i64>> { let n = a.len(); let mut ret = vec![vec![0; n]; n]; for i in 0..n { for j in 0..n { for k in 0..n { ret[i][k] += a[i][j] * b[j][k]; ret[i][k] %= mo; } } } ret } fn squpow(a: &[Vec<i64>], mut e: i64, mo: i64) -> Vec<Vec<i64>> { let n = a.len(); let mut sum = vec![vec![0; n]; n]; for i in 0..n { sum[i][i] = 1; } let mut cur = a.to_vec(); while e > 0 { if e % 2 == 1 { sum = squmul(&sum, &cur, mo); } cur = squmul(&cur, &cur, mo); e /= 2; } sum } const MOD: i64 = 1_000_000_007; // Tags: matrix-exponentiation fn main() { input! { l: i64, n: usize, m: usize, k: [usize; m], } let mut pop = vec![false; n]; for k in k { pop[k - 1] = true; } let init = (-1, -1); let next = |(x, y)| -> Vec<(i32, i32)> { if (x, y) == (-2, -2) { return vec![(-2, -2); n]; } let mut v = vec![]; for i in 0..n as i32 { if x == i { v.push((x, min(n as i32, y + 1))); } else if x >= 0 && x == y && pop[x as usize] { v.push((-2, -2)); } else { v.push((i, 0)); } } v }; let (reg, mat) = Reg::new(&[init, (-2, -2)], next); let pw = squpow(&mat, l, MOD); let ii = reg.find_index(&init); let mut tot = pw[ii][reg.find_index(&(-2, -2))]; for i in 0..n as i32 { if pop[i as usize] { tot += pw[ii][reg.find_index(&(i, i))]; tot %= MOD; } } println!("{}", tot); }