結果

問題 No.753 最強王者決定戦
ユーザー koba-e964koba-e964
提出日時 2018-12-19 21:53:08
言語 Rust
(1.77.0)
結果
AC  
実行時間 346 ms / 1,000 ms
コード長 3,831 bytes
コンパイル時間 2,743 ms
コンパイル使用メモリ 191,076 KB
実行使用メモリ 8,912 KB
最終ジャッジ日時 2023-10-26 00:07:42
合計ジャッジ時間 5,031 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 337 ms
8,912 KB
testcase_01 AC 346 ms
8,912 KB
testcase_02 AC 338 ms
8,912 KB
testcase_03 AC 341 ms
8,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($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),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

/// Generates an Iterator over subsets of univ, in the descending order. 
/// Verified by: http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=3050308
struct SubsetIter { bits: Option<usize>, univ: usize }
impl Iterator for SubsetIter {
    type Item = usize;
    fn next(&mut self) -> Option<usize> {
        match self.bits {
            None => None,
            Some(bits) => {
                let ans = bits;
                self.bits =
                    if bits == 0 { None }
                else { Some((bits - 1) & self.univ) };
                Some(ans)
            }
        }
    }
}
fn subsets(univ: usize) -> SubsetIter {
    SubsetIter { bits: Some(univ), univ }
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($format:expr) => (write!(out,$format).unwrap());
        ($format:expr, $($args:expr),+) => (write!(out,$format,$($args),*).unwrap())
    }
    let n = 16;
    input! {
        a: [[i32; n]; n]
    }
    let mut a = a;
    for i in 0 .. n {
        for j in i + 1 .. n {
            a[j][i] = -a[i][j];
        }
    }
    let mut dp = vec![vec![0i64; 1 << n]; n];
    for i in 0 .. n {
        dp[i][1usize.wrapping_shl(i as u32)] = 1;
    }
    let mut s = 1;
    while s < n as u32 {
        s *= 2;
        for bits in 0 .. 1usize << n {
            if bits.count_ones() != s { continue; }
            for sub in subsets(bits) {
                if sub.count_ones() != s / 2 { continue; }
                let sub2 = bits - sub;
                for i in 0 .. n {
                    if (sub & 1 << i) == 0 { continue; }
                    for j in 0 .. n {
                        if (sub2 & 1 << j) == 0 { continue; }
                        if a[i][j] == 1 {
                            dp[i][bits] += dp[i][sub] * dp[j][sub2];
                        }
                    }
                }
            }
        }
    }
    for i in 0 .. n {
        puts!("{}\n", dp[i][(1 << n) - 1] << (n - 1));
    }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0