結果

問題 No.1653 Squarefree
ユーザー fukafukatanifukafukatani
提出日時 2021-08-22 21:35:36
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 440 ms / 2,000 ms
コード長 2,557 bytes
コンパイル時間 12,637 ms
コンパイル使用メモリ 384,180 KB
実行使用メモリ 19,800 KB
最終ジャッジ日時 2024-11-06 20:47:45
合計ジャッジ時間 25,473 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 432 ms
19,756 KB
testcase_01 AC 10 ms
6,816 KB
testcase_02 AC 9 ms
6,820 KB
testcase_03 AC 9 ms
6,816 KB
testcase_04 AC 9 ms
6,816 KB
testcase_05 AC 9 ms
6,816 KB
testcase_06 AC 9 ms
6,820 KB
testcase_07 AC 10 ms
6,820 KB
testcase_08 AC 9 ms
6,816 KB
testcase_09 AC 10 ms
6,816 KB
testcase_10 AC 10 ms
6,816 KB
testcase_11 AC 241 ms
19,800 KB
testcase_12 AC 237 ms
19,668 KB
testcase_13 AC 433 ms
19,604 KB
testcase_14 AC 436 ms
19,564 KB
testcase_15 AC 431 ms
19,624 KB
testcase_16 AC 421 ms
19,664 KB
testcase_17 AC 424 ms
19,568 KB
testcase_18 AC 421 ms
19,732 KB
testcase_19 AC 418 ms
19,584 KB
testcase_20 AC 420 ms
19,580 KB
testcase_21 AC 430 ms
19,640 KB
testcase_22 AC 425 ms
19,656 KB
testcase_23 AC 429 ms
19,484 KB
testcase_24 AC 432 ms
19,560 KB
testcase_25 AC 433 ms
19,692 KB
testcase_26 AC 433 ms
19,564 KB
testcase_27 AC 440 ms
19,680 KB
testcase_28 AC 420 ms
19,672 KB
testcase_29 AC 435 ms
19,680 KB
testcase_30 AC 431 ms
19,492 KB
testcase_31 AC 436 ms
19,656 KB
testcase_32 AC 430 ms
19,676 KB
testcase_33 AC 434 ms
19,480 KB
testcase_34 AC 436 ms
19,620 KB
testcase_35 AC 434 ms
19,596 KB
testcase_36 AC 9 ms
6,816 KB
testcase_37 AC 9 ms
6,816 KB
testcase_38 AC 10 ms
6,820 KB
testcase_39 AC 10 ms
6,816 KB
testcase_40 AC 9 ms
6,816 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `ng`
  --> src/main.rs:51:22
   |
51 |             let (ok, ng) = binary_search(0, numbers[i] + 1, |m| m * m <= numbers[i]);
   |                      ^^ help: if this is intentional, prefix it with an underscore: `_ng`
   |
   = note: `#[warn(unused_variables)]` on by default

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<i128>();
    let (l, r) = (v[0], v[1]);
    let primes = get_primes(1000000);

    let mut flags = vec![true; (r - l) as usize + 1];
    let mut numbers = (l..=r).collect::<Vec<_>>();

    for p in primes {
        let mut cur = (l / p) * p;
        if cur < l {
            cur += p;
        }
        while cur <= r {
            let idx = (cur - l) as usize;
            if numbers[idx] % p == 0 {
                numbers[idx] /= p;
            }
            if numbers[idx] % p == 0 {
                flags[idx] = false;
            }
            cur += p;
        }
    }

    let mut ans = 0;
    for num in l..=r {
        if num == 1 {
            ans += 1;
            continue;
        }
        let i = (num - l) as usize;
        if flags[i] {
            let (ok, ng) = binary_search(0, numbers[i] + 1, |m| m * m <= numbers[i]);
            if ok * ok != numbers[i] || numbers[i] == 1 {
                ans += 1;
            }
        }
    }
    println!("{}", ans);
}

type Input = i128;
fn binary_search<F>(lb: Input, ub: Input, criterion: F) -> (Input, Input)
where
    F: Fn(Input) -> bool,
{
    assert_eq!(criterion(lb), true);
    assert_eq!(criterion(ub), false);
    let mut ok = lb;
    let mut ng = ub;
    while ng - ok > 1 {
        let mid = (ng + ok) / 2;
        if criterion(mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    (ok, ng)
}

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 read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn get_primes(n: i128) -> Vec<i128> {
    let mut is_prime = vec![true; n as usize + 1];
    let mut primes = Vec::new();
    is_prime[0] = false;
    is_prime[1] = false;

    for i in 2..n + 1 {
        if is_prime[i as usize] {
            primes.push(i);
            let mut j = 2 * i;
            while j <= n {
                is_prime[j as usize] = false;
                j += i;
            }
        }
    }
    primes
}
0