EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2023
  3. Final Round

Codey and Facto

https://www.hackerrank.com/contests/codenection-2023-final-round-open-category/challenges/cn-c9

PreviousCodey and ApplesNextCodey and Zoey

Last updated 2 months ago

Question

Codey is very interested in facto, a concept he learned in the mathematics class last week.

If you are given an integer n and an integer m, a facto of n and m is an array of integers such that the product of every element in the array is n, and the length of the array is m.

For example, when n=8,m=2n=8, m=2n=8,m=2, one of the facto is [4,2][4, 2][4,2], as 4∗2=84 * 2 = 84∗2=8, and the array's length is 222. Other examples of facto for n=8,m=2n=8,m=2n=8,m=2include [−4,−2],[1,8][-4,-2],[1,8][−4,−2],[1,8], and [8,1][8,1][8,1].

Codey wants you to find the total number of unique facto that exist for the n and m modulo 109+710^9+7109+7. Recall that a modulo b is the remainder of a when divided by b.

Input Format

The first line contains two integers, n and m, where n represents the number n, and m represents the number of facto.

Constraints

0≤n,m≤1060 \le n,m \le 10^60≤n,m≤106

Output Format

Output the total number of unique facto for n and m modulo 109+710^9 + 7109+7.

Sample Inputs:

Input

8 2

Output

8

Explanation


Solution - Math Formula ft. combinations

Huge thanks to _sheng_ from discord server, which gives the most important math formula for this question.

From his words, let n = 12, m = 10, first break down into factors with powers:

For each prime p with power a, we can use this formula below to find all the distribution of factors:

Finally, multiply that number to get the final result: (mod it if needed)

Feel free to use another example for that formula, so, all we need to do is convert this math formula into code.

Don't forget to MOD the answers owob!

Here's my code solution:

import math

MOD = 10**9 + 7

def fcm(m, n):
    MOD = 10**9 + 7
    
    def prime_factorize(x):
        factors = {}
        d = 2
        while d * d <= x:
            while x % d == 0:
                factors[d] = factors.get(d, 0) + 1
                x //= d
            d += 1
        if x > 1:
            factors[x] = factors.get(x, 0) + 1
        return factors

    prime_factors = prime_factorize(m)

    result = 1
    for prime, exponent in prime_factors.items():
        result *= math.comb(exponent + n - 1, n - 1)
        result %= MOD

    result *= pow(2, n - 1, MOD)
    result %= MOD

    return result

m, n = map(int, input().strip().split())
print(fcm(m, n))

Some people had proposed a recursive solution, but it will hit recursive depth reached error when n > 1000, so in this question, recursive method is not able to clear all test cases.

facto for n=8,m=2n = 8, m=2n=8,m=2 includes: [1,8],[8,1],[−1,−8],[−8,−1],[4,2],[2,4],[−2,−4],[−4,−2][1,8],[8,1],[-1,-8],[-8,-1],[4,2],[2,4],[-2,-4],[-4,-2][1,8],[8,1],[−1,−8],[−8,−1],[4,2],[2,4],[−2,−4],[−4,−2]

12=22+3112 = 2^2 + 3^112=22+31

then, let n=pan = p^an=pa.

∑a+n−1Cn−1\sum {_{a+n-1}C_{n-1}}∑a+n−1​Cn−1​

For same examples, 222^222 can be described as:

2+10−1C10−1=11C9=55_{2+10-1}C_{10-1} = {_{11}C_9} =552+10−1​C10−1​=11​C9​=55

Meanwhile, 313^131 can be described as:

1+10−1C10−1=10C9=10_{1+10-1}C_{10-1} = {_{10}C_9} = 101+10−1​C10−1​=10​C9​=10

Multiply them together, 55∗10=55055*10=55055∗10=550

Finally, as we have 2n−12^{n-1}2n−1 ways to arrange the remaining "1"s, so we calculate the permutation of that:

210−1=5122^{10-1} = 512210−1=512

550∗512=281600550 * 512 = 281600550∗512=281600

🇲🇾