complete 1359.rs 20947.rs

This commit is contained in:
2026-01-09 21:35:04 -08:00
parent 73240ede3f
commit 080b007094
2 changed files with 176 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
use std::io::stdin;
fn main() {
let mut line = String::new();
stdin().read_line(&mut line).unwrap();
let mut iter = line.trim().split(' ').map(|x| x.parse::<usize>().unwrap());
let (n, m, k) = (
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
);
let mut comb_mem = [[0u64; 9]; 9];
for i in 0..=n {
comb_mem[i][0] = 1; // nC0 = 1
for j in 1..=i {
comb_mem[i][j] = comb_mem[i - 1][j - 1] + comb_mem[i - 1][j];
}
}
let total_cases = comb_mem[n][m];
let mut winning_cases = 0u64;
for i in k..=m {
if n >= m && (n - m) >= (m - i) {
let ways_to_match = comb_mem[m][i];
let ways_to_pick_rest = comb_mem[n - m][m - i];
winning_cases += ways_to_match * ways_to_pick_rest;
}
}
let probability = winning_cases as f64 / total_cases as f64;
println!("{:.10}", probability);
}

View File

@@ -0,0 +1,138 @@
use std::io::{read_to_string, stdin, stdout, BufWriter, Write};
#[derive(Debug, Clone, Copy)]
pub enum Land {
Building,
Debris,
Bomb,
Empty,
}
impl Land {
pub fn from_char(ch: char) -> Self {
match ch {
'.' => Self::Empty,
'O' => Self::Building,
'X' => Self::Debris,
'B' => Self::Bomb,
_ => Self::Empty,
}
}
pub fn to_char(&self) -> char {
match self {
Land::Building => 'O',
Land::Debris => 'X',
Land::Bomb => 'B',
Land::Empty => '.',
}
}
}
fn find_bombs(n: usize, mut city: Vec<Vec<Land>>) -> Vec<Vec<Land>> {
let mut ables: Vec<Vec<bool>> = city
.iter()
.map(|row| {
row.iter()
.map(|land| match land {
Land::Empty => true,
_ => false,
})
.collect()
})
.collect();
for i in 0..n {
for j in 0..n {
let land = city[i][j];
match land {
Land::Empty => {}
Land::Debris => {}
Land::Bomb => {}
Land::Building => {
for iu in (0..i).rev() {
let search = city[iu][j];
match search {
Land::Empty => {
ables[iu][j] = false;
}
_ => {
break;
}
}
}
for iu in (i + 1..n) {
let search = city[iu][j];
match search {
Land::Empty => {
ables[iu][j] = false;
}
_ => {
break;
}
}
}
for ju in (0..j).rev() {
let search = city[i][ju];
match search {
Land::Empty => {
ables[i][ju] = false;
}
_ => {
break;
}
}
}
for ju in (j + 1..n) {
let search = city[i][ju];
match search {
Land::Empty => {
ables[i][ju] = false;
}
_ => {
break;
}
}
}
}
}
}
}
for i in 0..n {
for j in 0..n {
let able = ables[i][j];
if able {
city[i][j] = Land::Bomb;
}
}
}
city
}
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp.split_ascii_whitespace();
let n = iter.next().unwrap().parse::<usize>().unwrap();
let city: Vec<Vec<Land>> = (0..n)
.map(|_| {
iter.next()
.unwrap()
.trim()
.chars()
.map(|ch| Land::from_char(ch))
.collect()
})
.collect();
let city_with_bomb = find_bombs(n, city);
let mut stdout = BufWriter::new(stdout().lock());
city_with_bomb.iter().for_each(|x| {
x.iter().for_each(|land| {
stdout.write_all(&[land.to_char() as u8]).unwrap();
});
stdout.write_all(&['\n' as u8]).unwrap();
});
}