20240320 Finished

This commit is contained in:
jackfiled 2024-03-20 10:54:32 +08:00
parent 925e9051e2
commit 8c9a92ff9a
2 changed files with 57 additions and 1 deletions

View File

@ -79,3 +79,4 @@ mod p2684_maximum_number_of_moves_in_a_grid;
mod p310_minimum_height_trees; mod p310_minimum_height_trees;
mod p303_range_sum_query_immutable; mod p303_range_sum_query_immutable;
mod p1793_maximum_score_of_a_good_subarray; mod p1793_maximum_score_of_a_good_subarray;
mod p1969_minimum_non_zero_product_of_the_array_elements;

View File

@ -0,0 +1,55 @@
use std::convert::TryInto;
/**
* [1969] Minimum Non-Zero Product of the Array Elements
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn min_non_zero_product(p: i32) -> i32 {
let p = p as i64;
if p == 1 {
return 1;
}
let m = 1e9 as i64 + 7;
let x = Solution::fast_power(2, p, m) - 1;
let y = 1 << (p - 1);
dbg!(x, y);
return (Solution::fast_power(x - 1, y - 1, m) * x % m).try_into().unwrap();
}
fn fast_power(x: i64, n: i64, m: i64) -> i64 {
let mut result = 1;
let mut n = n;
let mut x = x;
while n != 0 {
if n & 1 != 0 {
result = result * x % m;
}
x = x * x % m;
n = n >> 1;
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1969() {
assert_eq!(1, Solution::min_non_zero_product(1));
assert_eq!(6, Solution::min_non_zero_product(2));
}
}