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 (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 blong 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 nvector<vector<long long>> &a)
{

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

int main()
{
    int n = 2;

    vector<vector<long long>> a = {{22}, {31}};

    divisorAnalysis(na);

    return 0;
}


No comments:

Post a Comment