結果

問題 No.2497 GCD of LCMs
ユーザー koba-e964koba-e964
提出日時 2023-12-21 02:13:06
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 466 ms / 2,000 ms
コード長 3,502 bytes
コンパイル時間 13,959 ms
コンパイル使用メモリ 398,228 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-27 10:40:14
合計ジャッジ時間 17,109 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,820 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 22 ms
6,940 KB
testcase_08 AC 148 ms
6,940 KB
testcase_09 AC 180 ms
6,944 KB
testcase_10 AC 230 ms
6,940 KB
testcase_11 AC 96 ms
6,940 KB
testcase_12 AC 329 ms
6,940 KB
testcase_13 AC 408 ms
6,940 KB
testcase_14 AC 42 ms
6,940 KB
testcase_15 AC 28 ms
6,944 KB
testcase_16 AC 466 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[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"));
}

fn gcd(mut x: i64, mut y: i64) -> i64 {
    while y != 0 {
        let r = x % y;
        x = y; y = r;
    }
    x.abs()
}

const MOD: i64 = 998_244_353;

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        n: usize, m: usize,
        a: [i64; n],
        uv: [(usize1, usize1); m],
    }
    let mut g = vec![vec![]; n];
    for &(u, v) in &uv {
        g[u].push(v);
        g[v].push(u);
    }
    let mut factors = a.clone();
    loop {
        let mut new = vec![];
        let mut changed = false;
        for i in 0..factors.len() {
            for j in 0..i {
                let g = gcd(factors[i], factors[j]);
                if g != 1 {
                    factors[i] /= g;
                    factors[j] /= g;
                    new.push(g);
                    changed = true;
                }
            }
        }
        if !changed {
            break;
        }
        for &f in &factors {
            if f != 1 {
                new.push(f);
            }
        }
        new.sort(); new.dedup();
        factors = new;
    }
    eprintln!("f = {:?}", factors);
    let mut ans = vec![1; n];
    for &fac in &factors {
        let mut e = vec![0; n];
        for i in 0..n {
            let mut v = a[i];
            while v % fac == 0 {
                v /= fac;
                e[i] += 1;
            }
        }
        let mut que = BinaryHeap::new();
        que.push((Reverse(e[0]), 0));
        const INF: u32 = 1 << 28;
        let mut dist = vec![INF; n];
        while let Some((Reverse(d), v)) = que.pop() {
            if dist[v] <= d { continue; }
            dist[v] = d;
            for &w in &g[v] {
                let nd = max(d, e[w]);
                que.push((Reverse(nd), w));
            }
        }
        for i in 0..n {
            for _ in 0..dist[i] {
                ans[i] = ans[i] * fac % MOD;
            }
        }
    }
    for a in ans {
        puts!("{}\n", a);
    }
}
0