I’m getting time limit exceeded in my code… Please some one help me out with this code.

My algorithm is when A - ((N+1) ^ (p+1)) < 0 then pth chamber will have 1 particle and the new value of A in my recursive function is A - ((N+1) ^ p).

This process will continue till A < N+1, then k[0] will be A.

Please see this link(I’m having problem placing code here) :Daniweb discussion forum

why do you have to use recursion for this problem…It can be easily solved without recursion…

it’s all about finding remainder when particles bombarded(A) is divided by particles that can be present inside a chamber(N)…

it is done until (A!=0).

Here’s a non-recursive code… it is also getting time limit exceeded in “test 8; Approx_N_value 100”

and it is running well for “test 1 to 7; Approx_N_value 10 to 1000000000”

#include <stdio.h>

#include <math.h>

main()

{

long int A;

int N, k;

scanf("%lu %d %d", &A, &N, &k);

int a[k]; /* stores no. of particles in a chamber */

int j;

long int power[101];

for (j = 0; j < k; ++j)

a[j] = 0;

```
for (j = 0; A - (power[j] = (long)pow(N+1, j)) >= 0 && j < 101; ++j)
;
while (A != 0) {
j = 0;
while (A - power[j] >= 0)
++j;
a[j-1] = A/power[j-1];
A = A % power[j-1];
}
for (j = 0; j < k; ++j)
if (j == k - 1)
printf("%d\n", a[j]);
else
printf("%d ", a[j]);
return 0;
```

}

develop a non-recursive code without the use array…read carefully what i have posted earlier …

not using array can easily pass the time limit problem…and do be carefully abt test case when n=0;

Ok i got your previous comment and had seen the code for it.

But, can Admin tell me for which test case my latest code exceeds time limit ?

sorry to disappoint you but the admin cannot tell in which test case your codes exceeds time limit…

You need to work out yourself to find out that…

It is a problem of converting base of numbers. After converting the number to the respective base select K bits.

link to learn base conversion

http://planetmath.org/BaseConversion.html

I didn’t get you properly.

Do you meant converting from double value(from math library) to long value is creating problem ?

I have made a separate function for pow and tried. It still didn’t worked.

#include<stdio.h>

int main()

{

int a,k,n;

```
scanf("%d%d%d",&a,&n,&k);
while(k--)
{
printf("%d ",a%(n+1));
a/=(n+1);
}
return 0;
```

}

here is the code. enjoy

Thanks for the code. But, I need to find out fault in my code.