全加和 UVa10943

1.题目描述:点击打开链接

2.解题思路:本题实际上就是求x1+x2+...+xk=n的非负整数解的个数。根据组合数学的结论,答案是C(n+k-1,k-1)。可以事先预处理算出所有的组合数。

3.代码:

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<long long, long long> PL;
#define me(s) memset(s,0,sizeof(s))
#define For(i,n) for(int i=0;i<(n);i++)
const int MOD = 1000000;
int n, k;
#define N 205
ll C[N][N];

void init()
{
	me(C);
	C[0][0] = 1;
	for (int i = 1; i < 200; i++)
	{
		C[i][0] = C[i][i] = 1;
		for (int j = 1; j < i; j++)
			C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
	}
}
int main()
{
	//freopen("t.txt", "r", stdin);
	init();
	while (~scanf("%d%d", &n, &k))
	{
		if (!n&&!k)break;
		int ans = C[n + k - 1][k - 1];
		printf("%d\n", ans);
	}
	return 0;
}

你可能感兴趣的:(计数,组合数)