P1445 [Violet] 樱花
算法
- 数学(素数筛,因子个数)
思路
对于原式做如下推导:
由上式联想到:
比较两式,发现缺少 项。加上 ,得:
由于等式左边是乘积的形式,所以 和 必定是 的一对因子。 将 质因数分解:
对于 的因子个数 有:
而 的因子个数 为:
根据乘法原理,最后的答案为所有 的乘积。
代码
#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1e9 + 7;
inline vector<int> GetPrimes(int Limit) { vector<int> primes; vector<bool> mark(Limit + 1);
for (int i = 2; i <= Limit; i ++) { if (mark[i] == false) { primes.emplace_back(i); }
for (int j = 0; i * primes[j] <= Limit; j ++) { mark[i * primes[j]] = true; if (i % primes[j] == 0) { break; } } }
return primes;}
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n; cin >> n;
vector<int> primes = GetPrimes(n); // * 注意由于是连乘,所以 answer 的初始值为 1. long long answer = 1;
for (auto&& p : primes) { int alpha = 0, number = n;
while (number) { alpha += number / p; number /= p; }
answer = (answer * (alpha + alpha + 1) % MOD) % MOD; }
cout << answer % MOD << '\n';
return 0;}
Comments
Quiet notes for this article.