結果

問題 No.2252 Find Zero
ユーザー cotton_fn_cotton_fn_
提出日時 2023-03-24 22:06:31
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 18,539 bytes
コンパイル時間 13,311 ms
コンパイル使用メモリ 378,828 KB
実行使用メモリ 25,232 KB
平均クエリ数 78.17
最終ジャッジ日時 2024-09-18 17:08:17
合計ジャッジ時間 13,516 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
24,592 KB
testcase_01 AC 65 ms
25,232 KB
testcase_02 AC 47 ms
25,232 KB
testcase_03 AC 57 ms
24,848 KB
testcase_04 AC 47 ms
25,092 KB
testcase_05 AC 46 ms
25,220 KB
testcase_06 AC 46 ms
24,836 KB
testcase_07 AC 45 ms
24,964 KB
testcase_08 AC 49 ms
24,476 KB
testcase_09 AC 54 ms
24,976 KB
testcase_10 AC 42 ms
24,836 KB
testcase_11 AC 42 ms
24,580 KB
testcase_12 AC 43 ms
25,220 KB
testcase_13 AC 46 ms
24,964 KB
testcase_14 AC 42 ms
24,580 KB
testcase_15 AC 43 ms
24,592 KB
testcase_16 AC 47 ms
24,836 KB
testcase_17 AC 44 ms
24,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use input::*;
use output::*;
use std::{
    collections::*,
    io::{self, BufWriter, Read, Write},
};
fn run<I: Input, O: Write>(mut ss: I, mut out: O) {
    let t: u32 = 1;
    for _ in 0..t {
        case(&mut ss, &mut out);
    }
}
fn case<I: Input, O: Write>(mut ss: I, out: O) {
    let mut out = Output::new(out);
    let n: usize = ss.parse();
    for i in (0..=n - 2).step_by(2) {
        out.line().put('?').put(i).put(i + 1);
        out.flush();
        let x: usize = ss.parse();
        if x == i || x == i + 1 {
            out.line().put('!').put(x ^ 1);
            out.flush();
            return;
        }
    }
    out.line().put('!').put(n - 1);
}
fn main() {
    let stdin = io::stdin();
    let ss = SplitWs::new(stdin.lock());
    let stdout = io::stdout();
    let out = BufWriter::new(stdout.lock());
    run(ss, out);
}
pub mod input {
    use std::{
        io::{self, prelude::*},
        marker::PhantomData,
    };
    #[macro_export]
    macro_rules ! input { ($ src : expr , $ ($ var : ident $ (($ count : expr $ (, $ map : expr) ?)) ? $ (: $ ty : ty) ?) ,* $ (,) ?) => { $ (input ! (@ $ src , $ var $ (($ count $ (, $ map) ?)) ? $ (: $ ty) ?) ;) * } ; (@ $ src : expr , $ var : ident $ (: $ ty : ty) ?) => { let $ var $ (: $ ty) ? = $ src . parse () ; } ; (@ $ src : expr , $ var : ident ($ count : expr) : $ ($ ty : ty) ?) => { let $ var $ (: $ ty) ? = $ src . seq () . take ($ count) . collect () ; } ; (@ $ src : expr , $ var : ident ($ count : expr , $ map : expr) : $ ($ ty : ty) ?) => { let $ var $ (: $ ty) ? = $ src . seq () . take ($ count) . map ($ map) . collect () ; } ; }
    pub trait Input {
        fn bytes(&mut self) -> &[u8];
        fn bytes_vec(&mut self) -> Vec<u8> {
            self.bytes().to_vec()
        }
        fn str(&mut self) -> &str {
            std::str::from_utf8(self.bytes()).unwrap()
        }
        fn parse<T: Parse>(&mut self) -> T {
            self.parse_with(DefaultParser)
        }
        fn parse_with<T>(&mut self, mut parser: impl Parser<T>) -> T {
            parser.parse(self)
        }
        fn seq<T: Parse>(&mut self) -> Seq<T, Self, DefaultParser> {
            self.seq_with(DefaultParser)
        }
        fn seq_with<T, P: Parser<T>>(&mut self, parser: P) -> Seq<T, Self, P> {
            Seq {
                input: self,
                parser,
                marker: PhantomData,
            }
        }
        fn collect<T: Parse, C: std::iter::FromIterator<T>>(&mut self, n: usize) -> C {
            self.seq().take(n).collect()
        }
    }
    impl<T: Input> Input for &mut T {
        fn bytes(&mut self) -> &[u8] {
            (**self).bytes()
        }
    }
    pub trait Parser<T> {
        fn parse<I: Input + ?Sized>(&mut self, s: &mut I) -> T;
    }
    impl<T, P: Parser<T>> Parser<T> for &mut P {
        fn parse<I: Input + ?Sized>(&mut self, s: &mut I) -> T {
            (**self).parse(s)
        }
    }
    pub trait Parse {
        fn parse<I: Input + ?Sized>(s: &mut I) -> Self;
    }
    pub struct DefaultParser;
    impl<T: Parse> Parser<T> for DefaultParser {
        fn parse<I: Input + ?Sized>(&mut self, s: &mut I) -> T {
            T::parse(s)
        }
    }
    pub struct Seq<'a, T, I: ?Sized, P> {
        input: &'a mut I,
        parser: P,
        marker: PhantomData<*const T>,
    }
    impl<'a, T, I: Input + ?Sized, P: Parser<T>> Iterator for Seq<'a, T, I, P> {
        type Item = T;
        #[inline]
        fn next(&mut self) -> Option<Self::Item> {
            Some(self.input.parse_with(&mut self.parser))
        }
        fn size_hint(&self) -> (usize, Option<usize>) {
            (!0, None)
        }
    }
    impl Parse for char {
        #[inline]
        fn parse<I: Input + ?Sized>(s: &mut I) -> Self {
            let s = s.bytes();
            debug_assert_eq!(s.len(), 1);
            *s.first().expect("zero length") as char
        }
    }
    macro_rules ! tuple { ($ ($ T : ident) ,*) => { impl <$ ($ T : Parse) ,*> Parse for ($ ($ T ,) *) { # [inline] # [allow (unused_variables)] # [allow (clippy :: unused_unit)] fn parse < I : Input + ? Sized > (s : & mut I) -> Self { ($ ($ T :: parse (s) ,) *) } } } ; }
    tuple!();
    tuple!(A);
    tuple!(A, B);
    tuple!(A, B, C);
    tuple!(A, B, C, D);
    tuple!(A, B, C, D, E);
    tuple!(A, B, C, D, E, F);
    tuple!(A, B, C, D, E, F, G);
    #[cfg(feature = "newer")]
    impl<T: Parse, const N: usize> Parse for [T; N] {
        fn parse<I: Input + ?Sized>(s: &mut I) -> Self {
            use std::{
                mem::{self, MaybeUninit},
                ptr,
            };
            struct Guard<T, const N: usize> {
                arr: [MaybeUninit<T>; N],
                i: usize,
            }
            impl<T, const N: usize> Drop for Guard<T, N> {
                fn drop(&mut self) {
                    unsafe {
                        ptr::drop_in_place(&mut self.arr[..self.i] as *mut _ as *mut [T]);
                    }
                }
            }
            let mut g = Guard::<T, N> {
                arr: unsafe { MaybeUninit::uninit().assume_init() },
                i: 0,
            };
            while g.i < N {
                g.arr[g.i] = MaybeUninit::new(s.parse());
                g.i += 1;
            }
            unsafe { mem::transmute_copy(&g.arr) }
        }
    }
    macro_rules! uint {
        ($ ty : ty) => {
            impl Parse for $ty {
                #[inline]
                fn parse<I: Input + ?Sized>(s: &mut I) -> Self {
                    let s = s.bytes();
                    s.iter().fold(0, |x, d| 10 * x + (0xf & d) as $ty)
                }
            }
        };
    }
    macro_rules! int {
        ($ ty : ty) => {
            impl Parse for $ty {
                #[inline]
                fn parse<I: Input + ?Sized>(s: &mut I) -> Self {
                    let f = |s: &[u8]| {
                        s.iter()
                            .fold(0 as $ty, |x, d| (10 * x).wrapping_add((0xf & d) as $ty))
                    };
                    let s = s.bytes();
                    if let Some((b'-', s)) = s.split_first() {
                        f(s).wrapping_neg()
                    } else {
                        f(s)
                    }
                }
            }
        };
    }
    macro_rules! float {
        ($ ty : ty) => {
            impl Parse for $ty {
                fn parse<I: Input + ?Sized>(s: &mut I) -> Self {
                    const POW: [$ty; 18] = [
                        1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13,
                        1e14, 1e15, 1e16, 1e17,
                    ];
                    let s = s.bytes();
                    let (minus, s) = if let Some((b'-', s)) = s.split_first() {
                        (true, s)
                    } else {
                        (false, s)
                    };
                    let (int, fract) = if let Some(p) = s.iter().position(|c| *c == b'.') {
                        (&s[..p], &s[p + 1..])
                    } else {
                        (s, &[][..])
                    };
                    let x = int
                        .iter()
                        .chain(fract)
                        .fold(0u64, |x, d| 10 * x + (0xf & *d) as u64);
                    let x = x as $ty;
                    let x = if minus { -x } else { x };
                    let exp = fract.len();
                    if exp == 0 {
                        x
                    } else if let Some(pow) = POW.get(exp) {
                        x / pow
                    } else {
                        x / (10.0 as $ty).powi(exp as i32)
                    }
                }
            }
        };
    }
    macro_rules! from_bytes {
        ($ ty : ty) => {
            impl Parse for $ty {
                #[inline]
                fn parse<I: Input + ?Sized>(s: &mut I) -> Self {
                    s.bytes().into()
                }
            }
        };
    }
    macro_rules! from_str {
        ($ ty : ty) => {
            impl Parse for $ty {
                #[inline]
                fn parse<I: Input + ?Sized>(s: &mut I) -> Self {
                    s.str().into()
                }
            }
        };
    }
    macro_rules ! impls { ($ m : ident , $ ($ ty : ty) ,*) => { $ ($ m ! ($ ty) ;) * } ; }
    impls!(uint, usize, u8, u16, u32, u64, u128);
    impls!(int, isize, i8, i16, i32, i64, i128);
    impls!(float, f32, f64);
    impls!(from_bytes, Vec<u8>, Box<[u8]>);
    impls!(from_str, String);
    #[derive(Clone)]
    pub struct SplitWs<T> {
        src: T,
        buf: Vec<u8>,
        pos: usize,
        len: usize,
    }
    const BUF_SIZE: usize = 1 << 26;
    impl<T: Read> SplitWs<T> {
        pub fn new(src: T) -> Self {
            Self {
                src,
                buf: vec![0; BUF_SIZE],
                pos: 0,
                len: 0,
            }
        }
        #[inline(always)]
        fn peek(&self) -> &[u8] {
            unsafe { self.buf.get_unchecked(self.pos..self.len) }
        }
        #[inline(always)]
        fn consume(&mut self, n: usize) -> &[u8] {
            let pos = self.pos;
            self.pos += n;
            unsafe { self.buf.get_unchecked(pos..self.pos) }
        }
        fn read(&mut self) -> usize {
            self.buf.copy_within(self.pos..self.len, 0);
            self.len -= self.pos;
            self.pos = 0;
            if self.len == self.buf.len() {
                self.buf.resize(2 * self.buf.len(), 0);
            }
            loop {
                match self.src.read(&mut self.buf[self.len..]) {
                    Ok(n) => {
                        self.len += n;
                        return n;
                    }
                    Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
                    Err(e) => panic!("io error: {:?}", e),
                }
            }
        }
    }
    impl<T: Read> Input for SplitWs<T> {
        #[inline]
        fn bytes(&mut self) -> &[u8] {
            loop {
                if let Some(del) = self.peek().iter().position(|c| c.is_ascii_whitespace()) {
                    if del > 0 {
                        let s = self.consume(del + 1);
                        return s.split_last().unwrap().1;
                    } else {
                        self.consume(1);
                    }
                } else if self.read() == 0 {
                    return self.consume(self.len - self.pos);
                }
            }
        }
    }
}
pub mod macros {
    extern "C" {
        pub fn isatty(fd: i32) -> i32;
    }
    #[macro_export]
    macro_rules ! w { ($ dst : expr , $ ($ arg : tt) *) => { if cfg ! (debug_assertions) && unsafe { $ crate :: macros :: isatty (1) } != 0 { write ! ($ dst , "\x1B[1;33m") . unwrap () ; write ! ($ dst , $ ($ arg) *) . unwrap () ; write ! ($ dst , "\x1B[0m") . unwrap () ; } else { write ! ($ dst , $ ($ arg) *) . unwrap () ; } } }
    #[macro_export]
    macro_rules ! wln { ($ dst : expr $ (, $ ($ arg : tt) *) ?) => { { if cfg ! (debug_assertions) && unsafe { $ crate :: macros :: isatty (1) } != 0 { write ! ($ dst , "\x1B[1;33m") . unwrap () ; writeln ! ($ dst $ (, $ ($ arg) *) ?) . unwrap () ; write ! ($ dst , "\x1B[0m") . unwrap () ; } else { writeln ! ($ dst $ (, $ ($ arg) *) ?) . unwrap () ; } # [cfg (debug_assertions)] $ dst . flush () . unwrap () ; } } }
    #[macro_export]
    macro_rules! w_iter {
        ($ dst : expr , $ fmt : expr , $ iter : expr , $ delim : expr) => {{
            let mut first = true;
            for elem in $iter {
                if first {
                    w!($dst, $fmt, elem);
                    first = false;
                } else {
                    w!($dst, concat!($delim, $fmt), elem);
                }
            }
        }};
        ($ dst : expr , $ fmt : expr , $ iter : expr) => {
            w_iter!($dst, $fmt, $iter, " ")
        };
    }
    #[macro_export]
    macro_rules ! w_iter_ln { ($ dst : expr , $ ($ t : tt) *) => { { w_iter ! ($ dst , $ ($ t) *) ; wln ! ($ dst) ; } } }
    #[macro_export]
    macro_rules ! e { ($ ($ t : tt) *) => { # [cfg (debug_assertions)] eprint ! ($ ($ t) *) } }
    #[macro_export]
    macro_rules ! eln { ($ ($ t : tt) *) => { # [cfg (debug_assertions)] eprintln ! ($ ($ t) *) } }
    #[macro_export]
    #[doc(hidden)]
    macro_rules ! __tstr { ($ h : expr $ (, $ t : expr) +) => { concat ! (__tstr ! ($ ($ t) ,+) , ", " , __tstr ! (@)) } ; ($ h : expr) => { concat ! (__tstr ! () , " " , __tstr ! (@)) } ; () => { "\x1B[94m[{}:{}]\x1B[0m" } ; (@) => { "\x1B[1;92m{}\x1B[0m = {:?}" } }
    #[macro_export]
    macro_rules ! d { ($ ($ a : expr) ,*) => { if std :: env :: var ("ND") . map (| v | & v == "0") . unwrap_or (true) { eln ! (__tstr ! ($ ($ a) ,*) , file ! () , line ! () , $ (stringify ! ($ a) , $ a) ,*) ; } } ; }
}
pub mod output {
    use std::io::{self, prelude::*};
    pub struct Output<W> {
        dst: W,
    }
    impl<W: Write> Output<W> {
        pub fn new(dst: W) -> Self {
            Self { dst }
        }
        pub fn flush(&mut self) {
            self.dst.flush().expect("io error");
        }
        pub fn line(&mut self) -> Line<W> {
            Line {
                out: self,
                ws: false,
                to_flush: cfg!(debug_assertions),
            }
        }
        pub fn putln<T: Put>(&mut self, value: T) -> &mut Self {
            self.line().put(value);
            self
        }
        pub fn iter_lines<T: Put>(&mut self, iter: impl IntoIterator<Item = T>) -> &mut Self {
            for value in iter {
                self.line().put(value);
            }
            self
        }
        fn byte(&mut self, c: u8) -> &mut Self {
            self.dst
                .write_all(std::slice::from_ref(&c))
                .expect("io error");
            self
        }
    }
    pub trait Put {
        fn put<W: Write>(&self, dst: &mut W) -> io::Result<()>;
    }
    impl<T: Put + ?Sized> Put for &T {
        fn put<W: Write>(&self, dst: &mut W) -> io::Result<()> {
            (*self).put(dst)
        }
    }
    impl Put for [u8] {
        fn put<W: Write>(&self, dst: &mut W) -> io::Result<()> {
            dst.write_all(self)
        }
    }
    impl Put for str {
        fn put<W: Write>(&self, dst: &mut W) -> io::Result<()> {
            dst.write_all(self.as_bytes())
        }
    }
    macro_rules! deref {
        ($ ty : ty) => {
            impl Put for $ty {
                fn put<W: Write>(&self, dst: &mut W) -> io::Result<()> {
                    (&**self).put(dst)
                }
            }
        };
    }
    deref!(Vec<u8>);
    deref!(Box<[u8]>);
    deref!(String);
    impl Put for char {
        fn put<W: Write>(&self, dst: &mut W) -> io::Result<()> {
            dst.write_all(self.encode_utf8(&mut [0; 4]).as_bytes())
        }
    }
    macro_rules! int {
        ($ uty : ident , $ ity : ident , $ N : expr) => {
            impl Put for $uty {
                fn put<W: Write>(&self, dst: &mut W) -> io::Result<()> {
                    let mut buf = [0; $N];
                    buf[0] = b'0';
                    let mut i = 0;
                    let mut x = *self;
                    while x > 0 {
                        buf[i] = b'0' + (x % 10) as u8;
                        x /= 10;
                        i += 1;
                    }
                    buf[..i].reverse();
                    dst.write_all(&buf[..i.max(1)])
                }
            }
            impl Put for $ity {
                fn put<W: Write>(&self, dst: &mut W) -> io::Result<()> {
                    let mut buf = [0; $N + 1];
                    buf[0] = b'-';
                    buf[1] = b'0';
                    let mut i = 1;
                    let neg = *self < 0;
                    let mut x = self.abs() as $uty;
                    while x > 0 {
                        buf[i] = b'0' + (x % 10) as u8;
                        x /= 10;
                        i += 1;
                    }
                    buf[1..i].reverse();
                    let l = 1 - neg as usize;
                    dst.write_all(&buf[l..i.max(2)])
                }
            }
        };
    }
    int!(u8, i8, 3);
    int!(u16, i16, 5);
    int!(u32, i32, 10);
    int!(u64, i64, 20);
    int!(u128, i128, 39);
    #[cfg(target_pointer_width = "32")]
    int!(usize, isize, 10);
    #[cfg(target_pointer_width = "64")]
    int!(usize, isize, 20);
    macro_rules! fmt {
        ($ ty : ident) => {
            impl Put for $ty {
                fn put<W: Write>(&self, dst: &mut W) -> io::Result<()> {
                    write!(dst, "{}", self)
                }
            }
        };
    }
    fmt!(f32);
    fmt!(f64);
    pub struct Byte(u8);
    impl Put for Byte {
        fn put<W: Write>(&self, dst: &mut W) -> io::Result<()> {
            dst.write_all(std::slice::from_ref(&self.0))
        }
    }
    macro_rules ! tuple { () => { } ; ($ T : ident $ (,$ U : ident) *) => { tuple ! ($ ($ U) ,*) ; impl <$ T : Put , $ ($ U : Put) ,*> Put for ($ T , $ ($ U) ,*) { fn put < W : Write > (& self , dst : & mut W) -> io :: Result < () > { #! [allow (non_snake_case)] let ($ T , $ ($ U) ,*) = self ; $ T . put (dst) ?; $ (Byte (b' ') . put (dst) ?; $ U . put (dst) ?;) * Ok (()) } } } ; }
    tuple!(A, B, C, D, E, F, G, H);
    pub struct Line<'a, W: Write> {
        out: &'a mut Output<W>,
        ws: bool,
        to_flush: bool,
    }
    impl<'a, W: Write> Line<'a, W> {
        pub fn endl(self) {}
        pub fn put<T: Put>(&mut self, value: T) -> &mut Self {
            self.ws();
            value.put(&mut self.out.dst).expect("io error");
            self.ws = true;
            self
        }
        pub fn iter<T: Put>(&mut self, iter: impl IntoIterator<Item = T>) -> &mut Self {
            for value in iter {
                self.put(value);
            }
            self
        }
        fn byte(&mut self, c: u8) -> &mut Self {
            self.out.dst.write_all(std::slice::from_ref(&c)).unwrap();
            self
        }
        fn ws(&mut self) -> &mut Self {
            if self.ws {
                self.byte(b' ');
            }
            self
        }
    }
    impl<'a, W: Write> Drop for Line<'a, W> {
        fn drop(&mut self) {
            self.out.byte(b'\n');
            if self.to_flush {
                self.out.flush();
            }
        }
    }
}
0