Explanation
For an integer , the sum of all divisors of the can be efficiently computed using the divisor function.
The formula states that if is the prime factorization of , then the divisors' sum is:
For this problem we need to compute sum of divisors of . If the prime factorization of is then the prime factorization of will be and we can compute the sum of divisors using the above formula, .
If , then we can compute the value of the expression using modular exponentiation and modular inverse. Otherwise, the expression equals .
We do need to compute the prime factors of , but thankfully this can be done in time.
Since, can be as large as , to avoid overflows we can use Euler's totient function and binary exponentiation to compute the powers modulo .
Implementation
Time Complexity:
C++
#include <fstream>using namespace std;#define ll long longconst 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!