結果

問題 No.2357 Guess the Function
ユーザー ikdikd
提出日時 2024-01-24 19:15:30
言語 Rust
(1.77.0)
結果
AC  
実行時間 31 ms / 1,000 ms
コード長 3,424 bytes
コンパイル時間 2,317 ms
コンパイル使用メモリ 185,972 KB
実行使用メモリ 24,012 KB
平均クエリ数 2.91
最終ジャッジ日時 2024-01-24 19:15:41
合計ジャッジ時間 4,459 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
24,000 KB
testcase_01 AC 23 ms
24,012 KB
testcase_02 AC 23 ms
24,012 KB
testcase_03 AC 23 ms
24,012 KB
testcase_04 AC 24 ms
24,012 KB
testcase_05 AC 23 ms
24,012 KB
testcase_06 AC 23 ms
24,012 KB
testcase_07 AC 24 ms
24,012 KB
testcase_08 AC 22 ms
24,012 KB
testcase_09 AC 22 ms
24,012 KB
testcase_10 AC 24 ms
24,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use scanner::Scanner;
use std::io;

fn main() {
    let mut scanner = Scanner::from(io::stdin().lock());

    println!("? 100");
    let y = scan!(u32, <~ scanner);
    if y == 99 {
        println!("! 99 100");
        return;
    }

    // (100 + A) % B = y,
    // ((100 - y) + A) % B = 0,
    // ((100 - y - 1) + A) % B = B - 1
    let x = 100 - y - 1; // >= 1
    println!("? {}", x);
    let z = scan!(u32, <~ scanner);
    let b = z + 1;
    assert!(y < b);
    let a = (0..b).filter(|a| (100 + a) % b == y).collect::<Vec<_>>();
    assert_eq!(a.len(), 1);
    println!("! {} {}", a[0], b);
}

// ✂ --- scanner --- ✂
#[allow(unused)]
mod scanner {
    use std::fmt;
    use std::io;
    use std::str;

    pub struct Scanner<R> {
        r: R,
        l: String,
        i: usize,
    }

    impl<R> Scanner<R>
    where
        R: io::BufRead,
    {
        pub fn new(reader: R) -> Self {
            Self {
                r: reader,
                l: String::new(),
                i: 0,
            }
        }

        pub fn scan<T>(&mut self) -> T
        where
            T: str::FromStr,
            T::Err: fmt::Debug,
        {
            self.skip_blanks();
            assert!(self.i < self.l.len()); // remain some character
            assert_ne!(&self.l[self.i..=self.i], " ");
            let rest = &self.l[self.i..];
            let len = rest
                .find(|ch| char::is_ascii_whitespace(&ch))
                .unwrap_or_else(|| rest.len());
            // parse self.l[self.i..(self.i + len)]
            let val = rest[..len]
                .parse()
                .unwrap_or_else(|e| panic!("{:?}, attempt to read `{}`", e, rest));
            self.i += len;
            val
        }

        pub fn scan_vec<T>(&mut self, n: usize) -> Vec<T>
        where
            T: str::FromStr,
            T::Err: fmt::Debug,
        {
            (0..n).map(|_| self.scan()).collect::<Vec<_>>()
        }

        fn skip_blanks(&mut self) {
            loop {
                match self.l[self.i..].find(|ch| !char::is_ascii_whitespace(&ch)) {
                    Some(j) => {
                        self.i += j;
                        break;
                    }
                    None => {
                        self.l.clear(); // clear buffer
                        let num_bytes = self
                            .r
                            .read_line(&mut self.l)
                            .unwrap_or_else(|_| panic!("invalid UTF-8"));
                        assert!(num_bytes > 0, "reached EOF :(");
                        self.i = 0;
                    }
                }
            }
        }
    }

    impl<'a> From<&'a str> for Scanner<&'a [u8]> {
        fn from(s: &'a str) -> Self {
            Self::new(s.as_bytes())
        }
    }

    impl<'a> From<io::StdinLock<'a>> for Scanner<io::BufReader<io::StdinLock<'a>>> {
        fn from(stdin: io::StdinLock<'a>) -> Self {
            Self::new(io::BufReader::new(stdin))
        }
    }

    #[macro_export]
    macro_rules! scan {
        (( $($t: ty),+ ), <~ $scanner: expr) => {
            ( $(scan!($t, <~ $scanner)),+ )
        };
        ([ $t: tt; $n: expr ], <~ $scanner: expr) => {
            (0..$n).map(|_| scan!($t, <~ $scanner)).collect::<Vec<_>>()
        };
        ($t: ty, <~ $scanner: expr) => {
            $scanner.scan::<$t>()
        };
    }
}
// ✂ --- scanner --- ✂
0