Click here to Skip to main content
14,924,103 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Objective-C
<pre lang="c++">
Problem : The Vita Sum

Tom the cat is brushing up his Math skills. He has a bag containing N balls of different colors. Now Tom can randomly pick any even number of balls from the bag. Tom wants to find out the sum of all such combinations of balls that he can pull out from the bag. Given he can pull out at max K balls in one pick.



Input Format:

First line contains two space separated numbers N and K



Output Format:

The output is the sum of all the combinations of balls he can pull out modulo 10^9+7 i.e. (1000000007)



Constraints:



1.0<=N,k<=10^9
2.N >= k




Sample Input and Output



SNo.

Input

Output

Explaination

1
4 4

8

We need 4C0 + 4C2+ 4C4= 1+6+1=8

2
8 3

29

We need 8C0 + 8C2= 1+28=29

What I have tried:

i tried solve the loop for the above problem.
Posted
Updated 28-Jul-18 7:13am
Comments
jeron1 29-Jul-16 14:07pm
   
"i tried solve the loop for the above problem." OK, post what you have tried, and ask a specific question.
phoenix@6714 29-Jul-16 15:01pm
   
how can i see the solution?
jeron1 29-Jul-16 15:25pm
   
Post what you have tried.
Richard MacCutchan 30-Jul-16 5:49am
   
Forget about programming for a moment, and use a piece of paper to write down the steps needed to solve the equations.
Richard MacCutchan 6-Jul-17 12:45pm
   
See my message above from last year.
Patrice T 7-Jul-17 3:11am
   
After 1 year, you still have nothing to show, no specific problem ?

Quote:
Need a C or cpp programming code for the below problem
No we don't do your HomeWork.
Ask your teacher/tutor for help, he is here for this.
You will never learn programming by asking others to do your work. Reading documentation, following tutos and practicing are the only means to learn programming.
   
Comments
phoenix@6714 29-Jul-16 15:01pm
   
how can i see the solution
C#
#include<stdio.h>
int fast_pow(long long base, long long n,long long M)
{
    if(n==0)
       return 1;
    if(n==1)
    return base;
    long long halfn=fast_pow(base,n/2,M);
    if(n%2==0)
        return ( halfn * halfn ) % M;
    else
        return ( ( ( halfn * halfn ) % M ) * base ) % M;
}
int findMMI_fermat(int n,int M)
{
    return fast_pow(n,M-2,M);
}
int main()
{
    long long fact[100001];
    fact[0]=1;
    int i=1;
    int MOD=1000000007;
    while(i<=100000)
    {
        fact[i]=(fact[i-1]*i)%MOD;
        i++;
    }
    
        int n,k;
        
        scanf("%d%d",&n,&k);
        if(k%2!=0)
        {
        	k=k-1;
		}
		
         long long numerator,denominator,mmi_denominator,ans=0;
        for(i=0;i<=k;i=i+2)
       
        //I declared these variable as long long so that there is no need to use explicit typecasting
     {   numerator=fact[n];
        denominator=(fact[i]*fact[n-i])%MOD;
        mmi_denominator=findMMI_fermat(denominator,MOD);
        ans=ans+(numerator*mmi_denominator)%MOD;
        
    }
    printf("%lld\n",ans);
    return 0;
}
   
v2
Comments
Richard MacCutchan 30-Jul-16 7:53am
   
You do not help people by doing their homework for them.
Lawliet_ 30-Jul-16 8:03am
   
This is not a homework question.It is a question asked in codevita 2016 (yesterday this competition was happen).
Richard MacCutchan 30-Jul-16 8:29am
   
The point remains; doing people's work for them does not help them learn.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900