显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=200005;
ll a[maxn],ans[maxn];
int n,m;
bool cmp(int x,int y)
{
return x>y;
}
int gcd(int a,int b)
{
if(a<b)
swap(a,b);
if(a%b==0)
return b;
else gcd(b,a%b);
}
void CTJ()
{
for(int i=1;i<=n;i++)
ans[0]+=a[i]*a[i];
for(int k=1;k<=n/2;k++)
{
int g=gcd(n,k),num=n/g;
if(ans[g])
continue;
for(int i=0;i<=n;i++)
{
if(i%num==0)
{
ans[g]+=a[i+1]*a[i+2];
continue;
}
if((i+1)%num==0)
{
ans[g]+=a[i]*a[i+1];
}
else
ans[g]+=a[i]*a[i+2];
}
}
}
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("%d",&a[i]);
sort(a+1,a+n+1,cmp);
CTJ();
while(m--)
{
int k=0;
scanf("%d",&k);
if(k==0)
printf("%lld\n",ans[0]);
else
{
k=gcd(n,k);
printf("%lld\n",ans[k]);
}
}
return 0;
}