General

# Author Problem Lang Verdict Time Memory Sent Judged
120564271 Contestant:
sansen
1540B - 19 Rust Accepted 249 ms 832 KB 2021-06-25 19:05:49 2021-06-25 21:01:37

→ Source
// ---------- begin ModInt ----------
// モンゴメリ乗算を用いる
// ほぼCodeforces用
// 注意
// new_unchecked は値xが 0 <= x < modulo であることを仮定
// ModInt の中身は正規化された値で持ってるので直接読んだり書いたりするとぶっ壊れる
// 奇素数のみ
mod modint {

use std::marker::*;
use std::ops::*;

pub trait Modulo {
fn modulo() -> u32;
fn rem() -> u32;
fn ini() -> u64;
fn reduce(x: u64) -> u32 {
assert!(x < (Self::modulo() as u64) << 32);
let b = (x as u32 * Self::rem()) as u64;
let t = x + b * Self::modulo() as u64;
let mut c = (t >> 32) as u32;
if c >= Self::modulo() {
c -= Self::modulo();
}
c as u32
}
}

pub enum Mod1_000_000_007 {}

impl Modulo for Mod1_000_000_007 {
fn modulo() -> u32 {
1_000_000_007
}
fn rem() -> u32 {
2226617417
}
fn ini() -> u64 {
582344008
}
}

pub enum Mod998_244_353 {}

impl Modulo for Mod998_244_353 {
fn modulo() -> u32 {
998_244_353
}
fn rem() -> u32 {
998244351
}
fn ini() -> u64 {
932051910
}
}

pub fn generate_umekomi_modulo(p: u32) {
assert!(p < (1 << 31) && p > 2 && p & 1 == 1 && (2u32..).take_while(|v| v * v <= p).all(|k| p % k != 0));
let mut t = 1u32;
let mut s = !p + 1;
let mut n = !0u32 >> 2;
while n > 0 {
if n & 1 == 1 {
t *= s;
}
s *= s;
n >>= 1;
}
let mut ini = (1u64 << 32) % p as u64;
ini = (ini << 32) % p as u64;
assert!(t * p == !0);
println!("pub enum Mod{} {{}}", p);
println!("impl Modulo for Mod{} {{", p);
println!("    fn modulo() -> u32 {{");
println!("        {}", p);
println!("    }}");
println!("    fn rem() -> u32 {{");
println!("        {}", t);
println!("    }}");
println!("    fn ini() -> u32 {{");
println!("        {}", ini);
println!("    }}");
println!("}}");
let mut f = vec![];
let mut n = p - 1;
for i in 2.. {
if i * i > n {
break;
}
if n % i == 0 {
f.push(i);
while n % i == 0 {
n /= i;
}
}
}
if n > 1 {
f.push(n);
}
let mut order = 1;
let mut n = p - 1;
while n % 2 == 0 {
n /= 2;
order <<= 1;
}
let z = (2u64..).find(|z| {
f.iter().all(|f| mod_pow(*z, ((p - 1) / *f) as u64, p as u64) != 1)
}).unwrap();
let zeta = mod_pow(z, ((p - 1) / order) as u64, p as u64);
println!("impl transform::NTTFriendly for Mod{} {{", p);
println!("    fn order() -> usize {{");
println!("        {}", order);
println!("    }}");
println!("    fn zeta() -> u32 {{");
println!("        {}", zeta);
println!("    }}");
println!("}}");
}

pub struct ModInt<T>(u32, PhantomData<T>);

impl<T> Clone for ModInt<T> {
fn clone(&self) -> Self {
ModInt::build(self.0)
}
}

impl<T> Copy for ModInt<T> {}

impl<T: Modulo> Add for ModInt<T> {
type Output = ModInt<T>;
fn add(self, rhs: Self) -> Self::Output {
let mut d = self.0 + rhs.0;
if d >= T::modulo() {
d -= T::modulo();
}
Self::build(d)
}
}

impl<T: Modulo> AddAssign for ModInt<T> {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}

impl<T: Modulo> Sub for ModInt<T> {
type Output = ModInt<T>;
fn sub(self, rhs: Self) -> Self::Output {
let mut d = T::modulo() + self.0 - rhs.0;
if d >= T::modulo() {
d -= T::modulo();
}
Self::build(d)
}
}

impl<T: Modulo> SubAssign for ModInt<T> {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}

impl<T: Modulo> Mul for ModInt<T> {
type Output = ModInt<T>;
fn mul(self, rhs: Self) -> Self::Output {
Self::build(T::reduce(self.0 as u64 * rhs.0 as u64))
}
}

impl<T: Modulo> MulAssign for ModInt<T> {
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}

impl<T: Modulo> Neg for ModInt<T> {
type Output = ModInt<T>;
fn neg(self) -> Self::Output {
if self.0 == 0 {
Self::zero()
} else {
Self::build(T::modulo() - self.0)
}
}
}

impl<T: Modulo> std::fmt::Display for ModInt<T> {
fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
write!(f, "{}", self.get())
}
}

impl<T: Modulo> std::fmt::Debug for ModInt<T> {
fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
write!(f, "{}", self.get())
}
}

impl<T: Modulo> std::str::FromStr for ModInt<T> {
type Err = std::num::ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val = s.parse::<u32>()?;
Ok(ModInt::new(val))
}
}

impl<T: Modulo> From<usize> for ModInt<T> {
fn from(val: usize) -> ModInt<T> {
ModInt::new_unchecked((val % T::modulo() as usize) as u32)
}
}

impl<T: Modulo> From<u64> for ModInt<T> {
fn from(val: u64) -> ModInt<T> {
ModInt::new_unchecked((val % T::modulo() as u64) as u32)
}
}

impl<T: Modulo> From<i64> for ModInt<T> {
fn from(val: i64) -> ModInt<T> {
let m = T::modulo() as i64;
ModInt::new((val % m + m) as u32)
}
}

impl<T> ModInt<T> {
fn build(d: u32) -> Self {
ModInt(d, PhantomData)
}
pub fn zero() -> Self {
Self::build(0)
}
pub fn is_zero(&self) -> bool {
self.0 == 0
}
}

impl<T: Modulo> ModInt<T> {
pub fn new_unchecked(d: u32) -> Self {
Self::build(T::reduce(d as u64 * T::ini()))
}
pub fn new(d: u32) -> Self {
Self::new_unchecked(d % T::modulo())
}
pub fn one() -> Self {
Self::new_unchecked(1)
}
pub fn get(&self) -> u32 {
T::reduce(self.0 as u64)
}
pub fn pow(&self, mut n: u64) -> Self {
let mut t = Self::one();
let mut s = *self;
while n > 0 {
if n & 1 == 1 {
t *= s;
}
s *= s;
n >>= 1;
}
t
}
pub fn inv(&self) -> Self {
assert!(!self.is_zero());
self.pow((T::modulo() - 2) as u64)
}
}

pub fn mod_pow(mut r: u64, mut n: u64, m: u64) -> u64 {
let mut t = 1 % m;
while n > 0 {
if n & 1 == 1 {
t = t * r % m;
}
r = r * r % m;
n >>= 1;
}
t
}
}
// ---------- end ModInt ----------
// ---------- begin Precalc ----------
mod precalc {
use super::modint::*;
pub struct Precalc<T> {
inv: Vec<ModInt<T>>,
fact: Vec<ModInt<T>>,
ifact: Vec<ModInt<T>>,
}
impl<T: Modulo> Precalc<T> {
pub fn new(n: usize) -> Precalc<T> {
let mut inv = vec![ModInt::one(); n + 1];
let mut fact = vec![ModInt::one(); n + 1];
let mut ifact = vec![ModInt::one(); n + 1];
for i in 2..(n + 1) {
fact[i] = fact[i - 1] * ModInt::new_unchecked(i as u32);
}
ifact[n] = fact[n].inv();
if n > 0 {
inv[n] = ifact[n] * fact[n - 1];
}
for i in (1..n).rev() {
ifact[i] = ifact[i + 1] * ModInt::new_unchecked((i + 1) as u32);
inv[i] = ifact[i] * fact[i - 1];
}
Precalc {
inv: inv,
fact: fact,
ifact: ifact,
}
}
pub fn inv(&self, n: usize) -> ModInt<T> {
assert!(n > 0);
self.inv[n]
}
pub fn fact(&self, n: usize) -> ModInt<T> {
self.fact[n]
}
pub fn ifact(&self, n: usize) -> ModInt<T> {
self.ifact[n]
}
pub fn perm(&self, n: usize, k: usize) -> ModInt<T> {
if k > n {
return ModInt::zero();
}
self.fact[n] * self.ifact[n - k]
}
pub fn comb(&self, n: usize, k: usize) -> ModInt<T> {
if k > n {
return ModInt::zero();
}
self.fact[n] * self.ifact[k] * self.ifact[n - k]
}
}
}
// ---------- end Precalc ----------

use modint::*;

type M = ModInt<Mod1_000_000_007>;

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
(source = $s:expr,$($r:tt)*) => { let mut iter =$s.split_whitespace();
input_inner!{iter, $($r)*}
};
($($r:tt)*) => {
let s = {
let mut s = String::new();
s
};
let mut iter = s.split_whitespace();
input_inner!{iter, $($r)*}
};
}

macro_rules! input_inner {
($iter:expr) => {}; ($iter:expr, ) => {};
($iter:expr,$var:ident : $t:tt$($r:tt)*) => { let$var = read_value!($iter,$t);
input_inner!{$iter$($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => {
( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ;$len:expr ]) => {
(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>() }; ($iter:expr, chars) => {
read_value!($iter, String).chars().collect::<Vec<char>>() }; ($iter:expr, bytes) => {
read_value!($iter, String).bytes().collect::<Vec<u8>>() }; ($iter:expr, usize1) => {
read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => {$iter.next().unwrap().parse::<\$t>().expect("Parse error")
};
}
// ---------- end input macro ----------

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

// 期待値
// 2頂点について確率を求めればいい
// a, bについて
// パスを考えるとab間の部分以外が根に選ばれたら結果は定まる
// そうでない時
// 左が選ばれる確率見たいなのがDPで計算できる

fn run() {
input! {
n: usize,
e: [(usize1, usize1); n - 1],
}
let mut g = vec![vec![]; n];
for (a, b) in e {
g[a].push(b);
g[b].push(a);
}
let dfs = |root: usize| -> (Vec<usize>, Vec<usize>) {
let mut parent = vec![n; n];
let mut topo = vec![root];
for i in 0..n {
let v = topo[i];
for &u in g[v].iter() {
if u != parent[v] {
parent[u] = v;
topo.push(u);
}
}
}
let mut size = vec![1; n];
for &v in topo.iter().rev() {
for &u in g[v].iter() {
if u != parent[v] {
size[v] += size[u];
}
}
}
(parent, size)
};
// dp[l][r]: l が選ばれる確率
let mut dp = vec![vec![M::zero(); n + 1]; n + 1];
for i in 1..=n {
dp[0][i] = M::one();
dp[i][0] = M::zero();
}
let pc = precalc::Precalc::new(200usize.pow(2));
for l in 1..=n {
for r in 1..=n {
dp[l][r] = (dp[l - 1][r] + dp[l][r - 1]) * pc.inv(2);
}
}
let mut ans = M::zero();
for root in 0..n {
let (parent, _) = dfs(root);
for i in (root + 1)..n {
let mut v = vec![];
let mut index = vec![None; n];
let mut pos = i;
while pos != root {
index[pos] = Some(v.len());
v.push(pos);
pos = parent[pos];
}
index[pos] = Some(v.len());
v.push(pos);
//            println!("{} -> {}: {:?}", root, i, v);
for j in 0..n {
let mut pos = j;
while index[pos].is_none() {
pos = parent[pos];
}
let x = index[pos].unwrap();
ans += dp[x][v.len() - x - 1];
}
}
}
ans *= pc.inv(n);
println!("{}", ans);
}

fn main() {
run();
}


?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
?
?
?