結果

問題 No.1258 コインゲーム
ユーザー o2co2c
提出日時 2020-10-16 23:17:51
言語 Rust
(1.77.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 6,470 bytes
コンパイル時間 4,109 ms
コンパイル使用メモリ 151,068 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2023-09-28 05:18:15
合計ジャッジ時間 10,645 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
4,376 KB
testcase_01 AC 22 ms
4,376 KB
testcase_02 AC 8 ms
4,376 KB
testcase_03 AC 69 ms
4,380 KB
testcase_04 AC 77 ms
4,380 KB
testcase_05 AC 13 ms
4,376 KB
testcase_06 AC 57 ms
4,380 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 29 ms
4,376 KB
testcase_10 AC 68 ms
4,376 KB
testcase_11 AC 74 ms
4,376 KB
testcase_12 AC 55 ms
4,376 KB
testcase_13 AC 17 ms
4,376 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 66 ms
4,376 KB
testcase_16 AC 13 ms
4,376 KB
testcase_17 AC 72 ms
4,376 KB
testcase_18 AC 29 ms
4,376 KB
testcase_19 AC 25 ms
4,376 KB
testcase_20 AC 54 ms
4,376 KB
testcase_21 AC 70 ms
4,376 KB
testcase_22 AC 48 ms
4,380 KB
testcase_23 AC 37 ms
4,376 KB
testcase_24 AC 15 ms
4,380 KB
testcase_25 AC 34 ms
4,376 KB
testcase_26 AC 29 ms
4,376 KB
testcase_27 AC 68 ms
4,380 KB
testcase_28 AC 17 ms
4,376 KB
testcase_29 AC 20 ms
4,376 KB
testcase_30 AC 85 ms
5,992 KB
testcase_31 AC 21 ms
4,376 KB
testcase_32 AC 9 ms
4,376 KB
testcase_33 AC 61 ms
4,380 KB
testcase_34 AC 70 ms
4,376 KB
testcase_35 AC 25 ms
4,376 KB
testcase_36 AC 69 ms
4,376 KB
testcase_37 AC 27 ms
4,376 KB
testcase_38 AC 66 ms
4,376 KB
testcase_39 AC 19 ms
4,380 KB
testcase_40 AC 86 ms
5,908 KB
testcase_41 AC 86 ms
5,948 KB
testcase_42 AC 87 ms
6,004 KB
testcase_43 AC 85 ms
6,016 KB
testcase_44 AC 86 ms
5,948 KB
testcase_45 AC 86 ms
5,940 KB
testcase_46 AC 88 ms
5,996 KB
testcase_47 AC 90 ms
5,944 KB
testcase_48 AC 87 ms
5,988 KB
testcase_49 AC 86 ms
6,000 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: struct `Fact` is never constructed
   --> Main.rs:204:8
    |
204 | struct Fact {
    |        ^^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: associated function `new` is never used
   --> Main.rs:209:8
    |
209 |     fn new(n: usize) -> Self {
    |        ^^^

warning: associated function `c` is never used
   --> Main.rs:224:8
    |
224 |     fn c(&self, n: usize, r: usize) -> Mint {
    |        ^

warning: associated function `p` is never used
   --> Main.rs:231:8
    |
231 |     fn p(&self, n: usize, r: usize) -> Mint {
    |        ^

warning: 4 warnings emitted

ソースコード

diff #

#![allow(unused_imports)]
use {std::cmp::*, std::collections::*, std::io::Write, std::ops::*};

#[allow(unused_macros)]
macro_rules !dbg {($($e :expr ) ,*) =>{#[cfg (debug_assertions ) ] $({let (e ,mut err ) =(stringify !($e ) ,std ::io ::stderr () ) ;writeln !(err ,"{} = {:?}" ,e ,$e ) .unwrap () } ) *} ;}
#[allow(unused_macros)]
macro_rules !min {($a :expr $(,) *) =>{{$a } } ;($a :expr ,$b :expr $(,) *) =>{{std ::cmp ::min ($a ,$b ) } } ;($a :expr ,$($rest :expr ) ,+$(,) *) =>{{std ::cmp ::min ($a ,min !($($rest ) ,+) ) } } ;}
#[allow(unused_macros)]
macro_rules !chmin {($base :expr ,$($cmps :expr ) ,+$(,) *) =>{{let cmp_min =min !($($cmps ) ,+) ;if $base >cmp_min {$base =cmp_min ;true } else {false } } } ;}
#[allow(unused_macros)]
macro_rules !max {($a :expr $(,) *) =>{{$a } } ;($a :expr ,$b :expr $(,) *) =>{{std ::cmp ::max ($a ,$b ) } } ;($a :expr ,$($rest :expr ) ,+$(,) *) =>{{std ::cmp ::max ($a ,max !($($rest ) ,+) ) } } ;}
#[allow(unused_macros)]
macro_rules !chmax {($base :expr ,$($cmps :expr ) ,+$(,) *) =>{{let cmp_max =max !($($cmps ) ,+) ;if $base <cmp_max {$base =cmp_max ;true } else {false } } } ;}

#[allow(dead_code)]
mod scanner {
    use std;
    use std::io::Read;
    use std::str::FromStr;
    use std::str::SplitWhitespace;
    pub struct Scanner<'a> {
        it: SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            match self.it.next().unwrap().parse::<T>() {
                Ok(v) => v,
                _ => panic!("Scanner error"),
            }
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
    pub fn read_string() -> String {
        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }
}

fn main() {
    let sc = scanner::read_string();
    let mut sc = scanner::Scanner::new(&sc);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());

    let s: usize = sc.next();
    for _ in 0..s {
        let n: usize = sc.next();
        let m = Mint(sc.next());
        let x: usize = sc.next();
        if x == 0 {
            writeln!(out, "{}", ((Mint(1) + m).pow(n) + (Mint(1) - m).pow(n)) / 2).unwrap();
        } else {
            writeln!(out, "{}", ((Mint(1) + m).pow(n) - (Mint(1) - m).pow(n)) / 2).unwrap();
        }
    }
}

const MOD: i64 = 1000000007;
#[derive(Copy, Clone)]
pub struct Mint(i64);
impl Mint {
    fn new(x: i64) -> Self {
        Mint(x.rem_euclid(MOD))
    }
    fn pow(self, n: usize) -> Self {
        match n {
            0 => Mint::new(1),
            _ => {
                let mut a = self.pow(n >> 1);
                a *= a;
                if n & 1 == 1 {
                    a *= self;
                }
                a
            }
        }
    }
    fn inv(self) -> Self {
        self.pow((MOD - 2) as usize)
    }
}
impl std::ops::Neg for Mint {
    type Output = Mint;
    fn neg(self) -> Self::Output {
        Self::new(-self.0)
    }
}
impl std::ops::AddAssign<Mint> for Mint {
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
        self.0 %= MOD;
    }
}
impl std::ops::AddAssign<i64> for Mint {
    fn add_assign(&mut self, rhs: i64) {
        *self += Mint::new(rhs);
    }
}
impl<T> std::ops::Add<T> for Mint
where
    Mint: std::ops::AddAssign<T>,
{
    type Output = Self;
    fn add(self, other: T) -> Self {
        let mut res = self;
        res += other;
        res
    }
}
impl std::ops::SubAssign<Mint> for Mint {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
        if self.0 < 0 {
            self.0 += MOD;
        }
    }
}
impl std::ops::SubAssign<i64> for Mint {
    fn sub_assign(&mut self, rhs: i64) {
        *self -= Mint::new(rhs);
    }
}
impl<T> std::ops::Sub<T> for Mint
where
    Mint: std::ops::SubAssign<T>,
{
    type Output = Self;
    fn sub(self, other: T) -> Self {
        let mut res = self;
        res -= other;
        res
    }
}
impl std::ops::MulAssign<Mint> for Mint {
    fn mul_assign(&mut self, rhs: Self) {
        self.0 *= rhs.0;
        self.0 %= MOD;
    }
}
impl std::ops::MulAssign<i64> for Mint {
    fn mul_assign(&mut self, rhs: i64) {
        *self *= Mint::new(rhs);
    }
}
impl<T> std::ops::Mul<T> for Mint
where
    Mint: std::ops::MulAssign<T>,
{
    type Output = Self;
    fn mul(self, other: T) -> Self {
        let mut res = self;
        res *= other;
        res
    }
}
impl std::ops::DivAssign<Mint> for Mint {
    fn div_assign(&mut self, rhs: Self) {
        *self *= rhs.inv();
    }
}
impl std::ops::DivAssign<i64> for Mint {
    fn div_assign(&mut self, rhs: i64) {
        *self /= Mint::new(rhs);
    }
}
impl<T> std::ops::Div<T> for Mint
where
    Mint: std::ops::DivAssign<T>,
{
    type Output = Self;
    fn div(self, other: T) -> Self {
        let mut res = self;
        res /= other;
        res
    }
}
impl std::fmt::Display for Mint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl std::ops::Deref for Mint {
    type Target = i64;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl std::ops::DerefMut for Mint {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}
struct Fact {
    fact: Vec<Mint>,
    ifact: Vec<Mint>,
}
impl Fact {
    fn new(n: usize) -> Self {
        let mut fact = vec![Mint::new(1); n + 1];
        let mut ifact = vec![Mint::new(1); n + 1];
        for i in 1..n + 1 {
            fact[i] = fact[i - 1] * i as i64;
        }
        ifact[n] = fact[n as usize].inv();
        for i in (1..n + 1).rev() {
            ifact[i - 1] = ifact[i] * i as i64;
        }
        Fact {
            fact: fact,
            ifact: ifact,
        }
    }
    fn c(&self, n: usize, r: usize) -> Mint {
        if r > n {
            Mint::new(0)
        } else {
            self.fact[n] * self.ifact[r] * self.ifact[n - r]
        }
    }
    fn p(&self, n: usize, r: usize) -> Mint {
        if r > n {
            Mint::new(0)
        } else {
            self.fact[n] * self.ifact[n - r]
        }
    }
}
0