- 组合数问题
- 2017-09-22 00:02:30 @
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
int yanghui[2005][2005],n,m,k,t,count=0;
memset(yanghui,0,sizeof(yanghui));
yanghui[1][1]=1;
for(int i=2;i<=2000;i++)//初始化杨辉三角
{
for(int j=1;j<=i;j++)
{
yanghui[i][j]=yanghui[i-1][j]+yanghui[i-1][j-1];
}
}
cin>>t>>k;//首行输入
for(int i=1;i<=t;i++)
{
cin>>n>>m;
for(int p=0;p<=n;p++){
for(int q=0;q<=min(m,p);q++){
if(yanghui[p+2][q+2]%k==0) count++;//求组合数
}
}
printf("%d",count);
count=0;
}
return 0;
}
5 条评论
-
孙鹤鸣 (sunheming) LV 10 @ 2020-03-02 15:05:37
Var c: array[0..2000, 0..2000] of longint; a: array[0..2000, 0..2000] of longint; t, k, m, n, i, j: integer; Begin //初始化 read(t, k); for i:= 0 to 2000 do for j:= 0 to 2000 do c[i, j]:= -1; //构建杨辉三角形 for i:= 0 to 2000 do begin c[i, 0]:= 1; c[i, i]:= 1; end; for i:= 1 to 2000 do for j:= 1 to (i-1) do c[i, j]:= (c[i-1, j-1] + c[i-1, j]) mod k; //避免超范围 //处理杨辉三角形 for i:= 0 to 2000 do for j:= 0 to 2000 do if c[i, j] = 0 then c[i, j]:= 1 //如果整除则为1 else c[i, j]:= 0; //前缀和 for j:= 0 to 2000 do a[0, j]:= 0; for i:= 1 to 2000 do for j:= 0 to 2000 do a[i, j]:= a[i-1, j] + c[i, j]; for j:= 1 to 2000 do for i:= 0 to 2000 do a[i, j]:= a[i, j] + a[i, j-1]; //IO for i:= 1 to t do begin read(n, m); writeln(a[n, m]); end; End.
-
2017-11-10 08:29:35@
函数开在外面==而且没*取模*
-
2017-11-09 21:53:30@
数组太大建议开在主函数外,不然很容易炸,我一开始也这样
-
2017-10-22 10:41:37@
是不是数组需要开main外面?
我把数组开在外面就没有runtime error了. -
2017-09-22 00:02:52@
QAQ萌新给虐惨了
- 1
信息
- ID
- 2006
- 难度
- 6
- 分类
- (无)
- 标签
- 递交数
- 1821
- 已通过
- 502
- 通过率
- 28%
- 被复制
- 8
- 上传者