結果

問題 No.1879 How many matchings?
ユーザー akakimidoriakakimidori
提出日時 2022-03-18 22:10:55
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 4,612 bytes
コンパイル時間 13,383 ms
コンパイル使用メモリ 378,312 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-03 06:58:03
合計ジャッジ時間 11,461 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 WA -
testcase_03 AC 1 ms
5,248 KB
testcase_04 WA -
testcase_05 AC 1 ms
5,248 KB
testcase_06 WA -
testcase_07 AC 1 ms
5,248 KB
testcase_08 WA -
testcase_09 AC 1 ms
5,248 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
5,248 KB
testcase_14 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::io::Write`
   --> src/main.rs:153:5
    |
153 | use std::io::Write;
    |     ^^^^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default

warning: type alias `Map` is never used
   --> src/main.rs:156:6
    |
156 | type Map<K, V> = BTreeMap<K, V>;
    |      ^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
   --> src/main.rs:157:6
    |
157 | type Set<T> = BTreeSet<T>;
    |      ^^^

warning: type alias `Deque` is never used
   --> src/main.rs:158:6
    |
158 | type Deque<T> = VecDeque<T>;
    |      ^^^^^

ソースコード

diff #

pub trait SemiRing: Clone {
    fn zero() -> Self;
    fn one() -> Self;
    fn add(&self, rhs: &Self) -> Self;
    fn mul(&self, rhs: &Self) -> Self;
}

pub struct Kitamasa<T> {
    c: Vec<T>,
    tmp: std::cell::RefCell<Vec<T>>,
}

impl<T: SemiRing> Kitamasa<T> {
    pub fn new(c: Vec<T>) -> Self {
        assert!(!c.is_empty());
        Self {
            c: c,
            tmp: std::cell::RefCell::new(vec![]),
        }
    }
    pub fn normalize(&self, d: &mut Vec<T>) {
        let n = self.c.len();
        for i in (n..d.len()).rev() {
            let v = d.pop().unwrap();
            for (d, c) in d[(i - n)..].iter_mut().zip(&self.c) {
                *d = d.add(&v.mul(c));
            }
        }
    }
    pub fn next(&self, d: &mut Vec<T>) {
        d.insert(0, T::zero());
        self.normalize(d);
    }
    pub fn twice(&self, d: &mut Vec<T>) {
        assert!(!d.is_empty());
        let mut tmp = self.tmp.borrow_mut();
        tmp.clear();
        tmp.resize(d.len() * 2 - 1, T::zero());
        for (i, a) in d.iter().enumerate() {
            for (tmp, b) in tmp[i..].iter_mut().zip(d.iter()) {
                *tmp = tmp.add(&a.mul(b));
            }
        }
        std::mem::swap(&mut *tmp, d);
        drop(tmp);
        self.normalize(d);
    }
    pub fn merge(&self, x: &[T], y: &[T]) -> Vec<T> {
        assert!(x.len() > 0 && y.len() > 0);
        let mut res = vec![T::zero(); x.len() + y.len() - 1];
        for (i, a) in x.iter().enumerate() {
            for (res, b) in res[i..].iter_mut().zip(y) {
                *res = a.mul(b).add(res);
            }
        }
        self.normalize(&mut res);
        res
    }
    pub fn kth_coefficient(&self, k: usize) -> Vec<T> {
        let mut t = vec![T::one()];
        if k > 0 {
            let p = (k + 1).next_power_of_two().trailing_zeros() - 1;
            for i in (0..=p).rev() {
                self.twice(&mut t);
                if k >> i & 1 == 1 {
                    self.next(&mut t);
                }
            }
        }
        t.resize(self.c.len(), T::zero());
        t
    }
    pub fn find_kth(&self, a: &[T], k: usize) -> T {
        assert_eq!(a.len(), self.c.len());
        let d = self.kth_coefficient(k);
        a.iter()
            .zip(d)
            .fold(T::zero(), |s, a| a.0.mul(&a.1).add(&s))
    }
}

const MOD: u64 = 1_000_000_007;

impl SemiRing for u64 {
    fn zero() -> Self {
        0
    }
    fn one() -> Self {
        1
    }
    fn add(&self, rhs: &Self) -> Self {
        let mut v = *self + *rhs;
        if v >= MOD {
            v -= MOD;
        }
        v
    }
    fn mul(&self, rhs: &Self) -> Self {
        *self * *rhs % MOD
    }
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
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_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_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::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn run() {
    input! {
        n: u64,
    }
    let m = n / 2;
    let ans = if n % 2 == 0 {
        let kita = Kitamasa::new(vec![1, 1]);
        kita.find_kth(&[1, 1], m as usize)
    } else {
        let kita = Kitamasa::new(vec![MOD - 1, MOD - 2, 1, 2]);
        kita.find_kth(&[1, 2, 4, 10], m as usize)
    };
    println!("{}", ans);
}

fn main() {
    run();
}
0