結果
問題 | No.2949 Product on Tree |
ユーザー | Moss_Local |
提出日時 | 2024-10-26 07:02:54 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 351 ms / 2,000 ms |
コード長 | 12,258 bytes |
コンパイル時間 | 17,552 ms |
コンパイル使用メモリ | 399,472 KB |
実行使用メモリ | 50,988 KB |
最終ジャッジ日時 | 2024-10-26 07:03:30 |
合計ジャッジ時間 | 33,923 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,820 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 346 ms
34,304 KB |
testcase_04 | AC | 318 ms
33,416 KB |
testcase_05 | AC | 324 ms
34,380 KB |
testcase_06 | AC | 324 ms
34,512 KB |
testcase_07 | AC | 317 ms
33,628 KB |
testcase_08 | AC | 340 ms
34,576 KB |
testcase_09 | AC | 328 ms
34,944 KB |
testcase_10 | AC | 331 ms
35,384 KB |
testcase_11 | AC | 326 ms
36,192 KB |
testcase_12 | AC | 320 ms
38,644 KB |
testcase_13 | AC | 321 ms
38,980 KB |
testcase_14 | AC | 331 ms
40,604 KB |
testcase_15 | AC | 326 ms
43,096 KB |
testcase_16 | AC | 320 ms
41,088 KB |
testcase_17 | AC | 334 ms
42,096 KB |
testcase_18 | AC | 329 ms
42,000 KB |
testcase_19 | AC | 333 ms
44,620 KB |
testcase_20 | AC | 346 ms
43,972 KB |
testcase_21 | AC | 331 ms
44,512 KB |
testcase_22 | AC | 322 ms
45,060 KB |
testcase_23 | AC | 328 ms
35,008 KB |
testcase_24 | AC | 326 ms
35,064 KB |
testcase_25 | AC | 330 ms
34,996 KB |
testcase_26 | AC | 341 ms
34,812 KB |
testcase_27 | AC | 331 ms
35,004 KB |
testcase_28 | AC | 331 ms
35,172 KB |
testcase_29 | AC | 337 ms
35,388 KB |
testcase_30 | AC | 329 ms
36,108 KB |
testcase_31 | AC | 339 ms
37,332 KB |
testcase_32 | AC | 331 ms
37,584 KB |
testcase_33 | AC | 347 ms
41,696 KB |
testcase_34 | AC | 351 ms
46,504 KB |
testcase_35 | AC | 341 ms
43,288 KB |
testcase_36 | AC | 342 ms
48,444 KB |
testcase_37 | AC | 346 ms
48,704 KB |
testcase_38 | AC | 342 ms
45,324 KB |
testcase_39 | AC | 343 ms
46,052 KB |
testcase_40 | AC | 344 ms
50,392 KB |
testcase_41 | AC | 351 ms
47,588 KB |
testcase_42 | AC | 341 ms
50,988 KB |
testcase_43 | AC | 123 ms
28,720 KB |
testcase_44 | AC | 127 ms
29,128 KB |
testcase_45 | AC | 161 ms
36,892 KB |
testcase_46 | AC | 145 ms
33,368 KB |
testcase_47 | AC | 110 ms
24,808 KB |
testcase_48 | AC | 149 ms
34,252 KB |
コンパイルメッセージ
warning: unnecessary parentheses around assigned value --> src/main.rs:266:19 | 266 | self.0 *= (rhs.0 % MOD); | ^ ^ | = note: `#[warn(unused_parens)]` on by default help: remove these parentheses | 266 - self.0 *= (rhs.0 % MOD); 266 + self.0 *= rhs.0 % MOD; | warning: variable does not need to be mutable --> src/main.rs:119:9 | 119 | let mut vec: Vec<i64> = read_vec(); | ----^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default warning: variable does not need to be mutable --> src/main.rs:125:9 | 125 | let mut vec: Vec<i64> = read_vec(); | ----^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/main.rs:130:9 | 130 | let mut vec: Vec<usize> = read_vec(); | ----^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/main.rs:136:9 | 136 | let mut vec: Vec<f64> = read_vec(); | ----^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/main.rs:141:9 | 141 | let mut vec: Vec<char> = read_vec(); | ----^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/main.rs:146:9 | 146 | let mut vec: Vec<usize> = read_vec(); | ----^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/main.rs:151:9 | 151 | let mut vec: Vec<i64> = read_vec(); | ----^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/main.rs:157:9 | 157 | let mut vec: Vec<usize> = read_vec(); | ----^^^ | | | help: remove this `mut`
ソースコード
// -*- coding:utf-8-unix -*- // #![feature(map_first_last)] #![allow(dead_code)] #![allow(unused_imports)] #![allow(unused_macros)] // use core::num; use std::cmp::*; use std::fmt::*; use std::hash::*; use std::io::BufRead; use std::iter::FromIterator; use std::*; use std::{cmp, collections, fmt, io, iter, ops, str}; const INF: i64 = 1223372036854775807; const UINF: usize = INF as usize; const LINF: i64 = 2147483647; const INF128: i128 = 1223372036854775807000000000000; const MOD1: i64 = 1000000007; const MOD9: i64 = 998244353; const MOD: i64 = MOD9; // const MOD: i64 = MOD2; const UMOD: usize = MOD as usize; const M_PI: f64 = 3.14159265358979323846; // use proconio::input; // const MOD: i64 = INF; use cmp::Ordering::*; use std::collections::*; use std::io::stdin; use std::io::stdout; use std::io::Write; macro_rules! p { ($x:expr) => { //if expr println!("{}", $x); }; } macro_rules! vp { // vector print separate with space ($x:expr) => { println!( "{}", $x.iter() .map(|x| x.to_string()) .collect::<Vec<_>>() .join(" ") ); }; } macro_rules! d { ($x:expr) => { eprintln!("{:?}", $x); }; } macro_rules! yn { ($val:expr) => { if $val { println!("Yes"); } else { println!("No"); } }; } macro_rules! map{ // declear btreemap ($($key:expr => $val:expr),*) => { { let mut map = ::std::collections::BTreeMap::new(); $( map.insert($key, $val); )* map } }; } macro_rules! set{ // declear btreemap ($($key:expr),*) => { { let mut set = ::std::collections::BTreeSet::new(); $( set.insert($key); )* set } }; } //input output #[allow(dead_code)] 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() } #[allow(dead_code)] fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } #[allow(dead_code)] fn read_mat<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> { (0..n).map(|_| read_vec()).collect() } #[allow(dead_code)] fn readii() -> (i64, i64) { let mut vec: Vec<i64> = read_vec(); (vec[0], vec[1]) } #[allow(dead_code)] fn readiii() -> (i64, i64, i64) { let mut vec: Vec<i64> = read_vec(); (vec[0], vec[1], vec[2]) } #[allow(dead_code)] fn readuu() -> (usize, usize) { let mut vec: Vec<usize> = read_vec(); (vec[0], vec[1]) } #[allow(dead_code)] fn readff() -> (f64, f64) { let mut vec: Vec<f64> = read_vec(); (vec[0], vec[1]) } fn readcc() -> (char, char) { let mut vec: Vec<char> = read_vec(); (vec[0], vec[1]) } fn readuuu() -> (usize, usize, usize) { let mut vec: Vec<usize> = read_vec(); (vec[0], vec[1], vec[2]) } #[allow(dead_code)] fn readiiii() -> (i64, i64, i64, i64) { let mut vec: Vec<i64> = read_vec(); (vec[0], vec[1], vec[2], vec[3]) } #[allow(dead_code)] fn readuuuu() -> (usize, usize, usize, usize) { let mut vec: Vec<usize> = read_vec(); (vec[0], vec[1], vec[2], vec[3]) } use std::collections::VecDeque; macro_rules! M { (a :expr ) => { M::new({ a }) }; } #[derive(Copy, Clone, Debug)] pub struct M(i64); impl M { fn new(x: i64) -> Self { M(x.rem_euclid(MOD)) } fn pow(self, n: usize) -> Self { match n { 0 => M::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 M { type Output = M; fn neg(self) -> Self::Output { Self::new(-self.0) } } impl std::ops::AddAssign<M> for M { fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0; self.0 %= MOD; } } impl std::ops::AddAssign<i64> for M { fn add_assign(&mut self, rhs: i64) { *self += M::new(rhs); } } impl std::ops::AddAssign<usize> for M { fn add_assign(&mut self, rhs: usize) { *self += M::new(rhs as i64); } } impl<T> std::ops::Add<T> for M where M: std::ops::AddAssign<T>, { type Output = Self; fn add(self, other: T) -> Self { let mut res = self; res += other; res } } impl std::ops::SubAssign<M> for M { fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0; if self.0 < 0 { self.0 %= MOD; self.0 += MOD; } } } impl std::ops::SubAssign<i64> for M { fn sub_assign(&mut self, rhs: i64) { *self -= M::new(rhs); if (*self).0 < 0 { self.0 %= MOD; self.0 += MOD; } } } impl std::ops::SubAssign<usize> for M { fn sub_assign(&mut self, rhs: usize) { *self -= M::new(rhs as i64); if (*self).0 < 0 { self.0 %= MOD; self.0 += MOD; } } } impl<T> std::ops::Sub<T> for M where M: std::ops::SubAssign<T>, { type Output = Self; fn sub(self, other: T) -> Self { let mut res = self; res -= other; res } } impl std::ops::MulAssign<M> for M { fn mul_assign(&mut self, rhs: Self) { self.0 %= MOD; self.0 *= (rhs.0 % MOD); self.0 %= MOD; } } impl std::ops::MulAssign<i64> for M { fn mul_assign(&mut self, rhs: i64) { *self *= M::new(rhs); } } impl std::ops::MulAssign<usize> for M { fn mul_assign(&mut self, rhs: usize) { *self *= M::new(rhs as i64); } } impl<T> std::ops::Mul<T> for M where M: std::ops::MulAssign<T>, { type Output = Self; fn mul(self, other: T) -> Self { let mut res = self; res *= other; res } } impl std::ops::DivAssign<M> for M { fn div_assign(&mut self, rhs: Self) { *self *= rhs.inv(); } } impl std::ops::DivAssign<i64> for M { fn div_assign(&mut self, rhs: i64) { *self /= M::new(rhs); } } impl std::ops::DivAssign<usize> for M { fn div_assign(&mut self, rhs: usize) { *self /= M::new(rhs as i64); } } impl<T> std::ops::Div<T> for M where M: 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 M { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl std::ops::Deref for M { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 } } impl std::ops::DerefMut for M { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } #[allow(dead_code)] pub fn gcd(a: usize, b: usize) -> usize { if b == 0 { a } else { gcd(b, a % b) } } #[allow(dead_code)] pub fn lcm(a: usize, b: usize) -> usize { a / gcd(a, b) * b } #[allow(dead_code)] /// (gcd, x, y) pub fn extgcd(a: i64, b: i64) -> (i64, i64, i64) { if b == 0 { (a, 1, 0) } else { let (gcd, x, y) = extgcd(b, a % b); (gcd, y, x - (a / b) * y) } } #[allow(dead_code)] /// x ^ n % m pub fn mod_pow(x: usize, n: usize, m: usize) -> usize { let mut res = 1; let mut x = x % m; let mut n = n; while n > 0 { if n & 1 == 1 { res = (res * x) % m; } x = (x * x) % m; n >>= 1; } res } pub struct Combination { m: usize, f_table: Vec<usize>, moi: Vec<usize>, } impl Combination { // 0 <= size <= 10^8 is constrained. pub fn new(mod_num: usize, table_size: usize) -> Self { Self { m: mod_num, f_table: vec![0; table_size], moi: vec![0; 0], } } pub fn build(&mut self) { let size = self.f_table.len(); self.f_table = self.fact_table(size, self.m); self.moi = self.fact_inv_table(size, self.m); } fn fact_table(&mut self, len: usize, m: usize) -> Vec<usize> { let mut res = vec![1; len + 1]; for i in 2..len + 1 { res[i] = (res[i - 1] * i) % m; } res } fn fact_inv_table(&mut self, len: usize, m: usize) -> Vec<usize> { let mut res = vec![1; len + 1]; let mut inv = vec![1; len + 1]; //inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD for i in 2..len { inv[i] = (m - inv[m % i] * (m / i) % m) % m; res[i] = inv[i] * res[i - 1]; // res[i] = self.mod_inverse(i, m) * res[i - 1]; // res[i] = 1; res[i] %= m; } res } pub fn p(&mut self, n: usize, k: usize) -> i64 { let p = MOD as usize; if k == 0 { return 1; } if n < k { 0 } else { let (a1, e1) = self.mod_fact(n, p); let (a2, e2) = self.mod_fact(k, p); let (a3, e3) = self.mod_fact(n - k, p); if e1 > e2 + e3 { 0 } else { let moi = self.mod_inverse(a3 % p, p); (a1 * self.mod_inverse(a3 % p, p) % p) as i64 } } } pub fn c(&mut self, n: usize, k: usize) -> i64 { let p = MOD as usize; if n == 0 && k == 0 { return 1; } if n == 0 { return 0; } if k == 0 { return 1; } if n < k { 0 } else { let (a1, e1) = self.mod_fact(n, p); let (a2, e2) = self.mod_fact(k, p); let (a3, e3) = self.mod_fact(n - k, p); if e1 > e2 + e3 { 0 } else { (((a1 * &self.moi[k]) % p * &self.moi[n - k]) % p) as i64 } } } pub fn h(&mut self, n: usize, k: usize) -> i64 { return self.c(n + k - 1, k); } pub fn factorial(&mut self, n: usize) -> i64 { return self.p(n, n); } fn extgcd(&mut self, a: i64, b: i64) -> (i64, i64, i64) { if b == 0 { (a, 1, 0) } else { let (gcd, x, y) = extgcd(b, a % b); (gcd, y, x - (a / b) * y) } } fn mod_inverse(&mut self, a: usize, m: usize) -> usize { let (_, x, _) = self.extgcd(a as i64, m as i64); ((m as i64 + x) as usize % m) % m } fn mod_fact(&mut self, n: usize, p: usize) -> (usize, usize) { if n == 0 { (1, 0) } else { let (a, b) = self.mod_fact(n / p, p); let pow = b + n / p; if n / p % 2 != 0 { (a * (p - self.f_table[(n % p) as usize]) % p, pow) } else { (a * self.f_table[(n % p) as usize] % p, pow) } } } } fn dfs(v: usize, p: usize, graph: &Vec<Vec<(usize, usize)>>, dp: &mut Vec<Vec<M>>, a: &Vec<usize>) { let mut mul = M(1); let mut sum = M(0); let mut sum_pow2 = M(0); let mut have_child = false; for &(u, w) in &graph[v] { if u == p { continue; } dfs(u, v, graph, dp, a); mul *= dp[u][0]; sum += dp[u][0]; sum_pow2 += dp[u][0] * dp[u][0]; have_child = true; } if !have_child { mul = M(0); } dp[v][0] = M(a[v] as i64) * (M(1) + sum); dp[v][1] = M(a[v] as i64) * (sum + (sum * sum - sum_pow2) / M(2)); } fn main() { let n: usize = read(); let mut a: Vec<usize> = read_vec(); let mut graph = vec![vec![(0 as usize, 0 as usize); (0) as usize]; (n) as usize]; for i in 0..n - 1 { let (mut a, mut b) = readuu(); a -= 1; b -= 1; graph[a].push((b, 1)); graph[b].push((a, 1)); } let mut dp = vec![vec![M(0), M(0)]; n]; dfs(0, n, &graph, &mut dp, &a); let mut res = M(0); for i in 0..n { res += dp[i][1]; } // d!(dp); println!("{}", res); }