3655. 区间乘法查询后的异或 II

This commit is contained in:
li_chx 2026-04-10 10:34:20 +08:00
parent 8cc2ab780c
commit 8cac824082
Signed by: li_chx
GPG Key ID: C7CF27EFA1E58BAC
1 changed files with 59 additions and 88 deletions

View File

@ -2,102 +2,73 @@ use crate::arr::make_matrix;
mod arr; mod arr;
struct Robot { struct Solution {}
a: i32, const MOD: i64 = 1_000_000_007;
b: i32,
c: i32, fn mod_pow(mut a: i64, mut e: i64) -> i64 {
d: i32, let mut r = 1i64;
width: i32, a %= MOD;
height: i32, while e > 0 {
length: i32, if (e & 1) == 1 {
no_step: bool, r = r * a % MOD;
current: i32, }
a = a * a % MOD;
e >>= 1;
}
r
} }
/** fn mod_inv(x: i64) -> i64 {
* `&self` means the method takes an immutable reference. // 仅当 x % MOD != 0 时存在逆元
* If you need a mutable reference, change it to `&mut self` instead. mod_pow(x, MOD - 2)
*/ }
impl Robot { impl Solution {
fn new(width: i32, height: i32) -> Self { pub fn xor_after_queries(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> i32 {
Robot { let mut nums = nums.into_iter().map(|x| x as i64).collect::<Vec<i64>>();
a: 0, let pivot = queries.len().isqrt();
b: height - 1, let mut diff = vec![vec![1_i64; nums.len() + pivot]; pivot];
c: height + width - 2, for q in &queries {
d: height * 2 + width - 3, let [l, r, k, v]: [i32; 4] = q.as_slice().try_into().unwrap();
width, let k = k as usize;
height, let l = l as usize;
length: width * 2 + height * 2 - 4, let r = r as usize;
no_step: true, if k >= pivot {
current: height - 1, 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;
}
} }
} for i in 1..pivot {
for j in 0..i {
fn step(&mut self, num: i32) { for k in (j + i..nums.len() + 1).step_by(i) {
self.no_step = false; diff[i][k] = (diff[i][k - i] * diff[i][k]) % MOD;
self.current = (self.current + num) % self.length; }
} }
fn get_pos(&self) -> Vec<i32> {
if self.no_step {
return vec![0, 0];
} }
if self.current > self.d { for i in 1..pivot {
return vec![self.width - (self.current - self.d) - 1, self.height - 1]; for j in 0..nums.len() {
nums[j] = (nums[j] * diff[i][j]) % MOD
}
} }
if self.current > self.c { for i in 1..nums.len() {
return vec![self.width - 1, self.current - self.c]; nums[0] ^= nums[i];
} }
if self.current > self.b { nums[0] as i32
return vec![self.current - self.b, 0];
}
vec![0, self.height - 1 - self.current]
}
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()
} }
} }
/**
* 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() { fn main() {
let opers = vec![ println!(
"Robot", "step", "step", "getPos", "getDir", "step", "step", "step", "getPos", "getDir", "{:?}",
]; Solution::xor_after_queries(
let data = make_matrix(" [[20, 13], [50], [0], [], [], [2], [1], [4], [], []]"); vec![562, 62],
let mut robot = Robot::new(data[0][0], data[0][1]); make_matrix(
for i in 0..opers.len() { "[[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]]"
let oper = opers[i]; )
println!("{:#?}", oper); )
match oper { );
"step" => {
robot.step(data[i][0]);
}
"getPos" => {
println!("{:#?}", robot.get_pos());
}
"getDir" => {
println!("{:#?}", robot.get_dir());
}
_ => {}
}
}
} }