UVA 10820 Send a Table

Source: Internet
Author: User

Original question:
When participating in programming contests, your sometimes face the following problem:you know
How to Calcutale the output for the given input values, but your algorithm was the 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 iterations 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 to a program,
Submit it to the judge, et voila:accepted in 0.000 seconds! (Some would argue that this is cheating,
But Remember:in love and programming contests everything is 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 calculated from and 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 the 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 be derived
From them (Answer (2, 2), Answer (3, 3) and Answer (4, 4) from Answer (1, 1), Answer (2, 4) from
Answer (1, 2), and Answer (4, 2) from Answer (2, 1)). Note that the function Answer are not symmetric,
So Answer (3, 2) can is derived from Answer (2, 3).
Now-what are we want do is:for any values of N from 1 upto and including 50000, give the
Number of function Jimmy have to pre-calculate.
Input
The input file contains at the most lines of inputs. Each line contains a integer less than 50001 which
Indicates the value of N. Input is terminated by a line which contains a zero. This line should isn't be
Processed.
Output
For each line of input produce one line of output. This line contains a integer which indicates how
Many values Jimmy have to pre-calculate for a certain value of N.
Sample Input
2
5
0
Sample Output
3
19
Effect:
Tell you now to give you a function f (x, Y) that lets you hit the table. However, this function has a property, that is, according to F (x, y) can calculate F (kx,ky), K is any integer, so with f (x, y), F (kx,ky) will not be saved. Now give you a number n, ask how many data you need to save, such as n=2 when you save 3 ((), (2,1)

#include <bits/stdc++.h>
using namespace std;

FStream in,out;

int f[50001];
int phi[50001];
int main ()
{
    Ios::sync_with_stdio (false);  In.open ("Data.txt");  Out.open ("Input.txt");
    for (int i=2;i<=50000;i++)
        phi[i]=0;
    Phi[1]=1;
    F[1]=1;
    for (int i=2;i<=50000;i++)//euler
    {
        if (!phi[i]) for
            (int j=i;j<=50000;j+=i)
            {
                if (! PHI[J])
                    phi[j]=j;
                phi[j]=phi[j]/i* (i-1);
            }
    }
    for (int i=2;i<=50000;i++)
        f[i]=f[i-1]+phi[i]*2;
    int n;
    while (Cin>>n,n)
    {
        cout<<f[n]<<endl;
    }  in.close ();  out.close ();
    return 0;
}

Answer:
Stuck on a different question, found a simple topic to dine. The result found the problem, haha = =.

The topic has a strong recursion, so start with the relationship between F[n] and f[n-1].
Draw a two-dimensional table of 1 to N

 (1,3) .... (1,n-1)
(1,n)                               (2,1).
.                               .
.                               .
.
(n-1,1)            (n,1) ..... ..... (n,n) It is known that the table is symmetrical, and the nth column (x, y) is actually observed (X,n), and X is from 1 to N. If x and n are not coprime, then a number k will make xk=x,nk=n. So just find the number of 1 to N and N not coprime, that is, the Euler function of N, plus f[n-1] is the answer. 

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.