由于我们仅仅关心个数而并不关心区间,所以能够递推来写。如果我们知道了第以i个数为结尾的全部区间上的最大公约数,我们就能够统计他的个数。而我们在讨论i+1的时候,我们是能够推出以第i+1个数结尾的全部最大公约数的(和第i个数的结果依次取GCD即可),然后统计个数(事实上就是直接加进去)。
最后记得每次把第i个数的结果加到ANS里面即可了。
代码并不长。时间也不长。果然是由于map太快了么= =||
#include <iostream>
#include <cstring>
#include <map>
#include <cstdio>
using namespace std;
int a[100005],n,q,c;
int gcd(int a,int b)
{
if (!b) return a;
else return gcd(b,a%b);
}
map<int,long long> ans;
map<int,int> last;
map<int,int> now;
map<int,int>::iterator itr;
int main()
{
scanf("%d",&n);
for (int i=0;i<n;i++) scanf("%d",&a[i]);
ans.clear();
for (int i=0;i<n;i++)
{
swap(last,now);
now.clear();
for (itr=last.begin();itr!=last.end();itr++)
now[gcd(itr->first,a[i])]+=itr->second;
now[a[i]]++;
for (itr=now.begin();itr!=now.end();itr++)
ans[itr->first]+=itr->second;
}
scanf("%d",&q);
for (int i=0;i<q;i++)
{
scanf("%d",&c);
printf("%I64d\n",ans[c]);
}
return 0;
}