Problem>[ALATE]: https://www.codechef.com/LOCAUG17/problems/ALATE Please give suggestions to improve my code so that it passes Sub task 2 without TLE(Please help me) #include bits/stdc++.h
#define ll long long
#define MOD 1000000007
#define N 1000005
using namespace std;
ll a[N],n;
ll func(ll x)
{
ll k,sum=0;
for(ll i=x;i<=n;i+=x)
{
k=(a[i]*a[i])%MOD;
sum=(sum+(a[i]*a[i]))%MOD;
sum%=MOD;
}
return sum;
}
int main()
{
ll t,q,i,j,qno,x,y;
scanf("%lld",&t);
while(t)
{
scanf("%lld%lld",&n,&q);
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(j=1;j<=q;j++)
{
scanf("%lld",&qno);
if(qno==1)
{
scanf("%lld",&x);
printf("%lld\n",func(x));
}
else
{
scanf("%lld%lld",&x,&y);
a[x]=y;
}
}
}
return 0;
}
asked 29 Aug '17, 00:31

Hey @ramini, To solve this question, ALATE from August Loc you need to do some precomputation so as to fit the answer in specified timelimit. Since "func" calculates the sum of a given index x and all it's multiple in the given array(squares). So we can precompute all the values for x.
Since you have all the precomputed values than your query of type 1 can be answered in O(1) but the problem arises in the update query for that you need to know which values are actually going to be affected by the change of that particular x. (this is also precomputed) so whenever a query of 2nd type occurs than you have to iterate over the values of sum[i] which are going to be affected and update their new values which are sum[i]arr[i]arr[i]+newValue newValue; EDIT: First we will precompute which are going to affected by the index x.
After that we just need to iterate over the affected values and update them as they are stored in v[x]
where nsq is new square value. Hope this helps! (Code) answered 29 Aug '17, 01:38
If not clear, please have a look at the code.if after that also your doubt is not clear.. comment below.
(29 Aug '17, 01:44)

first store all the divisor of 1 to 100000 using sieve .make a dp to store all the func(x) value for all indexes.for query no 2 update all the indexes which divides the x by iterating through all the divisors of x. hope this helps answered 29 Aug '17, 01:13

Can you please be more elaborate? If possible can you share your code with comments if you've done it. answered 29 Aug '17, 01:32

Can you please explain how did you update the values of sum[i]? answered 29 Aug '17, 01:57

I understood finally. Thank you so so so much!!! You made me learn something. answered 29 Aug '17, 02:19

Hey @sandeep_007 I wrote the code as you said. But I am getting an error as shown in Ideone. What's wrong in it?Please help bro.
answered 29 Aug '17, 12:29
Hey @ramini it was great to hear from you again, you just did a small mistake, an array of that much size cannot be initialized so you have to make a vector.
(29 Aug '17, 12:37)
if you aren't familiar with vector, follow the tutorials on hackerrank.
(29 Aug '17, 12:38)

@sandeep_007 it will be helpful for me if you check my solution ! even i had updated for query 2 https://www.codechef.com/viewsolution/15134001 i had thought over it for hours! plz ! answered 29 Aug '17, 12:42

Hey @sandeep_007, I still get tle even subtask1 , can you look at it. ideone answered 29 Aug '17, 20:13

Why my answer is wrong???? plz help... answered 01 Sep '17, 02:49
