• Loading...

Problem 1: Coprimes

Points:

Small Case: 10 Answer: 1232

Large Case: 20 Answer: 4027392

A function ,f(n) of a positive integer n is defined to be the number of positive integers less than or equal to n that are coprime to n (i.e. having no common positive factors other than 1).

For example, f(9)=6 since the six numbers 1, 2, 4, 5, 7 and 8 are coprime to 9.

Find the value of f(n) for

Small Case: n=2468

Large Case: n=12345678

Solution:

f(n) is euler totient function.

f(n) is given by

where p are prime factors of n.


#include<iostream>

#include<cstdlib>

#include<cstdio>

#include<vector>

#include<map>

#include<cmath>

#define ll long long int

#define MAX 255800ll

using namespace std;

bool ar[MAX]={0};

ll pr[MAX]={0},len=0;

ll func(ll n)

{

		ll ans=n;

        ll y=(ll)sqrt(n+1);

        for(ll i=0,x=0;pr[i]<=y && i<len && pr[i]<=n;i++)

        {

            if(n%pr[i]==0)

            {

                x=pr[i];

                ans=(ans*(x-1))/x;

                while(n%x==0)

                    n/=x;

            }

        }

        if(n>1)

            ans=(ans*(n-1))/n;

        //printf("%lld\n",ans);

		return ans;

}

int main()

{

    ll i,j,n,ans=0,y;

    for(i=2;i<=MAX;i++)

        if(ar[i]==0)

        {

            pr[len++]=i;

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

                ar[j]=1;

        }

    scanf("%lld",&n);

    ans=func(n);

    printf("%lld\n",ans);

    return 0;

}

Submit your solution

You need to be logged in to submit a solution.

discussions


There is no discussion yet. Feel free to ask an question you might have regarding this problem


post a question

Loading...

Problem 1
Problem 2
Problem 3
Problem 4
Problem 5 (bonus) Expired
Problem 1
Problem 2
Problem 3
Problem 4 (bonus) Expired
Top Scorers
rng_58 235
Anton_Lunyov 225
tourist 190
Silence 180
cedricl2 175
cgy4ever 175
DrKorbin 165
Koen 155
bbbasic 150
lunae_2 150
Submissions
Problem 1
10 pt
not submitted
98
submissions

19
correct (19 %)



25 pt
not submitted
2
submissions

1
correct (50 %)


Problem 2
5 pt
not submitted
95
submissions

38
correct (40 %)



10 pt
not submitted
19
submissions

15
correct (78 %)


Problem 3
5 pt
not submitted
153
submissions

117
correct (76 %)



15 pt
not submitted
44
submissions

25
correct (56 %)


Problem 4
10 pt
not submitted
152
submissions

54
correct (35 %)



15 pt
not submitted
23
submissions

10
correct (43 %)


Problem 5
10 pt
not submitted
36
submissions

28
correct (77 %)



20 pt
not submitted
6
submissions

2
correct (33 %)


Problem 6
5 pt
not submitted
41
submissions

29
correct (70 %)



10 pt
not submitted
15
submissions

14
correct (93 %)


Problem 7
5 pt
not submitted
38
submissions

17
correct (44 %)



10 pt
not submitted
16
submissions

15
correct (93 %)


Problem 8
5 pt
not submitted
127
submissions

104
correct (81 %)



15 pt
not submitted
47
submissions

34
correct (72 %)


Problem 9
5 pt
not submitted
86
submissions

35
correct (40 %)



15 pt
not submitted
16
submissions

15
correct (93 %)


Problem 10
5 pt
not submitted
45
submissions

23
correct (51 %)



15 pt
not submitted
15
submissions

14
correct (93 %)


Problem 11
5 pt
not submitted
81
submissions

27
correct (33 %)



15 pt
not submitted
8
submissions

3
correct (37 %)


Problem 12
5 pt
not submitted
104
submissions

88
correct (84 %)



10 pt
not submitted
31
submissions

21
correct (67 %)