#include<bits/stdc++.h> usingnamespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define per(i, a, b) for (int i = (b); (i--) > (a);) intreadint(){ int v; scanf("%d", &v); return v; } intgcd(int a, int b){ while (b != 0) { int tmp = a % b; a = b; b = tmp; } return a; } constint PWR = 21; int lrg[600010][PWR]; // l..r gcd intmain(){ int n = readint(); int m = readint(); rep(i, 1, n + 1) lrg[i][0] = readint(); rep(pwr, 1, PWR) { // i..(i+(1<<pwr)-1) int hstep = 1 << (pwr - 1); // half step rep(i, 1, n + 1) { lrg[i][pwr] = lrg[i][pwr - 1]; if (i + hstep <= n) { lrg[i][pwr] = gcd(lrg[i][pwr], lrg[i + hstep][pwr - 1]); } } } rep(i, 0, m) { // O(m * int l = readint(); int r = readint(); int ans = 1; int res = lrg[r][0]; while (r >= l) { // O(log(a_i) * per(pwr, 0, PWR) { // O(log(n) * int pos = r - (1 << pwr) + 1; if (pos < l) continue; // gcd( gcd[r-(1<<pwr)+1....r], res) // int g = gcd(lrg[pos][pwr], res); // if (g != res) // continue; if (lrg[pos][pwr] % res != 0) continue;
// 最大2的倍数长度 且 gcd(gcd[pos..r],res) == res r = pos - 1; if (pos == l) break; // O( *log(a_i) ) int newres = gcd(lrg[r][pwr], res); // [pwr]长度,少算很多轮? ans += newres != res; res = newres; break; } } printf("%d\n", ans); } return0; }