PROSUM - Editorial

try to replace long int i,count,a,c; with long long int i,count,a,c;

But your description is different to what is your last submission - CodeChef: Practical coding for everyone

Please edit your code or add a link to submission (hopefully with better formatting).

Did you noticed that n*n will lead to int overflow for n up to 10^5 ?

Sorry to have mislead you. What I have done is I calculated the 1’s and 0’s in c. Then I reduced n(the total no. of elements) by c.So,now n is the total no. of elements >=2. And count is the number of 2’s. Then i applied the formula n*(n-1)/2 - count*(count-1)/2. Also, replacing by long long int didn’t work.

Maybe I did something wrong, but your last submission is not working at all - bvezTB - Online C++ Compiler & Debugging Tool - Ideone.com

there was scanf("%ld",&a);, I replaced it with scanf("%lld",&a);

Ok. The above got AC.But why would they need to be declared as long long int. Wouldn’t they get converted automatically when n is formulated ?

declare the variables as unsigned long long .Here is the link to the corrected code
http://www.codechef.com/viewsolution/3619806

1 Like

problem is in two*x

well two * x is correct , answer is coming from that . here two denotes count of 2 and x denotes count of numbers greater than 2 . So he is two * x counts the combination of 2’s with numbers greater than 2 . then he adds x*(x-1) to this for other combinations of numbers greater than 2 . So i think that is correct .

got it thanks for help …
Missed a question in challenge.

@rishabh1994: I refered to two * x for technical problem - int overflow, not logical bug… @pardeep_gill was aware of that problem (((long long)x*(x-1)), but forgot to handle it in all places see here - ywYmic - Online C++ Compiler & Debugging Tool - Ideone.com also using long long for variables is correct answer, my comment was an addition - another possible approach…

what is wrong with this code??? Its giving wrong answer…

#include<stdio.h>
void main()
{
int t;
long long int s,n=0,ctr=0,ctr1=0,tot=0;
scanf("%d",&t);
while(t–)
{
scanf("%lld",&s);
ctr=0;ctr1=0;
while(s–)
{
scanf("%lld",&n);
if(n>2)
ctr++;
else if(n==2)
ctr1++;
}
tot=ctr*(ctr1+((ctr-1)/2));
printf("%lld\n",tot);
}
}

Thanks for the solution…But I have seen that formula in almost every solution…can you please tell me what is wrong with my solution or can you please provide the test case where my solution fails?

Your logic has some minor mistake, because for
Input :
1
5
1000000 0 9999 100 10 1000000
Output:
6

But your code gives answer as 3, which is wrong. So, better check for extreme limits of given constraints.

Its always better to check for constraints of the problem, before writing the code ! :slight_smile:

Thanks for the help but for your given test case my above code is also giving answer as 6!

Try using scanf().

declare the variables as unsigned long long .Here is the link to the corrected code CodeChef: Practical coding for everyone . This may help you.

Finally! Thanks vikascingh…After using scanf() my code was accepted immediately…here is the link…but What was the problem with getchar_unlocked()?