結果

問題 No.732 3PrimeCounting
ユーザー koba-e964koba-e964
提出日時 2018-12-12 02:09:15
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 1,384 ms / 3,000 ms
コード長 9,634 bytes
コンパイル時間 13,181 ms
コンパイル使用メモリ 394,412 KB
実行使用メモリ 76,956 KB
最終ジャッジ日時 2024-09-24 17:51:54
合計ジャッジ時間 128,317 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,240 ms
76,828 KB
testcase_01 AC 1,224 ms
76,700 KB
testcase_02 AC 1,237 ms
76,700 KB
testcase_03 AC 1,229 ms
76,696 KB
testcase_04 AC 1,255 ms
76,696 KB
testcase_05 AC 1,232 ms
76,956 KB
testcase_06 AC 1,308 ms
76,828 KB
testcase_07 AC 1,271 ms
76,824 KB
testcase_08 AC 1,237 ms
76,828 KB
testcase_09 AC 1,223 ms
76,700 KB
testcase_10 AC 1,234 ms
76,696 KB
testcase_11 AC 1,231 ms
76,700 KB
testcase_12 AC 1,252 ms
76,828 KB
testcase_13 AC 1,258 ms
76,828 KB
testcase_14 AC 1,257 ms
76,828 KB
testcase_15 AC 1,333 ms
76,700 KB
testcase_16 AC 1,251 ms
76,700 KB
testcase_17 AC 1,249 ms
76,700 KB
testcase_18 AC 1,237 ms
76,828 KB
testcase_19 AC 1,245 ms
76,824 KB
testcase_20 AC 1,214 ms
76,696 KB
testcase_21 AC 1,283 ms
76,832 KB
testcase_22 AC 1,277 ms
76,700 KB
testcase_23 AC 1,225 ms
76,828 KB
testcase_24 AC 1,221 ms
76,828 KB
testcase_25 AC 1,230 ms
76,828 KB
testcase_26 AC 1,253 ms
76,824 KB
testcase_27 AC 1,247 ms
76,700 KB
testcase_28 AC 1,225 ms
76,828 KB
testcase_29 AC 1,246 ms
76,696 KB
testcase_30 AC 1,229 ms
76,828 KB
testcase_31 AC 1,227 ms
76,828 KB
testcase_32 AC 1,239 ms
76,824 KB
testcase_33 AC 1,259 ms
76,828 KB
testcase_34 AC 1,249 ms
76,700 KB
testcase_35 AC 1,319 ms
76,828 KB
testcase_36 AC 1,319 ms
76,704 KB
testcase_37 AC 1,254 ms
76,832 KB
testcase_38 AC 1,246 ms
76,824 KB
testcase_39 AC 1,228 ms
76,824 KB
testcase_40 AC 1,222 ms
76,700 KB
testcase_41 AC 1,257 ms
76,824 KB
testcase_42 AC 1,238 ms
76,824 KB
testcase_43 AC 1,233 ms
76,700 KB
testcase_44 AC 1,244 ms
76,828 KB
testcase_45 AC 1,252 ms
76,696 KB
testcase_46 AC 1,236 ms
76,700 KB
testcase_47 AC 1,251 ms
76,824 KB
testcase_48 AC 1,259 ms
76,696 KB
testcase_49 AC 1,280 ms
76,700 KB
testcase_50 AC 1,242 ms
76,824 KB
testcase_51 AC 1,232 ms
76,696 KB
testcase_52 AC 1,242 ms
76,828 KB
testcase_53 AC 1,228 ms
76,828 KB
testcase_54 AC 1,220 ms
76,828 KB
testcase_55 AC 1,237 ms
76,828 KB
testcase_56 AC 1,228 ms
76,704 KB
testcase_57 AC 1,273 ms
76,704 KB
testcase_58 AC 1,314 ms
76,704 KB
testcase_59 AC 1,238 ms
76,704 KB
testcase_60 AC 1,229 ms
76,828 KB
testcase_61 AC 1,232 ms
76,700 KB
testcase_62 AC 1,220 ms
76,696 KB
testcase_63 AC 1,224 ms
76,700 KB
testcase_64 AC 1,224 ms
76,700 KB
testcase_65 AC 1,384 ms
76,832 KB
testcase_66 AC 1,222 ms
76,700 KB
testcase_67 AC 1,210 ms
76,700 KB
testcase_68 AC 1,239 ms
76,828 KB
testcase_69 AC 1,225 ms
76,828 KB
testcase_70 AC 1,216 ms
76,700 KB
testcase_71 AC 1,219 ms
76,828 KB
testcase_72 AC 1,205 ms
76,700 KB
testcase_73 AC 1,214 ms
76,828 KB
testcase_74 AC 1,239 ms
76,828 KB
testcase_75 AC 1,221 ms
76,700 KB
testcase_76 AC 1,231 ms
76,700 KB
testcase_77 AC 1,243 ms
76,700 KB
testcase_78 AC 1,240 ms
76,828 KB
testcase_79 AC 1,250 ms
76,828 KB
testcase_80 AC 1,237 ms
76,700 KB
testcase_81 AC 1,231 ms
76,700 KB
testcase_82 AC 1,242 ms
76,828 KB
testcase_83 AC 1,213 ms
76,696 KB
testcase_84 AC 1,226 ms
76,828 KB
testcase_85 AC 1,235 ms
76,700 KB
testcase_86 AC 1,278 ms
76,828 KB
testcase_87 AC 1,297 ms
76,828 KB
testcase_88 AC 1,242 ms
76,824 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 {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($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);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}
/// FFT (in-place)
/// R: Ring + Copy
/// Verified by: ATC001-C (http://atc001.contest.atcoder.jp/submissions/1175827)
mod fft {
    use std::ops::*;
    fn inplace_internal_fft<R>(
        f: &[R], output: &mut [R], pztbl: &[R], one: R,
        x: usize, fstart: usize, fstep: usize,
        n: usize, ostart: usize)
        where R: Copy +
        Add<Output = R> +
        Sub<Output = R> +
        Mul<Output = R> {
        if n == 1 {
            output[ostart] = f[fstart];
            return;
        }
        inplace_internal_fft(f, output, pztbl, one, x + 1,
                             fstart, 2 * fstep, n / 2, ostart);
        inplace_internal_fft(f, output, pztbl, one, x + 1,
			     fstart + fstep, 2 * fstep, n / 2, ostart + n / 2);
        let mut cnt = 0;
        for i in 0 .. n / 2 {
            let pzeta = pztbl[cnt];
            let f0 = output[ostart + i];
            let f1 = output[ostart + i + n / 2];
            let tmp = pzeta * f1;
            output[ostart + i] = f0 + tmp;
            output[ostart + i + n / 2] = f0 - tmp;
            cnt += 1 << x;
        }
    }
    /// n should be a power of 2. zeta is a primitive n-th root of unity.
    /// one is unity
    /// Note that the result should be multiplied by 1/sqrt(n).
    pub fn transform<R>(f: &[R], zeta: R, one: R) -> Vec<R>
        where R: Copy +
        Add<Output = R> +
        Sub<Output = R> +
        Mul<Output = R> {
        let n = f.len();
        assert!(n.is_power_of_two());
        let mut pztbl = vec![one; n];
        for i in 1 .. n {
            pztbl[i] = pztbl[i - 1] * zeta;
        }
        let mut output = vec![zeta; n];
        inplace_internal_fft(&f, &mut output, &pztbl, one, 0, 0, 1, n, 0);
        output
    }
}

/// Verified by: https://beta.atcoder.jp/contests/arc099/submissions/3515280
mod mod_int {
    use std::ops::*;
    pub trait Mod: Copy + Clone {
        fn m() -> i64;
    }
    #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
    pub struct ModInt<M: Mod> { pub x: i64, phantom: ::std::marker::PhantomData<*const M> }
    impl<M: Mod> ModInt<M> {
        fn check_integrity(self) {
            debug_assert!(self.x >= 0);
            debug_assert!(self.x < M::m());
        }
        // x >= 0
        pub fn new(x: i64) -> Self { ModInt::new_internal(x % M::m()) }
        fn new_internal(x: i64) -> Self { ModInt { x: x, phantom: ::std::marker::PhantomData } }
        #[allow(dead_code)]
        pub fn mul_fast(self, other: Self) -> Self {
            self.check_integrity();
            other.check_integrity();
            ModInt::new_internal(self.x * other.x % M::m())
        }
        #[allow(dead_code)]
        pub fn mul_slow(self, other: Self) -> Self {
            // Naive multiplication in order to avoid overflow
            self.check_integrity();
            other.check_integrity();
            let mut sum = ModInt::new_internal(0);
            let mut cur = self;
            let mut e = other.x;
            if self.x < other.x {
                cur = other;
                e = self.x;
            }
            while e > 0 {
                if e % 2 == 1 {
                    sum += cur;
                }
                cur += cur;
                e /= 2;
            }
            sum
        }
        pub fn pow(self, mut e: i64) -> Self {
            self.check_integrity();
            debug_assert!(e >= 0);
            let mut sum = ModInt::new_internal(1);
            let mut cur = self;
            while e > 0 {
                if e % 2 != 0 {
                    sum *= cur;
                }
                cur *= cur;
                e /= 2;
            }
            sum
        }
        #[allow(dead_code)]
        pub fn inv(self) -> Self { self.pow(M::m() - 2) }
    }
    impl<M: Mod> Add for ModInt<M> {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            self.check_integrity();
            other.check_integrity();
            let mut sum = self.x + other.x;
            if sum >= M::m() { sum -= M::m(); }
            ModInt::new_internal(sum)
        }
    }
    impl<M: Mod> Sub for ModInt<M> {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            self.check_integrity();
            other.check_integrity();
            let mut sum = self.x - other.x;
            if sum < 0 { sum += M::m(); }
            ModInt::new_internal(sum)
        }
    }
    impl<M: Mod> Mul for ModInt<M> {
        type Output = Self;
        fn mul(self, other: Self) -> Self {
            self.mul_fast(other)
        }
    }
    impl<M: Mod> AddAssign for ModInt<M> {
        fn add_assign(&mut self, other: Self) {
            *self = *self + other;
        }
    }
    impl<M: Mod> SubAssign for ModInt<M> {
        fn sub_assign(&mut self, other: Self) {
            *self = *self - other;
        }
    }
    impl<M: Mod> MulAssign for ModInt<M> {
        fn mul_assign(&mut self, other: Self) {
            *self = *self * other;
        }
    }
    impl<M: Mod> ::std::fmt::Display for ModInt<M> {
        fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
            self.x.fmt(f)
        }
    }
    impl<M: Mod> ::std::fmt::Debug for ModInt<M> {
        fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
            self.x.fmt(f)
        }
    }
} // mod mod_int

macro_rules! define_mod {
    ($struct_name: ident, $modulo: expr) => {
        #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
        struct $struct_name {}
        impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } }
    }
}
const MOD1: i64 = 998244353;
const MOD2: i64 = 1004535809;
define_mod!(P1, MOD1);
define_mod!(P2, MOD2);
type ModInt1 = mod_int::ModInt<P1>;
type ModInt2 = mod_int::ModInt<P2>;

use mod_int::*;

const N: usize = 1 << 20;

fn calc<M: Mod>(n: usize, pr: &[bool], zeta: ModInt<M>) -> ModInt<M> {
    let zeta = zeta.pow((M::m() - 1) / N as i64);
    let zeta_inv = zeta.inv();
    let mut f = vec![ModInt::new(0); N];
    let mut f2 = vec![ModInt::new(0); N];
    let mut f3 = vec![ModInt::new(0); N];
    for i in 1 .. n + 1 {
        if pr[i] {
            f[i] = ModInt::new(1);
            f2[2 * i] = ModInt::new(1);
            f3[3 * i] = ModInt::new(1);
        }
    }
    // f^3 - 3 * f2 * f + 2 * f3
    let f = fft::transform(&f, zeta, ModInt::new(1));
    let f2 = fft::transform(&f2, zeta, ModInt::new(1));
    let f3 = fft::transform(&f3, zeta, ModInt::new(1));
    let mut g = vec![ModInt::new(0); N];
    for i in 0 .. N {
        g[i] = f[i].pow(3) - ModInt::new(3) * f2[i] * f[i] + f3[i] + f3[i];
    }
    let g = fft::transform(&g, zeta_inv, ModInt::new(1));
    let mut tot = ModInt::new(0);
    for i in 2 .. N {
        if pr[i] { tot += g[i]; }
    }
    tot *= ModInt::new(N as i64).inv();
    tot *= ModInt::new(6).inv();
    tot
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($format:expr) => (write!(out,$format).unwrap());
        ($format:expr, $($args:expr),+) => (write!(out,$format,$($args),*).unwrap())
    }
    let mut pr = vec![true; N];
    pr[0] = false;
    pr[1] = false;
    for i in 2 .. N {
        if !pr[i] { continue; }
        for j in 2 .. (N - 1) / i + 1 { pr[i * j] = false; }
    }
    input! {
        n: usize,
    }
    let a: ModInt1 = calc(n, &pr, ModInt::new(3));
    let b: ModInt2 = calc(n, &pr, ModInt::new(3));
    let factor2 = ModInt2::new(P1::m()).inv();
    let factor1 = ModInt1::new(P2::m()).inv();
    puts!("{}\n", ((b * factor2).x * P1::m() + (a * factor1).x * P2::m()) % (P1::m() * P2::m()));
}

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