結果
問題 | No.1502 Many Simple Additions |
ユーザー | koba-e964 |
提出日時 | 2023-08-29 10:10:16 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 122 ms / 2,000 ms |
コード長 | 3,715 bytes |
コンパイル時間 | 14,353 ms |
コンパイル使用メモリ | 390,980 KB |
実行使用メモリ | 20,520 KB |
最終ジャッジ日時 | 2024-06-09 20:53:43 |
合計ジャッジ時間 | 17,667 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,812 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,944 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,944 KB |
testcase_10 | AC | 1 ms
6,940 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 1 ms
6,940 KB |
testcase_13 | AC | 1 ms
6,940 KB |
testcase_14 | AC | 1 ms
6,940 KB |
testcase_15 | AC | 1 ms
6,940 KB |
testcase_16 | AC | 1 ms
6,944 KB |
testcase_17 | AC | 1 ms
6,944 KB |
testcase_18 | AC | 1 ms
6,944 KB |
testcase_19 | AC | 1 ms
6,944 KB |
testcase_20 | AC | 1 ms
6,944 KB |
testcase_21 | AC | 1 ms
6,940 KB |
testcase_22 | AC | 1 ms
6,940 KB |
testcase_23 | AC | 1 ms
6,944 KB |
testcase_24 | AC | 1 ms
6,944 KB |
testcase_25 | AC | 1 ms
6,944 KB |
testcase_26 | AC | 2 ms
6,944 KB |
testcase_27 | AC | 61 ms
16,600 KB |
testcase_28 | AC | 64 ms
17,108 KB |
testcase_29 | AC | 8 ms
6,944 KB |
testcase_30 | AC | 122 ms
19,760 KB |
testcase_31 | AC | 119 ms
20,520 KB |
testcase_32 | AC | 84 ms
19,600 KB |
testcase_33 | AC | 60 ms
14,880 KB |
testcase_34 | AC | 64 ms
14,892 KB |
testcase_35 | AC | 61 ms
14,476 KB |
testcase_36 | AC | 33 ms
8,880 KB |
testcase_37 | AC | 33 ms
8,832 KB |
testcase_38 | AC | 45 ms
11,904 KB |
testcase_39 | AC | 28 ms
7,424 KB |
testcase_40 | AC | 14 ms
6,944 KB |
testcase_41 | AC | 4 ms
6,940 KB |
testcase_42 | AC | 68 ms
16,532 KB |
testcase_43 | AC | 1 ms
6,944 KB |
コンパイルメッセージ
warning: unused import: `BufWriter` --> src/main.rs:5:22 | 5 | use std::io::{Write, BufWriter}; | ^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused import: `Write` --> src/main.rs:5:15 | 5 | use std::io::{Write, BufWriter}; | ^^^^^
ソースコード
#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::{Write, BufWriter}; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes.by_ref().map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr,) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => (read_value!($next, usize) - 1); ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); read_value!($next, [$t; len]) }}; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); } impl<T: PartialOrd> Change for T { fn chmax(&mut self, x: T) { if *self < x { *self = x; } } fn chmin(&mut self, x: T) { if *self > x { *self = x; } } } fn main() { // In order to avoid potential stack overflow, spawn a new thread. let stack_size = 104_857_600; // 100 MB let thd = std::thread::Builder::new().stack_size(stack_size); thd.spawn(|| solve()).unwrap().join().unwrap(); } const MOD: i64 = 1_000_000_007; fn dfs( v: usize, g: &[Vec<(usize, i64)>], k: i64, vis: &mut [bool], eq: &mut Vec<i64>, pot: &mut [(i64, i64)], p: (i64, i64), ) -> (i64, i64) { let (mut lo, mut hi) = if p.0 == 1 { (1 - p.1, k - p.1) } else { (p.1 - k, p.1 - 1) }; if vis[v] { if pot[v].0 == p.0 { return if pot[v].1 == p.1 { (lo, hi) } else { (1, 0) // contradiction }; } eq.push((p.1 - pot[v].1) * 2 / (pot[v].0 - p.0)); return (lo, hi); } vis[v] = true; pot[v] = p; for &(w, c) in &g[v] { let (sublo, subhi) = dfs(w, g, k, vis, eq, pot, (-p.0, c - p.1)); lo = max(lo, sublo); hi = min(hi, subhi); } (lo, hi) } fn calc(n: usize, xyz: &[(usize, usize, i64)], k: i64) -> i64 { let mut g = vec![vec![]; n]; for &(x, y, z) in xyz { g[x].push((y, z)); g[y].push((x, z)); } let mut vis = vec![false; n]; let mut ans = 1; let mut pot = vec![(0, 0); n]; for i in 0..n { let mut eq = vec![]; if !vis[i] { let (mi, ma) = dfs(i, &g, k, &mut vis, &mut eq, &mut pot, (1, 0)); eq.dedup(); if mi > ma || eq.len() >= 2 { return 0; } if eq.len() == 1 { let x = eq[0]; if x % 2 != 0 || mi > x / 2 || x / 2 > ma { return 0; } } else { ans = ans * (ma - mi + 1) % MOD; } } } ans } fn solve() { input! { n: usize, m: usize, k: i64, xyz: [(usize1, usize1, i64); m], } println!("{}", (calc(n, &xyz, k) - calc(n, &xyz, k - 1) + MOD) % MOD); }