結果

問題 No.1267 Stop and Coin Game
ユーザー StrorkisStrorkis
提出日時 2020-10-24 13:11:04
言語 Rust
(1.77.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 3,662 bytes
コンパイル時間 3,426 ms
コンパイル使用メモリ 161,536 KB
実行使用メモリ 8,900 KB
最終ジャッジ日時 2023-09-28 20:23:52
合計ジャッジ時間 3,666 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 59 ms
8,816 KB
testcase_11 AC 12 ms
4,380 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 68 ms
8,900 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 75 ms
8,784 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 69 ms
8,840 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 16 ms
4,376 KB
testcase_38 AC 18 ms
4,376 KB
testcase_39 AC 6 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 4 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{self, BufRead, Write};
use std::str::FromStr;
use std::fmt;

struct Io1<T>(T);

impl<T: FromStr> FromStr for Io1<T> {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(s.trim_end().parse().ok().unwrap())
    }
}

struct Io2<T1, T2>(T1, T2);

impl<T1: FromStr, T2: FromStr> FromStr for Io2<T1, T2> {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut iter = s.split_whitespace();
        Ok(Io2(
            iter.next().unwrap().parse().ok().unwrap(),
            iter.next().unwrap().parse().ok().unwrap(),
        ))
    }
}

struct Io3<T1, T2, T3>(T1, T2, T3);

impl<T1, T2, T3> FromStr for Io3<T1, T2, T3>
where
    T1: FromStr, T2: FromStr, T3: FromStr
{
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut iter = s.split_whitespace();
        Ok(Io3(
            iter.next().unwrap().parse().ok().unwrap(),
            iter.next().unwrap().parse().ok().unwrap(),
            iter.next().unwrap().parse().ok().unwrap(),
        ))
    }
}

struct Ios(Vec<char>);

impl std::str::FromStr for Ios {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Ios(s.chars().collect()))
    }
}

impl fmt::Display for Ios {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.iter().collect::<String>())
    }
}

struct Iov<T>(Vec<T>);

impl<T: FromStr> FromStr for Iov<T> {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let iter = s.split_whitespace();
        Ok(Iov(iter.map(|x| x.parse().ok().unwrap()).collect()))
    }
}

impl<T: fmt::Display> fmt::Display for Iov<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}",
            self.0.iter().map(|x| x.to_string())
                .collect::<Vec<_>>().join(" ")
        )
    }
}

use std::collections::HashMap;

struct Solver<'a> {
    reader: io::BufReader<io::StdinLock<'a>>,
    writer: io::BufWriter<io::StdoutLock<'a>>,
}

impl Solver<'_> {
    fn read<T: FromStr>(&mut self) -> T {
        let mut input = String::new();
        self.reader.read_line(&mut input).unwrap();
        input.trim_end().parse().ok().unwrap()
    }

    fn writeln<T: fmt::Display>(&mut self, ans: T) {
        writeln!(self.writer, "{}", ans).unwrap();
    }

    fn rec(
        n: usize, a: &Vec<i64>, map: &mut HashMap<i32, bool>,
        v: i64, mut state: i32, flag: bool
    ) -> bool {
        if v < 0 { return flag; }
        if let Some(value) = map.get(&state) { return *value; }

        for i in 0..n {
            if state & 1 << i != 0 { continue; }
            state |= 1 << i;
            let value = Self::rec(n, a, map, v - a[i], state, !flag);
            map.insert(state, value);
            if value == flag { return flag; }
            state ^= 1 << i;
        }
        !flag
    }

    fn solve(&mut self) {
        let Io2(n, v): Io2<usize, i64> = self.read();
        let Iov(a): Iov<i64> = self.read();

        if a.iter().sum::<i64>() <= v {
            return self.writeln("Draw");
        }
        
        let mut map = HashMap::new();
        if Self::rec(n, &a, &mut map, v, 0, true) {
            self.writeln("First");
        } else {
            self.writeln("Second");
        }
    }

    fn run() {
        let (stdin, stdout) = (io::stdin(), io::stdout());
        let reader = io::BufReader::new(stdin.lock());
        let writer = io::BufWriter::new(stdout.lock());
        let mut solver = Solver { reader, writer };
        solver.solve();
    }
}

fn main() {
    Solver::run();
}
0