What is wrong with my code ? Kindly help me
The Question
Given an integer N. Integers A and B are chosen randomly in the range
[1..N]. Calculate the probability that the Greatest Common Divisor(GCD) of
A and B equals to B. Input
The first line of the input contains an integer T denoting the number of
test cases. The description of T test cases follows. Each test case
consists of a single integer N on a separate line. Output
For each test case, output a single line containing probability as an
irreducible fraction. Example
Input: 3 1 2 3
Output: 1/1 3/4 5/9 Constraints
1<=T<=103
1<=N<=109
My code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 40960
long int favCases( long int N )
{
int i;
long int favNumberOfCases=N,sum=0;
if( N<10000 )
{
for ( i = 2; i <= N; i++ )
{
favNumberOfCases = favNumberOfCases + N/i;
}
return favNumberOfCases;
}
else
{
for ( i = 2; i <= N / 10000; i++ )
{
favNumberOfCases = favNumberOfCases + N/i;
}
for ( i = 1; i <= 9999; i++ )
{
sum = sum + ( N / i - N / ( i + 1 ) ) * i;
}
return favNumberOfCases+sum;
}
}
long int gcd(long int a, long int b)
{
long int r;
while( b != 0 )
{
r = a % b;
a = b;
b = r;
}
return a;
}
int main( int argc, char *argv[] )
{
char buffer[SIZE];
int i = 0, c, count = 0;
long int N = 0;
int T;
long int favCases( long int N );
scanf( "%d", &T );
scanf( "%ld", &N );
while ( ( c = fread( buffer, sizeof (char), SIZE, stdin ) ) > 0 )
{
for ( i = 0; i < c; i++ )
{
if ( buffer[i] == '\n' )
{
if( count == T ) break;
printf( "%ld/%ld\n",
( favCases(N) / gcd( favCases(N), N * N ) ),
( N * N ) / gcd( favCases(N), N * N )
);
N = 0;
count++;
}
else
{
N = ( N * 10 ) + ( buffer[i] - '0' );
}
}
if( count == T ) break;
}
return (0);
}
The code is giving correct outputs and is compiling quite quickly but
still codechef is saying that my answer is wrong ? I have being trying to
figure out the error but can't find one. I have checked for corner cases
etc. Kindly help me.
No comments:
Post a Comment