Here is my C solution for Sherlock and Queries @ Hackerrank.
Watson gives to Sherlock an array: \$A_1, A_2, ..., A_N\$. He also gives to Sherlock two other arrays: \$B_1, B_2, ..., B_M\$ and \$C_1, C_2, ..., C_M\$.
Then Watson asks Sherlock to perform the following program:
for i = 1 to M do for j = 1 to N do if j % B[i] == 0 then A[j] = A[j] * C[i] endif end do end do
Can you help Sherlock and tell him the resulting array
A
? You should print all the array elements modulo \$(10^9 + 7)\$.Input Format
The first line contains two integer numbers \$N\$ and \$M\$. The next line contains \$N\$ integers, the elements of array
A
. The next two lines contains \$M\$ integers each, the elements of arrayB
andC
.Output Format
Print \$N\$ integers, the elements of array
A
after performing the program modulo \$(10^9 + 7)\$.Constraints
\$1 ≤ N, M ≤ 10^5\$
\$1 ≤ B[i] ≤ N\$
\$1 ≤ A[i], C[i] ≤ 10^5\$Sample Input
4 3 1 2 3 4 1 2 3 13 29 71
Sample Output
13 754 2769 1508
How should I optimize it as it is showing teminated due to time out?
#include<stdio.h>
int main()
{
int n,m,i,j;
scanf("%d %d ",&n,&m);
long int a[n+1],b[m+1],c[m+1];
for(i=1;i<=n;i++)
scanf("%ld",&a[i]);
for(i=1;i<=m;i++)
scanf("%ld",&b[i]);
for(i=1;i<=m;i++)
scanf("%ld",&c[i]);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(j%b[i]==0)
{
a[j]=(a[j]*c[i])%1000000007;
}
}
}
for(i=1;i<=n;i++)
printf("%ld ",a[i]%1000000007);
return 0;
}