here is only basic implementation of problems for beginners. If you have any problem with any solution or any basic concept of programming or you want more efficient solution you can mail me.
my suggestion is not to copy and paste codes from here try to understand the logic and think why you were not able to solve it.

Wednesday 31 December 2014

Playing with GCD

problem statement is here


#include<stdio.h>
long long ar[100004];
void etf(){
     long long k,i,z;
     ar[0]=0;
     for(k=1;k<100001;k++){
      long long n=k;
      long long r=n;
        for(i=2;i*i<=n;i++){ 
          if (n%i==0) 
          r-=r/i; 
          while(n%i==0) 
          n/=i; 
       } 
       if (n>1)
        r-=r / n; 
       z=k-r;
       ar[k]=ar[k-1]+z;
   } 
}
int main(){
    long long t,num,c=1;
    etf();
    scanf("%lld",&t);
    while(t--){
        scanf("%lld",&num);
        printf("Case %lld: %lld\n",c,ar[num]);
        c++;
    }
    return 0;
}

No comments:

Post a Comment