显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=400000+5;
int n,m;
ll a[N];
ll ans[N]={0};
ll b[N]={0};
void pre(ll p){
int t=n/p,c=2,d=0;
ll sumc=0;
while(d<=n){
while(c<=t){
b[++d]=a[c];c+=2;
}
if ((n/p)%2==0)c-=3;
else c--;
while(c>=t-n/p+1){
b[++d]=a[c];c-=2;
}
sumc+=b[t]*b[t-n/p+1];
for (int i=t-n/p+1;i<t;i++){
sumc+=b[i]*b[i+1];
}
c=t+2;t+=n/p;
}
ans[p]=sumc;
return ;
}
int gcd(int x,int y){
if (y==0)return x;
return gcd(y,x%y);
}
int main(){
freopen ("noi_online2020_ring.in","r",stdin);
freopen ("noi_online2020_ring.out","w",stdout);
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++){
scanf("%lld",&a[i]);
ans[0]+=a[i]*a[i];
}
sort(a+1,a+n+1);
for (int i=1;i<=n/2;i++){
if (n%i==0){
pre(i);
}
}
for (int i=1;i<=m;i++){
int k;scanf("%d",&k);
if (k==0){
printf("%lld\n",ans[k]);
continue;
}
int g=gcd(k,n);
printf("%lld\n",ans[g]);
}
return 0;
}