Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:47:05+00:00 2026-06-11T20:47:05+00:00

(This is derived from a recently completed programming competition) You are given two arrays

  • 0

(This is derived from a recently completed programming competition)

You are given two arrays of 10^5 ints in the range 1..10^7 inclusive:

int N[100000] = { ... }
int D[100000] = { ... }

Imagine the rational number X be the result of multiplying together all the elements of N and dividing by all the elements of D.

Modify the two arrays without changing the value of X (and without assigning any element out of range) such that the product of N and the product of D have no common factor.

A naive solution (I think) would work would be…

for (int i = 0; i < 100000; i++)
    for (int j = 0; j < 100000; j++)
    {
        int k = gcd(N[i], D[j]); // euclids algorithm

        N[i] /= k;
        D[j] /= k;
    }

…but this is too slow.

What is a solution that takes less than around 10^9 operations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:47:07+00:00Added an answer on June 11, 2026 at 8:47 pm

    Factorise all numbers in the range 1 to 107. Using a modification of a Sieve of Eratosthenes, you can factorise all numbers from 1 to n in O(n*log n) time (I think it’s a bit better, O(n*(log log n)²) or so) using O(n*log log n) space. Better than that is probably creating an array of just the smallest prime factors.

    // Not very optimised, one could easily leave out the even numbers, or also the multiples of 3
    // to reduce space usage and computation time
    int *spf_sieve = malloc((limit+1)*sizeof *spf_sieve);
    int root = (int)sqrt(limit);
    for(i = 1; i <= limit; ++i) {
        spf_sieve[i] = i;
    }
    for(i = 4; i <= limit; i += 2) {
        spf_sieve[i] = 2;
    }
    for(i = 3; i <= root; i += 2) {
        if(spf_sieve[i] == i) {
            for(j = i*i, step = 2*i; j <= limit; j += step) {
                if (spf_sieve[j] == j) {
                    spf_sieve[j] = i;
                }
            }
        }
    }
    

    To factorise a number n > 1 using that sieve, look up its smallest prime factor p, determine its multiplicity in the factorisation of n (either by looking up recursively, or by simply dividing until p doesn’t evenly divide the remaining cofactor, which is faster depends) and the cofactor. While the cofactor is larger than 1, look up the next prime factor and repeat.

    Create a map from primes to integers

    Go through both arrays, for each number in N, add the exponent of each prime in its factorisation to the value in the map, for the numbers in D, subtract.

    Go through the map, if the exponent of the prime is positive, enter p^exponent to the array N (you may need to split that across several indices if the exponent is too large, and for small values, combine several primes into one entry – there are 664579 primes less than 107, so the 100,000 slots in the arrays may not be enough to store each appearing prime with the correct power), if the exponent is negative, do the same with the D array, if it’s 0, ignore that prime.

    Any unused slots in N or D are then set to 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(This is derived from a recently completed programming contest) You are given G, a
We can specify a derived from constraint on generic type parameters like this: class
I have a class derived from Dictionary. I need this class to simulate a
In the header, I'm defining bool isActive. In classes derived from this one, I
This question is derived from my previous SO question's commends . I am confused
I have a function in my class (derived from this function) to generate a
I'm creating a custom view derived from GridView. This contains a custom ImageView with
I have 500,000 values for a variable derived from financial markets. Specifically, this variable
Derived from this question and related to this question : If I construct an
i have a class 'MyTextBox' that derives from the default TextBox in Silverlight. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.