Table of Contents

ExplanationImplementation

Explanation

For an integer AA, the sum of all divisors of the AA can be efficiently computed using the divisor function.

The formula states that if d1p1d2p2...dkpkd_1^{p_1} \cdot d_2^{p_2} \cdot ... \cdot d_k^{p_k} is the prime factorization of AA, then the divisors' sum is:

S=i=1kdipi+11di1.S = \prod_{i=1}^{k}\frac{d_i^{p_i+1} - 1}{d_i -1}.

For this problem we need to compute sum of divisors of ABA^B. If the prime factorization of AA is d1p1d2p2...dkpkd_1^{p_1} \cdot d_2^{p_2} \cdot ... \cdot d_k^{p_k} then the prime factorization of ABA^B will be d1Bp1d2Bp2...dkBpkd_1^{B\cdot p_1} \cdot d_2^{B\cdot p_2} \cdot ... \cdot d_k^{B\cdot p_k} and we can compute the sum of divisors using the above formula, S=i=1kdiBpi+11di1S = \prod_{i=1}^{k}\frac{d_i^{B\cdot p_i+1} - 1}{d_i -1}.

If di≢1(mod109+7)d_i\not \equiv 1 \pmod{10^9+7}, then we can compute the value of the expression diBpi+11di1(mod109+7)\frac{d_i^{B\cdot p_i+1} - 1}{d_i -1}\pmod{10^9+7} using modular exponentiation and modular inverse. Otherwise, the expression equals 1+di+di2+...+diBpiBpi+1(mod109+7)1+d_i + d_i^2 + ... + d_i^{B\cdot p_i}\equiv B\cdot p_i+1\pmod{10^9+7}.

We do need to compute the prime factors of AA, but thankfully this can be done in O(A)\mathcal{O}(\sqrt{A}) time.

Since, BB can be as large as 101810^{18}, to avoid overflows we can use Euler's totient function and binary exponentiation to compute the powers modulo 109+710^9+7.

Implementation

Time Complexity: O(A+logAlogB)\mathcal{O}(\sqrt{A}+\log A\cdot \log B)

C++

#include <fstream>
using namespace std;
#define ll long long
const int MOD = 1e9 + 7;
Code Snippet: Binary Exponentiation (Click to expand)

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!