MARBLES - Editorial

Explanation
We think this question as, we put some border lines to marbles as we group them with their colors, so we create k marble group (so we need k-1 lines).

So this question will be a permutation question. We can first pick k marbles and then we need to pick n-k marbles + (k-1) border lines. So we need to order total n-1 pieces, k-1 of them and n-k of them are equal each other.

So we must calculate (n-1)! / ((k-1)!*(n-k)!) which is equal to C(n-1,k-1) which is combination.

C++ Solution

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	
	for(int i=1;i<=t;i++){
	    long long int n,k;
	    cin>>n>>k;
	    
	    long long int sum =1;
	    
	    for(long long int i=1;i<k;i++)
	    {
	        sum = sum*((n-k)+i)/i;
	    }
	    cout<<sum<<endl;
	}
	return 0;
}

Python Solution

import math
# cook your dish here
t = int(input())
while t>0:
    n,k = map(int,input().split())
    print(math.comb(n-1,k-1))
    t -= 1

Java Solution

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner s=new Scanner(System.in);
		int t=s.nextInt();
		while(t-->0)
		{
		    int n=s.nextInt();
		    int k=s.nextInt();
		    long a=1;
		    for(long i=1;i<k;i++)
		    {
		        a=a*((n-k)+i)/i;
		    }
		    System.out.println(a);
		}
	}
}

How does this Editorial thing works?
I’ve think sometimes in problems, but I don’t know how this works nor who is allowed to make this posts?