显示代码纯文本
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
using namespace std;
int nc,mc;
ull n[200005];
ull ans[100002];
int p[100002];
ull pfh;
//bool cmp(ull x,ull y)
//{
// return x>y;
//}
int gcd(int x,int y)
{
while(x)
{
if(x<y) swap(x,y);
x%=y;
}
return y;
}
int main()
{
freopen("noi_online2020_ring.in","r",stdin);
freopen("noi_online2020_ring.out","w",stdout);
cin>>nc>>mc;
for(int i=1;i<=nc;i++)
{
scanf("%llu",&n[i]);
pfh+=(n[i]*n[i]);
}
sort(n+1,n+1+nc);
int s1;
int s2;
int s3;
int s4;
ull l1,l2;
for(int i1=1;i1<=mc;i1++)
{
scanf("%d",&s1);
// cout<<endl;
if(s1==0)
{
cout<<pfh<<endl;
continue;
}
s2=gcd(nc,s1);
if(p[s2])
{
printf("%llu\n",ans[s2]);
continue;
}
// cout<<"ans:"<<ans<<endl;
for(int i2=1;i2<=s2;i2++)
{
s3=nc/s2;
s4=nc-(s3*i2);
l1=n[s4+s3];
l2=n[s4+s3-1];
ans[s2]+=(l1*l2);
s3-=2;
while(s3)
{
if(l1<l2) swap(l1,l2);
ans[s2]+=(l1*n[s3+s4]);
l1=n[s3+s4];
s3--;
}
if(nc/s2!=2) ans[s2]+=(l2*n[1+s4]);
// cout<<"i2:"<<ans<<endl;
}
if(nc/s2==2) ans[s2]<<=1;
printf("%llu\n",ans[s2]);
p[s2]=1;
}
return 0;
}
//98108
//89450
//76939
//69222
//67045
//54982
//42661
//29015
//21448
//6403
//1/
//2
//3/
//4
//5/
//6
//7/
//8
//9/
//10