I am working on this code from 3 days from a website called Codechef, the code compiles, executes and even give correct results but my code is taking 1.01 sec, but the time limit for question is 1sec, I have tried various ways but neither of them is giving a result that can work within 1 sec.
The question is about finding n-th smallest palindrome number, say n=9, then another k-th smallest palindrome number, say k=13, then I have to do this :- [(nth)^(nth+1)]* [(nth)^(nth+2)].......[(nth) ^(kth)], where nth+1 refers to 10th smallest palindrome number, nth+2 refers to 11th smallest palindrome number and so on , {T, L and R} are the inputs , L and R are less than or equal to 5(10^3), as the output will be very large so the judge wants the answer as output modulo 10^9+7 , link of the question - https://www.codechef.com/JULY21C/problems/CHEFORA
#include <stdio.h>
int main(void) {
int T;
scanf("%d",&T);
while(T--){
int arr[5001]={0};
long int product=1;
int j=0,big=1000000007;
int L,R,sumR=0,sum2=0;
scanf("%d %d",&L,&R);
int n,p,k,h;
for(n=1;n<10;n++){
arr[j++]=n;
if(sumR==R-1) break;
sumR++;
}
for(n=1;n<10;n++){
for(p=0;p<10;p++){
arr[j++]=n*100+p*10+n;
if(sumR==R-1) break;
sumR++;
}if(sumR==R-1) break;
}
for(n=1;n<10;n++){
for(p=0;p<10;p++){
for(k=0;k<10;k++){
arr[j++]=n*10000+p*1000+k*100+p*10+n;
if(sumR==R-1) break;
sumR++;
}if(sumR==R-1) break;
}if(sumR==R-1) break;
}
for(n=1;n<10;n++){
for(p=0;p<10;p++){
for(k=0;k<10;k++){
for(h=0;h<10;h++){
arr[j++]=n*1000000+p*100000+k*10000+h*1000+k*100+p*10+n;
if(sumR==R-1) break;
sumR++;
}if(sumR==R-1) break;
}if(sumR==R-1) break;
}if(sumR==R-1) break;
}
for(j=L+1;j<=R;j++)
sum2+=arr[j-1];
for(j=1;j<=sum2;j++)
product=(product*arr[L-1])%big;
printf("%ld\n",product);
}
return 0;
}