Product of divisors - TLE


Hi…I am a newbie at codechef…so i was practicing an easy program D1 given in the codechef site for calculating product of divisors…but the online judge keeps giving me the error “time limit exceeded”…can you help me in optimising the problem?

using namespace std;
int main()
    unsigned long N, t=1;
    int i;
    unsigned long *a=new unsigned long [N];
    unsigned long *b=new unsigned long [N];
    for(i=0; i<N; i++)
    scanf ("%d", a+i);

    for (int j=0; j<N; j++)
        for(int k=1; k<a[j]; k++)
			if (a[j]%k==0)

	for(int y=0; y<N;y++)
		printf("%.4d\n", b[y]);

	delete []a; delete []b;

You have to realize that your code has O(MAXT*MAXN) complexity, so for the boundaries from statement (MAXT = 300.000 and MAXN = 500.000) it’s too much…

there is a tutorial for this question:

~look at the variable constraints, it will give you a good idea of what kind of complexity you will need, and it will save you time from coding algorithms exceeding this complexity ^^

1 Like