Code HDU 5212

Source: Internet
Author: User


Code
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 202 Accepted Submission (s): 81


Problem Description
WLD 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;

}


Input
There is multiple Cases. (at the 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).


Output
For each case:

Print an integer,denoting what the function returns.


Sample Input

5
1 3 4) 2 4



Sample Output

64

Hint
GCD (x, y) means the greatest common divisor of x and Y.


Links: http://www.cnblogs.com/JoeFan/p/4458629.html

1003 Code

This problem requires some knowledge of the inverse and linear sieve of the mo.
Define f (x) =x? (x?1)
The title is Sigma (f (gcd (Ai,aj) |i!=j,1≤i,j≤n)
The value of Miu's function in [1,10000] is first obtained by using a linear sieve
Using the inverse formula of the VLOGV we can solve the function g in [1,10000] by brute force, where G satisfies:
Sigma (g (d) |x
The answer to this is:
Sigma (g (d) CNT (d) CNT (d) |1≤d≤10000), wherein the CNT function satisfies:
CNT (x) = the number of multiples of x in A1,a2,.., an
The value of CNT can also be calculated by O (VLOGV) violence
So the total time complexity is O (VLOGV)

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 <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <set > #include <map> #include <vector> #include <stack> #include <queue> #include <string># include<cstdlib> #include <cmath>//#include <bits/stdc++.h>using namespace Std;template<class T    >inline T Read (t&x) {char C;    while ((C=getchar ()) <=32) if (c==eof) return 0;    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} //-------ZCC IO Template------const int MAXN=1E4+10;CONST double inf=99999999; #define Lson (rt<<1), L,m#define Rson ( rt<<1|1), M+1,r#define M ((l+r) >>1) #define for (i,t,n) for (int i= (t);i< (n); i++) typedef long Long LL; typedef double DB;TYPEDEF pair<int,int> P; #define BUG printf ("---\ n"); #define MOD 10007int yinzhi[maxn];int F[MAXN    ];int Main () {int n;        while (read (n)) {memset (yinzhi,0,sizeof (Yinzhi));            for (i,0,n) {int A;            Read (a);                    for (int j=1; j*j<=a; J + +) {if (a%j==0) {yinzhi[j]++;                if (j*j!=a) yinzhi[a/j]++;        }}} int ans=0;            for (int i=10000; i>=1; i--) {f[i]= (yinzhi[i]*yinzhi[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;    } writeln (ANS); } return 0;} /*612 5 4 6265 6 45*/



Code HDU 5212

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.