CSES - Divisor Analysis
Author: Andi Qu
Time Complexity: .
Number of divisors
Each divisor of the number can be written as where .
Since there are choices for , the number of divisors is simply .
We can calculate this by iterating through the prime factors in time.
Sum of divisors
Let the sum of divisors when only considering the first prime factors be . The answer will be .
We can calculate each using fast exponentiation and modular inverses in time.
Product of divisors
Let the product and number of divisors when only considering the first prime factors be and respectively. The answer will be .
Again, we can calculate each using fast exponentiation in time, but there's a catch! It might be tempting to use from your previously-calculated values in part 1 of this problem, but those values will yield wrong answers.
This is because in general. However, by Fermat's little theorem, for prime , so we can just store modulo to calculate .
C++
#include <bits/stdc++.h>typedef long long ll;using namespace std;const ll MOD = 1e9 + 7;ll expo(ll base, ll pow) {ll ans = 1;while (pow) {if (pow & 1) ans = ans * base % MOD;
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!