大二第二次月赛--买水果

阅读 43

2022-07-29


                                                 买水果


时间限制:1000 ms  |  内存限制:65535


       今天zz突然想吃水果了,然后他到超市去买水果,现超市有n个不同的水果,zz要买m个水果

(m<=n);那么zz有多少种买水果的方式??


输入 输入T组数据(T<=100)

然后输入T行,每行输入两个数n,m(0< n < 2^31);

输出 每一行输出一个数(保证在0~2^31); 样例输入

2
4 2
3 3

样例输出

6
1

此题巨坑,求组合数公式的应用,

数据量给的大,一不小心就超时。

思路:注意每次计算一次就除一次。

参考代码:

<span style="font-family:Microsoft YaHei;">#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<deque>
#include<ctime>
#include<queue>
#include<stack>
#include<string>
using namespace std;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
LL zhu_he(LL a,LL b)
{
b=a-b<b?a-b:b;
LL ans=1;
for(int i=1; i<=b; i++)
{
ans=ans*(a-i+1);
ans=ans/i;
}
return ans;
}
int main()
{
int t;
LL n,m;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&m);
// if(m==0||n<m)
// printf("1\n");
// else
printf("%lld\n",zhu_he(n,m));
}
return 0;
} </span>





精彩评论(0)

0 0 举报