QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#541867#8932. Bingoucup-team296#AC ✓390ms12936kbRust35.8kb2024-08-31 21:14:212024-08-31 21:14:22

Judging History

你现在查看的是最新测评结果

  • [2024-08-31 21:14:22]
  • 评测
  • 测评结果:AC
  • 用时:390ms
  • 内存:12936kb
  • [2024-08-31 21:14:21]
  • 提交

answer

// 
pub mod solution {
//{"name":"uc8_a","group":"Manual","url":"","interactive":false,"timeLimit":2000,"tests":[{"input":"","output":""}],"testType":"multiNumber","input":{"type":"stdin","fileName":null,"pattern":null},"output":{"type":"stdout","fileName":null,"pattern":null},"languages":{"java":{"taskClass":"uc8_a"}}}

use crate::algo_lib::collections::min_max::MinimMaxim;
use crate::algo_lib::collections::slice_ext::indices::Indices;
use crate::algo_lib::io::input::Input;
use crate::algo_lib::io::output::Output;
use crate::algo_lib::misc::test_type::TaskType;
use crate::algo_lib::misc::test_type::TestType;
use crate::algo_lib::numbers::inf_int::InfInt;
use crate::algo_lib::string::str::StrReader;
use crate::value;

type PreCalc = ();

fn solve(input: &mut Input, out: &mut Output, _test_case: usize, _data: &mut PreCalc) {
    let mut n = input
        .read_str()
        .into_iter()
        .map(|c| (c - b'0') as i64)
        .collect::<Vec<_>>();
    let m = input.read_long();

    n.reverse();
    let mut carry = 1;
    for i in 0..n.len() {
        n[i] += carry;
        if n[i] == 10 {
            n[i] = 0;
        } else {
            carry = 0;
            break;
        }
    }
    if carry != 0 {
        n.push(carry);
    }
    // let mut digs = 0;
    // let mut mc = m;
    // while mc > 0 {
    //     digs += 1;
    //     mc /= 10;
    // }
    let mut rem = 0;
    for &d in n.iter().rev() {
        rem *= 10;
        rem += d;
        rem %= m;
    }
    value!(Limit: i64 = 1_000_000_000);
    type Int = InfInt<i64, Limit>;
    let mut ans = Int::new((m - rem) % m);
    let mut plus_one_cost = Int::new(1);
    let mut ten = Int::new(1);
    for i in 0..=n.len() {
        let mut mc = m;
        let mut j = i;
        let mut cost = Int::new(0);
        let mut carry = 0;
        let mut first = true;
        let mut c_ten = ten;
        let mut bonus = Int::new(0);
        while mc > 0 {
            let d = mc % 10;
            let mut nn = *n.get(j).unwrap_or(&0) + carry;
            if d != nn {
                if first {
                    first = false;
                    cost += plus_one_cost + bonus;
                    nn += 1;
                }
            }
            if d >= nn {
                cost += c_ten * Int::new(d - nn);
                carry = 0;
            } else {
                cost += c_ten * Int::new(10 + d - nn);
                carry = 1;
            }
            mc /= 10;
            j += 1;
            bonus += c_ten * Int::new(9);
            c_ten *= Int::new(10);
        }
        ans.minim(cost);
        plus_one_cost += ten * Int::new(9 - n.get(i).unwrap_or(&0));
        ten *= Int::new(10);
    }
    let mut ans = ans.n;
    for i in n.indices() {
        n[i] += ans;
        ans = n[i] / 10;
        n[i] %= 10;
    }
    while ans > 0 {
        n.push(ans % 10);
        ans /= 10;
    }
    for &d in n.iter().rev() {
        out.print(d);
    }
    out.print_line(());
}

pub static TEST_TYPE: TestType = TestType::MultiNumber;
pub static TASK_TYPE: TaskType = TaskType::Classic;

pub(crate) fn run(mut input: Input, mut output: Output) -> bool {
    let mut pre_calc = ();

    match TEST_TYPE {
        TestType::Single => solve(&mut input, &mut output, 1, &mut pre_calc),
        TestType::MultiNumber => {
            let t = input.read();
            for i in 1..=t {
                solve(&mut input, &mut output, i, &mut pre_calc);
            }
        }
        TestType::MultiEof => {
            let mut i = 1;
            while input.peek().is_some() {
                solve(&mut input, &mut output, i, &mut pre_calc);
                i += 1;
            }
        }
    }
    output.flush();
    match TASK_TYPE {
        TaskType::Classic => {
            input.skip_whitespace();
            input.peek().is_none()
        }
        TaskType::Interactive => true,
    }
}

}
pub mod algo_lib {
pub mod collections {
pub mod iter_ext {
pub mod collect {
pub trait IterCollect<T>: Iterator<Item = T> + Sized {
    fn collect_vec(self) -> Vec<T> {
        self.collect()
    }
}

impl<T, I: Iterator<Item = T> + Sized> IterCollect<T> for I {}
}
}
pub mod min_max {
pub trait MinimMaxim<Rhs = Self>: PartialOrd + Sized {
    fn minim(&mut self, other: Rhs) -> bool;

    fn maxim(&mut self, other: Rhs) -> bool;
}

impl<T: PartialOrd> MinimMaxim for T {
    fn minim(&mut self, other: Self) -> bool {
        if other < *self {
            *self = other;
            true
        } else {
            false
        }
    }

    fn maxim(&mut self, other: Self) -> bool {
        if other > *self {
            *self = other;
            true
        } else {
            false
        }
    }
}

impl<T: PartialOrd> MinimMaxim<T> for Option<T> {
    fn minim(&mut self, other: T) -> bool {
        match self {
            None => {
                *self = Some(other);
                true
            }
            Some(v) => v.minim(other),
        }
    }

    fn maxim(&mut self, other: T) -> bool {
        match self {
            None => {
                *self = Some(other);
                true
            }
            Some(v) => v.maxim(other),
        }
    }
}
}
pub mod slice_ext {
pub mod indices {
use std::ops::Range;

pub trait Indices {
    fn indices(&self) -> Range<usize>;
}

impl<T> Indices for [T] {
    fn indices(&self) -> Range<usize> {
        0..self.len()
    }
}
}
}
pub mod vec_ext {
pub mod default {
pub fn default_vec<T: Default>(len: usize) -> Vec<T> {
    let mut v = Vec::with_capacity(len);
    for _ in 0..len {
        v.push(T::default());
    }
    v
}
}
}
}
pub mod io {
pub mod input {
use crate::algo_lib::collections::vec_ext::default::default_vec;
use std::io::Read;

pub struct Input<'s> {
    input: &'s mut dyn Read,
    buf: Vec<u8>,
    at: usize,
    buf_read: usize,
}

macro_rules! read_impl {
    ($t: ty, $read_name: ident, $read_vec_name: ident) => {
        pub fn $read_name(&mut self) -> $t {
            self.read()
        }

        pub fn $read_vec_name(&mut self, len: usize) -> Vec<$t> {
            self.read_vec(len)
        }
    };

    ($t: ty, $read_name: ident, $read_vec_name: ident, $read_pair_vec_name: ident) => {
        read_impl!($t, $read_name, $read_vec_name);

        pub fn $read_pair_vec_name(&mut self, len: usize) -> Vec<($t, $t)> {
            self.read_vec(len)
        }
    };
}

impl<'s> Input<'s> {
    const DEFAULT_BUF_SIZE: usize = 4096;

    pub fn new(input: &'s mut dyn Read) -> Self {
        Self {
            input,
            buf: default_vec(Self::DEFAULT_BUF_SIZE),
            at: 0,
            buf_read: 0,
        }
    }

    pub fn new_with_size(input: &'s mut dyn Read, buf_size: usize) -> Self {
        Self {
            input,
            buf: default_vec(buf_size),
            at: 0,
            buf_read: 0,
        }
    }

    pub fn get(&mut self) -> Option<u8> {
        if self.refill_buffer() {
            let res = self.buf[self.at];
            self.at += 1;
            if res == b'\r' {
                if self.refill_buffer() && self.buf[self.at] == b'\n' {
                    self.at += 1;
                }
                return Some(b'\n');
            }
            Some(res)
        } else {
            None
        }
    }

    pub fn peek(&mut self) -> Option<u8> {
        if self.refill_buffer() {
            let res = self.buf[self.at];
            Some(if res == b'\r' { b'\n' } else { res })
        } else {
            None
        }
    }

    pub fn skip_whitespace(&mut self) {
        while let Some(b) = self.peek() {
            if !char::from(b).is_whitespace() {
                return;
            }
            self.get();
        }
    }

    pub fn next_token(&mut self) -> Option<Vec<u8>> {
        self.skip_whitespace();
        let mut res = Vec::new();
        while let Some(c) = self.get() {
            if char::from(c).is_whitespace() {
                break;
            }
            res.push(c);
        }
        if res.is_empty() {
            None
        } else {
            Some(res)
        }
    }

    //noinspection RsSelfConvention
    pub fn is_exhausted(&mut self) -> bool {
        self.peek().is_none()
    }

    //noinspection RsSelfConvention
    pub fn is_empty(&mut self) -> bool {
        self.skip_whitespace();
        self.is_exhausted()
    }

    pub fn read<T: Readable>(&mut self) -> T {
        T::read(self)
    }

    pub fn read_vec<T: Readable>(&mut self, size: usize) -> Vec<T> {
        let mut res = Vec::with_capacity(size);
        for _ in 0..size {
            res.push(self.read());
        }
        res
    }

    pub fn read_char(&mut self) -> char {
        self.skip_whitespace();
        self.get().unwrap().into()
    }

    read_impl!(u32, read_unsigned, read_unsigned_vec);
    read_impl!(u64, read_u64, read_u64_vec);
    read_impl!(usize, read_size, read_size_vec, read_size_pair_vec);
    read_impl!(i32, read_int, read_int_vec, read_int_pair_vec);
    read_impl!(i64, read_long, read_long_vec, read_long_pair_vec);
    read_impl!(i128, read_i128, read_i128_vec);

    fn refill_buffer(&mut self) -> bool {
        if self.at == self.buf_read {
            self.at = 0;
            self.buf_read = self.input.read(&mut self.buf).unwrap();
            self.buf_read != 0
        } else {
            true
        }
    }
}

pub trait Readable {
    fn read(input: &mut Input) -> Self;
}

impl Readable for char {
    fn read(input: &mut Input) -> Self {
        input.read_char()
    }
}

impl<T: Readable> Readable for Vec<T> {
    fn read(input: &mut Input) -> Self {
        let size = input.read();
        input.read_vec(size)
    }
}

macro_rules! read_integer {
    ($($t:ident)+) => {$(
        impl Readable for $t {
            fn read(input: &mut Input) -> Self {
                input.skip_whitespace();
                let mut c = input.get().unwrap();
                let sgn = match c {
                    b'-' => {
                        c = input.get().unwrap();
                        true
                    }
                    b'+' => {
                        c = input.get().unwrap();
                        false
                    }
                    _ => false,
                };
                let mut res = 0;
                loop {
                    assert!(c.is_ascii_digit());
                    res *= 10;
                    let d = (c - b'0') as $t;
                    if sgn {
                        res -= d;
                    } else {
                        res += d;
                    }
                    match input.get() {
                        None => break,
                        Some(ch) => {
                            if ch.is_ascii_whitespace() {
                                break;
                            } else {
                                c = ch;
                            }
                        }
                    }
                }
                res
            }
        }
    )+};
}

read_integer!(i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize);

macro_rules! tuple_readable {
    ($($name:ident)+) => {
        impl<$($name: Readable), +> Readable for ($($name,)+) {
            fn read(input: &mut Input) -> Self {
                ($($name::read(input),)+)
            }
        }
    }
}

tuple_readable! {T}
tuple_readable! {T U}
tuple_readable! {T U V}
tuple_readable! {T U V X}
tuple_readable! {T U V X Y}
tuple_readable! {T U V X Y Z}
tuple_readable! {T U V X Y Z A}
tuple_readable! {T U V X Y Z A B}
tuple_readable! {T U V X Y Z A B C}
tuple_readable! {T U V X Y Z A B C D}
tuple_readable! {T U V X Y Z A B C D E}
tuple_readable! {T U V X Y Z A B C D E F}

impl Read for Input<'_> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if self.at == self.buf_read {
            self.input.read(buf)
        } else {
            let mut i = 0;
            while i < buf.len() && self.at < self.buf_read {
                buf[i] = self.buf[self.at];
                i += 1;
                self.at += 1;
            }
            Ok(i)
        }
    }
}
}
pub mod output {
use crate::algo_lib::collections::vec_ext::default::default_vec;
use std::cmp::Reverse;
use std::io::stderr;
use std::io::Stderr;
use std::io::Write;

#[derive(Copy, Clone)]
pub enum BoolOutput {
    YesNo,
    YesNoCaps,
    PossibleImpossible,
    Custom(&'static str, &'static str),
}

impl BoolOutput {
    pub fn output(&self, output: &mut Output, val: bool) {
        (if val { self.yes() } else { self.no() }).write(output);
    }

    fn yes(&self) -> &str {
        match self {
            BoolOutput::YesNo => "Yes",
            BoolOutput::YesNoCaps => "YES",
            BoolOutput::PossibleImpossible => "Possible",
            BoolOutput::Custom(yes, _) => yes,
        }
    }

    fn no(&self) -> &str {
        match self {
            BoolOutput::YesNo => "No",
            BoolOutput::YesNoCaps => "NO",
            BoolOutput::PossibleImpossible => "Impossible",
            BoolOutput::Custom(_, no) => no,
        }
    }
}

pub struct Output<'s> {
    output: &'s mut dyn Write,
    buf: Vec<u8>,
    at: usize,
    auto_flush: bool,
    bool_output: BoolOutput,
}

impl<'s> Output<'s> {
    const DEFAULT_BUF_SIZE: usize = 4096;

    pub fn new(output: &'s mut dyn Write) -> Self {
        Self {
            output,
            buf: default_vec(Self::DEFAULT_BUF_SIZE),
            at: 0,
            auto_flush: false,
            bool_output: BoolOutput::YesNoCaps,
        }
    }

    pub fn new_with_auto_flush(output: &'s mut dyn Write) -> Self {
        Self {
            output,
            buf: default_vec(Self::DEFAULT_BUF_SIZE),
            at: 0,
            auto_flush: true,
            bool_output: BoolOutput::YesNoCaps,
        }
    }

    pub fn flush(&mut self) {
        if self.at != 0 {
            self.output.write_all(&self.buf[..self.at]).unwrap();
            self.output.flush().unwrap();
            self.at = 0;
        }
    }

    pub fn print<T: Writable>(&mut self, s: T) {
        s.write(self);
        self.maybe_flush();
    }

    pub fn print_line<T: Writable>(&mut self, s: T) {
        self.print(s);
        self.put(b'\n');
        self.maybe_flush();
    }

    pub fn put(&mut self, b: u8) {
        self.buf[self.at] = b;
        self.at += 1;
        if self.at == self.buf.len() {
            self.flush();
        }
    }

    pub fn maybe_flush(&mut self) {
        if self.auto_flush {
            self.flush();
        }
    }

    pub fn print_per_line<T: Writable>(&mut self, arg: &[T]) {
        for i in arg {
            i.write(self);
            self.put(b'\n');
        }
    }

    pub fn print_iter<T: Writable, I: Iterator<Item = T>>(&mut self, iter: I) {
        let mut first = true;
        for e in iter {
            if first {
                first = false;
            } else {
                self.put(b' ');
            }
            e.write(self);
        }
    }

    pub fn set_bool_output(&mut self, bool_output: BoolOutput) {
        self.bool_output = bool_output;
    }
}

impl Write for Output<'_> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let mut start = 0usize;
        let mut rem = buf.len();
        while rem > 0 {
            let len = (self.buf.len() - self.at).min(rem);
            self.buf[self.at..self.at + len].copy_from_slice(&buf[start..start + len]);
            self.at += len;
            if self.at == self.buf.len() {
                self.flush();
            }
            start += len;
            rem -= len;
        }
        self.maybe_flush();
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.flush();
        Ok(())
    }
}

pub trait Writable {
    fn write(&self, output: &mut Output);
}

impl Writable for &str {
    fn write(&self, output: &mut Output) {
        output.write_all(self.as_bytes()).unwrap();
    }
}

impl Writable for String {
    fn write(&self, output: &mut Output) {
        output.write_all(self.as_bytes()).unwrap();
    }
}

impl Writable for char {
    fn write(&self, output: &mut Output) {
        output.put(*self as u8);
    }
}

impl<T: Writable> Writable for [T] {
    fn write(&self, output: &mut Output) {
        output.print_iter(self.iter());
    }
}

impl<T: Writable, const N: usize> Writable for [T; N] {
    fn write(&self, output: &mut Output) {
        output.print_iter(self.iter());
    }
}

impl<T: Writable + ?Sized> Writable for &T {
    fn write(&self, output: &mut Output) {
        T::write(self, output)
    }
}

impl<T: Writable> Writable for Vec<T> {
    fn write(&self, output: &mut Output) {
        self.as_slice().write(output);
    }
}

impl Writable for () {
    fn write(&self, _output: &mut Output) {}
}

macro_rules! write_to_string {
    ($($t:ident)+) => {$(
        impl Writable for $t {
            fn write(&self, output: &mut Output) {
                self.to_string().write(output);
            }
        }
    )+};
}

write_to_string!(u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);

macro_rules! tuple_writable {
    ($name0:ident $($name:ident: $id:tt )*) => {
        impl<$name0: Writable, $($name: Writable,)*> Writable for ($name0, $($name,)*) {
            fn write(&self, out: &mut Output) {
                self.0.write(out);
                $(
                out.put(b' ');
                self.$id.write(out);
                )*
            }
        }
    }
}

tuple_writable! {T}
tuple_writable! {T U:1}
tuple_writable! {T U:1 V:2}
tuple_writable! {T U:1 V:2 X:3}
tuple_writable! {T U:1 V:2 X:3 Y:4}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6 B:7}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6 B:7 C:8}

impl<T: Writable> Writable for Option<T> {
    fn write(&self, output: &mut Output) {
        match self {
            None => (-1).write(output),
            Some(t) => t.write(output),
        }
    }
}

impl Writable for bool {
    fn write(&self, output: &mut Output) {
        let bool_output = output.bool_output;
        bool_output.output(output, *self)
    }
}

impl<T: Writable> Writable for Reverse<T> {
    fn write(&self, output: &mut Output) {
        self.0.write(output);
    }
}

static mut ERR: Option<Stderr> = None;

pub fn err() -> Output<'static> {
    unsafe {
        if ERR.is_none() {
            ERR = Some(stderr());
        }
        Output::new_with_auto_flush(ERR.as_mut().unwrap())
    }
}
}
}
pub mod misc {
pub mod test_type {
pub enum TestType {
    Single,
    MultiNumber,
    MultiEof,
}

pub enum TaskType {
    Classic,
    Interactive,
}
}
pub mod value {
use std::hash::Hash;

pub trait Value<T>: Copy + Eq + Hash {
    fn val() -> T;
}

pub trait ConstValue<T>: Value<T> {
    const VAL: T;
}

impl<T, V: ConstValue<T>> Value<T> for V {
    fn val() -> T {
        Self::VAL
    }
}

#[macro_export]
macro_rules! value {
    ($name: ident: $t: ty = $val: expr) => {
        #[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Default)]
        pub struct $name {}

        impl $crate::algo_lib::misc::value::ConstValue<$t> for $name {
            const VAL: $t = $val;
        }
    };
}

pub trait DynamicValue<T>: Value<T> {
    //noinspection RsSelfConvention
    fn set_val(t: T);
}

#[macro_export]
macro_rules! dynamic_value {
    ($name: ident: $t: ty, $val: ident) => {
        static mut $val: Option<$t> = None;

        #[derive(Copy, Clone, Eq, PartialEq, Hash, Default)]
        struct $name {}

        impl $crate::algo_lib::misc::value::DynamicValue<$t> for $name {
            fn set_val(t: $t) {
                unsafe {
                    $val = Some(t);
                }
            }
        }

        impl $crate::algo_lib::misc::value::Value<$t> for $name {
            fn val() -> $t {
                unsafe { $val.unwrap() }
            }
        }
    };
    ($name: ident: $t: ty) => {
        dynamic_value!($name: $t, VAL);
    };
    ($name: ident: $t: ty = $val: expr) => {
        dynamic_value!($name: $t);

        $name::set_val($val);
    };
    ($name: ident: $t: ty = $val: expr, $val_static: ident) => {
        dynamic_value!($name: $t, $val_static);

        $name::set_val($val);
    };
}
}
}
pub mod numbers {
pub mod inf_int {
use crate::algo_lib::io::output::Output;
use crate::algo_lib::io::output::Writable;
use crate::algo_lib::misc::value::Value;
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;
use crate::algo_lib::numbers::num_traits::algebra::IntegerMultiplicationMonoid;
use crate::algo_lib::numbers::num_traits::algebra::Zero;
use std::marker::PhantomData;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Mul;
use std::ops::MulAssign;

#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone)]
pub struct InfInt<T, V: Value<T>> {
    pub n: T,
    phantom: PhantomData<V>,
}

impl<T: Ord, V: Value<T>> InfInt<T, V> {
    pub fn new(n: T) -> Self {
        Self {
            n: n.min(V::val()),
            phantom: Default::default(),
        }
    }

    pub fn is_infinity(&self) -> bool {
        self.n == V::val()
    }
}

impl<T: AdditionMonoidWithSub + Ord + Copy, V: Value<T>> AddAssign for InfInt<T, V> {
    fn add_assign(&mut self, rhs: Self) {
        if rhs.is_infinity() || V::val() - rhs.n <= self.n {
            self.n = V::val();
        } else {
            self.n += rhs.n;
        }
    }
}

impl<T: AdditionMonoidWithSub + Ord + Copy, V: Value<T>> Add for InfInt<T, V> {
    type Output = Self;

    fn add(mut self, rhs: Self) -> Self::Output {
        self += rhs;
        self
    }
}

impl<T: IntegerMultiplicationMonoid + Zero + Ord + Copy, V: Value<T>> MulAssign for InfInt<T, V> {
    fn mul_assign(&mut self, rhs: Self) {
        if rhs.n != T::zero() && V::val() / rhs.n < self.n {
            self.n = V::val();
        } else {
            self.n *= rhs.n;
        }
    }
}

impl<T: IntegerMultiplicationMonoid + Zero + Ord + Copy, V: Value<T>> Mul for InfInt<T, V> {
    type Output = Self;

    fn mul(mut self, rhs: Self) -> Self::Output {
        self *= rhs;
        self
    }
}

impl<T: Writable, V: Value<T>> Writable for InfInt<T, V> {
    fn write(&self, output: &mut Output) {
        self.n.write(output)
    }
}
}
pub mod num_traits {
pub mod algebra {
use crate::algo_lib::numbers::num_traits::invertible::Invertible;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Div;
use std::ops::DivAssign;
use std::ops::Mul;
use std::ops::MulAssign;
use std::ops::Neg;
use std::ops::Rem;
use std::ops::RemAssign;
use std::ops::Sub;
use std::ops::SubAssign;

pub trait Zero {
    fn zero() -> Self;
}

pub trait One {
    fn one() -> Self;
}

pub trait AdditionMonoid: Add<Output = Self> + AddAssign + Zero + Eq + Sized {}

impl<T: Add<Output = Self> + AddAssign + Zero + Eq> AdditionMonoid for T {}

pub trait AdditionMonoidWithSub: AdditionMonoid + Sub<Output = Self> + SubAssign {}

impl<T: AdditionMonoid + Sub<Output = Self> + SubAssign> AdditionMonoidWithSub for T {}

pub trait AdditionGroup: AdditionMonoidWithSub + Neg<Output = Self> {}

impl<T: AdditionMonoidWithSub + Neg<Output = Self>> AdditionGroup for T {}

pub trait MultiplicationMonoid: Mul<Output = Self> + MulAssign + One + Eq + Sized {}

impl<T: Mul<Output = Self> + MulAssign + One + Eq> MultiplicationMonoid for T {}

pub trait IntegerMultiplicationMonoid:
    MultiplicationMonoid + Div<Output = Self> + Rem<Output = Self> + DivAssign + RemAssign
{
}

impl<T: MultiplicationMonoid + Div<Output = Self> + Rem<Output = Self> + DivAssign + RemAssign>
    IntegerMultiplicationMonoid for T
{
}

pub trait MultiplicationGroup:
    MultiplicationMonoid + Div<Output = Self> + DivAssign + Invertible<Output = Self>
{
}

impl<T: MultiplicationMonoid + Div<Output = Self> + DivAssign + Invertible<Output = Self>>
    MultiplicationGroup for T
{
}

pub trait SemiRing: AdditionMonoid + MultiplicationMonoid {}

impl<T: AdditionMonoid + MultiplicationMonoid> SemiRing for T {}

pub trait SemiRingWithSub: AdditionMonoidWithSub + SemiRing {}

impl<T: AdditionMonoidWithSub + SemiRing> SemiRingWithSub for T {}

pub trait Ring: SemiRing + AdditionGroup {}

impl<T: SemiRing + AdditionGroup> Ring for T {}

pub trait IntegerSemiRing: SemiRing + IntegerMultiplicationMonoid {}

impl<T: SemiRing + IntegerMultiplicationMonoid> IntegerSemiRing for T {}

pub trait IntegerSemiRingWithSub: SemiRingWithSub + IntegerSemiRing {}

impl<T: SemiRingWithSub + IntegerSemiRing> IntegerSemiRingWithSub for T {}

pub trait IntegerRing: IntegerSemiRing + Ring {}

impl<T: IntegerSemiRing + Ring> IntegerRing for T {}

pub trait Field: Ring + MultiplicationGroup {}

impl<T: Ring + MultiplicationGroup> Field for T {}

macro_rules! zero_one_integer_impl {
    ($($t: ident)+) => {$(
        impl Zero for $t {
            fn zero() -> Self {
                0
            }
        }

        impl One for $t {
            fn one() -> Self {
                1
            }
        }
    )+};
}

zero_one_integer_impl!(i128 i64 i32 i16 i8 isize u128 u64 u32 u16 u8 usize);
}
pub mod invertible {
pub trait Invertible {
    type Output;

    fn inv(&self) -> Option<Self::Output>;
}
}
}
}
pub mod string {
pub mod str {
use crate::algo_lib::collections::iter_ext::collect::IterCollect;
use crate::algo_lib::io::input::Input;
use crate::algo_lib::io::input::Readable;
use crate::algo_lib::io::output::Output;
use crate::algo_lib::io::output::Writable;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use std::iter::Copied;
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ops::Index;
use std::ops::IndexMut;
use std::slice::Iter;
use std::slice::IterMut;
use std::slice::SliceIndex;
use std::str::FromStr;
use std::vec::IntoIter;

pub enum Str<'s> {
    Extendable(Vec<u8>, PhantomData<&'s [u8]>),
    Owned(Box<[u8]>, PhantomData<&'s [u8]>),
    Ref(&'s [u8]),
}

impl Default for Str<'static> {
    fn default() -> Self {
        Self::new()
    }
}

impl Str<'static> {
    pub fn new() -> Self {
        Str::Extendable(Vec::new(), PhantomData)
    }

    pub fn with_capacity(cap: usize) -> Self {
        Str::Extendable(Vec::with_capacity(cap), PhantomData)
    }
}

impl<'s> Str<'s> {
    pub fn push(&mut self, c: u8) {
        self.transform_to_extendable();
        self.as_extendable().push(c)
    }

    pub fn pop(&mut self) -> Option<u8> {
        self.transform_to_extendable();
        self.as_extendable().pop()
    }

    pub fn as_slice(&self) -> &[u8] {
        match self {
            Str::Extendable(s, _) => s.as_ref(),
            Str::Owned(s, _) => s.as_ref(),
            Str::Ref(s) => s,
        }
    }

    pub fn len(&self) -> usize {
        self.as_slice().len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn iter(&self) -> Copied<Iter<u8>> {
        match self {
            Str::Extendable(v, _) => v.iter(),
            Str::Owned(v, _) => v.iter(),
            Str::Ref(v) => v.iter(),
        }
        .copied()
    }

    pub fn iter_mut(&mut self) -> IterMut<u8> {
        self.transform_to_owned();
        self.as_mut_slice().iter_mut()
    }

    pub fn sort(&mut self) {
        self.transform_to_owned();
        self.as_mut_slice().sort_unstable();
    }

    pub fn into_owned(mut self) -> Str<'static> {
        self.transform_to_owned();
        match self {
            Str::Extendable(v, _) => Str::Extendable(v, PhantomData),
            Str::Owned(v, _) => Str::Owned(v, PhantomData),
            _ => unreachable!(),
        }
    }

    fn transform_to_extendable(&mut self) {
        match self {
            Str::Extendable(_, _) => {}
            Str::Owned(_, _) => {
                let mut fake = Str::new();
                std::mem::swap(self, &mut fake);
                if let Str::Owned(s, _) = fake {
                    *self = Str::Extendable(s.into_vec(), PhantomData)
                } else {
                    unreachable!();
                }
            }
            Str::Ref(s) => *self = Str::Extendable(s.to_vec(), PhantomData),
        }
    }

    fn as_extendable(&mut self) -> &mut Vec<u8> {
        match self {
            Str::Extendable(s, _) => s,
            _ => panic!("unreachable"),
        }
    }

    fn transform_to_owned(&mut self) {
        if let Str::Ref(s) = self {
            *self = Str::Owned(s.to_vec().into_boxed_slice(), PhantomData)
        }
    }

    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        self.transform_to_owned();
        match self {
            Str::Extendable(s, _) => s.as_mut_slice(),
            Str::Owned(s, _) => s.as_mut(),
            _ => panic!("unreachable"),
        }
    }

    pub fn into_string(self) -> String {
        match self {
            Str::Extendable(v, _) => unsafe { String::from_utf8_unchecked(v) },
            Str::Owned(v, _) => unsafe { String::from_utf8_unchecked(v.into_vec()) },
            Str::Ref(v) => String::from_utf8_lossy(v).into_owned(),
        }
    }

    pub fn reverse(&mut self) {
        self.as_mut_slice().reverse();
    }

    pub fn trim(&self) -> Str<'_> {
        let mut start = 0;
        let mut end = self.len();
        while start < end && (self[start] as char).is_whitespace() {
            start += 1;
        }
        while start < end && (self[end - 1] as char).is_whitespace() {
            end -= 1;
        }
        self[start..end].into()
    }

    pub fn split<'a, 'b>(&'a self, sep: impl Into<Str<'b>>) -> Vec<Str<'a>>
    where
        's: 'a,
    {
        let sep = sep.into();
        let mut res = Vec::new();
        let mut start = 0;
        for i in 0..self.len() {
            if self[i..].starts_with(sep.as_slice()) {
                res.push(self[start..i].into());
                start = i + sep.len();
            }
        }
        res.push(self[start..].into());
        res
    }

    pub fn parse<F: FromStr>(self) -> F
    where
        F::Err: Debug,
    {
        self.into_string().parse().unwrap()
    }

    pub fn parse_vec<T: Readable>(&self) -> Vec<T> {
        let mut bytes = self.as_slice();
        let mut input = Input::new(&mut bytes);
        let mut res = Vec::new();
        while !input.is_exhausted() {
            res.push(input.read());
        }
        res
    }

    pub fn qty(&self, from: u8, to: u8) -> Vec<usize> {
        let mut res = vec![0; (to - from + 1) as usize];
        for &c in self.as_slice() {
            res[(c - from) as usize] += 1;
        }
        res
    }

    pub fn qty_lower(&self) -> Vec<usize> {
        self.qty(b'a', b'z')
    }
}

impl<'s> IntoIterator for Str<'s> {
    type Item = u8;
    type IntoIter = IntoIter<u8>;

    #[allow(clippy::unnecessary_to_owned)]
    fn into_iter(self) -> Self::IntoIter {
        match self {
            Str::Extendable(v, _) => v.into_iter(),
            Str::Owned(v, _) => v.into_vec().into_iter(),
            Str::Ref(v) => v.to_vec().into_iter(),
        }
    }
}

impl From<String> for Str<'static> {
    fn from(s: String) -> Self {
        Str::Extendable(s.into(), PhantomData)
    }
}

impl<'s> From<&'s str> for Str<'s> {
    fn from(s: &'s str) -> Self {
        Str::Ref(s.as_bytes())
    }
}

impl From<Vec<u8>> for Str<'static> {
    fn from(s: Vec<u8>) -> Self {
        Str::Extendable(s, PhantomData)
    }
}

impl<'s> From<&'s [u8]> for Str<'s> {
    fn from(s: &'s [u8]) -> Self {
        Str::Ref(s)
    }
}

impl<'s, const N: usize> From<&'s [u8; N]> for Str<'s> {
    fn from(s: &'s [u8; N]) -> Self {
        Str::Ref(s)
    }
}

impl<'s> From<&'s String> for Str<'s> {
    fn from(s: &'s String) -> Self {
        Str::Ref(s.as_bytes())
    }
}

impl<'s> From<&'s Vec<u8>> for Str<'s> {
    fn from(s: &'s Vec<u8>) -> Self {
        Str::Ref(s.as_slice())
    }
}

impl From<u8> for Str<'static> {
    fn from(c: u8) -> Self {
        Str::Owned(Box::new([c]), PhantomData)
    }
}

impl From<char> for Str<'static> {
    fn from(c: char) -> Self {
        Str::from(c as u8)
    }
}

impl<'s, 't: 's> From<&'s Str<'t>> for Str<'s> {
    fn from(value: &'s Str<'t>) -> Self {
        Str::Ref(value.as_slice())
    }
}

impl<R: SliceIndex<[u8]>> Index<R> for Str<'_> {
    type Output = R::Output;

    fn index(&self, index: R) -> &Self::Output {
        self.as_slice().index(index)
    }
}

impl<R: SliceIndex<[u8]>> IndexMut<R> for Str<'_> {
    fn index_mut(&mut self, index: R) -> &mut Self::Output {
        self.transform_to_owned();
        self.as_mut_slice().index_mut(index)
    }
}

impl Clone for Str<'_> {
    fn clone(&self) -> Self {
        match self {
            Str::Extendable(s, _) => s.clone().into(),
            Str::Owned(s, _) => s.to_vec().into(),
            Str::Ref(s) => Str::Ref(s),
        }
    }
}

impl<'r, 's, S: Into<Str<'r>>> AddAssign<S> for Str<'s> {
    fn add_assign(&mut self, rhs: S) {
        self.transform_to_extendable();
        self.as_extendable()
            .extend_from_slice(rhs.into().as_slice());
    }
}

impl<'r, 's, S: Into<Str<'r>>> Add<S> for Str<'s> {
    type Output = Str<'s>;

    fn add(mut self, rhs: S) -> Self::Output {
        self += rhs;
        self
    }
}

impl Readable for Str<'static> {
    fn read(input: &mut Input) -> Self {
        input.next_token().unwrap().into()
    }
}

impl Writable for Str<'_> {
    fn write(&self, output: &mut Output) {
        for c in self.as_slice() {
            output.put(*c);
        }
        output.maybe_flush();
    }
}

impl Display for Str<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        <String as Display>::fmt(&String::from_utf8(self.as_slice().to_vec()).unwrap(), f)
    }
}

impl Hash for Str<'_> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.as_slice().hash(state);
    }
}

impl<'r> PartialEq<Str<'r>> for Str<'_> {
    fn eq(&self, other: &Str<'r>) -> bool {
        self.as_slice().eq(other.as_slice())
    }
}

impl Eq for Str<'_> {}

impl<'r> PartialOrd<Str<'r>> for Str<'_> {
    fn partial_cmp(&self, other: &Str<'r>) -> Option<Ordering> {
        self.as_slice().partial_cmp(other.as_slice())
    }
}

impl Ord for Str<'_> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_slice().cmp(other.as_slice())
    }
}

impl FromIterator<u8> for Str<'static> {
    fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self {
        Self::Extendable(iter.into_iter().collect_vec(), Default::default())
    }
}

impl<'r> FromIterator<&'r u8> for Str<'static> {
    fn from_iter<T: IntoIterator<Item = &'r u8>>(iter: T) -> Self {
        Self::Extendable(iter.into_iter().cloned().collect_vec(), Default::default())
    }
}

impl Deref for Str<'_> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

impl DerefMut for Str<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_slice()
    }
}

pub trait StrReader {
    fn read_str(&mut self) -> Str<'static>;
    fn read_str_vec(&mut self, n: usize) -> Vec<Str<'static>>;
    fn read_line(&mut self) -> Str<'static>;
    fn read_line_vec(&mut self, n: usize) -> Vec<Str<'static>>;
    fn read_lines(&mut self) -> Vec<Str<'static>>;
}

impl StrReader for Input<'_> {
    fn read_str(&mut self) -> Str<'static> {
        self.read()
    }

    fn read_str_vec(&mut self, n: usize) -> Vec<Str<'static>> {
        self.read_vec(n)
    }

    fn read_line(&mut self) -> Str<'static> {
        let mut res = Str::new();
        while let Some(c) = self.get() {
            if c == b'\n' {
                break;
            }
            res.push(c);
        }
        res
    }

    fn read_line_vec(&mut self, n: usize) -> Vec<Str<'static>> {
        let mut res = Vec::with_capacity(n);
        for _ in 0..n {
            res.push(self.read_line());
        }
        res
    }

    fn read_lines(&mut self) -> Vec<Str<'static>> {
        let mut res = Vec::new();
        while !self.is_exhausted() {
            res.push(self.read_line());
        }
        if let Some(s) = res.last() {
            if s.is_empty() {
                res.pop();
            }
        }
        res
    }
}
}
}
}
fn main() {
    let mut sin = std::io::stdin();
    let input = algo_lib::io::input::Input::new(&mut sin);
    let mut stdout = std::io::stdout();
    let output = algo_lib::io::output::Output::new(&mut stdout);
    solution::run(input, output);
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 2172kb

input:

6
7 3
12 3
9 10
249 51
1369 37
2 1

output:

9
13
10
251
1370
3

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 74ms
memory: 2152kb

input:

100000
3196282243 28
7614814237 33
2814581084 97
1075124401 58
7822266214 100
1767317768 31
7189709841 75
9061337538 69
6552679231 38
9946082148 18
5497675062 54
7787300351 65
4310767261 68
4811341953 100
3265496130 31
8294404054 62
2845521744 90
1114254672 26
6442013672 13
3744046866 40
3289624367 ...

output:

3196282244
7614814251
2814581097
1075124424
7822266300
1767317769
7189709850
9061337569
6552679238
9946082160
5497675063
7787300365
4310767268
4811342000
3265496131
8294404062
2845521790
1114254674
6442013673
3744046867
3289624375
6477935360
1292587551
5504674689
2898829180
7882736025
2846033387
923...

result:

ok 100000 lines

Test #3:

score: 0
Accepted
time: 144ms
memory: 2380kb

input:

100000
81390699571930639918 18
48435143897560239761 20
51628960043353404809 75
47871552664477358704 12
59273263135375104780 37
76916933870890715781 71
23716799616473386311 99
68152894841119747794 73
87132912926681514765 23
14152130046962902029 76
46737628796812988809 24
40572731276115804589 44
26281...

output:

81390699571930639920
48435143897560239780
51628960043353404825
47871552664477358712
59273263135375104781
76916933870890715782
23716799616473386312
68152894841119747819
87132912926681514784
14152130046962902060
46737628796812988824
40572731276115804612
26281826064684565884
31440839508345860244
322404...

result:

ok 100000 lines

Test #4:

score: 0
Accepted
time: 191ms
memory: 2364kb

input:

3000
7455619882273084107817302538515753878442453229160411398764628307708900718973255504130291326067024207515690930909093270962092669445260372092520050302437090344417275699335988483584829739890137897388569114922455183012826259500289116227868472807384706143668392682061768042886347879057062217228560088...

output:

745561988227308410781730253851575387844245322916041139876462830770890071897325550413029132606702420751569093090909327096209266944526037209252005030243709034441727569933598848358482973989013789738856911492245518301282625950028911622786847280738470614366839268206176804288634787905706221722856008829738...

result:

ok 3000 lines

Test #5:

score: 0
Accepted
time: 181ms
memory: 3176kb

input:

30
744510720233756810275704474604569745531035132640480367036243214564743016462196305795005354253050610470663589619681077004760475741831076832118956334526511556189341001350810877421229216943948759000423729628987372479322027768925371301094347427331321252113757179047865411408129452046409100799804797802...

output:

744510720233756810275704474604569745531035132640480367036243214564743016462196305795005354253050610470663589619681077004760475741831076832118956334526511556189341001350810877421229216943948759000423729628987372479322027768925371301094347427331321252113757179047865411408129452046409100799804797802630...

result:

ok 30 lines

Test #6:

score: 0
Accepted
time: 354ms
memory: 3028kb

input:

30
105809806761073271256887955844712146828376005200803410278562148742151831107043468856832007772397456498455138267388212588031778017994162605374749926516470689670500596864238694115641474868867283410369346289694466106606968664652794746652928273585660548259964572696052192486090603389036781439603103550...

output:

105809806761073271256887955844712146828376005200803410278562148742151831107043468856832007772397456498455138267388212588031778017994162605374749926516470689670500596864238694115641474868867283410369346289694466106606968664652794746652928273585660548259964572696052192486090603389036781439603103550889...

result:

ok 30 lines

Test #7:

score: 0
Accepted
time: 347ms
memory: 11000kb

input:

3
6662543589767466036246495639712784214474800991502397969575900509392047845463534421346617412312874112216857204224202275839595569716788606196210901109061789975871578517491994493982488930577332027254701120487517960025453194037512904252125592964194448410502834649100626540616447074805775399798416968996...

output:

666254358976746603624649563971278421447480099150239796957590050939204784546353442134661741231287411221685720422420227583959556971678860619621090110906178997587157851749199449398248893057733202725470112048751796002545319403751290425212559296419444841050283464910062654061644707480577539979841696899669...

result:

ok 3 lines

Test #8:

score: 0
Accepted
time: 195ms
memory: 11000kb

input:

3
2256815269292565600156592193265637162224642022040502404316754847487816065925979403817785481924286639616366305614486960621565248701689067791722502719550041520364571182248705842386853601004910941840817347333021115646730530046766144383475985584378580286873533357460940983992705079090280123003684728456...

output:

225681526929256560015659219326563716222464202204050240431675484748781606592597940381778548192428663961636630561448696062156524870168906779172250271955004152036457118224870584238685360100491094184081734733302111564673053004676614438347598558437858028687353335746094098399270507909028012300368472845644...

result:

ok 3 lines

Test #9:

score: 0
Accepted
time: 213ms
memory: 2164kb

input:

3000
1207038022435933497172867897608582834259296434513291920701948658102620971426545864576004516739100232550813726497575096647995838994831563955620812388750705335658097405810012745415674568116125791908722845971700778242654229220680637480729636188583094437004798976145084978037723467998680024921907308...

output:

120703802243593349717286789760858283425929643451329192070194865810262097142654586457600451673910023255081372649757509664799583899483156395562081238875070533565809740581001274541567456811612579190872284597170077824265422922068063748072963618858309443700479897614508497803772346799868002492190730846477...

result:

ok 3000 lines

Test #10:

score: 0
Accepted
time: 236ms
memory: 2124kb

input:

3000
5187892603368075393366934145761537065733813260653168421673419435040503882271162798390450740194183522231455416476478191713916592902549286209613912969893791259298651451942049981218891583442405056389087471219061688102180898906527834095792495015886014196868276353417569226991138737552057896779213092...

output:

518789260336807539336693414576153706573381326065316842167341943504050388227116279839045074019418352223145541647647819171391659290254928620961391296989379125929865145194204998121889158344240505638908747121906168810218089890652783409579249501588601419686827635341756922699113873755205789677921309299202...

result:

ok 3000 lines

Test #11:

score: 0
Accepted
time: 69ms
memory: 2080kb

input:

100000
6826989967 99
7841681495 15
3418122299 23
1701647194 72
9299999983 93
1881999956 82
1123299980 33
3771371885 19
1309999986 31
3877999989 78
9357331671 75
7627599957 76
1678299927 83
8841256998 57
8260576238 25
1799999994 18
3154099999 41
9276743997 44
4591431162 68
3499999996 35
8003134976 35...

output:

6826989968
7841681500
3418122300
1701647200
9299999993
1881999958
1123300000
3771371900
1310000000
3878000000
9357331675
7627599958
1678299983
8841257000
8260576250
1800000000
3154100000
9276744000
4591431168
3500000000
8003135000
5504020000
1255390000
4126500000
6300000000
7478000000
5763230000
666...

result:

ok 100000 lines

Test #12:

score: 0
Accepted
time: 119ms
memory: 2132kb

input:

100000
4044835167 404504
8453174053 180343
2345170166 345322
8666517296 666590
8889410862 895103
6539631279 398778
8763575235 742233
3347416626 347507
6020459808 204689
2518651157 186574
1970898496 970950
3644468912 446818
2284133423 265848
4297464212 297490
9673618882 687317
2262910438 26292
460442...

output:

4045040000
8453180343
2345322000
8666590000
8889510300
6539877800
8763742233
3347507000
6020468900
2518657400
1970950000
3644681800
2284166016
4297490000
9673687317
2262920000
4604460456
5602450000
9441133300
9662607060
1137808896
5763236104
1265770000
9709569000
2270400726
8750103140
4985572109
607...

result:

ok 100000 lines

Test #13:

score: 0
Accepted
time: 390ms
memory: 2132kb

input:

30000
2258423372953702192256514103021059999999999999999999999999999999999999999999999999999999999942889028 410302106
9236851817519460666717196537240869999999999999999999999999999999999999999999999999999999999883401708 653724087
528305359636938134447446654161626435106860486355385047059638284973309516...

output:

2258423372953702192256514103021060000000000000000000000000000000000000000000000000000000000000000000
9236851817519460666717196537240870000000000000000000000000000000000000000000000000000000000000000000
52830535963693813444744665416162643510686048635538504705963828497330951691000000000000000000000000...

result:

ok 30000 lines

Test #14:

score: 0
Accepted
time: 348ms
memory: 2148kb

input:

3000
5922323879507505766800528767037919597502198414242047957236861868104971055619344440437107671176937451514382066899903609033675738466227459315946342972297607721778919880762618920008986787526795904399466415526677955192892547202914540028866220838677781672961400903843675918899999999999999999999999999...

output:

592232387950750576680052876703791959750219841424204795723686186810497105561934444043710767117693745151438206689990360903367573846622745931594634297229760772177891988076261892000898678752679590439946641552667795519289254720291454002886622083867778167296140090384367591890000000000000000000000000000000...

result:

ok 3000 lines

Test #15:

score: 0
Accepted
time: 330ms
memory: 2220kb

input:

300
79503684358998967099842390859918474661077381309414120162088118845509995775769599862634805068256914519241333788469171358889239210321345976686972530426806265499919511256567835592757432091301073156228966702566684249419578352892680950286666144449266128886167409392158527498584296552596909871547436747...

output:

795036843589989670998423908599184746610773813094141201620881188455099957757695998626348050682569145192413337884691713588892392103213459766869725304268062654999195112565678355927574320913010731562289667025666842494195783528926809502866661444492661288861674093921585274985842965525969098715474367473579...

result:

ok 300 lines

Test #16:

score: 0
Accepted
time: 332ms
memory: 2964kb

input:

30
963989556914230908082271728546945777135479223654091867288007126245836594537638569203178109698540536134315332558020645906558121951862746368709679951030471570045529976669887496328009080338319231117849873217415406534659827460233181034192437276876779118787074109399261707873162971159516672507806779438...

output:

963989556914230908082271728546945777135479223654091867288007126245836594537638569203178109698540536134315332558020645906558121951862746368709679951030471570045529976669887496328009080338319231117849873217415406534659827460233181034192437276876779118787074109399261707873162971159516672507806779438881...

result:

ok 30 lines

Test #17:

score: 0
Accepted
time: 348ms
memory: 10932kb

input:

3
5251523982592970506228422825473158460046709412102650259324461772573819982478268554863488566358166621796735028520826739449136398485099429491505550466187845809470304336668405076216439622257295011604734741380276770594862747289699140439236774183654203024248651235112580263413687129799928450508420430094...

output:

525152398259297050622842282547315846004670941210265025932446177257381998247826855486348856635816662179673502852082673944913639848509942949150555046618784580947030433666840507621643962225729501160473474138027677059486274728969914043923677418365420302424865123511258026341368712979992845050842043009466...

result:

ok 3 lines

Test #18:

score: 0
Accepted
time: 177ms
memory: 3120kb

input:

30
627919418377410357943555763628655192389689054924973950611879805274209177346126760861588844600423459519850583010250101760720518969321844870291790270120612460050314573390014064010258558280381655682905366739760793312813232203778926302125707670987782450886207303011675839927569831089665011115588292404...

output:

627919418377410357943555763628655192389689054924973950611879805274209177346126760861588844600423459519850583010250101760720518969321844870291790270120612460050314573390014064010258558280381655682905366739760793312813232203778926302125707670987782450886207303011675839927569831089665011115588292404149...

result:

ok 30 lines

Test #19:

score: 0
Accepted
time: 165ms
memory: 3028kb

input:

30
764080955258682719471086701870262765599374530101516574475330551922255442091459958688289946340233580617988205812251968123424819960658592846247598815326003118826452825551875685355929397031899182527979539632844954405219059237546143875107808300965485161976399348689613744946767210379782917080998523510...

output:

764080955258682719471086701870262765599374530101516574475330551922255442091459958688289946340233580617988205812251968123424819960658592846247598815326003118826452825551875685355929397031899182527979539632844954405219059237546143875107808300965485161976399348689613744946767210379782917080998523510627...

result:

ok 30 lines

Test #20:

score: 0
Accepted
time: 172ms
memory: 10800kb

input:

3
6028978024074413159342407738765076385763911038536831317696787959027528197366178750790544749013673661567582902671539726842479448599455873905708255453884865704773086705053235875806914264836730112475075045703811837740246613047422355645457818355939368587060246878107326418339579284201653817689773671953...

output:

602897802407441315934240773876507638576391103853683131769678795902752819736617875079054474901367366156758290267153972684247944859945587390570825545388486570477308670505323587580691426483673011247507504570381183774024661304742235564545781835593936858706024687810732641833957928420165381768977367195319...

result:

ok 3 lines

Test #21:

score: 0
Accepted
time: 166ms
memory: 11004kb

input:

3
5021181897690139043423621132705730237575386907971979814912425232029109374214407872841234899499833146294421742332452796878337581201268864699781904270882612294625097518085924677142481018311453949030110398429573396190105839011124187467223158307238425121693199936952649840784178844249496013886360158949...

output:

502118189769013904342362113270573023757538690797197981491242523202910937421440787284123489949983314629442174233245279687833758120126886469978190427088261229462509751808592467714248101831145394903011039842957339619010583901112418746722315830723842512169319993695264984078417884424949601388636015894947...

result:

ok 3 lines

Test #22:

score: 0
Accepted
time: 30ms
memory: 2144kb

input:

100000
983 89
38 751
34 201
841 39
453 873
674 65
560 216
914 644
670 345
622 518
583 938
137 719
359 78
448 860
741 907
236 629
893 549
844 999
63 345
320 66
990 723
215 467
665 24
682 756
853 604
262 160
153 30
278 31
232 631
495 360
347 519
259 906
541 992
167 33
661 233
908 720
55 332
17 153
595...

output:

989
751
201
858
873
715
648
1288
690
1036
938
719
378
860
907
629
1098
999
345
330
1446
467
672
756
1208
320
180
279
631
720
519
906
992
198
699
1440
332
153
752
206
1534
1188
977
1142
1232
741
754
482
891
504
376
921
852
658
974
700
266
792
500
897
976
570
1258
869
120
578
616
1274
1240
860
453
229...

result:

ok 100000 lines

Test #23:

score: 0
Accepted
time: 70ms
memory: 2140kb

input:

100000
145493 709877
915888 170549
680394 2504
24665 758139
242644 365499
994789 535590
518365 297247
852392 834337
480961 945750
485671 126953
262033 11673
199902 473744
91856 743475
779195 394619
60537 616388
570144 756645
285494 510464
298148 932570
110058 627373
686867 469501
82153 559208
231338...

output:

709877
1023294
681088
758139
365499
1071180
594494
1668674
945750
507812
268479
473744
743475
789238
616388
756645
510464
932570
627373
939002
559208
272262
957378
749236
980046
1307232
883500
695250
1351682
495376
391899
1403758
674864
399172
826044
486136
797852
368134
1341368
476373
998655
234605...

result:

ok 100000 lines

Test #24:

score: 0
Accepted
time: 134ms
memory: 2180kb

input:

100000
22102638 716270982
114744220 553198855
952648509 973042933
12679271 411855162
766516692 376478639
797071538 701616593
832850102 567088052
350649206 231532599
950645496 638081144
879396229 769653386
7765699 641178981
664333826 683883038
953508821 193902679
470742245 399344120
571075073 4774108...

output:

716270982
553198855
973042933
411855162
1129435917
1403233186
1134176104
463065198
1276162288
1539306772
641178981
683883038
969513395
798688240
954821692
885018278
759825152
720908055
304133428
841067258
1086439989
172971588
1018041974
1194071806
1186808712
1520671194
1640903698
406899456
841206665...

result:

ok 100000 lines

Test #25:

score: 0
Accepted
time: 63ms
memory: 2152kb

input:

100000
223876976 28
826504481 32
636375005 4
96535351 65
198842419 24
831169545 73
576953641 82
935299711 20
387543800 23
179825475 79
473820636 86
461889861 8
534725270 34
388928056 62
64278765 63
549355282 3
708135976 13
290272912 29
411662229 49
516874672 54
115846918 82
627529269 63
23910223 33
...

output:

223876996
826504512
636375008
96535352
198842420
831169573
576953682
935299720
387543813
179825476
473820686
461889862
534725271
388928062
64278774
549355283
708135977
290272913
411662230
516874716
115846976
627529329
23910233
590830376
319867594
585259345
740335580
846844351
652565836
303759440
212...

result:

ok 100000 lines

Test #26:

score: 0
Accepted
time: 71ms
memory: 2280kb

input:

100000
95 410302106
52 820706056
67 87747886
43 380552276
40 627914953
23 374682702
8 733202611
86 386357954
3 550477660
64 659482338
63 766955734
79 625128385
13 406562646
8 777253591
37 653724087
100 107364480
58 953592822
68 213009532
67 302548407
41 282013297
95 228056767
92 407864420
18 1265642...

output:

410302106
820706056
87747886
380552276
627914953
374682702
733202611
386357954
550477660
659482338
766955734
625128385
406562646
777253591
653724087
107364480
953592822
213009532
302548407
282013297
228056767
407864420
126564216
867640132
89634672
168202079
693460954
910218987
330951691
513234559
11...

result:

ok 100000 lines

Test #27:

score: 0
Accepted
time: 26ms
memory: 2144kb

input:

99856
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 6...

output:

2
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
...

result:

ok 99856 lines

Test #28:

score: 0
Accepted
time: 221ms
memory: 12912kb

input:

3
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

result:

ok 3 lines

Test #29:

score: 0
Accepted
time: 228ms
memory: 11040kb

input:

3
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

result:

ok 3 lines

Test #30:

score: 0
Accepted
time: 34ms
memory: 2336kb

input:

24573
1 1000000000
2 1000000000
3 1000000000
4 1000000000
5 1000000000
6 1000000000
7 1000000000
8 1000000000
9 1000000000
10 1000000000
11 1000000000
12 1000000000
13 1000000000
14 1000000000
15 1000000000
16 1000000000
17 1000000000
18 1000000000
19 1000000000
20 1000000000
21 1000000000
22 100000...

output:

1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
100...

result:

ok 24573 lines

Test #31:

score: 0
Accepted
time: 238ms
memory: 12936kb

input:

3
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

result:

ok 3 lines

Test #32:

score: 0
Accepted
time: 237ms
memory: 11064kb

input:

3
8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888...

output:

888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888...

result:

ok 3 lines

Extra Test:

score: 0
Extra Test Passed