#include
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll comb(ll n, ll r)
{ ll i,p,q,m,x,y;
ll res=1;
for(i=1;i<=r;i++)
{ p=n-i+1;q=i;
m=__gcd(p,q);
x=p/m;
y=q/m;
res=res*x;
res=res/y;
}
return res;
}
int main() {
ll t;
cin>>t;
while(t–)
{
ll n,k,p,res,x,y;
cin>>n>>k;
p=n-k;
x=p+k-1;
y=k-1;
if(y>x/2)
{
y=x-y;
}
res=comb(x,y);
cout<<res<<endl;
}
return 0;
}
This question is directly taken from SPOJ . My solution is getting accepted there in SPOJ finally after a lot of optimisations, But my code is still not working here. Sometimes it shows “session not judged”, or simply wrong answer in 4 out of 5 test cases. Cant really figure it out.