Skip to content

Commit

Permalink
Add codeforces/539/1d.rs codeforces/539/remain.txt codeforces/539/url…
Browse files Browse the repository at this point in the history
….txt
  • Loading branch information
koba-e964 committed Sep 7, 2023
1 parent ffe30ac commit f4033db
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
153 changes: 153 additions & 0 deletions codeforces/539/1d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
use std::io::Read;

fn get_word() -> String {
let stdin = std::io::stdin();
let mut stdin=stdin.lock();
let mut u8b: [u8; 1] = [0];
loop {
let mut buf: Vec<u8> = Vec::with_capacity(16);
loop {
let res = stdin.read(&mut u8b);
if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
break;
} else {
buf.push(u8b[0]);
}
}
if buf.len() >= 1 {
let ret = String::from_utf8(buf).unwrap();
return ret;
}
}
}

fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

/// Verified by https://atcoder.jp/contests/abc198/submissions/21774342
mod mod_int {
use std::ops::*;
pub trait Mod: Copy { fn m() -> i64; }
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ModInt<M> { pub x: i64, phantom: ::std::marker::PhantomData<M> }
impl<M: Mod> ModInt<M> {
// x >= 0
pub fn new(x: i64) -> Self { ModInt::new_internal(x % M::m()) }
fn new_internal(x: i64) -> Self {
ModInt { x: x, phantom: ::std::marker::PhantomData }
}
pub fn pow(self, mut e: i64) -> Self {
debug_assert!(e >= 0);
let mut sum = ModInt::new_internal(1);
let mut cur = self;
while e > 0 {
if e % 2 != 0 { sum *= cur; }
cur *= cur;
e /= 2;
}
sum
}
#[allow(dead_code)]
pub fn inv(self) -> Self { self.pow(M::m() - 2) }
}
impl<M: Mod> Default for ModInt<M> {
fn default() -> Self { Self::new_internal(0) }
}
impl<M: Mod, T: Into<ModInt<M>>> Add<T> for ModInt<M> {
type Output = Self;
fn add(self, other: T) -> Self {
let other = other.into();
let mut sum = self.x + other.x;
if sum >= M::m() { sum -= M::m(); }
ModInt::new_internal(sum)
}
}
impl<M: Mod, T: Into<ModInt<M>>> Sub<T> for ModInt<M> {
type Output = Self;
fn sub(self, other: T) -> Self {
let other = other.into();
let mut sum = self.x - other.x;
if sum < 0 { sum += M::m(); }
ModInt::new_internal(sum)
}
}
impl<M: Mod, T: Into<ModInt<M>>> Mul<T> for ModInt<M> {
type Output = Self;
fn mul(self, other: T) -> Self { ModInt::new(self.x * other.into().x % M::m()) }
}
impl<M: Mod, T: Into<ModInt<M>>> AddAssign<T> for ModInt<M> {
fn add_assign(&mut self, other: T) { *self = *self + other; }
}
impl<M: Mod, T: Into<ModInt<M>>> SubAssign<T> for ModInt<M> {
fn sub_assign(&mut self, other: T) { *self = *self - other; }
}
impl<M: Mod, T: Into<ModInt<M>>> MulAssign<T> for ModInt<M> {
fn mul_assign(&mut self, other: T) { *self = *self * other; }
}
impl<M: Mod> Neg for ModInt<M> {
type Output = Self;
fn neg(self) -> Self { ModInt::new(0) - self }
}
impl<M> ::std::fmt::Display for ModInt<M> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
self.x.fmt(f)
}
}
impl<M: Mod> From<i64> for ModInt<M> {
fn from(x: i64) -> Self { Self::new(x) }
}
} // mod mod_int

macro_rules! define_mod {
($struct_name: ident, $modulo: expr) => {
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $struct_name {}
impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } }
}
}
const MOD: i64 = 1_000_000_007;
define_mod!(P, MOD);
type MInt = mod_int::ModInt<P>;

// Depends on MInt.rs
fn fact_init(w: usize) -> (Vec<MInt>, Vec<MInt>) {
let mut fac = vec![MInt::new(1); w];
let mut invfac = vec![0.into(); w];
for i in 1..w {
fac[i] = fac[i - 1] * i as i64;
}
invfac[w - 1] = fac[w - 1].inv();
for i in (0..w - 1).rev() {
invfac[i] = invfac[i + 1] * (i as i64 + 1);
}
(fac, invfac)
}

// https://codeforces.com/contest/1109/problem/D
// Tags: pruefer-code
fn main() {
let n: usize = get();
let m: usize = get();
let _a: usize = get();
let _b: usize = get();
let mm = MInt::new(m as i64);
let (fac, invfac) = fact_init(n + m + 1);
let mut tot = MInt::new(0);

for d in 1..std::cmp::min(n - 1, m) + 1 {
// rest: weights
let mut tmp = mm.pow((n - 1 - d) as i64);
// a-b: vertices
tmp *= fac[n - 2] * invfac[n - 2 - (d - 1)];
// a-b: weights
tmp *= fac[m - 1] * invfac[m - d] * invfac[d - 1];
// rest: (n - 1 - d) vertices
// pruefer code: length = n-2-d, if it contains 1 (e-1) times then degree is e
// \sum_{e >= 1} (d+1)^e (n - d - 1)^{n - d - 2 - e} C(n - d - 2, e - 1)
// = (d + 1)n^{n-d-2}
if n >= d + 2 {
tmp *= MInt::new(d as i64 + 1) * MInt::new(n as i64).pow((n - 2 - d) as i64);
}
tot += tmp;
}
println!("{}", tot);
}
6 changes: 6 additions & 0 deletions codeforces/539/remain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2a
2b
1a
1b
1c
1e
2 changes: 2 additions & 0 deletions codeforces/539/url.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://codeforces.com/contest/1109 (Div. 1)
https://codeforces.com/contest/1113 (Div. 2)

0 comments on commit f4033db

Please sign in to comment.