Skip to content

Conversation

@Abhay-S-R
Copy link
Contributor

🎯 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.

    1. 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 + 7 for the final answer, and once modulo 10^9 + 6 for use in exponent calculations.

    2. 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 (p_i - 1) using Fermat's Little Theorem, i.e., $a^{-1} \equiv a^{mod-2} \pmod{mod}$.

    3. 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 large tau(N) by dividing either e_i or tau(N) before multiplication.

  • Time Complexity: $$O(k \cdot \log(\max(e_i, \text{mod})))$$, where k is 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:

    • The number of divisors ($\tau$), sum of divisors ($\sigma$), and product of divisors ($P$) are all multiplicative functions, allowing for the results of each prime power to be combined via multiplication.
    • Exponents in modular arithmetic operate modulo mod-1 (due to Fermat's Little Theorem). This is crucial for calculating the product of divisors correctly.
    • The most complex part is calculating the exponent (eᵢ * τ(N) / 2) without overflow. The solution correctly identifies that this product is always even and performs the division by 2 on either eᵢ or a factor of τ(N) before the modular multiplication chain.

✅ Checklist

Code Quality

  • Solution follows the required template format
  • Code is clean and well-commented
  • Variable names are descriptive
  • Proper error handling for edge cases (e.g., preventing negative modulo results)

Testing

  • Solution passes all CSES test cases
  • Tested with custom edge cases (perfect squares vs. non-perfect squares)
  • Handles large input constraints efficiently
  • No runtime errors or timeouts

Documentation

  • Added solution to appropriate category folder
  • File name follows naming convention (snake_case)
  • Added brief comment explaining the approach
  • Updated any relevant documentation if needed

Style Guide

  • Uses required headers (#include <bits/stdc++.h>)
  • Includes fast I/O optimization
  • Uses appropriate data types (long long for large numbers)
  • Follows competitive programming best practices

🏷️ Type of Change

  • 🐛 Bug fix (fixes an existing solution)
  • ✨ New problem solution
  • 📚 Documentation improvement
  • 🔧 Code optimization/refactoring
  • 🎃 Hacktoberfest contribution

🧪 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:

Input: 
2
5 2
7 2

Expected Output: 
9 1767 638120170

Actual Output: 
9 1767 638120170

📸 Screenshots

image

📎 Additional Notes

This submission replaces the initial template file.


For Maintainers:

  • Code review completed
  • Solution verified on CSES judge
  • Documentation updated if needed
  • Labels applied appropriately

@ks-iitjmu ks-iitjmu added enhancement New feature or request good first issue Good for newcomers hacktoberfest Issues/PRs for Hacktoberfest participation hacktoberfest-accepted Approved PRs for Hacktoberfest hacktoberfest_2025 Hacktoberfest 2025 specific contributions medium Medium difficulty problems category: mathematics Mathematical problems labels Oct 15, 2025
@ks-iitjmu ks-iitjmu merged commit ee7cbff into ks-iitjmu:main Oct 15, 2025
@Abhay-S-R Abhay-S-R deleted the feature/divisor_analysis branch October 29, 2025 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: mathematics Mathematical problems enhancement New feature or request good first issue Good for newcomers hacktoberfest Issues/PRs for Hacktoberfest participation hacktoberfest_2025 Hacktoberfest 2025 specific contributions hacktoberfest-accepted Approved PRs for Hacktoberfest medium Medium difficulty problems

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants