BZOJ 3560 DZY Loves Math V

来源:互联网 发布:吃鸡优化软件官网 编辑:程序博客网 时间:2024/06/06 13:18

题目链接:BZOJ 3560

首先,可以根据phi函数为积性函数,可以分解质因数,计算每个质因数对答案的贡献,最后乘起来即可。那么现在问题就来了,怎样计算质因数对答案的贡献?我们可以对每个数ai分解质因数,对于它的一个质因数p,记录p出现的次数bi,那么这个质数p对答案的贡献,是对于每个数ai,p的j(1<=j<=bi )次方求积的数的欧拉函数值。然后可以将公式化简。具体化简过程可以参见PoPoQQQ大神的博客:华丽丽的传送门。(不会用公式编辑器的哭了QAQ。)

#include<cstdio>#include<cstring>#include<iostream>#include<cmath>using namespace std;#define LL long longconst int maxn = (int)1e7 + 10;const int mod = 1000000000 + 7;int N;bool vis[4000 + 3];int prime[4000 + 3];LL f[maxn];inline int read(){int x = 0, f = 1;char ch = getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x * f;}void init(){int tot = 0;for(int i = 2; i <= 4000; ++i){if(!vis[i])prime[++tot] = i;for(int j = 1; j <= tot && i * prime[j] <= 4000; j++){vis[i * prime[j]] = 1;if(i % prime[j] == 0)break;}}for(int i = 1; i <= 10000000; ++i)f[i] = 1;}LL ni(LL x, LL y, LL m){LL t = 1;while(y){if(y & 1)t = (t * x) % m;y >>= 1;x = (x * x) % mod;}return t;}int main(){N = read();init();for(int i = 1; i <= N; ++i){int x = read();for(int j = 1; prime[j] * prime[j] <= x; ++j){LL t = 1, tot = 1;while(x % prime[j] == 0){t = (LL) t * prime[j] % mod;tot = (tot + t) % mod;x /= prime[j];}f[prime[j]] = (f[prime[j]] * tot) % mod;}if(x > 1)f[x] = ((LL) f[x] * x + (LL) f[x]) % mod;}LL ans = 1;for(int i = 2; i <= 10000000; ++i){if(f[i] == 1)continue;ans = ans * ((LL) (i - 1) * ni(i, mod - 2, mod) % mod * (f[i] - 1) % mod + 1) % mod;}ans = (ans + mod) % mod;cout << ans << endl;return 0;}


0 0
原创粉丝点击