Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- bool IsPrime(int64_t number) {
- int64_t divisor = 2;
- while (divisor * divisor <= number) {
- if (number % divisor == 0) {
- return false;
- }
- ++divisor;
- }
- return true;
- }
- int64_t MinDivisor(int64_t number) {
- int64_t divisor = 2;
- while (divisor * divisor <= number) {
- if (number % divisor == 0) {
- return divisor;
- }
- ++divisor;
- }
- return number;
- }
- // n = p_1^q_1 * p_2^q_2 ... p_n^q_n
- // 27 = {3: 3}
- // p^n: n + 1
- // pq: 4
- // p^n q^m: (n + 1)(m + 1)
- // n = p_1^q_1 * p_2^q_2 ... p_n^q_n: (q_1 + 1)...(q_n + 1)
- int64_t kPrime = 1e9 + 9;
- int64_t Pow(int64_t a, int64_t n, int64_t p) {
- if (n == 0) {
- return 1;
- }
- if (n % 2 == 0) {
- return Pow(a * a, n / 2, p) % p;
- }
- return Pow(a, n - 1, p) * a % p;
- }
- // 2^17 = 2^16 * 2 = (2^8)^2 * 2 = (((2^2)^2)^2)^2 * 2
- // log n
- std::unordered_map<int64_t, int64_t> Factorization(int64_t number) {
- int64_t divisor = 2;
- std::unordered_map<int64_t, int64_t> factorization;
- while (divisor * divisor <= number) {
- while (number % divisor == 0) {
- number /= divisor;
- ++factorization[divisor];
- }
- ++divisor;
- }
- if (number > 1) {
- factorization[number] = 1;
- }
- return factorization;
- }
- int64_t DivisorsCount(int64_t number) {
- auto factorization = Factorization(number);
- int64_t divisors_count = 1;
- for (auto [_, degree]: factorization) {
- divisors_count *= degree + 1;
- }
- return divisors_count;
- }
- int64_t DivisorsSum(int64_t number) {
- auto factorization = Factorization(number);
- int64_t divisors_sum = 1;
- for (auto [prime, degree]: factorization) {
- divisors_sum *= (Pow(prime, degree + 1, kPrime) - 1) / (prime - 1);
- }
- return divisors_sum;
- }
- // (a, b) = (b, a % b)
- // (a, 0) = a
- // (6, 4) = (4, 6 % 4) = (4, 2) = (2, 4 % 2) = (2, 0) = 2
- // (13, 8) = (8, 5) = (5, 3) = (3, 2) = (2, 1) = (1, 0) = 1
- // f_n = (...)^n + (...)^n
- // ax + by = 1
- // \exists x_0, y_0 \in Z: ax_0 + bx_0 = 1
- // x_0, y_0 называются частным решением
- // x = x_0 + b * t
- // y = y_0 - a * t
- // ax + by = a (x_0 + b * t) + b (y_0 - a * t) = ax_0 + abt + by_0 - abt = ax_0 + by_0 = 1
- // 2x + 3y = 1
- // -1 1
- // 2 -1
- // 5 -3
- // 8 -5
- // x = -1 + 3t
- // y = 1 - 2t
- // 2(-1 + 3t) + 3(1 - 2t) = -2 + 6t + 3 - 6t = 3 - 2 = 1
- // 5x + 7y = 1
- // x_0 = 10
- // y_0 = -7
- // x = 10 + 7t
- // y = -7 -5t
- // 42x + 101y = 1
- // 101 = 2 * 42 + 17
- // 42 = 2 * 17 + 8
- // 17 = 2 * 8 + 1
- // 1 = 17 - 2 * 8 = 17 - 2 * (42 - 2 * 17) = 17 - 2 * 42 + 4 * 17 = 5 * 17 - 2 * 42 =
- // 5 * (101 - 2 * 42) - 2 * 42 = 5 * 101 - 12 * 42
- // x_0 = -12
- // y_0 = 5
- // 37x + 79y = 1
- // 79 = 2 * 37 + 5
- // 37 = 7 * 5 + 2
- // 5 = 2 * 2 + 1
- // 1 = 5 - 2 * 2 = 5 - 2 * (37 - 7 * 5) = 5 - 2 * 37 + 14 * 5 = 15 * 5 - 2 * 37 =
- // 15 * (79 - 2 * 37) - 2 * 37 = 15 * 79 - 30 * 37 - 2 * 37 = 15 * 79 - 32 * 37
- // 2x + 4y = 1
- // ax + by = 1 имеет решения титгк (a, b) = 1
- // 4 = 2 * 2 + 0
- int64_t gcd(int64_t a, int64_t b) {
- if (b == 0) {
- return a;
- }
- return gcd(b, a % b);
- }
- int main() {
- int64_t number;
- std::cin >> number;
- std::cout << DivisorsSum(number);
- return 0;
- }
- // p: p + 1
- // p^2: p^
- // p^n: 1 + ... + p^n: (p^(n + 1) - 1) / (p - 1)
- // p_1^q_1 * p_2^q_2 ... p_n^q_n: (p_1^(q_1 + 1) - 1) / (p_1 - 1) * .. * (p_n^(q_n + 1) - 1) / (p_n - 1)
- // p^(n + 1) - 1 = p^(n + 1) - 1^(n + 1) = (p - 1)(o^n + ... + 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement