396A On Number of Decompositions into Multipliers 组合

首先将所有的数进行质因子分解,统计每个质数出现的次数。

假设第i个质数Pi出现了m次,则可以转化为将m个相同的球放入n个不同盒子的问题,允许盒子唯恐,即不放球。

那么这个问题解即为C(n+m-1,n-1),求解过程如下:

首先这个问题 等价于 将n+m个相同的球放入n个不同盒子,每个盒子都至少有一个球。

这样隔板法可解,将n+m个球排成一列,然后从n+m-1个缝隙选n-1个插入隔板,这样就将球分为n组,然后从每组中取走一个球。


公式出来了,剩下的就是枚举素数,累乘C(n+m-1,n-1)。


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <ctime>
#include <iomanip>

#pragma comment(linker,"/STACK:1024000000");
#define EPS (1e-6)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f
#define Mod 1000000007
#define mod 1000000007

/** I/O Accelerator Interface .. **/
#define g (c=getchar())
#define d isdigit(g)
#define p x=x*10+c-'0'
#define n x=x*10+'0'-c
#define pp l/=10,p
#define nn l/=10,n
template<class T> inline T& RD(T &x)
{
    char c;
    while(!d);
    x=c-'0';
    while(d)p;
    return x;
}
template<class T> inline T& RDD(T &x)
{
    char c;
    while(g,c!='-'&&!isdigit(c));
    if (c=='-')
    {
        x='0'-g;
        while(d)n;
    }
    else
    {
        x=c-'0';
        while(d)p;
    }
    return x;
}
inline double& RF(double &x)      //scanf("%lf", &x);
{
    char c;
    while(g,c!='-'&&c!='.'&&!isdigit(c));
    if(c=='-')if(g=='.')
        {
            x=0;
            double l=1;
            while(d)nn;
            x*=l;
        }
        else
        {
            x='0'-c;
            while(d)n;
            if(c=='.')
            {
                double l=1;
                while(d)nn;
                x*=l;
            }
        }
    else if(c=='.')
    {
        x=0;
        double l=1;
        while(d)pp;
        x*=l;
    }
    else
    {
        x=c-'0';
        while(d)p;
        if(c=='.')
        {
            double l=1;
            while(d)pp;
            x*=l;
        }
    }
    return x;
}
#undef nn
#undef pp
#undef n
#undef p
#undef d
#undef g
using namespace std;

LL num[510];

int pri[30010];

bool vis[1000100];

int ans[1000100];

LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    LL gcd=extend_gcd(b,a%b,x,y);
    LL t=x;
    x=y;
    y=t-a/b*x;
    return gcd;
}

LL Get_Inverse(LL num)
{
    LL x,y;
    extend_gcd(num,Mod,x,y);
    return (x%Mod+Mod)%Mod;
}

LL Cal(LL n,LL m)//计算组合数C(n,m)
{
    LL t1=1,t2=1;
    for(LL i=n;i>m;i--)
    {
        t1=(t1*i)%Mod;
        t2=(t2*(i-m))%Mod;
    }
    return t1*Get_Inverse(t2)%Mod;
}

int main()
{
    int Top,n = 100000,i,j;

    memset(vis,false,sizeof(vis));

    Top = 0;

    for(i = 2;i <= n; ++i)
    {
        if(vis[i] == true)
            continue;

        pri[Top++] = i;

        for(j = i+i;j <= n; j += i)
            vis[j] = true;
    }

    scanf("%d",&n);

    for(i = 1;i <= n; ++i)
        scanf("%I64d",&num[i]);

    for(i = 1;i <= n; ++i)
    {
        LL tmp = num[i];

        for(j = 0;j < Top; ++j)
        {
            while(tmp%pri[j] == 0)
                tmp /= pri[j];
        }

        if(tmp != 1)
            pri[Top++] = tmp;
    }


    sort(pri,pri+Top);

    memset(ans,0,sizeof(ans));

    for(i = 1;i <= n; ++i)
    {
        for(j = 0;j < Top; ++j)
        {
            while(num[i]%pri[j] == 0)
                ans[j]++,num[i] /= pri[j];
        }
    }

    LL sum = 1;

    for(i = 0;i < Top; ++i)
    {
        (sum *= Cal(ans[i]+n-1,n-1)) %= Mod;;
    }

    printf("%I64d\n",sum);

    return 0;
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值