 # Xenny and Coin Rankings Wrong Answer.Why?

#include
using namespace std;
int main()
{
int t;
long long int u,v,s,i,j;
cin>>t;
while(t–)
{i,j=0;
cin>>u>>v;
s=u+v;
j=(s*s+s)/2;
while(i<=u)
{j++;
i++;}
cout<<j;
if(t!=0)
cout<<"\n";
}
return 0;
}

What are you trying to do? Your code fails on the sample case itself, so i dont think you want test cases atm. It is a simple Q, but it needs some maths to do.

You need to know how to calculate Nth term of series where difference between terms is in AP. The answer can simply be stated as -

`````` ans= 1+ v*(v-1)/2 + (v+u-1)*(v+u)/2 - v*(v+1)/2;
``````

Will post derivation if you want, but to be honest, its a good Q for learning, so i recommend you look at google for method and then think yourself first. Your task should be how to derive the answer. If you’re able to think it now, you can do that in future contests as well.

I can give you 2 hints-

Click to view

The rank at corner of rectangle which is opposite to origin, is the highest for all cases. Now you just need to some formula to calculate that rank

Click to view

The standard steps for calculating rank along X are-

``````Series =1,3,6,10....Tn (Tn is the Nth term)

Let S be the sum of series
==>S=1+3+6+10...+Tn........Eq 1.
==>S=0+1+3+6+...+T(n-1)+Tn........Eq 2.

Subtracting Eq2 from Eq1-

0=1+2+3+4...(Tn-T[n-1])-Tn
==>Tn = n(n+1)/2.

Now, for answer we have to follow same step in Y direction. The procedure is same EXCEPT we are starting from n(n+1)/2 instead of 1.
``````
1 Like
//