結果
問題 | No.2725 Coprime Game 2 |
ユーザー |
|
提出日時 | 2024-04-13 11:14:54 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 170 ms / 2,000 ms |
コード長 | 1,769 bytes |
コンパイル時間 | 13,705 ms |
コンパイル使用メモリ | 380,564 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-03 00:00:42 |
合計ジャッジ時間 | 13,693 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 8 |
ソースコード
use std::io::Read;fn get_word() -> String {let stdin = std::io::stdin();let mut stdin=stdin.lock();let mut u8b: [u8; 1] = [0];loop {let mut buf: Vec<u8> = Vec::with_capacity(16);loop {let res = stdin.read(&mut u8b);if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {break;} else {buf.push(u8b[0]);}}if buf.len() >= 1 {let ret = String::from_utf8(buf).unwrap();return ret;}}}fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }fn gcd(x: i64, y: i64) -> i64 {if x < y { gcd(y, x) } else if y == 0 { x } else { gcd(y, x % y) }}fn win(n: i64) -> bool {let mut x = 2;while x < n && n % x == 0 { x += 1; }gcd(n, x) == 1}// https://yukicoder.me/problems/no/2725 (3)// 実験の結果、n = 2, 6, 18, 30, 42, 54, ... で後手必勝、それ以外で先手必勝であることがわかる。// これは n = 2, 6 + 12i とみなすことができる。// 6+12i が後手必勝なのは、先手は初手で奇数に行くしかなく、後手は 4 に行けば勝てるためである。// 他の場合で先手必勝なのもほとんど明らか。(12|N なら初手 5、2|N and not(3|N) なら初手 3 など)// -> WA。420, 840 が後手必勝であることを見落としていた。// 最小の約数でない正の整数が重要で、420 => 8, 840 => 9 だが、// これは元々の数と互いに素ではないので初手で行けない。fn main() {let t: i32 = get();for _ in 0..t {let n: i64 = get();println!("{}", if win(n) {"K"} else {"P"});}}