結果

問題 No.1514 Squared Matching
ユーザー fukafukatanifukafukatani
提出日時 2021-05-21 22:43:40
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 2,061 ms / 4,000 ms
コード長 2,071 bytes
コンパイル時間 11,866 ms
コンパイル使用メモリ 405,380 KB
実行使用メモリ 392,600 KB
最終ジャッジ日時 2024-10-10 09:26:50
合計ジャッジ時間 46,382 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: function `read_vec` is never used
  --> src/main.rs:88:4
   |
88 | fn read_vec<T: std::str::FromStr>() -> Vec<T> {
   |    ^^^^^^^^
   |
   = note: `#[warn(dead_code)]` 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 n = read::<usize>();

    let mut squares = vec![];
    for i in 1.. {
        if i * i > n {
            break;
        }
        squares.push(i as i64 * i as i64);
    }
    squares.reverse();

    let mut numbers = vec![0i64; n + 1];
    for i in 1..=n {
        numbers[i] = i as i64;
    }

    for &sq in &squares {
        let mut cur = sq;
        while cur <= n as i64 {
            if numbers[cur as usize] % sq == 0 {
                numbers[cur as usize] /= sq;
            }
            cur += sq
        }
    }
    if n < 50 {
        debug!(numbers);
    }
    squares.reverse();
    let criterion = |x, mid| {
        if mid == squares.len() {
            return false;
        }
        x * squares[mid] <= n as i64
    };
    let mut ans = 0;
    for i in 1..=n {
        let (ok, _) = binary_search(0, squares.len(), |mid: usize| criterion(numbers[i], mid));
        ans += ok as i64 + 1;
    }
    println!("{}", ans);
}

type Input = usize;
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()
}
0