結果
| 問題 |
No.10 +か×か
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-07-31 13:36:12 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 121 ms / 5,000 ms |
| コード長 | 1,295 bytes |
| コンパイル時間 | 13,293 ms |
| コンパイル使用メモリ | 405,796 KB |
| 実行使用メモリ | 28,168 KB |
| 最終ジャッジ日時 | 2024-07-31 13:36:27 |
| 合計ジャッジ時間 | 14,744 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 12 |
コンパイルメッセージ
warning: unused imports: `BTreeSet`, `HashMap`, `VecDeque`, `io::stdin`, `str::FromStr`, `sync::OnceLock`
--> src/main.rs:2:19
|
2 | collections::{BTreeSet, HashMap, HashSet, VecDeque},
| ^^^^^^^^ ^^^^^^^ ^^^^^^^^
3 | fmt::Debug,
4 | io::stdin,
| ^^^^^^^^^
5 | str::FromStr,
| ^^^^^^^^^^^^
6 | sync::OnceLock,
| ^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: unused import: `proconio::marker::Chars`
--> src/main.rs:9:5
|
9 | use proconio::marker::Chars;
| ^^^^^^^^^^^^^^^^^^^^^^^
ソースコード
use std::{
collections::{BTreeSet, HashMap, HashSet, VecDeque},
fmt::Debug,
io::stdin,
str::FromStr,
sync::OnceLock,
};
use proconio::marker::Chars;
fn main() {
proconio::input! {
n: u32,
t: u32,
a: [u32; n],
}
let result = f(a[0], t, &mut Vec::new(), &a[1..], &mut HashSet::new()).unwrap();
let result = result
.into_iter()
.map(|o| match o {
Op::Plus => '+',
Op::Mult => '*',
})
.collect::<String>();
println!("{result}");
}
#[derive(Debug, Clone, Copy)]
enum Op {
Plus,
Mult,
}
fn f(
v: u32,
t: u32,
op: &mut Vec<Op>,
a: &[u32],
memo: &mut HashSet<(u32, usize)>,
) -> Option<Vec<Op>> {
if a.is_empty() {
return (v == t).then(|| op.clone());
}
if memo.contains(&(v, a.len())) {
return None;
}
if v > t {
memo.insert((v, a.len()));
return None;
}
op.push(Op::Plus);
let plus_v = v + a[0];
if let Some(r) = f(plus_v, t, op, &a[1..], memo) {
return Some(r);
}
op.pop();
op.push(Op::Mult);
let mult_v = v * a[0];
if let Some(r) = f(mult_v, t, op, &a[1..], memo) {
return Some(r);
}
op.pop();
memo.insert((v, a.len()));
None
}