Codeforces Round #232 (Div. 1) A 解题报告
A. On Number of Decompositions into Multipliers
题目连接:http://codeforces.com/contest/396/problem/A
大意:给定n(n<=500)个数ai(1<=ai<=10^9),得到他们的乘积m,问将m分解成n个数相乘,有多少种方法.
思路:显然每个质因数都是独立的,如果质因数pi出现了ci次,那么把它分到n个数中,就有C(ci+n-1,n-1)种方法,然后把所有因数的答案相乘就是结果。于是我们可以先预处理出来组合数。然后对每个ai进行分解因式,最后的复杂度O(n*sqrt(max(ai))).
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <set> 6 #include <queue> 7 #include <set> 8 #include <map> 9 #include <cstring> 10 #include <functional> 11 #include <cmath> 12 typedef long long ll; 13 using namespace std; 14 const ll mod = 1000000007; 15 map<ll,ll> cnt; 16 17 int n; 18 ll a[1000]; 19 ll c[15000][1001]; 20 int main(){ 21 freopen("in.txt","r",stdin); 22 ios::sync_with_stdio(0); 23 c[0][0] = 1; 24 for(int i=1;i<15000;i++){ 25 for(int j=0;j<=min(i,1000);j++){ 26 if(j==0 || j==i) 27 c[i][j] = 1; 28 else 29 c[i][j] = ( c[i-1][j-1] +c[i-1][j] )%mod; 30 } 31 } 32 cin>>n; 33 for(int i=0;i<n;i++){ 34 cin>>a[i]; 35 } 36 37 for(int i=0;i<n;i++){ 38 39 40 for(ll j=2;j*j<=a[i];j++){ 41 42 if(a[i]%j==0){ 43 ll tmp = 0; 44 while(a[i]%j==0){ 45 a[i]/=j; 46 tmp++; 47 48 } 49 cnt[j]+=tmp; 50 51 } 52 53 } 54 if(a[i]!=1) 55 cnt[a[i]]++; 56 57 } 58 59 ll ans = 1; 60 61 for(map<ll,ll>::iterator i = cnt.begin();i!=cnt.end();i++){ 62 ll cc =(*i).second; 63 64 ans = (ans*c[cc+n-1][n-1])%mod; 65 } 66 67 cout<<ans%mod<<endl; 68 return 0; 69 }