有两种计算方式,直接计算或者递推计算:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 #define ll long long 6 const int MOD = 1000007; 7 const int maxk = 500; 8 ll C[maxk + 2][maxk + 2]; 9 // 线性算法,可以加取模 10 void get_C(){ 11 memset(C,0,sizeof C); 12 C[0][0] = 1; 13 for(int i = 0 ; i <= maxk ; i++){ 14 C[i][0] = C[i][i] = 1; 15 for(int j = 1 ; j < i ; j++) 16 C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]); 17 } 18 } 19 // 直接计算,不要随便取模,计算量过大时会有误差 20 long long cal_C(long long n,long long m){ 21 double ans = 1; 22 for(int i = 0 ; i < m ; i++) ans *= n - i; 23 for(int i = 0 ; i < m ; i++) ans /= i + 1; 24 return (long long)(ans + 0.5); 25 } 26 int main(){ 27 get_C(); 28 for(int i = 0 ; i < 225 ; i++) 29 for(int j = 0 ; j <= i && j < 10 ; j++)if(C[i][j] != cal_C(i,j)) 30 printf("%d %d :%lld,%lld ",i,j,C[i][j],cal_C(i,j)); 31 return 0; 32 }