CodeForces 615D (组合数学,欧拉降幂)

题意:
给出一个数n的m个素因子(1<=m<=200000),问n的所有因子的乘积,mod 1e9+7。


思路:
容易发现,n的所有因子一定是某几个素因子的乘积,所以我们只需要统计每个素因子在n的所有因子乘积中的贡献即可。
设一个素数在n中的次数为x,那这个数的内部贡献就是:x*(x+1)/2,内部可以取1,2,3...x个这个素数。另一个素数在n中出现的次数是y,那么这个数提供的外部贡献等于:(y+1),外部可以取0,1,2...y个这个素数。
为了优化,我们可以先求出外部贡献的前缀积,后缀积,计算当前素数的外部贡献直接用pre[i-1]*suf[i+1]。指数很大,需要用欧拉降幂,1e9+7是素数,所以指数模phi(1e9+7)=1e9+6即可。


代码:

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
#define pb push_back
#define X first
#define Y second
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pii pair<int,int>
#define qclear(a) while(!a.empty())a.pop();
#define lowbit(x) (x&-x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define mst(a,b) memset(a,b,sizeof(a))
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout1(x) cout<<x<<endl
#define IOS std::ios::sync_with_stdio(false)
#define SRAND srand((unsigned int)(time(0)))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
using namespace std;
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
const double eps=1e-8;
const int maxn=200005;
const int maxm=10005;

int m;
int maps[maxn];
pii p[maxn];
int pcur=1;
ll qpow(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1){
            res=res*a%mod;
        }
        b>>=1;
        a=a*a%mod;
    }
    return res;
}
ll pre[maxn];
ll suf[maxn];
void solve() {
    sd(m);
    int mx=0;
    for(int i=0;i<m;i++){
        int now;
        sd(now);
        mx=max(mx,now);
        maps[now]++;
    }
    for(int i=1;i<=mx;i++){
        if(!maps[i])continue;
        p[pcur].X=i;
        p[pcur].Y=maps[i];
        pcur++;
    }
    pcur--;
    mst(pre,0);
    mst(suf,0);
    pre[0]=1;
    pre[1]=p[1].Y+1;
    for(int i=1;i<=pcur;i++){
        pre[i]=pre[i-1]*(p[i].Y+1)%(mod-1);
    }
    suf[pcur+1]=1;
    suf[pcur]=p[pcur].Y+1;
    for(int i=pcur;i>=1;i--){
        suf[i]=suf[i+1]*(p[i].Y+1)%(mod-1);
    }
    ll ans=1;
    for(int i=1;i<=pcur;i++){
        ll now=p[i].X;
        ll nowcnt=p[i].Y;
        ll cnt=(((nowcnt)*(nowcnt+1))>>1)%(mod-1);
        cnt=cnt*pre[i-1]%(mod-1);
        cnt=cnt*suf[i+1]%(mod-1);
        cnt+=(mod-1);
        ll res=qpow(now,cnt)%mod;
        ans=ans*res%mod;
    }
    printf("%lld\n",ans);
    return ;
}
int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#else
    //    freopen("","r",stdin);
    //    freopen("","w",stdout);
#endif
    solve();
    return 0;
}

​

猜你喜欢

转载自blog.csdn.net/weixin_38378637/article/details/81146378