Feat: Add solution for Divisor analysis #103
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🎯 Problem Information
Problem Name: Divisor Analysis
Category: Mathematics
CSES Link: https://cses.fi/problemset/task/2182
Difficulty: Medium
📝 Description
This PR provides a complete C++ solution for the CSES 'Divisor Analysis' problem. The solution correctly calculates the number of divisors, the sum of divisors, and the product of divisors for a large number given its prime factorization, with all calculations performed under a modulo.
🧩 Solution Approach
Algorithm Used: The solution leverages key principles from number theory, particularly the properties of multiplicative functions, modular arithmetic, and Fermat's Little Theorem. The approach calculates each of the three required values separately.
Number of Divisors (τ(N)): Calculated using the formula$\tau(N) = \prod_{i=1}^{k} (e_i + 1)$ . This value is computed twice: once modulo
10^9 + 7for the final answer, and once modulo10^9 + 6for use in exponent calculations.Sum of Divisors (σ(N)): Calculated using the formula$\sigma(N) = \prod_{i=1}^{k} \frac{p_i^{e_i+1}-1}{p_i-1}$ . Division is handled by finding the modular multiplicative inverse of the denominator $a^{-1} \equiv a^{mod-2} \pmod{mod}$ .
(p_i - 1)using Fermat's Little Theorem, i.e.,Product of Divisors (P(N)): Calculated using the formula$P(N) = N^{\tau(N)/2}$ , which expands to $P(N) = \prod_{i=1}^{k} p_i^{e_i \cdot \tau(N)/2}$ . The final exponent for each prime is computed modulo
mod-1. The division by 2 is handled carefully to prevent overflow with largetau(N)by dividing eithere_iortau(N)before multiplication.Time Complexity:$$O(k \cdot \log(\max(e_i, \text{mod})))$$ , where
kis the number of distinct prime factors. The complexity is dominated by the loops that involve modular exponentiation.Space Complexity:$$O(k)$$ to store the prime factorization.
Key Insights:
mod-1(due to Fermat's Little Theorem). This is crucial for calculating the product of divisors correctly.(eᵢ * τ(N) / 2)without overflow. The solution correctly identifies that this product is always even and performs the division by 2 on eithereᵢor a factor ofτ(N)before the modular multiplication chain.✅ Checklist
Code Quality
Testing
Documentation
Style Guide
#include <bits/stdc++.h>)🏷️ Type of Change
🧪 Testing Details
Describe how you tested your solution:
The solution was verified against the CSES online judge and passed all test cases. It was also tested locally with several custom inputs, including perfect squares and numbers with large exponents, to ensure correctness.
Test Cases Used:
📸 Screenshots
📎 Additional Notes
This submission replaces the initial template file.
For Maintainers: