TLE for Sub task 2 (ALATE)

locaug17
tle

#1

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**a*)%MOD;
		sum=(sum+(a**a*))%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*);
		for(j=1;j<=q;j++)
		{
			scanf("%lld",&qno);
			if(qno==1)
			{
				scanf("%lld",&x);
				printf("%lld
",func(x));				
			}
			else
			{
				scanf("%lld%lld",&x,&y);
				a[x]=y;
			}		
		}
	}
    return 0;
}

#2

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


#3

Can you please be more elaborate? If possible can you share your code with comments if you’ve done it.


#4

Hey @ramini,

To solve this question, ALATE from August Loc you need to do some precomputation so as to fit the answer in specified time-limit.

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.

for(i=1;i<=n;i++)
{

s=0;

for(j=i;j<=n;j+=i){

  s = ( s + (arr**arr*) % 1000000007) % 1000000007 ;

}

sum* = s ;

}

Since you have all the pre-computed 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 pre-computed) so whenever a query of 2nd type occurs than you have to iterate over the values of sum* which are going to be affected and update their new values which are sum*-arr**arr*+newValue *newValue;

EDIT:- First we will pre-compute which are going to affected by the index x.

vector v[100001];
for(ii=1;ii<100001;ii++) {
for(jj=ii;jj<100001;jj+=ii)
v[jj].push_back(ii); }

After that we just need to iterate over the affected values and update them as they are stored in v[x]

for(i=0;i<v[x].size();i++)
{

          sum[v[x]*] = (sum[v[x]*] - sq[x] +

nsq+1000000007) %1000000007 ;

         }

where nsq is new square value.

Hope this helps!

(


[1])


  [1]: https://www.codechef.com/viewsolution/15155591

#5

Can you please explain how did you update the values of sum*?


#6

I understood finally. Thank you so so so much!!! You made me learn something.


#7

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.

#include bits/stdc++.h
#define MOD 1000000007
#define ll long long
using namespace std;
ll a[100001];
ll sum[100001]={0};
ll n;

ll s[100001][100001]={0};

void precompute()
{	
	for(ll i=1;i<=n;i++)
	{
		for(ll j=i;j<=n;j+=i)
		{
			sum*=(sum*+(a[j]*a[j])%MOD)%MOD;
		}
	}	
}
void effect()
{
	for(ll i=1;i<=100000;i++)
	{
		for(ll j=i;j<=100000;j+=i)
		{
			s*[j]=i;
		}
	}
}
int main()
{
	ll t;
	scanf("%lld",&t);
	effect();
	while(t--)
	{
		ll q,i,j,nsq,qno,x,y;
		scanf("%lld%lld",&n,&q);
		for(i=1;i<=n;i++)
		scanf("%lld",&a*);
		precompute();
		while(q--)
		{
			scanf("%lld",&qno);
			if(qno==1)
			{
				scanf("%lld",&x);
				printf("%lld
",sum[x]);
			}
			else if(qno==2)
			{
				scanf("%lld%lld",&x,&y);
				nsq=(y*y)%MOD;
				j=x;
				for(i=1;i<=n;i++)
				{
					if(s*[j]!=0)
					{
						sum[(s*[j])]=(sum[(s*[j])]-((a[x]*a[x])%MOD)+nsq)%MOD;
					}
				}
				a[x]=y;
			}
		}		
	}
	return 0;
}


#8

@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 !


#9

Hey @sandeep_007, I still get tle even subtask1 , can you look at it. ideone


#10

Hey @we7d,

Your solution goes wrong when you are taking input in %d while it should be %lld as ‘a’ is long long.

for(int
i=1;i<=n;i++){scanf("%d",a+i);a* =
a**a* %MOD;sum*=0;}

Hope this helps!


#11

#include
#include
#define mod 1000000007
#define sc scanf
#define ll long long int

using namespace std;
 
void update(ll A[],ll ar[],ll index,ll val)
{
	ll t=(val*val-ar[index]*ar[index]+3*mod)%mod;
	A[index]=(A[index]+t+3*mod)%mod;
	for(ll i=1;index!=1 && i<=sqrt(index);++i)
	{
		if(index%i==0)
		A*=(A*+t)%mod;
	}		
}
 
int main()
{
	int t;  sc("%d",&t);
	while(t--)
	{
		ll no,Q;  sc("%lld %lld",&no,&Q);
		ll ar[no+1]={0},A[no+1]={0};
		
		for(ll i=1;i<=no;++i)
		{
			sc("%lld",&ar*);
		//	ar*=(ar**ar*)%mod;
		//	A*=ar*;
			A*=(ar**ar*)%mod;
			for(ll j=1;i!=1 && j<=sqrt(i);++j)
			{
				if(i%j==0)
				A[j]=(A*+A[j])%mod;
			}
		}
/*		
		for(int i=1;i<=no;++i)
		cout<<ar*<<" ";
		cout<<endl;
		for(int i=1;i<=no;++i)
		cout<<A*<<" ";
		cout<<endl;		
*/		
		while(Q--)
		{
			ll c,x;  ll y;
			sc("%lld",&c);
			if(c==2)
			{
				sc("%lld %lld",&x,&y);
				update(A,ar,x,y);
				ar[x]=y;
			}
			else
			{
				sc("%lld",&x);
				printf("%lld

",A[x]%mod);

			}
		}
		
	}
	
	return 0;
} 

Why my answer is wrong??? plz help…


#12

If not clear, please have a look at the code.if after that also your doubt is not clear… comment below.


#13

updated the answer.


#14

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.


#15

if you aren’t familiar with vector, follow the tutorials on hackerrank.