結果

問題 No.1383 Numbers of Product
ユーザー koba-e964koba-e964
提出日時 2021-12-27 18:40:58
言語 Rust
(1.77.0)
結果
AC  
実行時間 812 ms / 2,000 ms
コード長 2,925 bytes
コンパイル時間 1,090 ms
コンパイル使用メモリ 192,416 KB
実行使用メモリ 54,256 KB
最終ジャッジ日時 2024-04-08 11:03:41
合計ジャッジ時間 22,374 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 463 ms
28,148 KB
testcase_08 AC 456 ms
28,148 KB
testcase_09 AC 570 ms
28,148 KB
testcase_10 AC 785 ms
54,256 KB
testcase_11 AC 774 ms
54,256 KB
testcase_12 AC 781 ms
54,256 KB
testcase_13 AC 751 ms
54,256 KB
testcase_14 AC 621 ms
28,148 KB
testcase_15 AC 497 ms
28,148 KB
testcase_16 AC 793 ms
54,256 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 1 ms
6,676 KB
testcase_19 AC 1 ms
6,676 KB
testcase_20 AC 1 ms
6,676 KB
testcase_21 AC 1 ms
6,676 KB
testcase_22 AC 1 ms
6,676 KB
testcase_23 AC 1 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 13 ms
6,676 KB
testcase_29 AC 596 ms
28,148 KB
testcase_30 AC 795 ms
54,256 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 148 ms
8,456 KB
testcase_33 AC 1 ms
6,676 KB
testcase_34 AC 1 ms
6,676 KB
testcase_35 AC 720 ms
54,256 KB
testcase_36 AC 665 ms
28,148 KB
testcase_37 AC 791 ms
54,256 KB
testcase_38 AC 797 ms
54,256 KB
testcase_39 AC 794 ms
54,256 KB
testcase_40 AC 812 ms
54,256 KB
testcase_41 AC 791 ms
54,256 KB
testcase_42 AC 799 ms
54,256 KB
testcase_43 AC 799 ms
54,256 KB
testcase_44 AC 792 ms
54,256 KB
testcase_45 AC 797 ms
54,256 KB
testcase_46 AC 1 ms
6,676 KB
testcase_47 AC 777 ms
54,256 KB
testcase_48 AC 780 ms
54,256 KB
testcase_49 AC 792 ms
54,256 KB
testcase_50 AC 789 ms
54,256 KB
testcase_51 AC 1 ms
6,676 KB
testcase_52 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::*;
use std::io::Read;

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

fn nth(a: i128, n: i64) -> i128 {
    let mut pass = 0;
    let mut fail = std::cmp::min(a, 1 << ((128 + n - 1) / n)) + 1;
    while fail - pass > 1 {
        let mid = (fail + pass) / 2;
        let mut tmp = 1i128;
        for _ in 0..n {
            tmp = tmp.saturating_mul(mid);
        }
        if tmp <= a {
            pass = mid;
        } else {
            fail = mid;
        }
    }
    pass
}

fn calc(x: i64, k: i64) -> (i64, bool) {
    let k = k as i128;
    let val = 4 * x as i128 + k * k;
    let y = nth(val, 2);
    ((y - k) as i64 / 2, y * y == val)
}

// https://yukicoder.me/problems/no/1383 (3.5)
// 20! ~= 2 * 10^18 > 10^18 であるため、B としては 1 <= B <= 18 の範囲まで考えれば良い。
// A についても B についても A(A + K)...(A+BK) は単調増加であるため、1 <= X <= N のとき f(X) <= 18 である。
// B >= 2 であれば A は 10^6 + 10^4 + .. 通り以下しかないので全列挙できる。B >= 2 について全列挙して、それが A(A + K) の形で表せるかどうかを調べれば良さそう。表せるのであればそれに対する f(X) の値が 1 増え、その後ここで見られなかった X = A(A + K) の形の整数全てに対して f(X) = 1 ということにすれば、f(X) = M である X の個数を数えることができる。
// -> K が 10^18 までということを見落とし、実装すべき式を間違えて WA。
fn main() {
    let n: i64 = get();
    let k: i64 = get();
    let m: i64 = get();
    let mut hm = HashMap::new();
    for b in 2i64..19 {
        let mut x = 1i64;
        loop {
            let mut prod = 1i64;
            for i in 0..b + 1 {
                prod = prod.saturating_mul(x.saturating_add(i.saturating_mul(k)));
            }
            if prod > n {
                break;
            }
            *hm.entry(prod).or_insert(0) += 1;
            x += 1;
        }
    }
    let mut ans = 0;
    let mut has = 0;
    for (&val, v) in hm.iter_mut() {
        if calc(val, k).1 {
            *v += 1;
            has += 1;
        }
        if *v == m {
            ans += 1;
        }
    }
    if m == 1 {
        let y = calc(n, k).0 - has;
        ans += y;
    }
    println!("{}", ans);
}
0