結果

問題 No.1330 Multiply or Divide
ユーザー phspls
提出日時 2023-01-08 20:11:48
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,381 bytes
コンパイル時間 12,106 ms
コンパイル使用メモリ 384,816 KB
実行使用メモリ 8,704 KB
最終ジャッジ日時 2024-12-15 23:42:13
合計ジャッジ時間 14,684 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 43 WA * 3
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused imports: `HashMap`, `HashSet`, `VecDeque`
 --> src/main.rs:1:24
  |
1 | use std::collections::{HashMap, VecDeque, HashSet};
  |                        ^^^^^^^  ^^^^^^^^  ^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused variable: `n`
  --> src/main.rs:18:9
   |
18 |     let n = nmp[0];
   |         ^ help: if this is intentional, prefix it with an underscore: `_n`
   |
   = note: `#[warn(unused_variables)]` on by default

ソースコード

diff #

use std::collections::{HashMap, VecDeque, HashSet};


fn costmap(val: usize, p: usize) -> (usize, usize) {
    let mut cnt = 1usize;
    let mut val = val;
    while val % p == 0 {
        cnt += 1;
        val /= p;
    }
    (val, cnt)
}

fn main() {
    let mut nmp = String::new();
    std::io::stdin().read_line(&mut nmp).ok();
    let nmp: Vec<usize> = nmp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nmp[0];
    let m = nmp[1];
    let p = nmp[2];
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    let maxval = *a.iter().max().unwrap();
    if maxval >= m {
        println!("1");
        return;
    }
    let paths = a.iter().map(|&v| costmap(v, p)).filter(|&v| v.0 > 1).collect::<Vec<_>>();
    if paths.is_empty() {
        println!("-1");
        return;
    }
    let mut cnt2max = vec![0usize; 31];
    for &(val, cnt) in paths.iter() {
        cnt2max[cnt] = cnt2max[cnt].max(val);
    }
    let mut result = 1usize << 60;
    for (i, &val) in cnt2max.iter().enumerate() {
        if val == 0 { continue; }
        for j in 1.. {
            if maxval * val.pow(j) >= m {
                result = result.min(i * j as usize + 1);
                break;
            }
        }
    }
    println!("{}", result);
}
0