CF - Orac & LCM
Authors: Benjamin Qi, Timothy Gao
Solution: Prime Factorization
For each prime, the second-to-lowest exponent of the prime that occurs in any of the numbers in the input is the exponent of this prime that will appear in the final answer.
Here's a short solution that accomplishes this without explicitly computing any prime factorizations!
C++
#include <bits/stdc++.h>using namespace std;using ll = long long;ll a, b;// a stores minimum exponents (gcd of all numbers seen so far)// b stores second minimum exponentsint n;
Java
import java.io.*;import java.util.*;public class oraclcm {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int N = Integer.parseInt(br.readLine());StringTokenizer st = new StringTokenizer(br.readLine());int[] nums = new int[N];
Python
import math"""a stores minimum exponents (gcd of all numbers seen so far)b stores second minimum exponents"""def red(): # restore invariant mentioned aboveglobal a, b
If it's hard to understand what exactly this code is doing at first glance, a good first step is to simulating the code in the case where each is a power of the same prime (say, ). If the algorithm works for this case, then it will also work in the general case since the contributions of different primes are computed independently and multiplied together.
Here's another way of thinking about this:
For a fixed ,
and we can update when increases by one. Taking the GCD of the results for each gives the answer.
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!