题目大意:给出mn,让你求C(m,n)。
思路:公式都给你了,就100,暴力就可以关键还是高精度。如果按照算法“它让你怎么做你就怎么做”,那么很显然你需要写一个高精度除法。然而可以证明,这个除法是不会产生余数的。所以我们可以数论分析,然后避免高精度除法。
方法就是暴力求每个数的质因数,然后把被除数和除数相同的质因数消去,最后除数肯定会被消没。这样只要做高精度乘法就可以了。
CODE:
#include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> #define MAX 110 #define BASE 1000 using namespace std; int n,m; int factor[MAX]; struct BigInt{ int num[MAX],len; BigInt(int _ = 0) { memset(num,0,sizeof(num)); if(_) { len = 1; num[1] = _; } else len = 0; } BigInt operator *(int a) { BigInt re; int temp = 0; for(int i = 1; i <= len; ++i) { re.num[i] = num[i] * a + temp; temp = re.num[i] / BASE; re.num[i] %= BASE; } re.len = len; while(temp) { re.num[++re.len] = temp % BASE; temp /= BASE; } return re; } void operator *=(int a) { *this = *this * a; } }; ostream &operator <<(ostream &os,const BigInt &a) { os << a.num[a.len]; for(int i = a.len - 1; i; --i) os << fixed << setfill('0') << setw(3) << a.num[i]; return os; } inline void Divide(int x,int c) { for(int i = 2; i * i <= x; ++i) if(x % i == 0) while(x % i == 0) { factor[i] += c; x /= i; } if(x != 1) factor[x] += c; } int main() { while(scanf("%d%d",&n,&m),m + n) { memset(factor,0,sizeof(factor)); for(int i = 1; i <= n; ++i) Divide(i,1); for(int i = 1; i <= m; ++i) Divide(i,-1); for(int i = 1; i <= n - m; ++i) Divide(i,-1); BigInt ans(1); for(int i = 1;i <= 100; ++i) for(int j = 1; j <= factor[i]; ++j) ans *= i; cout << n << " things taken " << m << " at a time is " << ans <<" exactly." << endl; } return 0; }
原文地址:http://blog.csdn.net/jiangyuze831/article/details/40544171