結果

問題 No.7 プライムナンバーゲーム
ユーザー iwot
提出日時 2019-12-21 00:15:51
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,560 bytes
コンパイル時間 10,879 ms
コンパイル使用メモリ 404,228 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-18 04:59:09
合計ジャッジ時間 11,713 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

fn read<T: std::str::FromStr>() -> T {
  let mut s = String::new();
  std::io::stdin().read_line(&mut s).ok();
  s.trim().parse().ok().unwrap()
}

fn main() {
  let n: usize = read();
  println!("{}", no7(n));
}

fn no7(n: usize) -> String {
  fn is_prime(n: usize) -> bool {
    if n < 2 {
      return false;
    } else if n == 2 {
      return true;
    } else if n % 2 == 0 {
      return false;
    }

    let sn = (n as f64).sqrt();
    for i in (3..=sn as usize).step_by(2) {
      if n % i == 0 {
        return false;
      }
    }

    true
  }

  let primes = (0..n).filter(|a| is_prime(*a)).collect::<Vec<usize>>();

  fn choice(n: usize, primes: &Vec<usize>) -> Option<usize> {
    for i in (0..primes.len()).rev() {
      let new_n = n - primes[i];
      if new_n <= 1 {
        continue
      }
      return Some(new_n)
    }
    None
  }

  fn me(n: usize, primes: Vec<usize>) -> String{
    if primes.len() > 0 {
      let new_n = choice(n, &primes);
      if let Some(new_n) = new_n {
        not_me(new_n, primes.iter().filter(|&a| new_n > *a).map(|a| *a).collect::<Vec<usize>>())
      } else {
        "Lose".to_string()
      }
    } else {
      "Lose".to_string()
    }
  }

  fn not_me(n: usize, primes: Vec<usize>) -> String {
    if primes.len() > 0 {
      let new_n = choice(n, &primes);
      if let Some(new_n) = new_n {
        me(new_n, primes.iter().filter(|&a| new_n > *a).map(|a| *a).collect::<Vec<usize>>())
      } else {
        "Win".to_string()
      }
    } else {
      "Win".to_string()
    }
  }

  me(n, primes)
}
0