数学问题当中的一些基本计数问题
一些基本的计数方法
三大原理: 加法原理、乘法原理、容斥原理
容斥原理:奇数个集合为正,偶数个集合为负
有重复元素的全排列。有k个元素,其中第i个有ni个,求全排列的的个数
(n1+n2+......+nk)!/(n1! n2! ......nk!)
可重复选择的组合。有n个不同的元素,每个元素可以选多次,一共选K个,有多少 种方法
C(n+k-1,n-1)=C(n+k-1,k)
1.加法原理的应用:
UVA11538链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2533
解题思路:
以水平为例,放白后有nm种,放黑后有(m-1)种
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
long long n,m;
while(cin>>n>>m)
{
if(n==0&&m==0) break;
if(n>m) swap(n,m);
cout<<n*m*(m-1)+n*m*(n-1)+2*n*(n-1)*(3*m-n-1)/3<<endl;
}
return 0;
}
2.加法问题的一类变形
UVA11401 链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2396
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1000000+10;
long long f[maxn];
int main()
{
f[3]=0;
for(long long x=4;x<=1000000;x++)
f[x]=f[x-1]+((x-1)*(x-2)/2-(x-1)/2)/2;
int n;
while(cin>>n)
{
if(n<3) break;
cout<<f[n]<<endl;
}
return 0;
}

浙公网安备 33010602011771号