提出 #46642235


ソースコード 拡げる

#![allow(unused)]

const MOD: u64 = 998_244_353;

fn main() {
    let sieve = SieveOfEratosthenes::new(1_000_000);

    let inp = readv::<u64>();
    let (a, b) = (inp[0], inp[1]);

    if b == 0 {
        println!("{}", 0);
        return;
    }

    // The product of the factor of a^b is a^(b * d(a^b) / 2)
    // The problem ask for the maximum time it can be divided by a, that is
    // if b * d(a^b) is odd, (d(a^b) - 1) / 2
    // if b * d(a^b) is even, d(a^b) / 2

    let mut d = Mint(1); // d(a ^ b)
    let mut is_odd = b % 2 == 1;
    for (p, e) in sieve.factorize(a) {
        d *= Mint(b % MOD) * Mint(e % MOD) + Mint(1);
        is_odd &= (e + 1) % 2 == 1;
    }

    if is_odd {
        println!("{}", (Mint(b % MOD) * d - Mint(1)) / Mint(2));
    } else {
        println!("{}", Mint(b % MOD) * d / Mint(2))
    }
}

struct SieveOfEratosthenes {
    primes: Vec<u64>,
}

impl SieveOfEratosthenes {
    fn new(max_val: usize) -> Self {
        let mut is_prime = vec![true; max_val + 1];
        let mut primes = vec![];
        for i in 2..=max_val {
            if is_prime[i] {
                primes.push(i as u64);
                let mut j = i * i;
                while j <= max_val {
                    is_prime[j] = false;
                    j += i;
                }
            }
        }
        Self { primes }
    }

    fn factorize(&self, mut x: u64) -> Vec<(u64, u64)> {
        assert!(x > 1);
        let mut res = vec![];
        for &p in self.primes.iter() {
            let mut exp = 0;
            while x % p == 0 {
                exp += 1;
                x = x / p;
            }
            if exp > 0 {
                res.push((p, exp))
            }
            if p * p > x {
                break;
            }
        }
        if x > 1 {
            res.push((x, 1));
        }
        res
    }
}

// x = p^a q^b r^c

// Number of factors of x:
//    d(x) = (a + 1)(b + 1)(c + 1)

// Sum of factors of x = ABC where
//    A = (p^(a + 1) - 1) / (p - 1)
//    B = (q^(b + 1) - 1) / (q - 1)
//    C = (r^(c + 1) - 1) / (r - 1)

// Product of factors of x:
//    x^(d(x) / 2)
// Note that d(x) / 2 may not be integer
// Take the factors of 36 as example:
// 1  2  3  4  6
// 36 18 12 9
// There are d(x) / 2 pairs which have product 36

#[derive(Debug, Copy, Clone)]
struct Mint(u64);

impl Mint {
    const MOD: u64 = 998_244_353;
    fn inv(&self) -> Mint {
        let mut a = self.0 as i64;
        let mut b = Mint::MOD as i64;
        let mut p = 1 as i64;
        let mut q = 0 as i64;
        while b != 0 {
            let (c, r) = (a / b, a % b);
            a = b;
            b = r;
            let tmp = p - c * q;
            p = q;
            q = tmp;
        }
        Mint(p.rem_euclid(Mint::MOD as i64) as u64)
    }
    fn pow(&self, mut b: u64) -> Mint {
        let mut base = *self;
        let mut res = Mint(1);
        while b != 0 {
            if b & 1 == 1 {
                res = res * base;
            }
            base = base * base;
            b >>= 1;
        }
        res
    }
}
impl Default for Mint {
    fn default() -> Self {
        Self(0)
    }
}
impl std::ops::Add for Mint {
    type Output = Mint;
    fn add(self, rhs: Mint) -> Mint {
        Mint((self.0 + rhs.0) % Mint::MOD)
    }
}
impl std::ops::Sub for Mint {
    type Output = Mint;
    fn sub(self, rhs: Mint) -> Mint {
        Mint((self.0 + Mint::MOD - rhs.0) % Mint::MOD)
    }
}
impl std::ops::Mul for Mint {
    type Output = Mint;
    fn mul(self, rhs: Mint) -> Mint {
        Mint(self.0 * rhs.0 % Mint::MOD)
    }
}
impl std::ops::Div for Mint {
    type Output = Mint;
    fn div(self, rhs: Mint) -> Mint {
        self * rhs.inv()
    }
}
impl std::ops::AddAssign for Mint {
    fn add_assign(&mut self, rhs: Self) {
        *self = Mint((self.0 + rhs.0) % Mint::MOD);
    }
}
impl std::ops::SubAssign for Mint {
    fn sub_assign(&mut self, rhs: Self) {
        *self = Mint((self.0 + Mint::MOD - rhs.0) % Mint::MOD);
    }
}
impl std::ops::MulAssign for Mint {
    fn mul_assign(&mut self, rhs: Self) {
        *self = Mint((self.0 * rhs.0) % Mint::MOD);
    }
}
impl std::ops::DivAssign for Mint {
    fn div_assign(&mut self, rhs: Self) {
        *self = *self * rhs.inv();
    }
}
impl std::fmt::Display for Mint {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

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()
}

fn readv<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_ascii_whitespace()
        .map(|t| t.parse().ok().unwrap())
        .collect()
}

fn reads() -> Vec<char> {
    read::<String>().chars().collect::<Vec<char>>()
}

fn mapv<T, S, F: Fn(&T) -> S>(arr: &Vec<T>, f: F) -> Vec<S> {
    arr.iter().map(f).collect()
}

fn join<T: ToString>(arr: &[T], sep: &str) -> String {
    arr.iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>()
        .join(sep)
}

提出情報

提出日時
問題 B - Product of Divisors
ユーザ amoshuangyc
言語 Rust (rustc 1.70.0)
得点 500
コード長 5266 Byte
結果 AC
実行時間 5 ms
メモリ 3644 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 3
AC × 67
セット名 テストケース
Sample 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt
All 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt, 02-min-01.txt, 03-max-01.txt, 04-mult-mod-sqrt-01.txt, 04-mult-mod-sqrt-02.txt, 04-mult-mod-sqrt-03.txt, 04-mult-mod-sqrt-04.txt, 04-mult-mod-sqrt-05.txt, 04-mult-mod-sqrt-06.txt, 04-mult-mod-sqrt-07.txt, 04-mult-mod-sqrt-08.txt, 04-mult-mod-sqrt-09.txt, 04-mult-mod-sqrt-10.txt, 05-square-even-even-01.txt, 05-square-even-even-02.txt, 05-square-even-even-03.txt, 05-square-even-even-04.txt, 05-square-even-even-05.txt, 06-square-even-odd-01.txt, 06-square-even-odd-02.txt, 06-square-even-odd-03.txt, 06-square-even-odd-04.txt, 06-square-even-odd-05.txt, 07-square-odd-even-01.txt, 07-square-odd-even-02.txt, 07-square-odd-even-03.txt, 07-square-odd-even-04.txt, 07-square-odd-even-05.txt, 08-square-odd-odd-01.txt, 08-square-odd-odd-02.txt, 08-square-odd-odd-03.txt, 08-square-odd-odd-04.txt, 08-square-odd-odd-05.txt, 09-random-01.txt, 09-random-02.txt, 09-random-03.txt, 09-random-04.txt, 09-random-05.txt, 09-random-06.txt, 09-random-07.txt, 09-random-08.txt, 09-random-09.txt, 09-random-10.txt, 10-mult-mod-01.txt, 10-mult-mod-02.txt, 10-mult-mod-03.txt, 10-mult-mod-04.txt, 10-mult-mod-05.txt, 11-prime-square-01.txt, 11-prime-square-02.txt, 11-prime-square-03.txt, 11-prime-square-04.txt, 12-prime-01.txt, 12-prime-02.txt, 12-prime-03.txt, 12-prime-04.txt, 13-2pow-01.txt, 13-2pow-02.txt, 13-2pow-03.txt, 13-2pow-04.txt, 13-2pow-05.txt, 13-2pow-06.txt, 13-2pow-07.txt, 13-2pow-08.txt, 13-2pow-09.txt
ケース名 結果 実行時間 メモリ
01-sample-01.txt AC 4 ms 3560 KiB
01-sample-02.txt AC 4 ms 3536 KiB
01-sample-03.txt AC 4 ms 3512 KiB
02-min-01.txt AC 4 ms 3484 KiB
03-max-01.txt AC 4 ms 3432 KiB
04-mult-mod-sqrt-01.txt AC 4 ms 3584 KiB
04-mult-mod-sqrt-02.txt AC 4 ms 3628 KiB
04-mult-mod-sqrt-03.txt AC 4 ms 3492 KiB
04-mult-mod-sqrt-04.txt AC 4 ms 3596 KiB
04-mult-mod-sqrt-05.txt AC 4 ms 3552 KiB
04-mult-mod-sqrt-06.txt AC 4 ms 3608 KiB
04-mult-mod-sqrt-07.txt AC 4 ms 3532 KiB
04-mult-mod-sqrt-08.txt AC 4 ms 3596 KiB
04-mult-mod-sqrt-09.txt AC 4 ms 3584 KiB
04-mult-mod-sqrt-10.txt AC 4 ms 3544 KiB
05-square-even-even-01.txt AC 4 ms 3580 KiB
05-square-even-even-02.txt AC 4 ms 3508 KiB
05-square-even-even-03.txt AC 5 ms 3604 KiB
05-square-even-even-04.txt AC 4 ms 3496 KiB
05-square-even-even-05.txt AC 4 ms 3584 KiB
06-square-even-odd-01.txt AC 4 ms 3596 KiB
06-square-even-odd-02.txt AC 4 ms 3472 KiB
06-square-even-odd-03.txt AC 4 ms 3580 KiB
06-square-even-odd-04.txt AC 4 ms 3536 KiB
06-square-even-odd-05.txt AC 4 ms 3424 KiB
07-square-odd-even-01.txt AC 4 ms 3388 KiB
07-square-odd-even-02.txt AC 4 ms 3632 KiB
07-square-odd-even-03.txt AC 4 ms 3512 KiB
07-square-odd-even-04.txt AC 4 ms 3548 KiB
07-square-odd-even-05.txt AC 4 ms 3560 KiB
08-square-odd-odd-01.txt AC 4 ms 3408 KiB
08-square-odd-odd-02.txt AC 4 ms 3448 KiB
08-square-odd-odd-03.txt AC 4 ms 3500 KiB
08-square-odd-odd-04.txt AC 4 ms 3512 KiB
08-square-odd-odd-05.txt AC 4 ms 3520 KiB
09-random-01.txt AC 4 ms 3560 KiB
09-random-02.txt AC 4 ms 3532 KiB
09-random-03.txt AC 4 ms 3520 KiB
09-random-04.txt AC 4 ms 3488 KiB
09-random-05.txt AC 4 ms 3604 KiB
09-random-06.txt AC 4 ms 3568 KiB
09-random-07.txt AC 4 ms 3572 KiB
09-random-08.txt AC 4 ms 3376 KiB
09-random-09.txt AC 4 ms 3484 KiB
09-random-10.txt AC 4 ms 3544 KiB
10-mult-mod-01.txt AC 4 ms 3632 KiB
10-mult-mod-02.txt AC 4 ms 3480 KiB
10-mult-mod-03.txt AC 4 ms 3536 KiB
10-mult-mod-04.txt AC 4 ms 3536 KiB
10-mult-mod-05.txt AC 4 ms 3576 KiB
11-prime-square-01.txt AC 4 ms 3480 KiB
11-prime-square-02.txt AC 4 ms 3568 KiB
11-prime-square-03.txt AC 5 ms 3600 KiB
11-prime-square-04.txt AC 4 ms 3540 KiB
12-prime-01.txt AC 5 ms 3432 KiB
12-prime-02.txt AC 4 ms 3516 KiB
12-prime-03.txt AC 4 ms 3572 KiB
12-prime-04.txt AC 4 ms 3472 KiB
13-2pow-01.txt AC 4 ms 3592 KiB
13-2pow-02.txt AC 4 ms 3572 KiB
13-2pow-03.txt AC 5 ms 3560 KiB
13-2pow-04.txt AC 4 ms 3540 KiB
13-2pow-05.txt AC 4 ms 3468 KiB
13-2pow-06.txt AC 4 ms 3532 KiB
13-2pow-07.txt AC 4 ms 3376 KiB
13-2pow-08.txt AC 4 ms 3544 KiB
13-2pow-09.txt AC 4 ms 3644 KiB