< Back to forum

sumq , codechef

question link: https://www.codechef.com/problems/SUMQ

answer link: https://www.codechef.com/viewsolution/17942109

I am pretty much sure, that my code is correct, still it is giving wrong answer.

The logic which i have used is, for a particular y, sum= (X(j) + Y) * (Z(k) +Y), where j or k can range between (0 and p) & (0 and r) repectively. Then i have expanded the sum calculating X(j)*Z(k) , Y{ X(j) + Z(k) } , Y^2 separately. Still the code is not running :'( :'( 

can u point out the mistake..??

Asked by: Samrat_De on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




1 Answer(s)

avatar
for(i=0;i<q;i++)
		{	unsigned long long int p=1;
			for(j=count_a;j<p&&b[i]>=a[j];j++)
			{	sum_a=(sum_a+a[j])%m;
				count_a++;
				size_a++;
			}

......

p is the size of the array A, but you are using local variable whose name is also p, in the above loop,since the priority is given to local variable, hence the value of p will become 1, thats why your loop for array A runs only once;

Second thing is that the approach you are using will get TLE, since p,q and r are order of 10^5, so your approach won't work.

Hint : Use prefix sum and binary search to make your code optimised.

Raghav_Grover last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.