[BZOJ3560] DZY Loves Math V

传送门
啊终于有一道我会自己做的啦ヾ(◍°∇°◍)ノ゙

发现可以对每个质因子分别考虑贡献,最后乘起来。
对于一个质因子 \(p\) ,设它在 \(i\) 个数里出现了 \(a_i\) 次,令 \(sum=\prod (1+p^1+p^2+ \cdots +p^{a_i})\)
那么它的贡献就是 \((sum-1) \cdot \dfrac{p-1}{p}+1\)

然后很有信仰地暴力分解一波质因子就过了呢ヾ(✿゚▽゚)ノ

#include<bits/stdc++.h>
#include<tr1/unordered_map>
#define LL long long
#define re register
#define fr(i,x,y) for(re int i=(x);i<=(y);i++)
#define rf(i,x,y) for(int i=(x);i>=(y);i--)
#define frl(i,x,y) for(int i=(x);i<(y);i++)
using namespace std;
using namespace tr1;
const int N=10000006;
const int INF=2147483647;
const int p=1e9+7;
int n;
LL s[N];

void read(int &x){
	char ch=getchar();x=0;
	for(;ch<'0'||ch>'9';ch=getchar());
	for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
}

inline void Add(LL &x,LL y){
	x+=y;
	while(x<0) x+=p;
	while(x>=p) x-=p;
}

LL qpow(LL a,int n){
	LL ans=1;
	for(LL sum=a;n;n>>=1,sum=sum*sum%p) if (n&1) ans=ans*sum%p;
	return ans;
}

int main(){
	read(n);
	int x;
	fr(i,1,n){
		read(x);
		for(int j=2;j*j<=x;j++)
		 if (x%j==0){
		 	LL cnt=1,xx=1;
		 	while(x%j==0) x/=j,xx=xx*j%p,Add(cnt,xx);
		 	if (!s[j]) s[j]=cnt;
		 	 else s[j]=s[j]*cnt%p;
		 }
		if (x){
			if (!s[x]) s[x]=x+1;
			 else s[x]=s[x]*(x+1)%p;
		}
	}
	LL ans=1;
	frl(i,1,N)
	 if (s[i]){
	 	LL sum=(s[i]-1)*(i-1)%p;
	 	sum=sum*qpow(i,p-2)%p;
	 	ans=ans*(sum+1)%p;
	 }
	cout<<ans<<endl;
	return 0;
}
posted @ 2019-07-30 19:02  ymzqwq  阅读(114)  评论(0编辑  收藏  举报