Tuesday, April 16, 2019

CHEFST: Chef and the stones

CHEFST: Chef and the stones

Link for the problem: CHEFST

SOLUTION:

For a given max possible no of stones that can be removed from each pile is m+(m-1)+(m-2)+...3+2+1. Which is m*(m+1)/2.  Let it be S. So we can see that between 1 and S any no of stones can be removed.

CODE:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long int n1,n2,m,n,s;
        cin>>n1>>n2>>m;
        s=(m*(m+1))/2;
        n=min(n1,n2);
        if(s>n)
        printf("%lld\n",max(n1,n2)-n);
        else
        {
            printf("%lld\n",n1+n2-2*s);
        }
    }
    return 0;

}

No comments:

Post a Comment

CHEFST: Chef and the stones

CHEFST: Chef and the stones Link for the problem:  CHEFST SOLUTION: For a given max possible no of stones that can be removed from ea...