Ultraviolet A 10299-relatives

Source: Internet
Author: User

Question: calculate an Euler's function of a given number (1 ~ N-1 number of n-1 and N mutual quality ).

Analysis: number theory, prime screening method, Euler's function.

Euler's function: Phi (n) = N * (1-1/P1) * (1-1/P2) * (1-1/P3 )*... * (1-1/Pt );

Here, we use the screening method to calculate the prime number within 50000, because the data range is within 1000000000,

Therefore, the number that cannot be divisible by the prime number in the first 50000 must also be a prime number, and each number N contains at most one;

Computing output.

Note: uva10179.

#include <iostream>#include <cstdlib>using namespace std;int fac[30];int prim[50000];int used[50000];int main(){for (int i = 0 ; i < 50000 ; ++ i)used[i] = 0;int save = 0;for (int i = 2 ; i < 50000 ; ++ i)if (!used[i]) {prim[save ++] = i;for (int j = 2*i ; j < 50000 ; j += i)used[j] = 1;}int n;while (cin >> n && n) {int count = 0,base = 0,m = n;while (n > 1 && base < save) {if (n%prim[base] == 0) {fac[count ++] = prim[base];while (n%prim[base] == 0)n /= prim[base];}base ++;}if (n > 1) fac[count ++] = n;long long ans = m;for (int i = 0 ; i < count ; ++ i)ans = ans/fac[i]*(fac[i]-1);if (n == 1) ans = 0LL;cout << ans << endl;}return 0;}


Ultraviolet A 10299-relatives

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.