Factorial Factors

本文介绍了一种计算N!中不同素数因子数量及其总因子数的方法。通过筛选法找出1到N间的全部素数,并记录每个合数的最大素因子,进而计算出每个数能分解成的最小素数因子数。

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

/**
题目:Factorial Factors
题意:输出N!的不同的素数的个数,以及素数约数的总个数;
思路:找N!不同素数约数的个数,即找出1到N素数的个数,可以通过筛法处理,同时可以记录非素数对应的最大素数约束,然后用该数除以最大素数约束,求出得到的数的对应素数约数个数(这个个数实际在之前计算出了)然后加一便是该数的对应最少的可以分解成多少素数的约数。
**/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int vis[100100], cnt[100100];
int main(){
	int T;
	scanf("%d", &T);
	while (T--){
		int n;
		scanf("%d", &n);
		memset(vis, 0, sizeof vis);
		memset(cnt, 0, sizeof cnt);
		int maxn = 0;
		for (int i = 2; i <= n; i++)if (!vis[i]){
			maxn++;cnt[i] = 1;
			for (int j = i+i; j <= n; j += i){
				cnt[j] = 1 + cnt[j/i];
				vis[j] = 1;
			}
		}
		int ans = 0;
		for (int i = 0; i <= n; i++) ans += cnt[i];		
		printf("%d %d\n", maxn, ans);
	}
	return 0;
}


#include <bits/stdc++.h> using namespace std; using ll = long long; // 分解质因数 vector<pair<ll, int>> factorize(ll n) { vector<pair<ll, int>> factors; for (ll i = 2; i * i <= n; ++i) { if (n % i == 0) { int cnt = 0; while (n % i == 0) { n /= i; cnt++; } factors.emplace_back(i, cnt); } } if (n > 1) factors.emplace_back(n, 1); return factors; } // 计算n!中质因数p的次数 int count_p_in_factorial(ll n, ll p) { int cnt = 0; while (n) { cnt += n / p; n /= p; } return cnt; } // 计算 (a^b) mod mod ll pow_mod(ll a, ll b, ll mod) { ll res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } // 扩展欧几里得求逆元 ll inv(ll a, ll mod) { ll m0 = mod, x0 = 0, x1 = 1; while (a > 1) { ll q = a / m0; ll t = m0; m0 = a % m0; a = t; t = x0; x0 = x1 - q * x0; x1 = t; } return x1 < 0 ? x1 + mod : x1; } // 计算n!除去p的因子后的模pe值 ll factorial_mod_pe(ll n, ll p, ll pe) { ll res = 1; while (n > 0) { for (ll i = 1; i <= n % pe; ++i) { if (i % p != 0) { res = res * i % pe; } } n /= p; if (n % 2 == 1 && p != 2) { res = res * (pe - 1) % pe; } res = pow_mod(res, n, pe); } return res; } // 中国剩余定理合并 ll crt(const vector<ll>& rem, const vector<ll>& mods) { ll prod = 1; for (ll m : mods) prod *= m; ll res = 0; for (int i = 0; i < rem.size(); ++i) { ll m = prod / mods[i]; res = (res + rem[i] * m % prod * inv(m, mods[i])) % prod; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; ll mod; cin >> Q >> mod; auto factors = factorize(mod); vector<ll> primes, pows; for (auto& [p, e] : factors) { primes.push_back(p); pows.push_back(pow(p, e)); } while (Q--) { ll n, r; cin >> n >> r; if (r > n) { cout << 0 << '\n'; continue; } vector<ll> remainders; bool possible = true; for (int i = 0; i < primes.size(); ++i) { ll p = primes[i], e = factors[i].second; ll pe = pows[i]; int cnt_n = count_p_in_factorial(n, p); int cnt_r = count_p_in_factorial(r, p); if (cnt_n < cnt_r) { possible = false; break; } int delta = cnt_n - cnt_r; if (delta >= e) { remainders.push_back(0); continue; } ll m = factorial_mod_pe(n, p, pe); ll k = factorial_mod_pe(r, p, pe); ll inv_k = inv(k, pe); ll res = m * inv_k % pe; res = res * pow_mod(p, delta, pe) % pe; remainders.push_back(res); } if (!possible) { cout << 0 << '\n'; continue; } ll ans = crt(remainders, pows); cout << ans % mod << '\n'; } return 0; }去掉上述代码中的注释
最新发布
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值