Problem:
Euler's Phi Function

For given integer n, count the totatives of n, that is, the positive integers less than or equal to n that are relatively prime to n.

Input

n

An integer n (1 ≤ n ≤ 1000000000).

Output

The number of totatives in a line.

Sample Input 1

6

Sample Output 1

2

Sample Input 2

1000000

Sample Output 2

400000

Solution in C++ (judge verdict: Time Limit Exceeded)

#include<stdio.h>
#include<stdlib.h>

int pr[50847534];
int fact[5000];
int x=0;
int n,pn;
int cnt = 0;
short *arr;

int nextpr(int p){
  int nxp=p+2;
  int i=0;
  int ok=1;
  pr[x]=p;
  x++;
  if(p==2)return 3;
  while(1){
    for(i=0;pr[i]*pr[i]<=nxp;i++){
      ok = 1;
      if(nxp%pr[i]==0){
        ok = 0;
        break;
      }
    }
    if(ok){
      return nxp;
    }else{
      nxp+=2;
    }
  }
}

int main(){
  int n2;
  int p=2,done=1,done2=1,prod,ans=0;
  int i,j,k;
  pn = 0;
  scanf("%d",&n);
  n2 = n;
  while(n>1){
    if(n%2==0){
      while(n%2==0)n /= 2;
      fact[pn]=2;
      pn++;
      done = 0;
    }else if(n%p==0){
      while(n%p==0)n /= p;
      fact[pn]=p;
      pn++;
      done = 0;
    }else{
      p = nextpr(p);
    }
    if(p*p>n2&&done){
      break;
    }
  }
  /*printf("facts:");
  for(i=0;i<pn;i++){
      printf(" %d",fact[i]);
  }
  printf("\n");*/
  arr = (short *)calloc(pn,sizeof(short));
    for(i=0;i<pn;i++)arr[i]=0;
    while(1){
        /*for(i=0;i<pn;i++)printf(" %d",arr[i]);*/
        prod = 1;
        for(j=0;j<pn;j++){
            if(arr[j]){
                prod*=fact[j];
                prod*=-1;
            }
        }
        ans += (n2/prod);
        /*printf(" prod=%d ans=%d\n",prod,ans);*/
        /*????????????arr????¬???????????????????????????????*/
        arr[0]+=1;
        for(k=0;k<pn;k++){
            if(arr[k]>1){
                if(k==pn-1)goto BREAK;
                arr[k]=0;
                arr[k+1]+=1;
            }
        }/*????????§arr????¬???????????????????????????????*/
    }
BREAK:
  printf("%d\n",ans);
}