http://www.codechef.com/problems/NUKES
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.
#include <stdio.h>
#include <math.h>
int k;
int N;
/* if [A - (N + 1) ^ (p + 1) < 0], then pth chamber will have one particle and new value of A i.e. A' will be [A - (N + 1) ^ p] and the same procedure will be applied to get the new value of p. This process will continue till A* will be less than N + 1 */
void recurse(int a[], int A)
{
if (A < N + 1) {
a[0] = A;
return;
}
int p = 0;
int A2 = 0;
while (A - pow(N + 1, p + 1) >= 0)
++p;
if (p < k) /* there are only k chambers, rest will go to waste */
++a[p];
A2 = A - pow(N + 1, p);
recurse(a, A2);
}
main()
{
unsigned long int A;
scanf("%lu %d %d", &A, &N, &k);
int a[k];
int j;
for (j = 0; j < k; ++j)
a[j] = 0;
recurse(a, A);
for (j = 0; j < k; ++j)
if (j == k - 1)
printf("%d\n", a[j]);
else
printf("%d ", a[j]);
return 0;
}
I'm getting this error :
http://www.codechef.com/submit/complete/52434-4579--506325bda1ce2