I am getting TLE here...probably bcoz i am using init function again and again ...
Is there a more efficient way to intiliaze arr[1000001] back to all zeros.
Problem code:
spoj.pl/problems/COMDIV/
#include<stdio.h>
#include<math.h>
#include<string.h>
#define MAX 1000001
int ca[MAX];
void init(int*arr){
int i=0;
for(i=0;i<MAX;i++) arr[i]=0;
}
void findiv1(int k)
{
int x=0;
int i;int rt=sqrt(k);
ca[k]=1;
if(k==1){ca[k]=1; return;}
for(i=1;i<=rt;i++)
{
if(k % i ==0)
{
ca[i]=1;
x=k/i;
if((x!=i)&&(x<=k/2))
ca[x]=1;
}
}
}
int findiv2(int k)
{
int x=0;int count=0;
int i;int rt=sqrt(k);
if(ca[k]==1) count++;
if(k==1){if(ca[k]==1) return 1;}
for(i=1;i<=rt;i++)
{
if(k % i ==0)
{
if(ca[i]==1) count++;
x=k/i;
if((x!=i)&&(x<=k/2))
if(ca[x]==1) count++;
}
}
return count;
}
int main()
{
int tc,a,b;
scanf("%d",&tc);
while(tc--){
scanf("%d%d",&a,&b);
findiv1(a);
printf("%d",findiv2(b));
init(ca);
}
return 0;
}