CodeTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Problem Descriptionwld likes playing with codes. One day and 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;
}
Inputthere is multiple Cases. (At most ten )
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) .
Outputfor each case:
Print an integer,denoting what the function returns.
Sample Input
51 3 4) 2 4
Sample Output
Hintgcd (x, y) means the greatest common divisor of x and Y.
Original: http://www.cnblogs.com/JoeFan/p/4458629.html
Test Instructions: gives the number of N, gcd (A[i], a[j]) * GCD (A[i], A[j]-1) and (1 <= i, j <= N).
Analysis: First, we analyze the impact of each number on the final answer. then we ask for: For each number, how many pairs it has for GCD. Obviously, for a number x, the two number that is gcd to it must be a multiple of x. If the multiples of X have K in the sequence, then the GCD with a maximum of k^2 logarithm is x.
It is also obvious that for two numbers, if they are multiples of x, then their gcd must also be multiples of x.
So, we find that multiples of X have K in the sequence, then there are k^2 logarithm satisfies two number is a multiple of x, this k^2 logarithmic gcd, either x, or 2x, 3x, 4x ...
Also, a number is a multiple of x, which must be a multiple of x. So a number pair of gcd in multiples of x must be included in this k^2 logarithm.
If we enumerate x from the large to the small, so that the contribution of X is computed, the multiple multiples of x are already exhausted. We use f (x) to denote the number of pairs with X as GCD.
then f (x) = K^2-f (2x)-F (3x)-f (4x) ... f (TX) (TX <= 10000, k = cnt[x])
This enumerates each x, then enumerates multiples of each x, and the complexity is calculated with a harmonic progression, about O (n logn).
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm>using namespace std; const int MAXN = 1e4 + 10;const int Mod = 10007;int CNT[MAXN], F[maxn];int main () {int n, A; while (~SCANF ("%d", &n)) {memset (CNT, 0, sizeof (CNT)); for (int i = 0; i < n; i++) {scanf ("%d", &a); for (int j = 1; J * J <= A; j + +) {if (a% J = = 0) {cnt[j]++; if (J * j! = a) cnt[a/j]++; }}} int ans = 0; for (int i = 10000; I >= 1; i--) {f[i] = cnt[i] * Cnt[i]% Mod; for (int j = i * 2; J <= 10000; j + = i) f[i] = (F[i]-f[j] + MoD)% MoD; int p = i * (i-1)% Mod; Ans = (ans + p * f[i]% mod)% MoD; } printf ("%d\n", ans); } return 0;}
HDU 5212 Code (repulsion or Momo inversion)