传送门:
http://codeforces.com/problemset/problem/615/D
给出关于n的乘法表达式,算n的所有因子的乘积%mod
就是求出每个质因子在最后乘式中出现的次数,利用dp的思想,pre[i]表示前i个质因子的组合方案数,则分为两种情况,即第i种取还是不取,注意这里指的是多少种组合方案,而不是质因子具体个数,所以取得时候要有n个质因子,则有n种取值方案(1到n(此时是一定取)),同理处理后缀last[i]
然后再具体计算某个因子的时候,算的是这个因子在与其余组合的时候出现了多少次,
即pre[i-1]乘上last[i+1],然后计算这个因子取1个,2个,,,n个时候,一共被算进去了多少次,(此时算的是因子的个数)即为(num*(num+1)/2)个(1加到n等差数列)
然后计算结果就可以了
值得注意的地方是开计数器的时候下标应该从1开始取,因为要处理前缀和后缀,其他一些小细节在代码英文注释中也有具体体现
还有就是费马小定理的运用,由于index数目过大(极限情况下计算每一个系数都需要2的maxn次,这样复杂度最后就变成了n*n)
所以需要优化,即n的(mod-1)次为1,
所以上面在处理index的时候,需要模上(mod-1)
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
const ll mod =1e9+7;
int factor[maxn];
int top=0;
ll n;
ll num[maxn];
ll pre[maxn],last[maxn];
ll quickpow(ll n,ll m){
ll ans=1ll;
// miss to judge if(n&1)
while(n){
if(n&(1ll))
ans=ans*m%mod;
m=m*m%mod;
n>>=1;
}
return ans%mod;
}
int a;
int main(){
cin>>n;
//miss to record the factor[] and the top
for(int i=1;i<=n;i++){
scanf("%d",&a);
if(!num[a]) factor[++top]=a;
num[a]++;
}
//do not miss +1 because the two situations and 0 and n+1
pre[0]=1;last[top+1]=1;
for(int i=1;i<=top;i++){
pre[i]=pre[i-1]*(num[factor[i]]+1)%(mod-1);
}
for(int i=top;i>=1;i--){
last[i]=last[i+1]*(num[factor[i]]+1)%(mod-1);
}
ll ans=1;
for(int i=1;i<=top;i++){
ll Num=num[factor[i]];
ll index=(pre[i-1]%(mod-1))*(last[i+1]%(mod-1))%(mod-1);
index=(index*((Num)*(Num+1)/2%(mod-1)))%(mod-1);
ans=ans*quickpow(index,factor[i])%mod;
}
cout<<ans<<endl;
return 0;
}