您的位置:首页 > 其它

[bzoj3560] DZY Loves Math V

2017-02-11 11:46 274 查看

题目大意

给定n个正整数的数组,求∑i1|a1∑i2|a2...∑in|anϕ(i1i2i3...in)模109+7的值。

n≤105

ai≤107

分析

计算这题要用到欧拉函数的性质。

1. 欧拉函数是积性函数

2. 如果n=pk(p是质数),ϕ(n)=(p−1)pk−1=pk∗p−1p

首先所有质数分开处理。

设当前质数为p,对于第i个数,假设它分解质因数后p的次数为ai,那么p的答案就是[(1+p1+...+pa1)(1+p1+...+pa2)...(1+p1+...+pan)−1]p−1p+1

式子的意义就是每个数分别取0,1…ai个p,减掉的1、最后加上的1是全部取1的。

最后把每个质数的答案乘起来就好了。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N=1e7,mo=1e9+7;

typedef long long LL;

int T,tot,p
,f[N+5],n,ans,s[N+5],t
,id[N+5],a
;

bool bz[N+5];

char c;

int read()
{
for (c=getchar();c<'0' || c>'9';c=getchar());
int x=c-48;
for (c=getchar();c>='0' && c<='9';c=getchar()) x=x*10+c-48;
return x;
}

int quick(int x,int y)
{
if (!y) return 1;
int ans=quick(x,y>>1);
ans=(LL)ans*ans%mo;
if (y&1) ans=(LL)ans*x%mo;
return ans;
}

int main()
{
for (int i=2;i<=N;i++)
{
if (!bz[i])
{
p[id[i]=tot++]=i;
}
for (int j=0;j<tot && i*p[j]<=N;j++)
{
bz[i*p[j]]=1;
if (i%p[j]==0) break;
}
}
for (int i=0;i<tot;i++) s[i]=1;
n=read();
for (int i=1;i<=n;i++) a[i]=read();
for (int i=1;i<=n;i++)
{
for (int j=0;p[j]*p[j]<=a[i];j++) if (a[i]%p[j]==0)
{
int sum=1;
for (int k=p[j];a[i]%p[j]==0;a[i]/=p[j],k*=p[j]) sum+=k;
s[j]=(LL)s[j]*sum%mo;
}
if (a[i]>1) s[id[a[i]]]=(LL)s[id[a[i]]]*(1+a[i])%mo;
}
ans=1;
for (int i=0;i<tot;i++)
{
s[i]=(s[i]+mo-1)%mo;
ans=(LL)ans*((LL)s[i]*(p[i]-1)%mo*quick(p[i],mo-2)%mo+1)%mo;
}
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: