HDU-4190-Number Sequence- inclusion and exclusion + multiple sets and combinations of r

HDU-4190-Number Sequence- inclusion and exclusion + multiple sets and combinations of r


【Problem Description】

You \ (n-\) number \ (B_i \) , asked how many of length \ (n-\) sequence \ (a_i \) , so \ (a_1 \ cdot a_2 \ dots a_n = b_1 \ cdot b_2 \ dots B_n \) . And \ (a_i> 1 \) .

【Solution】

All \ (B_i \) decomposition of the quality factor, respectively count the number of each quality factor appears, you can be sure, all \ (a_i \) must be selected from these combinations of different prime factor multiplied.

Assuming that no (a_i> 1 \) \ restriction, then assuming all \ (B_i \) Total \ (3 \) a quality factor, the quality factor of each frequency appear are \ (a, b, c \ ) times . The total \ ({a + n-1 \ choose n-1} \ cdot {b + n-1 \ choose n-1} \ cdot {c n-1 \ choose n-1 +} \) species length \ (n-\) a \ (a_i \) sequence. Similar i.e. total \ (n-\) different boxes, the \ (A \) red balls, \ (B \) th basketball, \ (C \) of a green ball into which \ (n-\) th how many different schemes box there, it can make.

But now the number of answers obtained, including the \ (a_i = 1 \) cases, need to be removed, that is, minus \ (1 \) positions empty program number, plus the \ (2 \) position is empty the number of schemes, minus \ (\ dots \) and so on. \ (I \) positions empty number scheme \ ({n \ choose i} \ cdot {a + n-1-i \ choose n-1-i} \ cdot {b + n-1-i \ . 1-I-n-the Choose} \ {C + CDOT. 1-I-n-\. 1-I-n-the Choose} \) . I.e., start \ (n-\) boxes species selected from the group \ (I \) positions, there are \ ({n \ choose i} \) kinds of programs, and then multiplied by the \ (A \) red balls, \ ( B \) th basketball, \ (C \) of a green ball into \ (Ni) \ several boxes of programs.


【Code】

#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<cstdio>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 1000005
#define int long long
const int mod=1e9+7;
int a[25];
int prime[maxn],cnt=0;
bool vis[maxn]={1,1};
void Euler(){ //欧拉筛
    for(int i=2;i<maxn;i++){
        if(!vis[i]) prime[++cnt]=i;
        for(int j=1;j<=cnt&&i*prime[j]<maxn;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
map<int,int>mp; //统计每个质因数出现的次数
void solve(int n){ //求质因数
    for(int i=1;i<=cnt&&prime[i]*prime[i]<=n;i++){
        int p=prime[i],num=0;
        if(n%p==0){
            while(n%p==0) n/=p,num++;   
            mp[p]+=num;
        }
    }
    if(n>1) mp[n]++;
}
int C[105][105]; //组合数
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);Euler();
    for(int i=0;i<105;i++) C[i][0]=1;
    for(int i=1;i<105;i++){ //预处理组合数
        for(int j=1;j<=i;j++){
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
        }
    }
    int n;
    while(cin>>n){
        mp.clear();
        for(int i=1;i<=n;i++) cin>>a[i],solve(a[i]);
        int ans=1;
        for(auto v:mp){ //求出ai没有限制时的方案数
            ans=(ans*C[v.second+n-1][n-1])%mod;
        }
        for(int i=1;i<n;i++){ //容斥减去ai=1的方案
            int tmp=C[n][i];
            for(auto v:mp){
                tmp=tmp*C[v.second+n-1-i][n-1-i]%mod;
            }
            ans=(ans+(i&1?-1:1)*tmp)%mod;
        }
        cout<<(ans+mod)%mod<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/--Simon/p/11642464.html