結果

問題 No.2262 Fractions
ユーザー akakimidoriakakimidori
提出日時 2023-04-07 22:49:01
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 4,763 bytes
コンパイル時間 11,703 ms
コンパイル使用メモリ 377,892 KB
実行使用メモリ 6,912 KB
最終ジャッジ日時 2024-05-05 07:22:59
合計ジャッジ時間 17,068 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
6,016 KB
testcase_01 AC 63 ms
5,376 KB
testcase_02 AC 58 ms
5,376 KB
testcase_03 AC 58 ms
5,376 KB
testcase_04 AC 58 ms
5,376 KB
testcase_05 AC 54 ms
5,376 KB
testcase_06 AC 57 ms
5,376 KB
testcase_07 AC 56 ms
5,376 KB
testcase_08 AC 51 ms
5,376 KB
testcase_09 AC 55 ms
5,376 KB
testcase_10 AC 53 ms
5,376 KB
testcase_11 AC 82 ms
5,376 KB
testcase_12 AC 86 ms
5,376 KB
testcase_13 AC 86 ms
5,376 KB
testcase_14 AC 85 ms
5,376 KB
testcase_15 AC 86 ms
5,376 KB
testcase_16 AC 48 ms
5,376 KB
testcase_17 AC 48 ms
5,376 KB
testcase_18 AC 46 ms
5,376 KB
testcase_19 AC 101 ms
5,936 KB
testcase_20 AC 94 ms
5,888 KB
testcase_21 AC 101 ms
6,016 KB
testcase_22 AC 96 ms
5,816 KB
testcase_23 AC 81 ms
5,552 KB
testcase_24 AC 113 ms
6,784 KB
testcase_25 AC 114 ms
6,784 KB
testcase_26 AC 114 ms
6,784 KB
testcase_27 AC 113 ms
6,784 KB
testcase_28 AC 112 ms
6,912 KB
testcase_29 AC 113 ms
6,784 KB
testcase_30 AC 114 ms
6,784 KB
testcase_31 AC 114 ms
6,784 KB
testcase_32 AC 114 ms
6,784 KB
testcase_33 AC 116 ms
6,784 KB
testcase_34 AC 119 ms
6,784 KB
testcase_35 AC 117 ms
6,784 KB
testcase_36 AC 116 ms
6,912 KB
testcase_37 AC 8 ms
5,376 KB
testcase_38 AC 8 ms
5,376 KB
testcase_39 AC 100 ms
5,632 KB
testcase_40 AC 102 ms
5,632 KB
testcase_41 AC 100 ms
5,632 KB
testcase_42 AC 99 ms
5,632 KB
testcase_43 AC 101 ms
5,632 KB
testcase_44 AC 117 ms
6,784 KB
testcase_45 AC 117 ms
6,784 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type alias `Map` is never used
 --> src/main.rs:4:6
  |
4 | type Map<K, V> = BTreeMap<K, V>;
  |      ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
 --> src/main.rs:5:6
  |
5 | type Set<T> = BTreeSet<T>;
  |      ^^^

warning: type alias `Deque` is never used
 --> src/main.rs:6:6
  |
6 | type Deque<T> = VecDeque<T>;
  |      ^^^^^

ソースコード

diff #

use std::collections::*;
use std::io::Write;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn run() {
    input! {
        t: usize,
        ask: [(usize, usize); t],
    }
    let n = 300000usize;
    let mut phi = (0..=n).collect::<Vec<_>>();
    let mut prime = vec![];
    enumerate_prime(n, |p| {
        prime.push(p);
        for i in 1..=(n / p) {
            phi[i * p] -= phi[i * p] / p;
        }
    });
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for (n, k) in ask {
        let s = phi[1..=n].iter().sum::<usize>();
        if k > 2 * s - 1 {
            writeln!(out, "-1").ok();
            continue;
        }
        if k == s {
            writeln!(out, "1/1").ok();
            continue;
        }
        let m = k.min(2 * s - k);
        let de = 1usize << 40;
        let mut ok = de;
        let mut ng = 0;
        while ok - ng > 1 {
            let mid = (ok + ng) / 2;
            let mut dp = vec![0; n + 1];
            for (i, dp) in dp.iter_mut().enumerate().skip(1) {
                *dp = i * mid / de;
            }
            for &p in prime.iter().take_while(|p| **p <= n) {
                for i in (1..=(n / p)).rev() {
                    dp[i * p] -= dp[i];
                }
            }
            let s = dp.iter().sum::<usize>();
            if s >= m {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        let (ok, ng) = (ok, ng);
        let mut l = (0, 1);
        let mut r = (1, 1);
        loop {
            let m = (l.0 + r.0, l.1 + r.1);
            if ng * m.1 < m.0 * de && m.0 * de <= ok * m.1 {
                r = m;
                break;
            }
            if ng * m.1 < m.0 * de {
                r = m;
            } else {
                l = m;
            }
        }
        let mut ans = r;
        if k > s {
            ans = (ans.1, ans.0);
        }
        writeln!(out, "{}/{}", ans.0, ans.1).ok();
    }
}

fn main() {
    run();
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[macro_export]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[macro_export]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------
// ---------- begin enumerate prime ----------
fn enumerate_prime<F>(n: usize, mut f: F)
where
    F: FnMut(usize),
{
    assert!(1 <= n && n <= 5 * 10usize.pow(8));
    let batch = (n as f64).sqrt().ceil() as usize;
    let mut is_prime = vec![true; batch + 1];
    for i in (2..).take_while(|p| p * p <= batch) {
        if is_prime[i] {
            let mut j = i * i;
            while let Some(p) = is_prime.get_mut(j) {
                *p = false;
                j += i;
            }
        }
    }
    let mut prime = vec![];
    for (i, p) in is_prime.iter().enumerate().skip(2) {
        if *p && i <= n {
            f(i);
            prime.push(i);
        }
    }
    let mut l = batch + 1;
    while l <= n {
        let r = std::cmp::min(l + batch, n + 1);
        is_prime.clear();
        is_prime.resize(r - l, true);
        for &p in prime.iter() {
            let mut j = (l + p - 1) / p * p - l;
            while let Some(is_prime) = is_prime.get_mut(j) {
                *is_prime = false;
                j += p;
            }
        }
        for (i, _) in is_prime.iter().enumerate().filter(|p| *p.1) {
            f(i + l);
        }
        l += batch;
    }
}
// ---------- end enumerate prime ----------
0