我们记 xi=gcd(xi−1,a[i]) ,则x中不同的数值最多有 1+loga[i] 个。因此我们就可以直接暴力统计答案。用map即可。从n到1枚举左端点,更新答案。
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 300010
inline char gc(){
static char buf[1<<16],*S,*T;
if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
map<int,int>pre,now;
map<int,long long>res;
int n,a[N];
inline int gcd(int x,int y){return y?gcd(y,x%y):x;}
int main(){
// freopen("a.in","r",stdin);
n=read();for(int i=1;i<=n;++i) a[i]=read();
map<int,int>::iterator it;
for(int i=n;i>=1;--i){
now.clear();now[a[i]]++;
for(it=pre.begin();it!=pre.end();++it){
int x=gcd(a[i],it->first);now[x]+=it->second;
}for(it=now.begin();it!=now.end();++it)
res[it->first]+=it->second;swap(now,pre);
}int m=read();
while(m--) printf("%I64d\n",res[read()]);
return 0;
}