結果

問題 No.2075 GCD Subsequence
ユーザー koba-e964koba-e964
提出日時 2023-08-01 02:16:15
言語 Rust
(1.77.0)
結果
AC  
実行時間 601 ms / 4,000 ms
コード長 2,459 bytes
コンパイル時間 2,008 ms
コンパイル使用メモリ 165,576 KB
実行使用メモリ 82,944 KB
最終ジャッジ日時 2024-04-19 02:01:44
合計ジャッジ時間 18,531 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 377 ms
77,568 KB
testcase_01 AC 383 ms
77,440 KB
testcase_02 AC 381 ms
77,440 KB
testcase_03 AC 379 ms
77,440 KB
testcase_04 AC 376 ms
77,440 KB
testcase_05 AC 374 ms
77,440 KB
testcase_06 AC 371 ms
77,440 KB
testcase_07 AC 368 ms
77,440 KB
testcase_08 AC 437 ms
82,304 KB
testcase_09 AC 480 ms
82,944 KB
testcase_10 AC 438 ms
82,432 KB
testcase_11 AC 463 ms
82,688 KB
testcase_12 AC 448 ms
82,560 KB
testcase_13 AC 438 ms
82,176 KB
testcase_14 AC 468 ms
82,688 KB
testcase_15 AC 452 ms
82,304 KB
testcase_16 AC 442 ms
82,304 KB
testcase_17 AC 485 ms
82,944 KB
testcase_18 AC 595 ms
82,816 KB
testcase_19 AC 594 ms
82,688 KB
testcase_20 AC 585 ms
82,816 KB
testcase_21 AC 589 ms
82,816 KB
testcase_22 AC 600 ms
82,688 KB
testcase_23 AC 600 ms
82,688 KB
testcase_24 AC 600 ms
82,688 KB
testcase_25 AC 601 ms
82,688 KB
testcase_26 AC 593 ms
82,816 KB
testcase_27 AC 598 ms
82,560 KB
testcase_28 AC 595 ms
77,528 KB
testcase_29 AC 373 ms
77,440 KB
testcase_30 AC 377 ms
77,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 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 ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

const MOD: i64 = 998_244_353;

// m <= 7 なので 2^m <= 128
fn main() {
    input! {
        n: usize,
        a: [usize; n],
    }
    const W: usize = 1_000_001;
    let mut fac = vec![vec![]; W];
    for i in 2..W {
        if !fac[i].is_empty() {
            continue;
        }
        for j in 1..(W - 1) / i + 1 {
            fac[i * j].push(i);
        }
    }
    let mut dp = vec![0; W];
    for a in a {
        let f = &fac[a];
        let m = f.len();
        let mut tot = 0;
        for bits in 1usize..1 << m {
            let mut prod = 1;
            for i in 0..m {
                if (bits & 1 << i) != 0 {
                    prod *= f[i];
                }
            }
            if bits.count_ones() % 2 == 0 {
                tot = tot + MOD - dp[prod];
            } else {
                tot += dp[prod];
            }
            if tot >= MOD {
                tot -= MOD;
            }
        }
        for bits in 0..1 << m {
            let mut prod = 1;
            for i in 0..m {
                if (bits & 1 << i) != 0 {
                    prod *= f[i];
                }
            }
            dp[prod] = (dp[prod] + tot + 1) % MOD;
        }
    }
    let mut tot = 0;
    for i in (1..W).rev() {
        for j in 2..(W - 1) / i + 1 {
            dp[i] += MOD - dp[i * j];
            if dp[i] >= MOD {
                dp[i] -= MOD;
            }
        }
        tot += dp[i];
        if tot >= MOD {
            tot -= MOD;
        }
    }
    println!("{}", tot);
}
0