結果

問題 No.2075 GCD Subsequence
ユーザー koba-e964koba-e964
提出日時 2023-08-01 02:13:43
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 591 ms / 4,000 ms
コード長 3,882 bytes
コンパイル時間 13,455 ms
コンパイル使用メモリ 403,816 KB
実行使用メモリ 82,944 KB
最終ジャッジ日時 2024-10-10 18:39:05
合計ジャッジ時間 30,197 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 336 ms
77,440 KB
testcase_01 AC 351 ms
77,440 KB
testcase_02 AC 349 ms
77,440 KB
testcase_03 AC 344 ms
77,568 KB
testcase_04 AC 346 ms
77,568 KB
testcase_05 AC 346 ms
77,440 KB
testcase_06 AC 348 ms
77,472 KB
testcase_07 AC 351 ms
77,440 KB
testcase_08 AC 418 ms
82,324 KB
testcase_09 AC 462 ms
82,908 KB
testcase_10 AC 415 ms
82,416 KB
testcase_11 AC 443 ms
82,688 KB
testcase_12 AC 430 ms
82,556 KB
testcase_13 AC 409 ms
82,176 KB
testcase_14 AC 435 ms
82,688 KB
testcase_15 AC 426 ms
82,304 KB
testcase_16 AC 426 ms
82,304 KB
testcase_17 AC 459 ms
82,928 KB
testcase_18 AC 584 ms
82,732 KB
testcase_19 AC 560 ms
82,688 KB
testcase_20 AC 557 ms
82,848 KB
testcase_21 AC 576 ms
82,816 KB
testcase_22 AC 580 ms
82,816 KB
testcase_23 AC 565 ms
82,944 KB
testcase_24 AC 561 ms
82,688 KB
testcase_25 AC 585 ms
82,816 KB
testcase_26 AC 575 ms
82,800 KB
testcase_27 AC 584 ms
82,688 KB
testcase_28 AC 591 ms
77,664 KB
testcase_29 AC 345 ms
77,440 KB
testcase_30 AC 356 ms
77,440 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `BufWriter`
 --> src/main.rs:5:22
  |
5 | use std::io::{Write, BufWriter};
  |                      ^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `Write`
 --> src/main.rs:5:15
  |
5 | use std::io::{Write, BufWriter};
  |               ^^^^^

warning: function `subsets` is never used
  --> src/main.rs:64:4
   |
64 | fn subsets(univ: usize) -> SubsetIter {
   |    ^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

ソースコード

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

/// Generates an Iterator over subsets of univ, in the descending order. 
/// Verified by: http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=3050308
struct SubsetIter { bits: Option<usize>, univ: usize }
impl Iterator for SubsetIter {
    type Item = usize;
    fn next(&mut self) -> Option<usize> {
        match self.bits {
            None => None,
            Some(bits) => {
                let ans = bits;
                self.bits =
                    if bits == 0 { None }
                else { Some((bits - 1) & self.univ) };
                Some(ans)
            }
        }
    }
}
fn subsets(univ: usize) -> SubsetIter {
    SubsetIter { bits: Some(univ), univ: univ }
}

const MOD: i64 = 998_244_353;

// m <= 7 なので 3^m <= 2187
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();
}

fn solve() {
    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