結果

問題 No.1653 Squarefree
ユーザー koba-e964
提出日時 2021-09-15 22:59:12
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 2,107 bytes
コンパイル時間 12,906 ms
コンパイル使用メモリ 381,696 KB
実行使用メモリ 11,704 KB
最終ジャッジ日時 2024-06-29 01:04:05
合計ジャッジ時間 20,555 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

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:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn sqrt(x: i64) -> i64 {
    if x <= 1 { return x; }
    let mut pass = 0;
    let mut fail = 1 << 30;
    while fail - pass > 1 {
        let mid = (pass + fail) / 2;
        if mid * mid <= x {
            pass = mid;
        } else {
            fail = mid;
        }
    }
    pass
}

fn main() {
    const W: usize = 1_000_001;
    input!(l: i64, r: i64);
    let n = (r - l + 1) as usize;
    let mut pr = vec![true; W];
    pr[0] = false;
    pr[1] = false;
    for i in 2..W {
        if !pr[i] {
            continue;
        }
        for j in 2..(W - 1) / i + 1 {
            pr[i * j] = false;
        }
    }
    let mut rem = vec![0; n];
    let mut sq = vec![false; n];
    for i in 0..n {
        rem[i] = l + i as i64;
    }
    for p in 2..W {
        if !pr[p] { continue; }
        let pp = p as i64;
        let mut x = (l + pp - 1) / pp * pp;
        while x <= r {
            let idx = (x - l) as usize;
            if x % (pp * pp) == 0 {
                sq[idx] = true;
            } else {
                rem[idx] /= pp;
            }
            x += pp;
        }
    }
    for i in 0..n {
        let x = sqrt(rem[i]);
        if x > 1 && rem[i] == x * x {
            sq[i] = true;
        }
    }
    println!("{}", sq.iter().filter(|&&x| !x).count());
}
0