Problem Description
WLD likes playing with codes.One day he is writing a function.Howerver,his computer breaks down because the function is too powerful.He is very sad.Can you help him?
The function:
int calc
{
int res=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
res+=gcd(a[i],a[j])*(gcd(a[i],a[j])-1);
res%=10007;
}
return res;
}
The function:
int calc
{
int res=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
res+=gcd(a[i],a[j])*(gcd(a[i],a[j])-1);
res%=10007;
}
return res;
}
Input
There are Multiple Cases.(At MOST
10
)
For each case:
The first line contains an integer N(1≤N≤10000) .
The next line contains N integers a1,a2,...,aN(1≤ai≤10000) .
For each case:
The first line contains an integer N(1≤N≤10000) .
The next line contains N integers a1,a2,...,aN(1≤ai≤10000) .
Output
For each case:
Print an integer,denoting what the function returns.
Print an integer,denoting what the function returns.
Sample Input
5 1 3 4 2 4
Sample Output
64Hintgcd(x,y) means the greatest common divisor of x and y.
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int d[10005];
int num[10005];
long long mod=10007;
long long yin[10005];
int mu[10005],prime[10005],tot;
bool check[10005];
int mx;
void getmu()
{
memset(check,0,sizeof(check));
mu[1]=1,tot=0;
for(int i=2;i<=mx;i++){
if(!check[i])
prime[tot++]=i,mu[i]=-1;
for(int j=0;j<tot;j++){
if(i*prime[j]>mx)
break;
check[i*prime[j]]=true;
if(i%prime[j]==0){
mu[i*prime[j]]=0;
break;
}
else
mu[i*prime[j]]=-mu[i];
}
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(yin,0,sizeof(yin));
memset(num,0,sizeof(num));
mx=0;
for(int i=0;i<n;i++)
{
scanf("%d",&d[i]);
mx=max(mx,d[i]);
num[d[i]]++;
}
for(int i=1;i<=mx;i++)
{
for(int j=i;j<=mx;j+=i)
{
yin[i]+=num[j];
}
}
for(int i=1;i<=mx;i++)
yin[i]=yin[i]*yin[i];
getmu();
long long ans=0;
for(int i=1;i<=mx;i++)
{
long long u=0;
for(int j=i;j<=mx;j+=i)
{
u=(u+yin[j]*mu[j/i]%mod)%mod;
}
ans+=u*i%mod*(i-1)%mod;
}
ans=(ans+mod)%mod;
printf("%I64d\n",ans);
}
return 0;
}