UVa 11417 GCD (Euler φ function)

Source: Internet
Author: User
Tags gcd greatest common divisor time limit

11417-gcd

Time limit:2.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=2412

Given the value of N, you'll have to find the value of G. The definition of G is given below:

Here GCD (i,j) means the greatest common divisor of integer i and integer J.

For those who have trouble understanding summation notation, the meaning of G. Given in the following code:

g=0;

for (i=1;i<n;i++)

for (j=i+1;j<=n;j++)

{

G+=GCD (I,J);

}

/*here GCD () is a function that finds the greatest common divisor of the two input numbers*/

Input

The input file contains at most lines of inputs. Each line contains an integer N (1<n<501). The meaning of N is given in the problem statement.  The Input is terminated by a line containing a single zero. This zero should is processed.

Output

For each line of the input produce one line of output. This line contains the value of G for corresponding N.

Sample input Output for sample input

10
100
500
0
67
13015
442011

How to ask

Thought: Can be directly calculated, complexity O (n^2 logn), but we can find a less complex algorithm O (N loglogn)

In 10 as an example, the reciprocal element has φ (10) = 4 (1,3,7,9), with which the gcd=2 has φ (10/2) = 4 (2,4,6,8), with which the gcd=5 has φ (10/5) = 1 (5)

Thus, the G value provided by 10 is 5*φ (2) +2*φ (5) +φ (10) =5+8+4=17

According to the above calculation process, the following formula can be obtained:

(square brackets mean Iverson convention, when the statement in square brackets is true when the value is 1, false time is 0, see "Specific mathematics" P21)

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Complete code:

/*0.013s*/
    
#include <cstdio>  
const int MAXN = 501;  
    
int PHI[MAXN], G[MAXN];  
    
void init ()  
{  
    int i, J;  
    for (i = 2; i < MAXN ++i)  
        phi[i] = i;  
    for (i = 2; i < MAXN. ++i)  
    {  
        if (phi[i) = i) for  
            (j = i; J < maxn; j = i)  
                phi[j] = phi[j]/I * (i-1);///compute the Euler φ function for  
        (j = 1; J * I < MAXN; ++j)  
            g[j * I] + + J * Phi[i];  
    }  
    for (i = 3; i < MAXN ++i)  
        g[i] + = g[i-1];  
}  
    
int main ()  
{  
    init ();  
    int n;  
    while (scanf ("%d", &n), N)  
        printf ("%d\n", G[n));  
    return 0;  
}

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.