complete 5369.rs 11689.rs 15649.rs
This commit is contained in:
65
storage/zeta/rs/completed/11689.rs
Normal file
65
storage/zeta/rs/completed/11689.rs
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
use std::io::stdin;
|
||||||
|
|
||||||
|
fn get_primes_up_to(n: u64) -> Vec<u64> {
|
||||||
|
let mut i = 3;
|
||||||
|
let mut primes = vec![2];
|
||||||
|
while i <= n {
|
||||||
|
let mut is_prime = true;
|
||||||
|
for &p in &primes {
|
||||||
|
if p * p > i {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if i % p == 0 {
|
||||||
|
is_prime = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if is_prime {
|
||||||
|
primes.push(i);
|
||||||
|
}
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
primes
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_divisors(n: u64) -> Vec<(u64, u64)> {
|
||||||
|
let primes = get_primes_up_to((n as f64).sqrt() as u64 + 1);
|
||||||
|
let mut divisors = vec![];
|
||||||
|
let mut remainder = n;
|
||||||
|
|
||||||
|
for &p in &primes {
|
||||||
|
if p * p > remainder {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let mut count = 0;
|
||||||
|
while remainder % p == 0 {
|
||||||
|
remainder /= p;
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
if count > 0 {
|
||||||
|
divisors.push((p, count));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if remainder > 1 {
|
||||||
|
divisors.push((remainder, 1));
|
||||||
|
}
|
||||||
|
divisors
|
||||||
|
}
|
||||||
|
|
||||||
|
fn euler_phi(n: u64) -> u64 {
|
||||||
|
let divisors = get_divisors(n);
|
||||||
|
let mut result = n;
|
||||||
|
for (p, _) in divisors {
|
||||||
|
result = result / p * (p - 1);
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut line = String::new();
|
||||||
|
stdin().read_line(&mut line).unwrap();
|
||||||
|
let n: u64 = line.trim().parse().unwrap();
|
||||||
|
println!("{}", euler_phi(n));
|
||||||
|
}
|
||||||
58
storage/zeta/rs/completed/15649.rs
Normal file
58
storage/zeta/rs/completed/15649.rs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
use std::io::{BufWriter, stdin, Write};
|
||||||
|
enum Operation {
|
||||||
|
VisIn,
|
||||||
|
VisOut
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut line = String::new();
|
||||||
|
stdin().read_line(&mut line).unwrap();
|
||||||
|
let mut out = BufWriter::new(std::io::stdout());
|
||||||
|
let mut iter = line
|
||||||
|
.trim()
|
||||||
|
.split(' ')
|
||||||
|
.map(|x| x.trim().parse::<u64>().unwrap());
|
||||||
|
|
||||||
|
let n = iter.next().unwrap();
|
||||||
|
let m = iter.next().unwrap();
|
||||||
|
|
||||||
|
let mut vis = vec![false; n as usize];
|
||||||
|
let mut arr: Vec<u64> = vec![];
|
||||||
|
let mut stack: Vec<(Operation, u64)> = vec![];
|
||||||
|
|
||||||
|
for i in (0..n).rev() {
|
||||||
|
stack.push((Operation::VisOut, i));
|
||||||
|
stack.push((Operation::VisIn, i));
|
||||||
|
}
|
||||||
|
|
||||||
|
while !stack.is_empty() {
|
||||||
|
let item = stack.pop().unwrap();
|
||||||
|
match item.0 {
|
||||||
|
Operation::VisIn => {
|
||||||
|
arr.push(item.1);
|
||||||
|
if arr.len() == m as usize {
|
||||||
|
for &val in &arr {
|
||||||
|
write!(out, "{} ", val + 1).unwrap();
|
||||||
|
}
|
||||||
|
writeln!(out, "").unwrap();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let idx = item.1 as usize;
|
||||||
|
|
||||||
|
vis[idx] = true;
|
||||||
|
for j in (0..n).rev() {
|
||||||
|
if !vis[j as usize] {
|
||||||
|
stack.push((Operation::VisOut, j));
|
||||||
|
stack.push((Operation::VisIn, j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Operation::VisOut => {
|
||||||
|
let idx = item.1 as usize;
|
||||||
|
vis[idx] = false;
|
||||||
|
arr.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
110
storage/zeta/rs/completed/5369.rs
Normal file
110
storage/zeta/rs/completed/5369.rs
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
use std::{collections::VecDeque, io::stdin};
|
||||||
|
|
||||||
|
enum Location {
|
||||||
|
Empty,
|
||||||
|
Asteroid,
|
||||||
|
Start,
|
||||||
|
Destination,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Point {
|
||||||
|
x: usize,
|
||||||
|
y: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_minimum_steps(field: &Vec<Vec<Location>>, start: &Point, destination: &Point) -> i32 {
|
||||||
|
let mut deq: VecDeque<(Point, i32)> = VecDeque::new();
|
||||||
|
let (n, m) = (field.len(), field[0].len());
|
||||||
|
let mut visited = vec![vec![false; field[0].len()]; field.len()];
|
||||||
|
deq.push_back((
|
||||||
|
Point {
|
||||||
|
x: start.x,
|
||||||
|
y: start.y,
|
||||||
|
},
|
||||||
|
0,
|
||||||
|
));
|
||||||
|
|
||||||
|
while deq.is_empty() == false {
|
||||||
|
let (current_point, steps) = deq.pop_front().unwrap();
|
||||||
|
if current_point.x == destination.x && current_point.y == destination.y {
|
||||||
|
return steps;
|
||||||
|
}
|
||||||
|
|
||||||
|
let directions = vec![
|
||||||
|
(0isize, 1isize),
|
||||||
|
(1isize, 0isize),
|
||||||
|
(0isize, -1isize),
|
||||||
|
(-1isize, 0isize),
|
||||||
|
];
|
||||||
|
|
||||||
|
for direction in directions {
|
||||||
|
let new_x = current_point.x as isize + direction.0;
|
||||||
|
let new_y = current_point.y as isize + direction.1;
|
||||||
|
|
||||||
|
if new_x >= 0 && new_x < m as isize && new_y >= 0 && new_y < n as isize {
|
||||||
|
let new_x_u = new_x as usize;
|
||||||
|
let new_y_u = new_y as usize;
|
||||||
|
|
||||||
|
if visited[new_y_u][new_x_u] {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
match &field[new_y_u][new_x_u] {
|
||||||
|
Location::Asteroid => continue,
|
||||||
|
_ => {
|
||||||
|
visited[new_y_u][new_x_u] = true;
|
||||||
|
deq.push_back((
|
||||||
|
Point {
|
||||||
|
x: new_x_u,
|
||||||
|
y: new_y_u,
|
||||||
|
},
|
||||||
|
steps + 1,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut line = String::new();
|
||||||
|
stdin().read_line(&mut line).unwrap();
|
||||||
|
let t = line.trim().parse::<usize>().unwrap();
|
||||||
|
(0..t).for_each(|_| {
|
||||||
|
line.clear();
|
||||||
|
stdin().read_line(&mut line).unwrap();
|
||||||
|
let n: usize = line.trim().parse().unwrap();
|
||||||
|
let mut starting_point = Point { x: 0, y: 0 };
|
||||||
|
let mut destination_point = Point { x: 0, y: 0 };
|
||||||
|
let field = (0..n)
|
||||||
|
.map(|i| {
|
||||||
|
line.clear();
|
||||||
|
stdin().read_line(&mut line).unwrap();
|
||||||
|
line.trim()
|
||||||
|
.char_indices()
|
||||||
|
.map(|item| {
|
||||||
|
let (j, c) = item;
|
||||||
|
if c == 's' {
|
||||||
|
starting_point = Point { x: j, y: i };
|
||||||
|
return Location::Start;
|
||||||
|
} else if c == 'd' {
|
||||||
|
destination_point = Point { x: j, y: i };
|
||||||
|
return Location::Destination;
|
||||||
|
} else if c == '*' {
|
||||||
|
return Location::Asteroid;
|
||||||
|
} else {
|
||||||
|
return Location::Empty;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect::<Vec<Location>>()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Vec<Location>>>();
|
||||||
|
let result = find_minimum_steps(&field, &starting_point, &destination_point);
|
||||||
|
println!("{}", result);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user