2, combination

2.1 combination formula

1. C(m,n)=C(m,m-n)
2. C(m,n)=C(m-1,n)+C(m-1,n-1)
derangement D(n) = n!(1 - 1/1! + 1/2! - 1/3! + ... + (-1)^n/n!)
= (n-1)(D(n-2) - D(n-1))
Q(n) = D(n) + D(n-1)
求和公式,k = 1..n
1. sum( k ) = n(n+1)/2
2. sum( 2k-1 ) = n^2
3. sum( k^2 ) = n(n+1)(2n+1)/6
4. sum( (2k-1)^2 ) = n(4n^2-1)/3
5. sum( k^3 ) = (n(n+1)/2)^2
6. sum( (2k-1)^3 ) = n^2(2n^2-1)
7. sum( k^4 ) = n(n+1)(2n+1)(3n^2+3n-1)/30
8. sum( k^5 ) = n^2(n+1)^2(2n^2+2n-1)/12
9. sum( k(k+1) ) = n(n+1)(n+2)/3
10. sum( k(k+1)(k+2) ) = n(n+1)(n+2)(n+3)/4
12. sum( k(k+1)(k+2)(k+3) ) = n(n+1)(n+2)(n+3)(n+4)/5

2.2 generating permutations

 

// gen_perm lexicographic order to produce P (n-, m)
 // gen_comb lexicographically generating composition C (n-, m)
 // gen_perm_swap generate full array transducer adjacent bits P (n-, n-)
 // generating element 1 used. .n represents
 // function after generating dummy call, passing a [] and n, a [0] .. a [n-1] is the result of a generated 
#define MAXN 100
 int COUNT; 
#include <the iostream. H>
 void dummy ( int * A, int n-) {
 int I; 
COUT << << COUNT ++ " : " ;
 for (I = 0 ; I <N- . 1 ; I ++ ) 
COUT << A [I] << '  ' ; 
COUT<<a[n-1]<<endl;
}
void _gen_perm(int* a,int n,int m,int l,int* temp,int* tag){
int i;
if (l==m)
dummy(temp,m);
else
for (i=0;i<n;i++)
if (!tag[i]){
temp[l]=a[i],tag[i]=1;
_gen_perm(a,n,m,l+1,temp,tag);
tag[i]=0;
}
}
void gen_perm(int n,int m){
int a[MAXN],temp[MAXN],tag[MAXN]={0},i;
for (i=0;i<n;i++)
a[i]=i+1;
_gen_perm(a,n,m,0,temp,tag);
}
void _gen_comb(int* a,int s,int e,int m,int& count,int* temp){
int i;
if (!m)
dummy(temp,count);
else
for (i=s;i<=e-m+1;i++){
temp[count++]=a[i];
_gen_comb(a,i+1,e,m-1,count,temp);
count--;
}
}
void gen_comb(int n,int m){
int a[MAXN],temp[MAXN],count=0,i;
for (i=0;i<n;i++)
a[i]=i+1;
_gen_comb(a,0,n-1,m,count,temp);
}
void _gen_perm_swap(int* a,int n,int l,int* pos,int* dir){
int i,p1,p2,t;
if (l==n)
dummy(a,n);
else{
_gen_perm_swap(a,n,l+1,pos,dir);
for (i=0;i<l;i++){
p2=(p1=pos[l])+dir[l];
t=a[p1],a[p1]=a[p2],a[p2]=t;
pos[a[p1]-1]=p1,pos[a[p2]-1]=p2;
_gen_perm_swap(a,n,l+1,pos,dir);
}
dir[l]=-dir[l];
}
}
void gen_perm_swap(int n){
int a[MAXN],pos[MAXN],dir[MAXN],i;
for (i=0;i<n;i++)
a[i]=i+1,pos[i]=i,dir[i]=-1;
_gen_perm_swap(a,n,0,pos,dir);
}

2.3 generating a gray code

// generates a Gray code the REFLECTED
 // Gray get the next one yard each call
 // 000 is the first code 000 ..., 100 ... 000 is the last code 
void Gray ( int n-, int * code) {
 int = T 0 , I;
 for (I = 0 ; I <n-; code = T + [I ++ ]);
 IF (& T . 1 )
 for (n-- ;! code [n-]; N-- ); 
code [n- - . 1 ] = . 1 the -code [N- . 1 ]; 
}

2.4 displacement (polya)

// Permutation loop section, polya principle
 @ Perm [0..n-1] is a permutation 0..n-1 (arrangement)
 // Returns the substitution cycle minimum, num returns the number of cycles of section 
#define MAXN 1000
 int GCD ( int A, int B) {
 return B GCD (B, A%? B): A; 
} 
int a polyA ( int * Perm, int n-, int & NUM) {
 int I, J, P, V [ MAXN] = { 0 }, RET = . 1 ;
 for (NUM = I = 0 ; I <n-; I ++ )
 IF (! {V [I])
 for (NUM ++, J = 0 , P = I;! V [P = perm [p]]; j ++) 
V [i] = 1 ; 
right * = j / gcd (right, j); 
} 
Return right; 
}

2.5 lexicographic full array

// lexicographic whole number and arrangement conversion 
int perm2num ( int n-, int * P) {
 int I, J, RET = 0 , K = . 1 ;
 for (I = N- 2 ; I> = 0 ; K = * N- (i-- ))
 for (I = J + . 1 ; J <n-; J ++ )
 IF (P [J] < P [I]) 
RET + = K;
 return RET; 
} 
void num2perm ( int n-, int * P, int T) {
 int I, J;
 for (I = N- . 1 ; I> =0;i--)
p[i]=t%(n-i),t/=n-i;
for (i=n-1;i;i--)
for (j=i-1;j>=0;j--)
if (p[j]<=p[i])
p[i]++;
}

2.6 lexicographically composition

// lexicographically combined with the converted number of
 // COMB is the number of combinations of C (n, m), large numbers replaced when necessary, note processing C (n-, m) = 0 | n-<m 
int COMB ( int n-, int m) {
 int RET = . 1 , I; 
m = m <(nm) m: (nm? );
 for (I = nm + . 1 ; I <= n-; RET * = (I ++ ));
 for ( I = . 1 ; I <= m; RET / = (I ++ ));
 return m < 0 ? 0 : RET;
 58 
} 
int comb2num ( int n-, int m, int * C) {
 int RET = COMB (n-, m ), i;
for (i=0;i<m;i++)
ret-=comb(n-c[i],m-i);
return ret;
}
void num2comb(int n,int m,int* c,int t){
int i,j=1,k;
for (i=0;i<m;c[i++]=j++)
for (;t>(k=comb(n-j,m-i-1));t-=k,j++);
}

 

Guess you like

Origin www.cnblogs.com/godoforange/p/11240456.html