2 3 4
4HintFor the sample input, P=3*4=12. Here are the number sequences that satisfy the condition: 2 6 3 4 4 3 6 2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define N 100010
#define mod 1000000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos(-1.0);
const double E=2.718281828;
typedef long long ll;
const int INF=1000010;
using namespace std;
int C[1001][45];///组合数
map<int,int>mp;
void init()
{
for(int i=0; i<=1000; i++)
C[i][0]=1;///i个选0个为1;
for(int i=1; i<=1000; i++)
for(int j=1; j<=42; j++)
C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;///c(n,k)=c(n-1,k)+c(n-1,k-1),可以自己证明
}
void Fenjie(int n)///分解质因数
{
for(int i=2; i*i<=n; i++)
{
if(n%i==0)
{
while(n%i==0)
{
mp[i]++;
n/=i;
}
}
if(n==1)
break;
}
if(n>1)
mp[n]++;
}
int main()
{
init();
int n;
while(cin>>n)
{
mp.clear();
int b;
for(int i=0; i<n; i++)
{
scanf("%d",&b);
Fenjie(b);
}
ll ans=0;
for(int i=0; i<n; i++)///容斥原理
{
ll tem=C[n][i];
map<int,int>::iterator it;
for(it=mp.begin(); it!=mp.end(); it++)///计算有i个盒子为空的组合数
{
int t=it->second+n-i-1;
tem*=C[t][n-i-1];
tem%=mod;
}
if(i%2)
ans-=tem,ans+=mod;
else
ans+=tem;
ans%=mod;
}
cout<<ans%mod<<endl;
}
return 0;
}
hdu 4390 Number Sequence (容斥原理)
原文:http://blog.csdn.net/acm_baihuzi/article/details/40680361