結果

問題 No.1396 Giri
ユーザー i110
提出日時 2021-02-16 18:57:42
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 2,039 bytes
コンパイル時間 13,443 ms
コンパイル使用メモリ 381,576 KB
実行使用メモリ 13,136 KB
最終ジャッジ日時 2024-09-13 08:38:54
合計ジャッジ時間 17,280 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;
macro_rules! tok { ($iter: expr) => { $iter.next().unwrap().parse().unwrap() } }
use std::collections::HashMap;

const M: usize = 998244353;

fn prime_division(mut a: usize, spfs: &Vec<usize>) -> Vec<(usize, usize)> {
    let mut primes: Vec<usize> = Vec::new();
    while a != 1 {
        primes.push(spfs[a]);
        a /= spfs[a];
    }
    primes.sort();
    primes.push(0);
    // println!("{:?}", primes);
    let mut groups: Vec<(usize, usize)> = Vec::new();
    let mut count = 1;
    for i in 1..primes.len() {
        if primes[i] != primes[i-1] {
            groups.push((primes[i-1], count));
            count = 0;
        }
        count += 1;
    }
    groups
}

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut iter = buf.split_whitespace();
    let n: usize = tok!(iter);

    let mut spfs: Vec<usize> = (0..=n).collect();
    {
        let mut a = 2;
        while a * a <= n {
            if spfs[a] == a {
                for b in (a*a..=n).step_by(a) {
                    if spfs[b] == b {
                        spfs[b] = a
                    }
                }
            }
            a += 1;
        }
    }
    let mut last_prime: usize = 0;
    for a in (2..=n).rev() {
        if spfs[a] == a {
            last_prime = a;
            break;
        }
    }

    // let mut counts: Vec<usize> = vec![0; 1000 + 1];
    let mut counts: HashMap<usize, usize> = HashMap::new();

    for a in 2..=n {
        if a == last_prime {
            continue;
        }
        // if a % 10000 == 0 { println!("{}", a); }
        let ps = prime_division(a, &spfs);
        // println!("a = {}, ps = {:?}", a, ps);
        for (p, cnt) in ps {
            if !counts.contains_key(&p) || counts[&p] < cnt {
                counts.insert(p, cnt);
            }
        }
    }

    let mut ans: usize = 1;
    for (p, cnt) in counts {
        for _ in 1..=cnt {
            ans = (ans * p) % M;
        }
    }

    println!("{}", ans);
}
0