需要针对以下问题的C或cpp编程代码 [英] Need a C or cpp programming code for the below problem

查看:99
本文介绍了需要针对以下问题的C或cpp编程代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre lang="c++">

问题:Vita Sum



汤姆猫正在刷他的数学技能。他有一个包含不同颜色N球的包。现在,汤姆可以从包中随机挑选任意数量的球。汤姆想要找出他可以从包中取出的所有这些球组合的总和。鉴于他可以在一个选秀权中拿出最多K球。







输入格式:



第一行包含两个空格分隔的数字N和K







输出格式:



输出是他可以拔出模数的所有组合的总和10 ^ 9 + 7即(1000000007)







限制条件:







1.0< = N,k< = 10 ^ 9

2.N> = k









样本输入和输出







SNo。



输入



输出



解释



1

4 4



8



我们需要4C0 + 4C2 + 4C4 = 1 + 6 + 1 = 8



2

8 3



29



我们需要8C0 + 8C2 = 1 + 28 = 29



我有什么ried:



i尝试解决上述问题的循环。

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.

推荐答案

引用:

需要针对以下问题的C或cpp编程代码

Need a C or cpp programming code for the below problem

不,我们不做你的HomeWork。

请你的老师/导师寻求帮助,他就在这里。

你永远不会通过让别人去做你的工作来学习编程。阅读文档,遵循tutos和练习是学习编程的唯一方法。

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.


#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;
}


这篇关于需要针对以下问题的C或cpp编程代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆