Divisor Analysis

Given an integer, your task is to find the number, sum, and product of its divisors. As an example, let us consider the number 12:

  • the number of divisors is 6 (they are 1,2,3,4,6,12)
  • the sum of divisors is 
  • the product of divisors is 

Since the input number may be large, it is given as a prime factorization.

Example:

Input:  n = 2, a = {{2, 2}, {3, 1}}
Output: 6 28 1728

Approach

C++

#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1e9 + 7;

long long exponentiationWithMod(long long a,
                                long long b, long long m)
{
    long long res = 1;
    while (b)
    {
        if (b % 2)
            res = res * a % m;
        a = a * a % m;
        b /= 2;
    }
    return res;
}

void divisorAnalysis(int n, vector<vector<long long>> &a)
{

    long long cnt = 1, sum = 1, mul = 1, cnt2 = 1;
    for (int i = 0; i < n; i++)
    {
        long long p = a[i][0];
        long long k = a[i][1];
        cnt = cnt * (k + 1) % MOD;
        sum = sum * (exponentiationWithMod(p, k + 1, MOD) - 1) % MOD *
              exponentiationWithMod(p - 1, MOD - 2, MOD) % MOD;
        mul = exponentiationWithMod(mul, k + 1, MOD) *
              exponentiationWithMod(exponentiationWithMod(p, k * (k + 1) / 2, MOD), cnt2, MOD) % MOD;
        cnt2 = cnt2 * (k + 1) % (MOD - 1);
    }
    cout << cnt << " " << sum << " " << mul << "\n";
}

int main()
{
    int n = 2;

    vector<vector<long long>> a = {{2, 2}, {3, 1}};

    divisorAnalysis(n, a);

    return 0;
}


No comments:

Post a Comment