use std::collections::HashMap; use proconio::input; fn main() { input! { n:usize, q:usize, a:[usize;n], lrk:[(usize,usize,usize);q], } let mut di = HashMap::new(); for i in 0..n { for d in divisors(a[i]) { di.entry(d).or_insert(vec![]).push(i + 1); } } for (l, r, k) in lrk { let ans = if let Some(is) = di.get(&k) { let ri = is.lower_bound(r + 1); let li = is.lower_bound(l); ri - li } else { 0 }; println!("{}", ans); } } use math::*; mod math { #[allow(dead_code)] pub fn pow(mut n: usize, mut e: usize, m: usize) -> usize { let mut res = 1; n %= m; while 0 < e { if e & 1 != 0 { res *= n; res %= m; } n *= n; n %= m; e >>= 1; } res } #[allow(dead_code)] pub fn gcd(a: usize, b: usize) -> usize { if b == 0 { a } else { gcd(b, a % b) } } #[allow(dead_code)] pub fn lcm(a: usize, b: usize) -> usize { a / gcd(a, b) * b } #[allow(dead_code)] pub fn prime_factor(mut n: usize) -> std::collections::HashMap { let mut pfacs = std::collections::HashMap::new(); for d in 2.. { if d * d > n { break; } while n % d == 0 { *pfacs.entry(d).or_insert(0) += 1; n /= d; } } if n != 1 { *pfacs.entry(n).or_insert(0) += 1; } pfacs } #[allow(dead_code)] pub fn divisors(n: usize) -> Vec { let mut divs = vec![]; for d in (1..).take_while(|&d| d * d <= n) { if n % d == 0 { divs.push(d); if n / d != d { divs.push(n / d); } } } divs } #[allow(dead_code)] pub fn eratosthenes_sieve(n: usize) -> Vec { let mut is_primes = vec![true; n + 1]; is_primes[0] = false; is_primes[1] = false; let mut primes = vec![]; for i in 2..=n { if !is_primes[i] { continue; } for j in (2 * i..=n).step_by(i) { is_primes[j] = false; } } for (val, is_prime) in is_primes.into_iter().enumerate() { if is_prime { primes.push(val); } } primes } #[allow(dead_code)] pub fn base_n(mut base10: usize, base: usize, len: usize) -> Vec { let mut base_n = vec![]; for _ in 0..len { base_n.push(base10 % base); base10 /= base; } base_n.reverse(); base_n } #[allow(dead_code)] pub fn div_floor(mut a: i64, mut b: i64) -> i64 { if b < 0 { a = -a; b = -b; } if a >= 0 { a / b } else { (a + 1) / b - 1 } } #[allow(dead_code)] pub fn div_ceil(mut a: i64, mut b: i64) -> i64 { if b < 0 { a = -a; b = -b; } if a > 0 { (a - 1) / b + 1 } else { a / b } } } use bound::*; mod bound { pub trait Bound { fn lower_bound(&self, val: T) -> usize; fn upper_bound(&self, val: T) -> usize; } impl Bound for [T] where T: Ord + Copy, { fn lower_bound(&self, val: T) -> usize { let mut ok = self.len(); let mut ng = !0; while ok.wrapping_sub(ng) > 1 { let m = ok.wrapping_add(ng) / 2; if val <= self[m] { ok = m; } else { ng = m; } } ok } fn upper_bound(&self, val: T) -> usize { let mut ok = !0; let mut ng = self.len(); while ng.wrapping_sub(ok) > 1 { let m = ng.wrapping_add(ok) / 2; if self[m] <= val { ok = m; } else { ng = m; } } ok } } }