結果

問題 No.1259 スイッチ
ユーザー fukafukatanifukafukatani
提出日時 2020-10-17 15:48:21
言語 Rust
(1.77.0)
結果
AC  
実行時間 277 ms / 2,000 ms
コード長 6,105 bytes
コンパイル時間 1,643 ms
コンパイル使用メモリ 147,932 KB
実行使用メモリ 17,432 KB
最終ジャッジ日時 2023-09-28 08:21:25
合計ジャッジ時間 12,859 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 0 ms
4,376 KB
testcase_02 AC 0 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 0 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 169 ms
11,232 KB
testcase_11 AC 241 ms
16,296 KB
testcase_12 AC 145 ms
10,628 KB
testcase_13 AC 88 ms
7,116 KB
testcase_14 AC 122 ms
9,456 KB
testcase_15 AC 82 ms
6,888 KB
testcase_16 AC 193 ms
14,040 KB
testcase_17 AC 115 ms
8,988 KB
testcase_18 AC 221 ms
15,636 KB
testcase_19 AC 221 ms
15,584 KB
testcase_20 AC 155 ms
11,292 KB
testcase_21 AC 243 ms
16,880 KB
testcase_22 AC 72 ms
6,264 KB
testcase_23 AC 211 ms
15,308 KB
testcase_24 AC 162 ms
11,944 KB
testcase_25 AC 157 ms
11,748 KB
testcase_26 AC 242 ms
17,008 KB
testcase_27 AC 196 ms
14,332 KB
testcase_28 AC 159 ms
11,716 KB
testcase_29 AC 201 ms
14,076 KB
testcase_30 AC 122 ms
9,440 KB
testcase_31 AC 185 ms
13,020 KB
testcase_32 AC 79 ms
6,660 KB
testcase_33 AC 251 ms
17,104 KB
testcase_34 AC 132 ms
9,904 KB
testcase_35 AC 192 ms
13,524 KB
testcase_36 AC 174 ms
12,512 KB
testcase_37 AC 170 ms
11,932 KB
testcase_38 AC 149 ms
10,964 KB
testcase_39 AC 234 ms
16,372 KB
testcase_40 AC 236 ms
16,564 KB
testcase_41 AC 196 ms
14,080 KB
testcase_42 AC 242 ms
16,860 KB
testcase_43 AC 151 ms
10,756 KB
testcase_44 AC 238 ms
16,100 KB
testcase_45 AC 277 ms
17,432 KB
testcase_46 AC 141 ms
10,248 KB
testcase_47 AC 253 ms
17,352 KB
testcase_48 AC 124 ms
9,132 KB
testcase_49 AC 244 ms
16,896 KB
testcase_50 AC 136 ms
10,244 KB
testcase_51 AC 76 ms
6,348 KB
testcase_52 AC 231 ms
16,364 KB
testcase_53 AC 113 ms
8,108 KB
testcase_54 AC 257 ms
16,368 KB
testcase_55 AC 80 ms
6,132 KB
testcase_56 AC 124 ms
9,168 KB
testcase_57 AC 218 ms
15,124 KB
testcase_58 AC 149 ms
11,036 KB
testcase_59 AC 182 ms
13,016 KB
testcase_60 AC 252 ms
17,352 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: associated function `set_modulus` is never used
  --> Main.rs:56:8
   |
56 |     fn set_modulus(m: i64) {
   |        ^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

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

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<i64>();
    let (n, k, m) = (v[0], v[1], v[2] as usize);
    let mut fact = vec![Modulo(1); n as usize + 1];
    for i in 1..n {
        fact[i as usize] = fact[i as usize - 1] * i;
    }
    let fact_inv = fact.iter().map(|&x| x.inv()).collect::<Vec<_>>();

    let mut ans = Modulo(0i64);
    for yaku in 1..n + 1 {
        if k % yaku == 0 {
            ans += fact[n as usize - 1] * fact_inv[(n - yaku) as usize] * Modulo(n).pow(n - yaku);
        }
    }
    if m != 1 {
        ans = (Modulo(n).pow(n) - ans) * Modulo(n - 1).inv();
    }
    println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct Modulo(i64);
static mut MODULUS: i64 = 1000_000_000 + 7;
impl Modulo {
    fn set_modulus(m: i64) {
        unsafe {
            MODULUS = m;
        }
    }
    fn get_modulus() -> i64 {
        unsafe { MODULUS }
    }
    fn new(x: i64) -> Modulo {
        let m = Modulo::get_modulus();
        if x < 0 {
            Modulo(x % m + m)
        } else if x < m {
            Modulo(x)
        } else {
            Modulo(x % m)
        }
    }
    fn pow(self, p: i64) -> Modulo {
        if p == 0 {
            Modulo(1)
        } else {
            let mut t = self.pow(p / 2);
            t *= t;
            if p & 1 == 1 {
                t *= self;
            }
            t
        }
    }
    fn inv(self) -> Modulo {
        self.pow(Modulo::get_modulus() - 2)
    }
}
impl std::fmt::Display for Modulo {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.0.fmt(f)
    }
}
impl std::ops::AddAssign for Modulo {
    fn add_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 += other.0;
        if self.0 >= m {
            self.0 -= m;
        }
    }
}
impl std::ops::MulAssign for Modulo {
    fn mul_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 *= other.0;
        self.0 %= m;
    }
}
impl std::ops::SubAssign for Modulo {
    fn sub_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 += m - other.0;
        if self.0 >= m {
            self.0 -= m;
        }
    }
}
macro_rules! impl_modulo_ops {
    ($imp:ident, $method:ident, $assign_imp:ident, $assign_method:ident) => {
        impl<'a> std::ops::$assign_imp<&'a Modulo> for Modulo {
            fn $assign_method(&mut self, other: &'a Modulo) {
                std::ops::$assign_imp::$assign_method(self, *other);
            }
        }
        impl std::ops::$imp for Modulo {
            type Output = Modulo;
            fn $method(self, other: Modulo) -> Modulo {
                let mut x = self;
                std::ops::$assign_imp::$assign_method(&mut x, other);
                x
            }
        }
        impl<'a> std::ops::$imp<Modulo> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: Modulo) -> Modulo {
                std::ops::$imp::$method(*self, other)
            }
        }
        impl<'a> std::ops::$imp<&'a Modulo> for Modulo {
            type Output = Modulo;
            fn $method(self, other: &'a Modulo) -> Modulo {
                std::ops::$imp::$method(self, *other)
            }
        }
        impl<'a, 'b> std::ops::$imp<&'b Modulo> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: &'b Modulo) -> Modulo {
                std::ops::$imp::$method(*self, *other)
            }
        }
        impl std::ops::$assign_imp<i64> for Modulo {
            fn $assign_method(&mut self, other: i64) {
                std::ops::$assign_imp::$assign_method(self, Modulo::new(other));
            }
        }
        impl<'a> std::ops::$assign_imp<&'a i64> for Modulo {
            fn $assign_method(&mut self, other: &'a i64) {
                std::ops::$assign_imp::$assign_method(self, *other);
            }
        }
        impl std::ops::$imp<i64> for Modulo {
            type Output = Modulo;
            fn $method(self, other: i64) -> Modulo {
                let mut x = self;
                std::ops::$assign_imp::$assign_method(&mut x, other);
                x
            }
        }
        impl<'a> std::ops::$imp<&'a i64> for Modulo {
            type Output = Modulo;
            fn $method(self, other: &'a i64) -> Modulo {
                std::ops::$imp::$method(self, *other)
            }
        }
        impl<'a> std::ops::$imp<i64> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: i64) -> Modulo {
                std::ops::$imp::$method(*self, other)
            }
        }
        impl<'a, 'b> std::ops::$imp<&'b i64> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: &'b i64) -> Modulo {
                std::ops::$imp::$method(*self, *other)
            }
        }
    };
}
impl_modulo_ops!(Add, add, AddAssign, add_assign);
impl_modulo_ops!(Mul, mul, MulAssign, mul_assign);
impl_modulo_ops!(Sub, sub, SubAssign, sub_assign);

use std::iter::Sum;
impl Sum for Modulo {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = Modulo>,
    {
        iter.fold(Modulo(0), |a, b| a + b)
    }
}

impl<'a> Sum<&'a Modulo> for Modulo {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = &'a Self>,
    {
        iter.fold(Modulo(0), |a, b| a + b)
    }
}
0