码迷,mamicode.com
首页 > 其他好文 > 详细

hdu4390-Number Sequence(容斥计算)

时间:2014-06-15 20:06:24      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   ext   

题意:给定b数列,计算有多少种数列 a1,a2,...,an 满足条件 a1*a2*...*an=b1*b2*…*bn (ai>1).


解法:处理出b数列中出现的所有质因子的数量记录在map中,然后进行容斥计算:bubuko.com,布布扣

代码:

/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=1000010;
const int INF=1000000007;

map<int,int> maps;
int num[30];
int n;
bool rem[1000010];
LL C[500][500];
int prime[Max/2];
int p=0;
void init()
{
    for(LL i=2; i<Max; i++)
        if(!rem[i])
        {
            prime[p++]=i;
            for(LL j=i*i; j<Max; j+=i)
                rem[j]=1;
        }
    for(int i=0; i<500; i++)
        for(int j=0; j<=i; j++)
            C[i][j]=j?(C[i-1][j-1]+C[i-1][j])%INF:1;
}
void make(int t)
{
    int tool=t;
    for(int i=0; prime[i]*prime[i]<=t; i++)
    {
        while(tool%prime[i]==0)
            maps[prime[i]]++,tool/=prime[i];
    }
    if(tool>=2)
        maps[tool]++;
}
LL getans(int m)
{
    LL ans=1;
    int k=n-m;
    for(map<int,int>::iterator p=maps.begin();p!=maps.end();p++)
    {
        int t=p->second;
        ans=(ans*C[k+t-1][k-1])%INF;
    }
    return ans;
}
int main()
{
    init();
    while(scanf("%d",&n)==1)
    {
        maps.clear();
        for(int i=0; i<n; i++)
            scanf("%d",num+i);
        for(int i=0; i<n; i++)
        {
            make(num[i]);
        }
        int ans=0;
        for(int i=0; i<n; i++)
        {
            if(i&1)
            {
                ans-=C[n][i]*getans(i)%INF;
                if(ans<0)
                    ans+=INF;
            }
            else
            {
                ans+=C[n][i]*getans(i)%INF;
                if(ans>=INF)
                    ans-=INF;
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣



                                                                 

hdu4390-Number Sequence(容斥计算),布布扣,bubuko.com

hdu4390-Number Sequence(容斥计算)

标签:style   class   blog   code   http   ext   

原文地址:http://blog.csdn.net/xiefubao/article/details/30479217

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!