3655. 区间乘法查询后的异或 II
This commit is contained in:
parent
8cc2ab780c
commit
8cac824082
139
src/main.rs
139
src/main.rs
|
|
@ -2,102 +2,73 @@ use crate::arr::make_matrix;
|
|||
|
||||
mod arr;
|
||||
|
||||
struct Robot {
|
||||
a: i32,
|
||||
b: i32,
|
||||
c: i32,
|
||||
d: i32,
|
||||
width: i32,
|
||||
height: i32,
|
||||
length: i32,
|
||||
no_step: bool,
|
||||
current: i32,
|
||||
struct Solution {}
|
||||
const MOD: i64 = 1_000_000_007;
|
||||
|
||||
fn mod_pow(mut a: i64, mut e: i64) -> i64 {
|
||||
let mut r = 1i64;
|
||||
a %= MOD;
|
||||
while e > 0 {
|
||||
if (e & 1) == 1 {
|
||||
r = r * a % MOD;
|
||||
}
|
||||
a = a * a % MOD;
|
||||
e >>= 1;
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl Robot {
|
||||
fn new(width: i32, height: i32) -> Self {
|
||||
Robot {
|
||||
a: 0,
|
||||
b: height - 1,
|
||||
c: height + width - 2,
|
||||
d: height * 2 + width - 3,
|
||||
width,
|
||||
height,
|
||||
length: width * 2 + height * 2 - 4,
|
||||
no_step: true,
|
||||
current: height - 1,
|
||||
fn mod_inv(x: i64) -> i64 {
|
||||
// 仅当 x % MOD != 0 时存在逆元
|
||||
mod_pow(x, MOD - 2)
|
||||
}
|
||||
impl Solution {
|
||||
pub fn xor_after_queries(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> i32 {
|
||||
let mut nums = nums.into_iter().map(|x| x as i64).collect::<Vec<i64>>();
|
||||
let pivot = queries.len().isqrt();
|
||||
let mut diff = vec![vec![1_i64; nums.len() + pivot]; pivot];
|
||||
for q in &queries {
|
||||
let [l, r, k, v]: [i32; 4] = q.as_slice().try_into().unwrap();
|
||||
let k = k as usize;
|
||||
let l = l as usize;
|
||||
let r = r as usize;
|
||||
if k >= pivot {
|
||||
for i in (l..=r).step_by(k) {
|
||||
nums[i] = (nums[i] * v as i64) % MOD
|
||||
}
|
||||
} else {
|
||||
diff[k][l] = (diff[k][l] * v as i64) % MOD;
|
||||
let r_idx = r - (r - l) % k + k;
|
||||
diff[k][r_idx] = (diff[k][r_idx] * mod_inv(v as i64)) % MOD;
|
||||
}
|
||||
}
|
||||
|
||||
fn step(&mut self, num: i32) {
|
||||
self.no_step = false;
|
||||
self.current = (self.current + num) % self.length;
|
||||
for i in 1..pivot {
|
||||
for j in 0..i {
|
||||
for k in (j + i..nums.len() + 1).step_by(i) {
|
||||
diff[i][k] = (diff[i][k - i] * diff[i][k]) % MOD;
|
||||
}
|
||||
|
||||
fn get_pos(&self) -> Vec<i32> {
|
||||
if self.no_step {
|
||||
return vec![0, 0];
|
||||
}
|
||||
if self.current > self.d {
|
||||
return vec![self.width - (self.current - self.d) - 1, self.height - 1];
|
||||
}
|
||||
if self.current > self.c {
|
||||
return vec![self.width - 1, self.current - self.c];
|
||||
for i in 1..pivot {
|
||||
for j in 0..nums.len() {
|
||||
nums[j] = (nums[j] * diff[i][j]) % MOD
|
||||
}
|
||||
if self.current > self.b {
|
||||
return vec![self.current - self.b, 0];
|
||||
}
|
||||
vec![0, self.height - 1 - self.current]
|
||||
for i in 1..nums.len() {
|
||||
nums[0] ^= nums[i];
|
||||
}
|
||||
|
||||
fn get_dir(&self) -> String {
|
||||
if self.no_step {
|
||||
return "East".to_string();
|
||||
}
|
||||
if self.current > self.d || self.current == 0 {
|
||||
return "West".to_string();
|
||||
}
|
||||
if self.current > self.c {
|
||||
return "North".to_string();
|
||||
}
|
||||
if self.current > self.b {
|
||||
return "East".to_string();
|
||||
}
|
||||
"South".to_string()
|
||||
nums[0] as i32
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your Robot object will be instantiated and called as such:
|
||||
* let obj = Robot::new(width, height);
|
||||
* obj.step(num);
|
||||
* let ret_2: Vec<i32> = obj.get_pos();
|
||||
* let ret_3: String = obj.get_dir();
|
||||
*/
|
||||
fn main() {
|
||||
let opers = vec![
|
||||
"Robot", "step", "step", "getPos", "getDir", "step", "step", "step", "getPos", "getDir",
|
||||
];
|
||||
let data = make_matrix(" [[20, 13], [50], [0], [], [], [2], [1], [4], [], []]");
|
||||
let mut robot = Robot::new(data[0][0], data[0][1]);
|
||||
for i in 0..opers.len() {
|
||||
let oper = opers[i];
|
||||
println!("{:#?}", oper);
|
||||
match oper {
|
||||
"step" => {
|
||||
robot.step(data[i][0]);
|
||||
}
|
||||
"getPos" => {
|
||||
println!("{:#?}", robot.get_pos());
|
||||
}
|
||||
"getDir" => {
|
||||
println!("{:#?}", robot.get_dir());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
println!(
|
||||
"{:?}",
|
||||
Solution::xor_after_queries(
|
||||
vec![562, 62],
|
||||
make_matrix(
|
||||
"[[0,1,2,7],[1,1,2,11],[0,1,2,2],[1,1,1,11],[1,1,2,1],[0,0,1,9],[0,1,2,4],[1,1,1,6],[0,0,2,17]]"
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue