UVA 10820 Send a Table euler_phi function

Source: Internet
Author: User


Except for the other foreign x, y does not equal

For X<y case Everything Y has phi (y) combination

F[x]= Phi (i) 2<=i<=x

The result is 2*f[x]+1

Problem A
Send a Table

Input: Standard Input

Output: Standard Output

When participating in programming contests, you sometimes face the following problem:you know what to calcutale the output For the given input values, but your algorithm is-too slow to ever pass the time limit. However hard to try, you just can ' t discover the proper break-off conditions that would bring down the number of iteratio NS to within acceptable limits.

Now if the range of input values are not too big, there is a-on-the-out of the. Let your PC rattle for half an hour and produce a table of answers for all possible input values, encode this table into a program, submit it to the judge, et voila:accepted in 0.000 seconds! (Some would argue is cheating, but remember:in love and programming contests everything are permitted).

Faced with the problem during one programming contest, Jimmy decided to apply such a ' technique '. But however hard he tried, he wasn ' t able to squeeze all his pre-calculated values into a program small enough to pass the Judge. The situation looked hopeless, until he discovered the following property regarding the Answers:the answers where Calcula Ted from-integers, but whenever the input values had a common factor, the answer could is easily derived from the Answer for which the input values were divided by that factor. To put it in other words:

Say Jimmy had to calculate a function Answer (x, y) where x and y is both integers in the range [1, N]. When he knows Answer (x, y), he can easily derive Answer (K*x, K*y), where K was any integer from it by applying some simple Calculations involving Answer (x, y) and K. For example if n=4, he is needs to know the answers for one out of the possible input value Combinations:answer (1, 1) , Answer (1, 2), Answer (2, 1), Answer (1, 3), Answer (2, 3), Answer (3, 2), Answer (3, 1), Answer (1, 4), Answer (3, 4), Answer (4 , 3) and Answer (4, 1). The other 5 can is derived from them (Answer (2, 2), Answer (3, 3) and Answer (4, 4) from Answer (1, 1), Answer (2, 4) from Ans Wer (1, 2), and Answer (4, 2) from Answer (2, 1)). Note that the function Answer are not symmetric, so Answer (3, 2) can isn't be derived from Answer (2, 3).

Now "What we want" is:for any values of N from 1 upto and including 50000, give the number of function Jimmy have To Pre-calculate.

Inputthe input file contains at the most lines of inputs. Each line contains an integer less than 50001 which indicates the value of N. Input was terminated by a line which contains A zero. This is should not being processed.

Output

For each line of input produce one line of output. This line contains an integer which indicates how many values Jimmy have to pre-calculate for a certain value of N.

Sample input Output for sample input

2

5

0

        

3

19

Problem Setter:little Joey

Special Thanks:shahriar Manzoor



#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn=55000;long Long int phi[maxn];void phi_table () {    phi[1]=1ll;    for (int i=2;i<maxn;i++)    {        if (!phi[i])        {for            (int j=i;j<maxn;j+=i)            {                if (!phi[j]) phi[ J]=j;                phi[j]=phi[j]/i* (i-1);    }}} for (int i=3;i<maxn;i++)    {        phi[i]+=phi[i-1];    }} int N;int Main () {    phi_table ();    while (scanf ("%d", &n)!=eof&&n)    {        if (n==1)        {            puts ("1"); continue;        }        printf ("%lld\n", 1+phi[n]*2);    }    return 0;}



Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

UVA 10820 Send a Table euler_phi function

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.