提出 #20096332


ソースコード 拡げる

#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]

use std::str::FromStr;
use std::io::*;
use std::collections::*;
use std::cmp::*;

struct Scanner<I: Iterator<Item = char>> {
    iter: std::iter::Peekable<I>,
}

macro_rules! exit {
    () => {{
        exit!(0)
    }};
    ($code:expr) => {{
        if cfg!(local) {
            writeln!(std::io::stderr(), "===== Terminated =====")
                .expect("failed printing to stderr");
        }
        std::process::exit($code);
    }}
}

impl<I: Iterator<Item = char>> Scanner<I> {
    pub fn new(iter: I) -> Scanner<I> {
        Scanner {
            iter: iter.peekable(),
        }
    }

    pub fn safe_get_token(&mut self) -> Option<String> {
        let token = self.iter
            .by_ref()
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        if token.is_empty() {
            None
        } else {
            Some(token)
        }
    }

    pub fn token(&mut self) -> String {
        self.safe_get_token().unwrap_or_else(|| exit!())
    }

    pub fn get<T: FromStr>(&mut self) -> T {
        self.token().parse::<T>().unwrap_or_else(|_| exit!())
    }

    pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
        (0..len).map(|_| self.get()).collect()
    }

    pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
        (0..row).map(|_| self.vec(col)).collect()
    }

    pub fn char(&mut self) -> char {
        self.iter.next().unwrap_or_else(|| exit!())
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.get::<String>().chars().collect()
    }

    pub fn mat_chars(&mut self, row: usize) -> Vec<Vec<char>> {
        (0..row).map(|_| self.chars()).collect()
    }

    pub fn line(&mut self) -> String {
        if self.peek().is_some() {
            self.iter
                .by_ref()
                .take_while(|&c| !(c == '\n' || c == '\r'))
                .collect::<String>()
        } else {
            exit!();
        }
    }

    pub fn peek(&mut self) -> Option<&char> {
        self.iter.peek()
    }
}

use std::fmt;
use std::ops::*;
use std::mem::swap;

use btree_map::IterMut;

#[derive(Copy, Clone, PartialEq, Debug)]
struct Mint(u64);

static mut MODULO: u64 = 1000000007;

impl Mint {
    #[inline]
    fn modulo() -> u64 {
        unsafe {
            MODULO
        }
    }
    fn set_modulo(n: u64) {
        unsafe {
            MODULO = n;
        }
    }
    #[inline]
    fn pow(self, power: u64) -> Self {
        if power == 0 {
            return Mint(1);
        }
        if power & 1 == 0 {
            let t = self.pow(power / 2);
            t * t
        } else {
            self * self.pow(power - 1)
        }
    }
    #[inline]
    fn inv(self) -> Self {
        let mut a = self.0 as i64;
        let (mut b, mut u, mut v) = (Self::modulo() as i64, 1, 0);
        while b != 0 {
            let t = a / b;
            a -= t * b; swap(&mut a, &mut b);
            u -= t * v; swap(&mut u, &mut v);
        }
        u %= Self::modulo() as i64;
        if u < 0 {
            u += Self::modulo() as i64;
        }
        Mint(u as u64)
    }
}

impl Add for Mint {
    type Output = Self;
    #[inline]
    fn add(self, rhs: Self) -> Self {
        let mut ret = self;
        ret.0 += rhs.0;
        if ret.0 >= Self::modulo() {
            ret.0 -= Self::modulo();
        }
        ret
    }
}

impl Add<u64> for Mint {
    type Output = Self;
    #[inline]
    fn add(self, rhs: u64) -> Self {
        self + Mint(rhs)
    }
}

impl AddAssign for Mint {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl AddAssign<u64> for Mint {
    #[inline]
    fn add_assign(&mut self, rhs: u64) {
        *self = *self + rhs;
    }
}

impl Sub for Mint {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: Self) -> Self {
        let mut ret = self;
        if ret.0 < rhs.0 {
            ret.0 += Self::modulo();
        }
        ret.0 -= rhs.0;
        ret
    }
}

impl Sub<u64> for Mint {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: u64) -> Self {
        self - Mint(rhs)
    }
}

impl SubAssign for Mint {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl SubAssign<u64> for Mint {
    #[inline]
    fn sub_assign(&mut self, rhs: u64) {
        *self = *self - rhs;
    }
}

impl Mul for Mint {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: Mint) -> Self {
        let mut ret = self;
        ret.0 *= rhs.0;
        ret.0 %= Self::modulo();
        ret
    }
}

impl Mul<u64> for Mint {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: u64) -> Self {
        let mut ret = self;
        ret.0 *= rhs;
        ret.0 %= Self::modulo();
        ret
    }
}

impl MulAssign for Mint {
    #[inline]
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl MulAssign<u64> for Mint {
    #[inline]
    fn mul_assign(&mut self, rhs: u64) {
        *self = *self * rhs;
    }
}

impl Div for Mint {
    type Output = Mint;
    #[inline]
    fn div(self, rhs: Self) -> Self {
        let mut ret = self;
        ret.0 *= rhs.inv().0;
        ret.0 %= Self::modulo();
        ret
    }
}

impl Div<u64> for Mint {
    type Output = Mint;
    #[inline]
    fn div(self, rhs: u64) -> Self {
        let mut ret = self;
        ret.0 *= Mint(rhs).inv().0;
        ret.0 %= Self::modulo();
        ret
    }
}

impl DivAssign for Mint {
    #[inline]
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

impl DivAssign<u64> for Mint {
    #[inline]
    fn div_assign(&mut self, rhs: u64) {
        *self = *self / rhs;
    }
}

impl Neg for Mint {
    type Output = Mint;
    fn neg(self) -> Self {
        Mint(0) - self
    }
}

impl fmt::Display for Mint {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

fn gcd<T>(v1: T, v2: T) -> T
where
    T: Rem<Output=T>
     + Copy
     + Default
     + PartialEq    
{
    let zero = T::default();
    match (v1, v2) {
        (v1, v2) if v2 == zero => v1,
        (v1, v2) => gcd(v2, v1 % v2)
    }
}

fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char));
    let T: usize = sc.get();
    for _ in 0..T {
        let mut N: u64 = sc.get();
        let mut S: u64 = sc.get();
        let mut K: u64 = sc.get();
        let g = gcd(N, K);
        if S % g != 0 {
            println!("-1");
            continue;
        }
        N /= g;
        S /= g;
        K /= g;
        Mint::set_modulo(N);
        let ans = -Mint(S) / K;
        println!("{}", ans);
    }
}

提出情報

提出日時
問題 E - Throne
ユーザ kaki_xxx
言語 Rust (1.42.0)
得点 500
コード長 6833 Byte
結果 AC
実行時間 6 ms
メモリ 2120 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 1
AC × 32
セット名 テストケース
Sample sample_01.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, sample_01.txt
ケース名 結果 実行時間 メモリ
hand_01.txt AC 6 ms 1880 KiB
random_01.txt AC 2 ms 1884 KiB
random_02.txt AC 3 ms 1976 KiB
random_03.txt AC 2 ms 1976 KiB
random_04.txt AC 3 ms 1924 KiB
random_05.txt AC 2 ms 2016 KiB
random_06.txt AC 2 ms 1932 KiB
random_07.txt AC 2 ms 2072 KiB
random_08.txt AC 2 ms 2016 KiB
random_09.txt AC 2 ms 1924 KiB
random_10.txt AC 2 ms 2028 KiB
random_11.txt AC 2 ms 2056 KiB
random_12.txt AC 2 ms 1948 KiB
random_13.txt AC 1 ms 2112 KiB
random_14.txt AC 2 ms 1956 KiB
random_15.txt AC 2 ms 2004 KiB
random_16.txt AC 2 ms 2056 KiB
random_17.txt AC 2 ms 2048 KiB
random_18.txt AC 3 ms 2108 KiB
random_19.txt AC 2 ms 1996 KiB
random_20.txt AC 2 ms 2036 KiB
random_21.txt AC 2 ms 1884 KiB
random_22.txt AC 2 ms 1976 KiB
random_23.txt AC 6 ms 2024 KiB
random_24.txt AC 4 ms 2004 KiB
random_25.txt AC 2 ms 2024 KiB
random_26.txt AC 2 ms 1944 KiB
random_27.txt AC 2 ms 1884 KiB
random_28.txt AC 2 ms 1956 KiB
random_29.txt AC 2 ms 2076 KiB
random_30.txt AC 2 ms 2120 KiB
sample_01.txt AC 2 ms 2012 KiB