Official Editorial

Implementation

Time Complexity: O(NlogN)\mathcal{O}(N\log N)

C++

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}

Java

import java.io.*;
import java.util.*;
public class Prod1ModN {
public static void main(String[] args) {
Kattio io = new Kattio();
int n = io.nextInt();
ArrayList<Long> coprimes = new ArrayList<>();

Python

import math
n = int(input())
coprimes = []
"""
This array stores all of the numbers
which are less than n and are coprime with n.
"""

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!