The number of divisor and the algorithm (quality factor and Euler function decomposition) according to the requirement

Source: Internet
Author: User

DescriptionOne day, qz met an easy problem. but after a 5-hout-long  contest in csu, he became very tired and he wanted to  call his girlfriend as soon as possible. As we all  know, qz is a genius in plenty of fields especially in  programming. but he did not want to write a program to  Solve this problem, he just want to call his girlfriend. now  he gave this problem to you, and he want you to  Solve this problem without programming. fortunately, yh heard about  qz ' S bad behavior, and he thought that not everyone is  as clever aS qz. finally, yh managed to persuade qz to ask you to  write a program to solve this problem. the problem is:Qz would give you only one number N (1<= n <= 10^9), and he wants to know the answer of a sub B, A and B is as F Ollow:A is sum of series of Numbers X (He-cares about X which is no larger than N). For each X, which exists an integer k satisfies that k * X = N.B is sum of the series of Numbers Y (He only cares about Y which is no larger than N). For each Y satisfies that GCD (Y, N) = 1.YH whispers to you GCD (x, Y) means the greatest Common Divisor of X and Y, and your should not let Qz know that YH Have told you the secret.Qz is so hurry so he had no time to give you the N one by one and he'll give you multiple cases.YH is so kind and he ask Qz this for eachinput Filethe number of N is about 30000.InputQz is so hurry to call his girlfriend so he have no time to explain the Input.OutputHelp Qz-to-output the answer to problems.Sample Input
1234
Sample Output
0213


Test instructions: Give a number n, calculate the approximate number of N and subtract no more than N and the numbers of N coprime, recorded as a-B.

There were only violent thoughts at the scene, but 10^9 timed out. Later listen to the topic of the explanation, said the use of Euler functions, was not very clear at the time.

Until today to see a nature: The above B has a formula, b= (N*F (n))/2.

F (n) represents the Euler function of N. (the relevant proof can be found in the number theory book).

The problem is converted to approximate n and. Euler function of N. All need to use decomposition factorization. The n is decomposed into N=p1^a1*p2^a2*...*pn^an.

The approximate of n and the equation of S (n), S (n) are no longer mentioned.


A general idea has been made. First, the prime table of square root n is constructed. Since factorization does not exceed the square root of N, then it is the process of decomposing factorization.

This topic can be used as a template for constraint and with n coprime and not greater than N.

#include <stdio.h> #include <math.h>typedef unsigned long long LL; #define MAXN 200000LL Primet[maxn];int pnum = 0;            LL n,a,b,res;bool flag[maxn];void getprimtable ()//sieve method to find the prime number {for (Long i = 2; i < MAXN; i++) if (!flag[i]) {            primet[pnum++] = i;        for (Long j = i + i; j < maxn; J + = i) flag[j] = true;    }}ll FF (ll x, ll y) {ll ret=1;    while (y--) ret*=x; return ret;}    ll EULARPK (ll p, int k)//Euler seek p^k{if (k = = 0) return 1;    LL ans = p-1;    while (--k) ans *= p; return ans;}    ll F (ll N)//seek F factorization {ll sum = 1;    Res=1;    int p, k;        for (int i = 0; primet[i]*primet[i]<= N; i++) {p = primet[i];            if (n% p = = 0) {k = 0;            while (n% p = = 0) {n/= p; k++;}  sum = sum * EULARPK (P, k);        P^k res*= ((FF (p,k+1)-1)/(p-1));        }} if (n > 1) {///decomposition to the last remaining element factor, special attention should be paid.         sum = sum * EULARPK (n, 1);    Res*= ((FF (n,2)-1)/(n-1)); } RETUrn sum;}    int main () {getprimtable ();        while (~SCANF ("%lld", &n)) {LL b=n*f (n)/2;        if (n = = 1) printf ("0\n");    else printf ("%lld\n", res-b); } return 0;}




The number of divisor and the algorithm (quality factor and Euler function decomposition) according to the requirement

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.