Code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 456 Accepted Submission(s): 182
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;
}
Input
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).
Output
For each case:
Print an integer,denoting what the function returns.
Sample Input
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MAX 10007
#define MOD 10007
using namespace std;
int f[MAX];
int n,a;
int cnt[MAX];
int main ( )
{
while ( ~scanf ( "%d" , &n ) )
{
memset ( cnt , 0 , sizeof ( cnt ) );
for ( int i = 1 ; i <= n ; i++ )
{
scanf ( "%d" , &a );
for ( int j = 1 ; j*j <= a ; j++ )
if ( a%j == 0 )
{
cnt[j]++;
if ( a/j != j )
cnt[a/j]++;
}
}
int ans = 0;
for ( int x = 10000 ; x > 0 ; x-- )
{
f[x] = cnt[x]*cnt[x]%MOD;
for ( int i = x*2 ; i <= 10000 ; i += x )
f[x] = ( f[x] - f[i] + MOD) %MOD;
int p = x*(x-1)%MOD;
ans = ( ans + p*f[x]%MOD )%MOD;
}
printf ( "%d\n" , ans );
}
return 0;
}