HDU 4390 组合数学&数论

来源:互联网 发布:手机淘宝阿里旺旺在哪 编辑:程序博客网 时间:2024/06/05 12:06

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4390

Number Sequence

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1067    Accepted Submission(s): 428


Problem Description
Given a number sequence b1,b2…bn.
Please count how many number sequences a1,a2,...,an satisfy the condition that a1*a2*...*an=b1*b2*…*bn (ai>1).
 

Input
The input consists of multiple test cases. 
For each test case, the first line contains an integer n(1<=n<=20). The second line contains n integers which indicate b1, b2,...,bn(1<bi<=1000000, b1*b2*…*bn<=1025).
 

Output
For each test case, please print the answer module 1e9 + 7.
 

Sample Input
23 4
 

Sample Output
4
Hint
For the sample input, P=3*4=12.Here are the number sequences that satisfy the condition:2 63 44 36 2
 

Source
2012 Multi-University Training Contest 10

将所有的b[i]进行素因子分解,则a和b的因子是完全一致的。

剩下的便是将所有b的因子,分给a

我们考虑某个素因子pi,如果有ci个,便成了子问题将ci个相同的物品放入到n个不同的容器中,种数为多少

但是要求ai>1,也就是容器不能为空,这是个问题。

我们考虑的是什么的情况,然后减去假设有一个确定是空的情况,发现可以用容斥原理解决

将m个物品放到到不同的n个容器中,结果为c(n+m-1,n-1)


#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;#define LL long long#define MOD 1000000007int n;LL C[501][501];LL cnt[1000001];void init(){    C[0][0]=1;    for(int i=1;i<501;i++){        C[i][0]=C[i][i]=1;        for(int j=1;j<i;j++)            C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD;    }}void doit(int x){    for(int i=2;i*i<=x;i++){        while(x%i==0){            x/=i;            cnt[i]++;        }    }    if(x>1)cnt[x]++;}vector<LL>p;void solve(){    p.clear();    for(int i=0;i<1000001;i++)        if(cnt[i])p.push_back(cnt[i]);    int len=p.size();    LL ans=1;    for(int i=0;i<len;i++)        ans=ans*C[p[i]+n-1][n-1]%MOD;    for(int i=1;i<n;i++){        LL tmp=C[n][i];        for(int j=0;j<len;j++)            tmp=tmp*C[p[j]+n-i-1][n-i-1]%MOD;        if(i&1)ans=((ans-tmp)%MOD+MOD)%MOD;        else ans=(ans+tmp)%MOD;    }    printf("%I64d\n",ans);}int main(){    init();    while(~scanf("%d",&n)){        memset(cnt,0,sizeof cnt);        for(int i=0;i<n;i++){            int x;scanf("%d",&x);            doit(x);        }        solve();    }    return 0;}



0 0
原创粉丝点击