From 8cac8240823ef3bfcf64f7b3f8ee024d313ea8e2 Mon Sep 17 00:00:00 2001 From: li_chx Date: Fri, 10 Apr 2026 10:34:20 +0800 Subject: [PATCH] =?UTF-8?q?3655.=20=E5=8C=BA=E9=97=B4=E4=B9=98=E6=B3=95?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=90=8E=E7=9A=84=E5=BC=82=E6=88=96=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 147 +++++++++++++++++++++------------------------------- 1 file changed, 59 insertions(+), 88 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0a55bf2..1f467bf 100644 --- a/src/main.rs +++ b/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, queries: Vec>) -> i32 { + let mut nums = nums.into_iter().map(|x| x as i64).collect::>(); + 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; - } - - fn get_pos(&self) -> Vec { - if self.no_step { - return vec![0, 0]; + 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; + } + } } - if self.current > self.d { - return vec![self.width - (self.current - self.d) - 1, self.height - 1]; + for i in 1..pivot { + for j in 0..nums.len() { + nums[j] = (nums[j] * diff[i][j]) % MOD + } } - if self.current > self.c { - return vec![self.width - 1, self.current - self.c]; + for i in 1..nums.len() { + nums[0] ^= nums[i]; } - if self.current > self.b { - 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() + 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 = 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]]" + ) + ) + ); }