Money Distributions | Practice | GeeksforGeeks

Find the number of ways in which N coins can be distributed among K pupils such that each pupil receives at least one coin each. Two distributions are said to be different only if they have at least one pupil who got a different number of coins.
 
Example 1:
Input: N = 7, K = 3
Output: 15
Explanation: 
Coin distribution can be any of the following 15 ways. 
{1,1,5}, {1,5,1}, {5,1,1}, {2,1,4}, {1,2,4}, {1,4,2}
{2,4,1}, {4,1,2}, {4,2,1}, {3,1,3}, {1,3,3}, {3,3,1}
{2,2,3}, {2,3,2}, {3,2,2}



Example 2:
Input: N = 5, K = 5
Output: 1
Explanation: {1, 1, 1, 1, 1} is the only way.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function totalWays() which takes N and K as input parameters and returns the number of possible distributions modulo 10^9+7.
 
Expected Time Complexity: O(N)
Expected Space Complexity: O(N)
 
Constraints:
1 <= N, K <= 100000
-1
sarkarrohit6505 months ago
Java Solution : 
Approach : modulo inverse  , nCr
public long power(long a,long b , long mod){
        
        long ans = 1L;
        while(b>0){
            if((b&1)!=0 ){
                ans = (ans*a)%mod;
            }
            a = (a*a)%mod;
            b>>=1;
        }
        return ans;
    }
    
    public int nCr(int N,int R){
        
        long res= 1L;
        //long deno=1L;
        if(R > N-R){
            R = N-R;
        }
        long mod =(long) 1e9+7;
        for(int i=0;i<R;i++){
            
            res = (res *(N-i))%mod;
            res= res*power(i+1 , mod-2 ,mod)%mod;
            
        }
        
        return (int)res;
    }
    public int totalWays(int N, int K)
    {
        if(K > N)return 0;
        if(K==N)return 1;
        
        return nCr(N-1,K-1);
        
    }
+5
sunnysaraff5 months ago
Explanation:- 
(1) Sum of K persons coins = N
(2) K1 + K2 + ...   Kk = N where Ki≥0 has solution nCr(N+K-1,K-1) according to standard principles of partitioning in Binomial Theorem (You must  have read about this in your lower classes)
(3) Here, we have to do some modification since above formula works when every Ki≥0 but in our problem Ki≥1 So, Substitute, Ki - 1 = Ui
(4) Transformed Eqn will be:- Sum of Ui = N - K
(5) Now, Apply the above formula on this modified Equation.
(6) Answer = nCr(N-1,K-1)
+1
gowthamreddyuppunuri5 months ago
int64_t power(int64_t a, int64_t b,int mod){
	    int64_t res = 1;
	    while(b>0){
	        if(b%2 != 0){
	            res = (res*a)%mod;
	        }
	        b /= 2;
	        a = (a*a)%mod;
	    }
	    return res;
	}
    int totalWays(int N, int K) {
        // Code here.
        if(K>N){
            return 0;//no possible ways
        }
        // C(N+K-1,K-1);// if atleast 0 is can be given
        // C(N+K-1-K,K-1); // if atleast 1 to be given
        //i.e C(N-1,K-1)
        int n = N-1;
        int r = K-1;
        r = min(r,n-r);
        int mod = 1e9+7;
        int64_t ans = 1;
        for(int i=0,j=n; i<r; i++,j--){
            ans = (ans*j)%mod;
            // ans = (ans/(i+1));
            ans = ans*power(i+1,mod-2,mod)%mod;
            //completey perform modular division and multiplication
            //don't do partially
        }
        return ans;
    }
+3
geminicode5 months ago
This is working in Python !!! Donno if it's expected in the question.
import math
class Solution:
	def totalWays(self, n,k):
		# Code here
		# we need to find n-1 C k-1
		if n<k :
		    return 0
		den = (math.factorial(k-1) * math.factorial(n-k))
        ans = math.factorial(n-1) // den
        return ans%1000000007
+1
mastermind_5 months ago
(C++) Combinatorics Solution,    answer  =        ,   ~ O(32*N) Time , O(1) Space.
const int mod = 1e9 + 7;
int64_t expo(int64_t a, int64_t b) {
    int64_t ans = 1;
    while (b) {
        if (b & 1)(ans *= a) %= mod;
        (a *= a) %= mod;
        b >>= 1;
    }
    return ans;
}
int totalWays(int n, int k) {
    if (n < k)return 0;
    int64_t ans = 1;
    for (int i = n - k + 1; i <= n - 1; i++)(ans *= i) %= mod;
    for (int i = 1; i <= k - 1; i++)(ans *= expo(i, mod - 2)) %= mod;
    return ans;
}
-3
asifalimnstu2 years ago
asifalimnstu
0.02 sec c++https://practice.geeksforge...
0
Tourist ka Dost2 years ago
Tourist ka Dost
Getting memory error, while trying to solve in python. I'm getting correct answers in custom input and Compile &  test. I think it's because of Python 32-bit installation. Please help!
-3
Ashish4 years ago
Ashish
do we have to print the cases as well? If yes, then how?
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.