BZOJ 3560 DZY Loves Math V

本文针对BZOJ3560题目提供了解决方案,利用质因数分解的方法来计算每个质因数对答案的贡献,并通过化简公式实现了高效的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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 long

const 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值