CodeChef submission 136785 (C) plaintext list. Status: TLE, problem SNCK04, contest SNACKDWN. By 3 (3), 2009-11-21 23:57:12.
#include<stdio.h> #include<stdlib.h> int gcd(int a,int b); int my_gcd(int a, int b) { int q,r,t; /* Normalize so that 0 < a <= b */ if((a == 0)||(b == 0)) return -1; if(a < 0) a = -a; if(b < 0) b = -b; if(b < a){ t = b; b = a; a = t; } /* Now a <= b and both >= 1. */ q = b/a; r = b - a*q; if(r == 0) return a; return my_gcd(a,r); } int phi(int n) { if(n<0)n=-n; /* handle a few trivial boundary cases */ if(n<=1)return 0; if(n==2)return 1; if(n==3)return 2; return phiphi(n,2); } /* This only gets called with y >= 3 and y > x >= 2 */ int phiphi(int y, int x) { int z; if(x+1 == y)return x; /* phi(prime p) = p-1 */ if((y%x)==0){ if(my_gcd(x,z=y/x)==1) return phi(x)*phi(z); /* multiplicative property */ else return x*phi(z); /* This is a tricky case. It may happen when x is a prime such that a power of x divides y. In case y = p^n, phi(y) = p^(n-1)(p-1) */ } else return phiphi(y,x+1); } int main() { int i,n,ans,k; int tc; int j,rem,m; while(tc--) { for(i=2,ans=n-1;i<=n;i++) { k=phi(i); // printf("phi returns %d\n",k); if(n>i) m=(n/i)-1; else m=0; ans+=m*k; rem=n%i; for(j=i*(m+1),rem+=j;j<=rem;j++) { if(gcd(n,j)==1) ans++; } // printf("ans is %d\n",ans); } } return 0; } int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); }
Comments

